diff --git a/client/src/main/kotlin/http/SessionApi.kt b/client/src/main/kotlin/http/SessionApi.kt new file mode 100644 index 0000000..75fa1c2 --- /dev/null +++ b/client/src/main/kotlin/http/SessionApi.kt @@ -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( + 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 -> { + // 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}") + } + } +} diff --git a/client/src/main/kotlin/screen/online/PlayOnlineDialog.kt b/client/src/main/kotlin/screen/online/PlayOnlineDialog.kt index 5b18ffa..d3da9b2 100644 --- a/client/src/main/kotlin/screen/online/PlayOnlineDialog.kt +++ b/client/src/main/kotlin/screen/online/PlayOnlineDialog.kt @@ -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") @@ -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() { diff --git a/client/src/main/kotlin/util/Globals.kt b/client/src/main/kotlin/util/Globals.kt index ce95e81..7a6f6d9 100644 --- a/client/src/main/kotlin/util/Globals.kt +++ b/client/src/main/kotlin/util/Globals.kt @@ -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) } diff --git a/core/src/main/java/util/XmlConstants.java b/core/src/main/java/util/XmlConstants.java index 5ffc9b7..8078f39 100644 --- a/core/src/main/java/util/XmlConstants.java +++ b/core/src/main/java/util/XmlConstants.java @@ -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"; diff --git a/server/src/main/java/util/XmlBuilderServer.java b/server/src/main/java/util/XmlBuilderServer.java index 3bd99ff..7050833 100644 --- a/server/src/main/java/util/XmlBuilderServer.java +++ b/server/src/main/java/util/XmlBuilderServer.java @@ -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);