void UpdatePlayerStats(UpdateStatsMsg msg) { foreach (NetworkConnection c in m_Connections) { SendToClient(JsonUtility.ToJson(msg), c); } }
void UpdateStats() { UpdateStatsMsg m = new UpdateStatsMsg(); m.ID = PlayerID; m.Position = playerGO.transform.position; m.Rotation = playerGO.transform.rotation; SendToServer(JsonUtility.ToJson(m)); }
void UpdateOtherPlayer(UpdateStatsMsg msg) { if (msg.ID != PlayerID) { GameObject Obj = FindPlayerObj(msg.ID); if (Obj) { Obj.transform.position = msg.Position; } } }
void UpdateStats() { UpdateStatsMsg m = new UpdateStatsMsg(); m.ID = PlayerID; m.Position = playerGO.transform.position; if (loginInfo.IsHost) { m.BallPosition = Ball.transform.position; } SendToServer(JsonUtility.ToJson(m)); }
void OnData(DataStreamReader stream) { 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.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); //Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; case Commands.REQUEST_ID: RequestIDMsg riMsg = JsonUtility.FromJson <RequestIDMsg>(recMsg); playerInfo.serverID = riMsg.ID; Debug.Log("Request ID message received!"); break; case Commands.PLAYER_SPAWN: PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg); SpawnOtherPlayer(psMsg); break; case Commands.UPDATE_STATS: UpdateStatsMsg usMsg = JsonUtility.FromJson <UpdateStatsMsg>(recMsg); UpdateOtherPlayer(usMsg); break; case Commands.PLAYER_DC: PlayerDCMsg pdMsg = JsonUtility.FromJson <PlayerDCMsg>(recMsg); KillPlayer(pdMsg); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i) { 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.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); //Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; case Commands.PLAYER_SPAWN: PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg); AllSpawnMsg.Add(psMsg); SpawnNewPlayer(psMsg); Debug.Log(psMsg.ID + " has joined the server!"); break; case Commands.UPDATE_STATS: UpdateStatsMsg usMsg = JsonUtility.FromJson <UpdateStatsMsg>(recMsg); UpdatePlayerStats(usMsg); break; case Commands.PLAYER_DC: PlayerDCMsg pdMsg = JsonUtility.FromJson <PlayerDCMsg>(recMsg); Debug.Log("Removed Spawn data of: " + pdMsg.PlayerID); AllSpawnMsg.Remove(FindPlayerSpawnMsg(pdMsg.PlayerID)); DCPlayer(pdMsg); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
void UpdateOtherPlayer(UpdateStatsMsg msg) { if (msg.ID != PlayerID) { if (!loginInfo.IsHost) { Ball.transform.position = msg.BallPosition; } GameObject Obj = FindPlayerObj(msg.ID); if (Obj) { Obj.transform.position = msg.Position; } } }
void OnData(DataStreamReader stream) { 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.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); //Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; case Commands.REQUEST_ID: RequestIDMsg riMsg = JsonUtility.FromJson <RequestIDMsg>(recMsg); //playerInfo.serverID = riMsg.ID; Debug.Log("Request ID message received!"); break; case Commands.PLAYER_SPAWN: PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg); SpawnOtherPlayer(psMsg); break; case Commands.UPDATE_STATS: UpdateStatsMsg usMsg = JsonUtility.FromJson <UpdateStatsMsg>(recMsg); UpdateOtherPlayer(usMsg); break; case Commands.GAME_RESULT: GameEndMsg geMsg = JsonUtility.FromJson <GameEndMsg>(recMsg); P1Diff.text = (int.Parse(geMsg.p1.points) - int.Parse(clientScript.AllPlayers[0].points)).ToString(); P2Diff.text = (int.Parse(geMsg.p2.points) - int.Parse(clientScript.AllPlayers[1].points)).ToString(); P3Diff.text = (int.Parse(geMsg.p3.points) - int.Parse(clientScript.AllPlayers[2].points)).ToString(); clientScript.AllPlayers[0] = geMsg.p1; clientScript.AllPlayers[1] = geMsg.p2; clientScript.AllPlayers[2] = geMsg.p3; File.WriteAllText(logPath, geMsg.LogData); break; case Commands.PLAYER_DC: PlayerDCMsg pdMsg = JsonUtility.FromJson <PlayerDCMsg>(recMsg); KillPlayer(pdMsg); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i) { 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.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); //Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; case Commands.PLAYER_SPAWN: PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg); AllSpawnMsg.Add(psMsg); SpawnNewPlayer(psMsg); Debug.Log(psMsg.ID + " has joined the server!"); break; case Commands.UPDATE_STATS: UpdateStatsMsg usMsg = JsonUtility.FromJson <UpdateStatsMsg>(recMsg); UpdatePlayerStats(usMsg); break; case Commands.GAME_START: // Log WriteToLog("\n\nGame ID: " + System.DateTime.Now.ToString() + UnityEngine.Random.value.ToString()); GameStartMsg gsMsg = JsonUtility.FromJson <GameStartMsg>(recMsg); // Log WriteToLog(gsMsg.p1.user_id + " has joined the match. - " + gsMsg.p1.joinTime); WriteToLog(gsMsg.p2.user_id + " has joined the match. - " + gsMsg.p1.joinTime); WriteToLog(gsMsg.p3.user_id + " has joined the match. - " + gsMsg.p1.joinTime); int result = UnityEngine.Random.Range(1, 4); Player winner = new Player(); // Log WriteToLog("Game started."); WriteToLog("RESULT: "); int p1Points = int.Parse(gsMsg.p1.points); int p2Points = int.Parse(gsMsg.p2.points); int p3Points = int.Parse(gsMsg.p3.points); int add; switch (result) { case 1: add = (int)((p2Points + p3Points) / p1Points) + 20; WriteToLog(gsMsg.p1.user_id + " earned: " + add + " points." + " (" + p1Points + " -> " + (p1Points + add) + ")"); p1Points += add; gsMsg.p1.points = p1Points.ToString(); winner = gsMsg.p1; WriteToLog(gsMsg.p2.user_id + " lost: " + (int)((p2Points * 0.05) + 20) + " points." + " (" + p2Points + " -> " + (p2Points - (int)((p2Points * 0.05) + 20)) + ")"); p2Points -= (int)((p2Points * 0.05) + 20); gsMsg.p2.points = p2Points.ToString(); WriteToLog(gsMsg.p3.user_id + " lost: " + (int)((p3Points * 0.05) + 20) + " points." + " (" + p3Points + " -> " + (p3Points - (int)((p3Points * 0.05) + 20)) + ")"); p3Points -= (int)((p3Points * 0.05) + 20); gsMsg.p3.points = p3Points.ToString(); break; case 2: add = (int)((p1Points + p3Points) / p2Points) + 20; WriteToLog(gsMsg.p2.user_id + " earned: " + add + " points." + " (" + p2Points + " -> " + (p2Points + add) + ")"); p2Points += add; gsMsg.p2.points = p2Points.ToString(); winner = gsMsg.p2; WriteToLog(gsMsg.p1.user_id + " lost: " + (int)((p1Points * 0.05) + 20) + " points." + " (" + p1Points + " -> " + (p1Points - (int)((p1Points * 0.05) + 20)) + ")"); p1Points -= (int)((p1Points * 0.05) + 20); gsMsg.p1.points = p1Points.ToString(); WriteToLog(gsMsg.p3.user_id + " lost: " + (int)((p3Points * 0.05) + 20) + " points." + " (" + p3Points + " -> " + (p3Points - (int)((p3Points * 0.05) + 20)) + ")"); p3Points -= (int)((p3Points * 0.05) + 20); gsMsg.p3.points = p3Points.ToString(); break; case 3: add = (int)((p2Points + p1Points) / p3Points) + 20; WriteToLog(gsMsg.p3.user_id + " earned: " + add + " points." + " (" + p3Points + " -> " + (p3Points + add) + ")"); p3Points += add; gsMsg.p3.points = p3Points.ToString(); winner = gsMsg.p3; WriteToLog(gsMsg.p2.user_id + " lost: " + (int)((p2Points * 0.05) + 20) + " points." + " (" + p2Points + " -> " + (p2Points - (int)((p2Points * 0.05) + 20)) + ")"); p2Points -= (int)((p2Points * 0.05) + 20); gsMsg.p2.points = p2Points.ToString(); WriteToLog(gsMsg.p1.user_id + " lost: " + (int)((p1Points * 0.05) + 20) + " points." + " (" + p1Points + " -> " + (p1Points - (int)((p1Points * 0.05) + 20)) + ")"); p1Points -= (int)((p1Points * 0.05) + 20); gsMsg.p1.points = p1Points.ToString(); break; } WriteToLog(winner.user_id + " WON!"); foreach (NetworkConnection c in m_Connections) { //// Example to send a handshake message: GameEndMsg m = new GameEndMsg(); m.p1 = gsMsg.p1; m.p2 = gsMsg.p2; m.p3 = gsMsg.p3; m.winner = winner; m.LogData = File.ReadAllText(logPath); SendToClient(JsonUtility.ToJson(m), c); } // Do the match break; case Commands.PLAYER_DC: PlayerDCMsg pdMsg = JsonUtility.FromJson <PlayerDCMsg>(recMsg); Debug.Log("Removed Spawn data of: " + pdMsg.PlayerID); AllSpawnMsg.Remove(FindPlayerSpawnMsg(pdMsg.PlayerID)); DCPlayer(pdMsg); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }