public HostNetwork(string username, MatchOrchestrator match, bool isHider) { hostName = username; this.match = match; this.isHider = isHider; player = Resources.FindObjectsOfTypeAll <GameObject>().FirstOrDefault(g => g.CompareTag("Player")).GetComponent <PlayerController>(); removedPlayersQueue = new List <string>(); }
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>(); }
public void updateHost() { if (!lobbyStarted) { return; } if (gameStarted && match != null) { int state = match.getState(); switch (state) { case 1: Broadcast("state/" + state + "/" + match.getHidingElapsedTime()); break; case 2: Broadcast("state/" + state + "/" + match.getSeekingElapsedTime()); break; case 3: Broadcast("state/" + state + "/" + match.getMatchElapsedTime() + "/" + match.getZoneCenter().x + "/" + match.getZoneCenter().y + "/" + match.getZoneCenter().z + "/" + match.zone.GetComponent <Transform>().localScale.x + "/" + match.zone.GetComponent <Transform>().localScale.y + "/" + match.zone.GetComponent <Transform>().localScale.z); break; case 4: Broadcast("state/" + state + "/" + match.getScoreboardElapsedTime()); break; case 5: goto case 1; case 6: goto case 2; case 7: goto case 3; case 8: goto case 4; case 9: Broadcast("stop"); endGameFlag = true; match.stop(); match = null; break; } } }
//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); } }
//Host Pressed on Start Match public void startMatch() { match = new MatchOrchestrator(guests); foreach (Guest g in guests) { g.seekingWins = 0; g.hidingWins = 0; } //Assigning each player a role (Hider or Seeker) List <bool> roles = match.generateRoles(); System.Random rand = new System.Random(); if (roles != null) { if (roles.Count > 1) { int hiders = 0, seekers = 0; for (int i = 0; i < roles.Count; i++) { if (roles[i]) { hiders++; } else { seekers++; } } if (hiders > seekers) { isHider = false; } else if (hiders < seekers) { isHider = true; } else { isHider = rand.Next(0, 2) == 1 ? false : true; } } else { isHider = !roles[0]; } } else { isHider = rand.Next(0, 2) == 1 ? false : true; } match.start(); foreach (Guest g in guests) { Broadcast("start/" + g.isHider, new List <Guest>() { g }); } gameStarted = true; }