public void StartMatch() { if (NumOfGamesText.text.Length > 0 && int.Parse(NumOfGamesText.text) > 0) { NumOfGames = int.Parse(NumOfGamesText.text); } if (clientScript.AllPlayers.Count == 3) { for (int i = 0; i < NumOfGames; i++) { GameStartMsg m = new GameStartMsg(); m.p1 = clientScript.AllPlayers[0]; m.p2 = clientScript.AllPlayers[1]; m.p3 = clientScript.AllPlayers[2]; SendToServer(JsonUtility.ToJson(m)); } } }
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; } }
private void ProcessMessage(Message m) { switch (m) { case MoveResMsg _: MoveResMsg resMove = (MoveResMsg)m; if (resMove.status == "OK") { Move(resMove.direction); } break; case PickUpResMsg _: //DO POPRAWY LOGIKA PickUpResMsg resPick = (PickUpResMsg)m; if (resPick.status == "OK") { TakePiece(); } break; case TestResMsg _: //DO POPRAWY LOGIKA TestResMsg resTest = (TestResMsg)m; if (resTest.status == "OK") { TestPiece(resTest.test); } break; case PlaceResMsg _: //DO POPRAWY LOGIKA PlaceResMsg resPlace = (PlaceResMsg)m; if (resPlace.status == "OK") { if (resPlace.placementResult == "Correct") { PlacePiece(PlacementResult.Correct); } else { PlacePiece(PlacementResult.Pointless); } } break; case DiscoverResMsg _: DiscoverResMsg resDiscover = (DiscoverResMsg)m; if (resDiscover.status == "OK") { Discover(resDiscover.fields); } break; case ConnectPlayerResMsg _: break; case ReadyResMsg _: break; case SetupResMsg _: break; case GameStartMsg _: GameStartMsg gameStart = (GameStartMsg)m; position = gameStart.position; board = gameStart.board; team.color = gameStart.team; if (gameStart.teamRole == TeamRole.Leader) { isLeader = true; } else { isLeader = false; } break; default: break; } }