public void regularUpdate() { if (!lobbyStarted) { return; } int caughtHiders = (isHider && !isCaught) ? 1 : 0; //Check if any client sent a message foreach (Guest g in guests) { if (!IsConnected(g.socket)) { disconnectList.Add(g); Debug.Log("Guest " + g.guestName + " disconnected."); } else { NetworkStream s = g.stream; if (s.DataAvailable) { StreamReader reader = new StreamReader(s, true); string data = reader.ReadLine(); if (data != null) { //Process received message from guest OnIncomingData(g, data); } } if (gameStarted) { caughtHiders += (g.isHider && !g.isCaught) ? 1 : 0; } } } if (match != null && gameStarted && caughtHiders == 0) { match.setMatchElapsedTime(MatchOrchestrator.MATCH_TIMEOUT); } if (match != null && guests.Count == 0) { endGameFlag = true; match.stop(); match = null; } //Re-arrange client list after disconnects (to avoid 'foreach' conflict) for (int i = 0; i < disconnectList.Count; i++) { removedPlayersQueue.Add(guests[guests.IndexOf(disconnectList[i])].playerController.getGuestName()); guests[guests.IndexOf(disconnectList[i])].socket.Close(); guests.Remove(guests[guests.IndexOf(disconnectList[i])]); } disconnectList = new List <Guest>(); }
//What to do when message is received from host private void OnIncomingData(string data) { string[] dataInfo = data.Split('/'); if (dataInfo[0] == "animator") { NetworkAnimator(dataInfo[1], float.Parse(dataInfo[2]), float.Parse(dataInfo[3]), float.Parse(dataInfo[4]), bool.Parse(dataInfo[5]), bool.Parse(dataInfo[6]), new Vector3(float.Parse(dataInfo[7]), float.Parse(dataInfo[8]), float.Parse(dataInfo[9])), new Vector3(float.Parse(dataInfo[10]), float.Parse(dataInfo[11]), float.Parse(dataInfo[12])), dataInfo[13], bool.Parse(dataInfo[14])); } else if (dataInfo[0] == "state") { int curState = int.Parse(dataInfo[1]); switch (curState) { case 1: match.setState(curState); match.setHidingElapsedTime(int.Parse(dataInfo[2])); break; case 2: match.setState(curState); match.setSeekingElapsedTime(int.Parse(dataInfo[2])); break; case 3: match.setState(curState); match.setMatchElapsedTime(int.Parse(dataInfo[2])); match.setZoneCenter(new Vector3(float.Parse(dataInfo[3]), float.Parse(dataInfo[4]), float.Parse(dataInfo[5]))); match.setZoneScale(new Vector3(float.Parse(dataInfo[6]), float.Parse(dataInfo[7]), float.Parse(dataInfo[8]))); break; case 4: match.setState(curState); match.setScoreboardElapsedTime(int.Parse(dataInfo[2])); break; case 5: goto case 1; case 6: goto case 2; case 7: goto case 3; case 8: goto case 4; } } else if (dataInfo[0] == "start") { match = new MatchOrchestrator(); isHider = bool.Parse(dataInfo[1]); gameStarted = true; } else if (dataInfo[0] == "caught") { if (dataInfo[1].Equals(guestName)) { isCaught = true; } else { foreach (PlayerNetworkController p in players) { if (p.getGuestName().Equals(dataInfo[1])) { p.setIsCaught(true); removedPlayersQueue.Add(p.getGuestName()); break; } } } } else if (dataInfo[0] == "stop") { endGameFlag = true; match.stop(); match = null; gameStarted = false; } else { newHost = dataInfo[0]; List <string> guestNames = new List <string>(); for (int i = 1; i < dataInfo.Length; i++) { string[] playerInfo = dataInfo[i].Split('\\'); if (guestName.Equals(playerInfo[0])) { score = int.Parse(playerInfo[1]); continue; } guestNames.Add(playerInfo[0]); if (players.Count == 0) { PlayerNetworkController playerNC = new PlayerNetworkController(playerInfo[0], int.Parse(playerInfo[1])); players.Add(playerNC); Debug.Log(playerInfo[0] + " joined"); continue; } for (int j = 0; j < players.Count; j++) { if (players[j].getGuestName().Equals(playerInfo[0])) { players[j].setScore(int.Parse(playerInfo[1])); goto skipAdd; } } PlayerNetworkController playernc = new PlayerNetworkController(playerInfo[0], int.Parse(playerInfo[1])); players.Add(playernc); Debug.Log(playerInfo[0] + " joined"); skipAdd :; } updatePlayers(guestNames); } }