async private void listenForServer() { bool run = true; while (run) { string serverResponse = await Task.Run(() => serverConnection.ListenForMessage()); string[] commands = serverResponse.Split('~'); int i = 0; while (commands[i] != "") { int prefix = int.Parse(commands[i].Split('/')[0]); Debug.Log(serverResponse); switch (prefix) { case 0: break; case 1: if (commands[i] == "1/0") { globalData.userName = loginLogic.username.text; if (loginLogic.credentialsError) { loginLogic.errorAnimator.SetBool("open", false); } mainMenuLogic.mainMenuCanvas.enabled = true; loginLogic.loginCanvas.enabled = false; } else { loginLogic.credentialsError = true; loginLogic.errorAnimator.SetBool("open", true); loginLogic.error.text = "Incorrect user or password"; } break; case 2: if (commands[i].Equals("2/0")) { registerLogin.registerWindow.enabled = false; loginLogic.loginCanvas.enabled = true; if (registerLogin.userError) { loginLogic.errorAnimator.SetBool("open", false); } } else { loginLogic.errorAnimator.SetBool("open", true); loginLogic.error.text = "Register Error: User already exists"; Debug.Log("Register Error: User already exists"); registerLogin.userError = true; } break; case 3: informationLogic.AgeText.text = commands[i].Replace("3/", ""); break; case 4: informationLogic.EmailText.text = commands[i].Replace("4/", ""); break; case 5: if (commands[i] == "5/0") { Debug.Log("Change spam successful!"); if (informationLogic.canvasError) { loginLogic.errorAnimator.SetBool("open", false); } } else { loginLogic.errorAnimator.SetBool("open", true); loginLogic.error.text = "Change spam error"; Debug.Log("Change spam error"); } break; case 6: informationLogic.spam.isOn = commands[i].Equals("6/1"); break; case 7: informationLogic.connectedUsersNotificationListen(commands[i]); gameCreationLogic.connectedUsersNotificationListen(commands[i]); break; case 8: gameCreationLogic.gameNumber = int.Parse(commands[i].Split('/')[1]); break; case 9: if (commands[i].Equals("9/0")) { if (invitationError) { loginLogic.errorAnimator.SetBool("open", false); } infoLogAnimator.SetBool("open", true); infoLogText.text = "Invitation sent!"; this.closeInfoLog(); } else { loginLogic.errorAnimator.SetBool("open", true); loginLogic.error.text = "Error sending invitation"; invitationError = true; } break; case 10: string user = commands[i].Split('/')[1].Split(',')[0]; int gameNumber = int.Parse(commands[i].Split('/')[1].Split(',')[1]); invitationLogic.gameNumber = gameNumber; gameCreationLogic.gameNumber = gameNumber; Debug.Log("gamenumber: "); Debug.Log(invitationLogic.gameNumber); invitationLogic.message.text = user + " has invited you to a game!"; invitationLogic.animator.SetBool("open", !invitationLogic.animator.GetBool("open")); break; case 11: gameCreationLogic.gamePlayersNotificationListen(commands[i]); globalData.gameUserList = commands[i].Replace("11/", "").Split(','); break; case 12: if (invitationLogic.gameNumber != -1) { serverConnection.SendMessage("13/"); int level = int.Parse(commands[i].Split(',')[1]); switch (level) { case 1: SceneManager.LoadScene("GameScenePython", LoadSceneMode.Single); run = false; break; case 2: SceneManager.LoadScene("GameSceneMatlab", LoadSceneMode.Single); run = false; break; } } break; case 13: joinAGameLogic.gamesNotificationUpdate(commands[i]); break; case 14: if (commands[i].Equals("14/0")) { gameCreationLogic.gameCreationCanvas.enabled = true; joinAGameLogic.mainCanvas.enabled = false; } else { Debug.Log("Error entering the game"); } break; //confirmation of exiting a game case 15: if (commands[i].Equals("15/0")) { infoLogAnimator.SetBool("open", true); infoLogText.text = "Exited game!"; } else { loginLogic.errorAnimator.SetBool("open", true); Debug.Log("Game exit Error"); loginLogic.error.text = "Game exit error Error"; } break; } i++; } } }
//Asynchronous function that runs until we join a game. //It listens and parses all the messages sent by the server. //Sometimes messages come concatenated so we split them by ~ async private void listenForServer() { bool run = true; while (run) { string serverResponse = await Task.Run(() => serverConnection.ListenForMessage()); string[] commands = serverResponse.Split('~'); int i = 0; while (commands[i] != "") { int prefix = int.Parse(commands[i].Split('/')[0]); Debug.Log(serverResponse); switch (prefix) { //login accepted / denied case 1: if (commands[i] == "1/0") { globalData.userName = loginLogic.username.text; if (loginLogic.credentialsError) { loginLogic.errorAnimator.SetBool("open", false); } mainMenuLogic.mainMenuCanvas.enabled = true; loginLogic.loginCanvas.enabled = false; } else { loginLogic.credentialsError = true; loginLogic.errorAnimator.SetBool("open", true); loginLogic.error.text = "Incorrect user or password"; } break; //register case 2: if (commands[i].Equals("2/0")) { registerLogin.registerWindow.enabled = false; loginLogic.loginCanvas.enabled = true; if (registerLogin.userError) { loginLogic.errorAnimator.SetBool("open", false); } } else { loginLogic.errorAnimator.SetBool("open", true); loginLogic.error.text = "Register Error: User already exists"; Debug.Log("Register Error: User already exists"); registerLogin.userError = true; } break; //gets the age case 3: informationLogic.AgeText.text = commands[i].Replace("3/", ""); break; //gets the email case 4: informationLogic.EmailText.text = commands[i].Replace("4/", ""); break; //confirmation that spam has been changed case 5: if (commands[i] == "5/0") { Debug.Log("Change spam successful!"); if (informationLogic.canvasError) { loginLogic.errorAnimator.SetBool("open", false); } } else { loginLogic.errorAnimator.SetBool("open", true); loginLogic.error.text = "Change spam error"; Debug.Log("Change spam error"); } break; //spam getter case 6: informationLogic.spam.isOn = commands[i].Equals("6/1"); break; //list of connected users, calls the functions to update the displayed users case 7: informationLogic.connectedUsersNotificationListen(commands[i]); gameCreationLogic.connectedUsersNotificationListen(commands[i]); break; //gets the number of game that the user just created case 8: gameCreationLogic.gameNumber = int.Parse(commands[i].Split('/')[1]); break; //confirmation that an invitation has been sent case 9: if (commands[i].Equals("9/0")) { if (invitationError) { loginLogic.errorAnimator.SetBool("open", false); } infoLogAnimator.SetBool("open", true); infoLogText.text = "Invitation sent!"; this.closeInfoLog(); } else { loginLogic.errorAnimator.SetBool("open", true); loginLogic.error.text = "Error sending invitation"; invitationError = true; } break; //reciving an invitation, popping up the dialog with the information updated. Set the game number to the class case 10: string user = commands[i].Split('/')[1].Split(',')[0]; int gameNumber = int.Parse(commands[i].Split('/')[1].Split(',')[1]); invitationLogic.gameNumber = gameNumber; gameCreationLogic.gameNumber = gameNumber; Debug.Log("gamenumber: "); Debug.Log(invitationLogic.gameNumber); invitationLogic.message.text = user + " has invited you to a game!"; invitationLogic.animator.SetBool("open", !invitationLogic.animator.GetBool("open")); break; //message containing all the users of a game. Updates the displayed lists and the gameUserList. case 11: gameCreationLogic.gamePlayersNotificationListen(commands[i]); globalData.gameUserList = commands[i].Replace("11/", "").Split(','); break; //message to start a game. Depending on the parameter we load a level or another case 12: if (invitationLogic.gameNumber != -1) { serverConnection.SendMessage("13/"); int level = int.Parse(commands[i].Split(',')[1]); switch (level) { case 1: SceneManager.LoadScene("GameScenePython", LoadSceneMode.Single); run = false; break; case 2: SceneManager.LoadScene("GameSceneMatlab", LoadSceneMode.Single); run = false; break; } } break; //receives list all all started games case 13: joinAGameLogic.gamesNotificationUpdate(commands[i]); break; //confirmation of joining a game, change the join game UI to create game UI case 14: if (commands[i].Equals("14/0")) { gameCreationLogic.gameCreationCanvas.enabled = true; joinAGameLogic.mainCanvas.enabled = false; } else { Debug.Log("Error entering the game"); } break; //confirmation of exiting a game case 15: if (commands[i].Equals("15/0")) { infoLogAnimator.SetBool("open", true); infoLogText.text = "Exited game!"; } else { loginLogic.errorAnimator.SetBool("open", true); Debug.Log("Game exit Error"); loginLogic.error.text = "Game exit error Error"; } break; case 16: scoreBoardLogic.scoreBoardNotificationListen(commands[i]); break; } i++; } } }