private static void EndMatch() { int aliveMonsters = 0, aliveSurvivors = 0; foreach (GamePlayer gp in GamePlayers.Values) { if (gp.IsMonster() && gp.IsAlive) { aliveMonsters++; } else if (gp.IsSurvivor() && gp.IsAlive) { aliveSurvivors++; } } if (aliveMonsters == 1 && aliveSurvivors == 0) { GamePlayer winner = GamePlayers.Values.First(); string msg = winner.Nickname + " (" + winner.Role + ") won the game."; foreach (GamePlayer connectedPlayer in Party) { Transmitter.Send(connectedPlayer.PlayerSocket, msg); } Game.ResetMatch(); } else if (aliveMonsters == 0 && aliveSurvivors > 0) { foreach (GamePlayer connectedPlayer in Party) { string msg = "Survivors won."; Transmitter.Send(connectedPlayer.PlayerSocket, msg); } Game.ResetMatch(); } }
public static void Attack(Socket socket) { string nickname = GetNicknameBySocket(socket); GamePlayer gp = GamePlayers[nickname]; List <GamePlayer> closerPlayers = GetCloserPlayers(gp); foreach (GamePlayer playerToAttack in closerPlayers) { gp.Attack(playerToAttack); string msgToAttacker, msgToAttacked; if (playerToAttack.IsAlive) { msgToAttacker = "You damaged " + gp.Damage + " life points to " + playerToAttack.Nickname + " ( " + playerToAttack.Life + " life remaining )"; msgToAttacked = gp.Nickname + " damaged you with " + gp.Damage + " life points. You got " + playerToAttack.Life + " remaining life points."; } else { msgToAttacker = "You killed " + playerToAttack.Nickname + "."; msgToAttacked = gp.Nickname + " killed you."; RemoveDeadPlayer(playerToAttack); } Transmitter.Send(gp.PlayerSocket, msgToAttacker); Transmitter.Send(playerToAttack.PlayerSocket, msgToAttacked); } EndMatch(); InspectCloserPlayers(nickname); }
public static void EndMatchByTimer() { int aliveMonsters = 0, aliveSurvivors = 0; foreach (GamePlayer gp in GamePlayers.Values) { if (gp.IsMonster()) { aliveMonsters++; } else if (gp.IsSurvivor()) { aliveSurvivors++; } } if (aliveSurvivors > 0) { foreach (GamePlayer connectedPlayer in Party) { string msg = "Survivors won."; Transmitter.Send(connectedPlayer.PlayerSocket, msg); } Game.ResetMatch(); } else if (aliveMonsters > 1 && aliveSurvivors == 0) { foreach (GamePlayer connectedPlayer in Party) { string msg = "Nobody won."; Transmitter.Send(connectedPlayer.PlayerSocket, msg); } Game.ResetMatch(); } }
public static void InspectCloserPlayers(string nickname) { if (IsActiveMatch()) { GamePlayer gp = GamePlayers[nickname]; string msg = "Your position is [ " + (gp.Spot.Item1 + 1) + " ][ " + (gp.Spot.Item2 + 1) + " ]"; Transmitter.Send(gp.PlayerSocket, msg); for (int auxI = -1; auxI <= 1; auxI++) { for (int auxJ = -1; auxJ <= 1; auxJ++) { int x = gp.Spot.Item1 + auxI; int y = gp.Spot.Item2 + auxJ; if (ValidIndex(x, y)) { if (!IsSamePlayer(x, y, gp.Spot.Item1, gp.Spot.Item2)) { if (!IsEmptySpot(x, y)) { GamePlayer gpToInspect = Matrix[x, y]; msg = "At [ " + (x + 1) + " ][ " + (y + 1) + " ] there is a " + gpToInspect.Role + " ( " + gpToInspect.Nickname + " ) with " + gpToInspect.Life + " remaining life points."; Transmitter.Send(gp.PlayerSocket, msg); } } } } } } }
public static void Execute(Socket socket) { while (true) { string command = Transmitter.Receive(socket); var cmd = Utils.ToLwr(command); if (Game.IsCurrentlyPlayingMatch(socket)) { if (Game.IsAlive(socket)) { if (cmd.Equals("attack")) { Game.Attack(socket); } else if (cmd.Equals("move")) { } else { Transmitter.Send(socket, "ATTACK or MOVE [][]"); } } else { Transmitter.Send(socket, "You lost this match. Wait until it finishes."); } } else { if (cmd.Equals("newplayer")) { string nick = Transmitter.Receive(socket); string avatar = Transmitter.Receive(socket); try { Player player = new Player() { Nickname = nick, Avatar = avatar }; Game.AddPlayer(player); Transmitter.Send(socket, "Player registered."); } catch (NicknameInUseEx ex) { Transmitter.Send(socket, ex.Message); } } else if (cmd.Equals("connect")) { string nick = Transmitter.Receive(socket); try { Game.ConnectPlayerToParty(GamePlayer.Create(socket, nick)); Transmitter.Send(socket, "Player connected to the game."); } catch (ConnectedNicknameInUseEx ex) { Transmitter.Send(socket, ex.Message); } catch (NotExistingPlayer ex) { Transmitter.Send(socket, ex.Message); } } else if (cmd.Equals("enter")) { string role = Transmitter.Receive(socket); try { Game.TryEnter(); string nickname = Game.GetNicknameBySocket(socket); Game.AssignRole(role, nickname); Game.AddPlayerToMatch(nickname); Transmitter.Send(socket, "Logged in to match correctely. Start to play."); } catch (NotActiveMatch ex) { Transmitter.Send(socket, ex.Message); } catch (IncorrectRoleEx ex) { Transmitter.Send(socket, ex.Message); } catch (MaxNumberOfPlayers ex) { Transmitter.Send(socket, ex.Message); } } else if (cmd.Equals("exit")) { IPEndPoint remoteIpEndPoint = socket.RemoteEndPoint as IPEndPoint; Console.WriteLine("Cliente " + remoteIpEndPoint.Address + " cerrado."); socket.Close(); break; } } } }