Skip to content

Commit

Permalink
start to hook up the client side
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Sep 22, 2024
1 parent 421e996 commit b6867d1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
49 changes: 49 additions & 0 deletions client/src/main/kotlin/http/SessionApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package http

import http.dto.BeginSessionResponse
import http.dto.DevCommandRequest
import javax.swing.JOptionPane
import kong.unirest.HttpMethod
import util.DialogUtilNew
import util.OnlineConstants
import util.UpdateManager.checkForUpdates

class SessionApi(private val httpClient: HttpClient) {
fun beginSession(name: String) {
val response =
httpClient.doCall<BeginSessionResponse>(
HttpMethod.POST,
Routes.DEV_COMMAND,
DevCommandRequest(name)
)

when (response) {
is FailureResponse -> handleBeginSessionFailure(response)
is CommunicationError ->
DialogUtilNew.showError(
"Error communicating with server: ${response.unirestException.message}"
)
is SuccessResponse<BeginSessionResponse> -> {
// Errr, store the sessionId someplace, then hook back into legacy code somehow to
// launch the lobby etc
val sessionResponse = response.body
}
}
}

private fun handleBeginSessionFailure(response: FailureResponse<*>) {
when (response.errorCode) {
UPDATE_REQUIRED -> {
val response =
DialogUtilNew.showQuestion(
"Your client must be updated to connect. Check for updates now?"
)

if (response == JOptionPane.YES_OPTION) {
checkForUpdates(OnlineConstants.ENTROPY_VERSION_NUMBER)
}
}
else -> DialogUtilNew.showError("An error occurred: ${response.errorMessage}")
}
}
}
11 changes: 10 additions & 1 deletion client/src/main/kotlin/screen/online/PlayOnlineDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import javax.swing.border.EmptyBorder
import screen.ScreenCache
import screen.SimpleDialog
import util.DialogUtilNew
import util.Globals

class PlayOnlineDialog : SimpleDialog() {
private val lblName = JLabel("Name")
Expand Down Expand Up @@ -39,7 +40,15 @@ class PlayOnlineDialog : SimpleDialog() {
}

private fun beginSession() {
// TODO - write me
dispose()

ScreenCache.showConnectingDialog()

try {
Globals.sessionApi.beginSession(textFieldUsername.text)
} finally {
ScreenCache.dismissConnectingDialog()
}
}

override fun okPressed() {
Expand Down
2 changes: 2 additions & 0 deletions client/src/main/kotlin/util/Globals.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package util
import http.DevApi
import http.HealthCheckApi
import http.HttpClient
import http.SessionApi

object Globals {
private val baseUrl = "http://localhost:8080"
private val httpClient = HttpClient(baseUrl)
val healthCheckApi = HealthCheckApi(httpClient)
val devApi = DevApi(httpClient)
val sessionApi = SessionApi(httpClient)
}
1 change: 0 additions & 1 deletion core/src/main/java/util/XmlConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public interface XmlConstants
public static final String RESPONSE_TAG_STATISTICS_NOTIFICATION = "StatisticsNotification";

//Server responses
public static final String RESPONSE_TAG_UPDATE_AVAILABLE = "UpdateAvailable";
public static final String RESPONSE_TAG_KICK_OFF = "KickOff";
public static final String RESPONSE_TAG_CHANGE_PASSWORD = "ChangePasswordResponse";
public static final String RESPONSE_TAG_CONNECT_SUCCESS = "ConnectSuccess";
Expand Down
11 changes: 0 additions & 11 deletions server/src/main/java/util/XmlBuilderServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ public static Document getKickOffResponse(String username, String reason)
return response;
}

public static Document getUpdateAvailableResponse(long fileSize, String versionNumber)
{
Document response = XmlUtil.factoryNewDocument();
Element rootElement = response.createElement(RESPONSE_TAG_UPDATE_AVAILABLE);
rootElement.setAttribute("FileSize", "" + fileSize);
rootElement.setAttribute("VersionNumber", versionNumber);

response.appendChild(rootElement);
return response;
}

public static Document getChangePasswordResponse(String username, String oldPass, String newPass)
{
String error = AccountUtil.changePassword(username, oldPass, newPass);
Expand Down

0 comments on commit b6867d1

Please sign in to comment.