public void AssignScore(DataCodes pResultCode) { switch (pResultCode) { case DataCodes.P1_ROUND_WON: if (Client.PlayerNum == 1) { Score += 1; ResultText.SetText("YOU WIN"); } else { ResultText.SetText("YOU LOSE"); } break; case DataCodes.P1_ROUND_LOST: if (Client.PlayerNum == 2) { Score += 1; ResultText.SetText("YOU WIN"); } else { ResultText.SetText("YOU LOSE"); } break; case DataCodes.ROUND_TIE: break; } ResultText.gameObject.SetActive(true); }
void Update() { // Update NetworkDriver Driver.ScheduleUpdate().Complete(); #region Update Connection List // Remove stale connections for (int i = 0; i < connections.Length; i++) { if (!connections[i].IsCreated) { connections.RemoveAtSwapBack(i); --i; } } // Accept new incoming connections NetworkConnection nc; while ((nc = Driver.Accept()) != default) { connections.Add(nc); Debug.Log("Accepted incoming connection"); } ServerConnectionsNumber.SetText("Open Connections: " + connections.Length); #endregion // Create a DataStreamReader and loop through all connections. DataStreamReader stream; for (int i = 0; i < connections.Length; i++) { // Skip a connection if it is stale. if (!connections[i].IsCreated) { continue; } // Pop event for current connection. Run while there is an event that needs to be processed. NetworkEvent.Type cmd; while ((cmd = Driver.PopEventForConnection(connections[i], out stream)) != NetworkEvent.Type.Empty) { if (cmd == NetworkEvent.Type.Data) { #region uint data uint dataCode = stream.ReadByte(); try { switch (dataCode) { case (uint)DataCodes.PING: SendActionToClient(connections[i], (uint)DataCodes.PING); break; case (uint)DataCodes.DEBUG_MESSAGE: SendActionToClient(connections[i], (uint)DataCodes.DEBUG_MESSAGE); break; case (uint)DataCodes.P1_READY: pOneReady = true; PlayersReady(); break; case (uint)DataCodes.P2_READY: pTwoReady = true; PlayersReady(); break; case (uint)DataCodes.PASS_TURN: SendActionToOther(connections[i], (uint)DataCodes.PASS_TURN); break; case (uint)DataCodes.P1_STEEN: P1_ACTION = DataCodes.P1_STEEN; SendActionToOther(connections[i], (uint)DataCodes.PASS_TURN); break; case (uint)DataCodes.P1_PAPIER: P1_ACTION = DataCodes.P1_PAPIER; SendActionToOther(connections[i], (uint)DataCodes.PASS_TURN); break; case (uint)DataCodes.P1_SCHAAR: P1_ACTION = DataCodes.P1_SCHAAR; SendActionToOther(connections[i], (uint)DataCodes.PASS_TURN); break; case (uint)DataCodes.P2_STEEN: P2_ACTION = DataCodes.P2_STEEN; DetermineTurnWinner(); SendActionToOther(connections[i], (uint)DataCodes.PASS_TURN); break; case (uint)DataCodes.P2_PAPIER: P2_ACTION = DataCodes.P2_PAPIER; DetermineTurnWinner(); SendActionToOther(connections[i], (uint)DataCodes.PASS_TURN); break; case (uint)DataCodes.P2_SCHAAR: P2_ACTION = DataCodes.P2_SCHAAR; DetermineTurnWinner(); SendActionToOther(connections[i], (uint)DataCodes.PASS_TURN); break; case (uint)DataCodes.END_GAME: EndGame(); break; default: break; } } catch (System.Exception e) { Debug.LogError(e.Message); } if (dataCode.ToString().StartsWith("3")) { int _data = int.Parse(dataCode.ToString().Split('3')[1]); if (Players.Count <= 0) { Players.Add(new DataStructs.User() { UserID = _data, PlayerNum = 1, Score = 0, Connection = connections[i] }); SendActionToClient(connections[i], (uint)DataCodes.ASSIGN_P1); } else { foreach (DataStructs.User user in Players) { // Check if the player has already logged in. if (user.UserID == _data) { SendActionToClient(connections[i], (uint)DataCodes.LOGIN_ERROR); duplicatePlayer = true; } } if (!duplicatePlayer) { Players.Add(new DataStructs.User() { UserID = _data, PlayerNum = 2, Score = 0, Connection = connections[i] }); SendActionToClient(connections[i], (uint)DataCodes.ASSIGN_P2); } } } #endregion } // On a disconnect event, reset connection to default values, making it a stale connection. else if (cmd == NetworkEvent.Type.Disconnect) { Debug.Log("A user disconnected from the server."); connections[i] = default; } } } }