public void PlayerWon() { BattleWinMsg m = new BattleWinMsg(); m.Lobby = MyLobby; SendToServer(JsonUtility.ToJson(m)); }
void OnData(DataStreamReader stream, int i) // i is the index in m_connection to get ip address { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.PLAYER_LOGIN: Debug.Log("Login request received"); PlayerLoginMsg loginMsg = JsonUtility.FromJson <PlayerLoginMsg>(recMsg); StartCoroutine(SendLoginWebRequest(loginMsg.userID, loginMsg.password, i)); break; case Commands.PLAYER_REGISTER: Debug.Log("Registration request received"); PlayerRegisterMsg registerMsg = JsonUtility.FromJson <PlayerRegisterMsg>(recMsg); StartCoroutine(SendRegisterWebRequest(registerMsg.userID, registerMsg.password, i)); break; case Commands.HOST_GAME: Debug.Log("HOSTED GAME received"); HostGameMsg hostMsg = JsonUtility.FromJson <HostGameMsg>(recMsg); HostNewLobby(hostMsg.player.id, i); break; case Commands.JOIN_GAME: Debug.Log("JOINED GAME received"); JoinGameMsg joinMsg = JsonUtility.FromJson <JoinGameMsg>(recMsg); JoinLobby(joinMsg.joinLobby.lobbyID, joinMsg.player.id, i); break; case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); SendToClient(JsonUtility.ToJson(hsMsg), m_Connections[i]); //Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); //Debug.Log("Player update message received!"); //update the specific players data foreach (NetworkObjects.NetworkPlayer player in connectedPlayers) { } break; case Commands.START_GAME: StartGameMsg startMsg = JsonUtility.FromJson <StartGameMsg>(recMsg); startMsg.successful = true; startMsg.Player1Char = UnityEngine.Random.Range(0, 4); int player2 = UnityEngine.Random.Range(0, 4); while (startMsg.Player1Char == player2) { player2 = UnityEngine.Random.Range(0, 4); } startMsg.Player2Char = player2; Debug.Log("Start Message Sucess:"); Debug.Log(startMsg.successful ? "Sucess": "Fail"); Debug.Log(startMsg); SendToClient(JsonUtility.ToJson(startMsg), m_Connections[i]); SendToClient(JsonUtility.ToJson(startMsg), m_Connections[startMsg.LobbyToStart.player2addr]); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; case Commands.REQUEST_AVAILABLE_LOBBIES: Debug.Log("Received request for lobbies"); AllAvailableLobbies n = new AllAvailableLobbies(); foreach (KeyValuePair <int, NetworkObjects.Lobby> Lobby in AvailableLobbies) { if (!Lobby.Value.full) { n.Lobbies.Add(Lobby.Value); } } //n.Lobbies = AvailableLobbies; //Debug.Log(JsonUtility.ToJson(AvailableLobbies[0])); //Debug.Log(JsonUtility.ToJson(n.Lobbies[0])); SendToClient(JsonUtility.ToJson(n), m_Connections[i]); break; case Commands.MOVE_TAKEN: MoveTakenMsg moveMsg = JsonUtility.FromJson <MoveTakenMsg>(recMsg); Debug.Log("Received Move from client"); if (moveMsg.Lobby.player1addr == i) //if player 1 made the move { SendToClient(JsonUtility.ToJson(moveMsg), m_Connections[moveMsg.Lobby.player2addr]); } else { SendToClient(JsonUtility.ToJson(moveMsg), m_Connections[moveMsg.Lobby.player1addr]); } break; case Commands.PLAYER_INFO: MyInfoMsg infoMsg = JsonUtility.FromJson <MyInfoMsg>(recMsg); Debug.Log("Received Player Info from " + infoMsg.Player.UserID); StartCoroutine(PlayerInfoWebRequest(infoMsg.Player.UserID, i)); break; case Commands.BATTLE_WON: BattleWinMsg winMsg = JsonUtility.FromJson <BattleWinMsg>(recMsg); Debug.Log("Received Move from client"); if (winMsg.Lobby.player1addr == i) //if player 1 won { StartCoroutine(SendWinAndLossWebRequest(winMsg.Lobby.Player1, winMsg.Lobby.Player2)); //update player 1 win and player 2 loss } else { StartCoroutine(SendWinAndLossWebRequest(winMsg.Lobby.Player2, winMsg.Lobby.Player1)); //update player 2 win and player 1 loss } //remove the lobby from the available lobbies list AvailableLobbies.Remove(winMsg.Lobby.lobbyID); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }