public bool TeamAvailableTo(int team, PlayerLobbyInfo player) { if (player.SteamID == 0) { return(!LobbyInfo.Players.Exists(match => match != player && match.GameTeam == team)); } else { bool PreventTeamCollissions = false; if (!player.HasPickedTeam) { PreventTeamCollissions = true; } if (PreventTeamCollissions) { return(!LobbyInfo.Players.Exists(match => match.SteamID != 0 && match.SteamID != player.SteamID && match.GameTeam == team)); } else { return(true); } } }
public int FirstKingdomAvailableTo(PlayerLobbyInfo player) { for (int kingdom = 1; kingdom <= Program.MaxPlayers; kingdom++) { if (KingdomAvailableTo(kingdom, player)) { return(kingdom); } } return(0); }
public int FirstTeamAvailableTo(PlayerLobbyInfo player) { for (int team = 1; team <= Program.MaxTeams; team++) { if (TeamAvailableTo(team, player)) { return(team); } } return(0); }
public bool KingdomAvailableTo(int kingdom, PlayerLobbyInfo player) { if (player.SteamID == 0) { return(!LobbyInfo.Players.Exists(match => match != player && match.GamePlayer == kingdom)); } else { return(!LobbyInfo.Players.Exists(match => match.SteamID != 0 && match.SteamID != player.SteamID && match.GamePlayer == kingdom)); } }
public bool TryToJoin(string name, PlayerLobbyInfo player) { if (!player.Spectator) { return(false); } if (!LobbyInfo.Players.Exists(_player => _player.SteamID == 0)) { return(false); } var kingdom = FirstKingdomAvailableTo(player); var team = FirstTeamAvailableTo(player); if (kingdom <= 0) { return(false); } player.GamePlayer = kingdom; player.GameTeam = team; player.Spectator = false; bool found_a_spot = false; for (int i = 0; i < LobbyInfo.Players.Count; i++) { if (LobbyInfo.Players[i].SteamID == 0) { LobbyInfo.Players[i] = player; found_a_spot = true; break; } } if (!found_a_spot) { return(false); } LobbyInfo.Spectators.RemoveAll(_player => _player.SteamID == player.SteamID); SendAnnouncement(name + " has joined the melee!"); return(true); }
public bool ProcessAsAction(string msg, UInt64 id, string name) { if (msg[0] != '%') { return(false); // Action message must start with a '%' } if (msg.Length < 3) { return(false); // Action message must have at least 3 characters, eg '%p3' } if (msg[1] == 'a') { try { string remainder = msg.Substring(2); if (remainder != null & remainder.Length > 0) { GameClass.Game.AddChatMessage("Game", remainder); return(true); } } catch { Console.WriteLine("bad chat command : {0}", msg); return(false); } return(false); } else { if (!SteamMatches.IsLobbyOwner()) { // Only the lobby owner can act on action messages. // Everyone else should ignore them, so return true, // signalling this action was already processed. return(true); } // Get the info for the player sending the message. PlayerLobbyInfo player = null; try { player = LobbyInfo.Players.Where(_player => _player.SteamID == id).First(); player.Spectator = false; } catch { player = LobbyInfo.Spectators.Where(_player => _player.SteamID == id).First(); player.Spectator = true; } // The third character in the message stores the numeric value. // Parse it and store in this variable. int value = -1; try { string valueStr = "" + msg[2]; int.TryParse(valueStr, out value); } catch { Console.WriteLine("bad chat command : {0}", msg); return(false); } // Join message. if (msg[1] == 'j') { // The numeric value for joining/spectating is 1/0. if (value != 0 && value != 1) { return(false); } if (value == 0) // Player wants to Spectate. { if (player.Spectator) { return(false); } for (int i = 0; i < LobbyInfo.Players.Count; i++) { if (LobbyInfo.Players[i].SteamID == player.SteamID) { LobbyInfo.Players[i] = new PlayerLobbyInfo(); } } player.Spectator = true; LobbyInfo.Spectators.RemoveAll(_player => _player.SteamID == player.SteamID); LobbyInfo.Spectators.Add(player); SendAnnouncement(name + " likes to watch."); } else // Player wants to Join. { TryToJoin(name, player); } } // Change kingdom message. else if (msg[1] == 'k' && !player.Spectator) { // The numeric value for player (kingdom) must be one of 1, 2, 3, 4. if (value <= 0 || value > Program.MaxPlayers) { return(false); } if (KingdomAvailableTo(value, player)) { SendAnnouncement(name + " has changed kingdoms!"); player.GamePlayer = value; } } // Change team message. else if (msg[1] == 't' && !player.Spectator) { // The numeric value for team must be one of 1, 2, 3, 4. if (value <= 0 || value > Program.MaxTeams) { return(false); } if (TeamAvailableTo(value, player)) { SendAnnouncement(name + " has changed teams!"); player.GameTeam = value; } } else { return(false); } } SetLobbyInfo(); return(true); }
public void BuildLobbyInfo(ulong joining_player_id = 0) { if (!SteamMatches.IsLobbyOwner()) { return; } PlayerLobbyInfo joining_player = null; var PrevInfo = LobbyInfo; LobbyInfo = new LobbyInfo(Program.MaxPlayers); int members = SteamMatches.GetLobbyMemberCount(); for (int i = 0; i < members; i++) { ulong SteamID = SteamMatches.GetMemberId(i); PlayerLobbyInfo player = new PlayerLobbyInfo(); player.Spectator = true; int index = 0; foreach (var prev_player in PrevInfo.Players) { if (prev_player.SteamID == SteamID) { player = LobbyInfo.Players[index] = prev_player; player.Spectator = false; } index++; } player.Name = SteamMatches.GetMemberName(i); if (player.Spectator) { player.SteamID = SteamMatches.GetMemberId(i); LobbyInfo.Spectators.Add(player); } if (player.SteamID == joining_player_id) { joining_player = player; } } // For every player that doesn't have a kingdom/team set, // choose an available initial value. foreach (var player in LobbyInfo.Players) { if (player.GamePlayer <= 0 || player.GamePlayer > Program.MaxPlayers) { player.GamePlayer = FirstKingdomAvailableTo(player); } if (player.GameTeam <= 0 || player.GameTeam > Program.MaxTeams) { player.GameTeam = FirstTeamAvailableTo(player); player.HasPickedTeam = true; } } // Set the current player to be the host. LobbyInfo.Players.ForEach(player => player.Host = player.SteamID == SteamCore.PlayerId()); LobbyInfo.Spectators.ForEach(player => player.Host = player.SteamID == SteamCore.PlayerId()); // If there is a joinging player try to add them and then rebuild. if (joining_player_id != 0 && joining_player != null) { TryToJoin(joining_player.Name, joining_player); BuildLobbyInfo(); return; } BuildArgs(); SetLobbyInfo(); }
public void ConstructArgs(StringBuilder teams, StringBuilder kingdoms, int num_players, string server, string users, string spectators, PlayerLobbyInfo player, bool spectator) { string type = player.Host ? "--server" : "--client"; player.Args = string.Format("{0} --p {1}", type, player.GamePlayer); }