Beispiel #1
0
        public void SetLobbyInfo()
        {
            if (!SteamMatches.IsLobbyOwner())
            {
                return;
            }

            // This ensures the lobby info changes and forces and update,
            // even if no relevant values changed.
            LobbyInfo.MarkAsChanged();

            // Assign unused teams/player spots to non-gamer players. (SteamID == 0).
            foreach (var player in LobbyInfo.Players)
            {
                if (player.SteamID != 0)
                {
                    continue;
                }
                player.GamePlayer    = FirstKingdomAvailableTo(player);
                player.GameTeam      = FirstTeamAvailableTo(player);
                player.HasPickedTeam = true;
            }

            SetLobbyName();
            BuildArgs();

            SteamMatches.SetLobbyData("Players", Jsonify(LobbyInfo.Players));
            SteamMatches.SetLobbyData("Spectators", Jsonify(LobbyInfo.Spectators));
            SteamMatches.SetLobbyData("Params", Jsonify(LobbyInfo.Params));
            SteamMatches.SetLobbyData("CommonArgs", LobbyInfo.CommonArgs);

            SteamMatches.SetLobbyData("NumPlayers", LobbyInfo.Players.Count(_player => _player.SteamID != 0).ToString());
            SteamMatches.SetLobbyData("NumSpectators", LobbyInfo.Spectators.Count.ToString());
            SteamMatches.SetLobbyData("MaxPlayers", Program.MaxPlayers.ToString());
        }
Beispiel #2
0
        public void OnLobbyChatUpdate(int StateChange, UInt64 id)
        {
            Console.WriteLine("lobby chat updated");

            if (Program.GameStarted)
            {
                // Game is already started so add player to the spectator list.
                if (Program.Server && StateChange == SteamMatches.ChatMember_Entered)
                {
                    Thread.Sleep(500);
                    Networking._Server.AddSpectator(id);
                    Console.WriteLine("Spectator joined, resynchronizing network.");
                    GameClass.World.SynchronizeNetwork();
                }
            }
            else
            {
                if (!IsHost && SteamMatches.IsLobbyOwner())
                {
                    Console.WriteLine("lobby owner left");
                    GameClass.Game.Send("setScreen", "disconnected-from-lobby", new { message = "The lobby host has left. Tell them they suck." });
                }
            }

            if (StateChange == SteamMatches.ChatMember_Entered)
            {
                BuildLobbyInfo(id);
            }
            else
            {
                BuildLobbyInfo();
            }
        }
Beispiel #3
0
        public void ResetLobby()
        {
            if (!SteamMatches.IsLobbyOwner())
            {
                return;
            }

            SteamMatches.SetLobbyData("CountDownStarted", "");
            SteamMatches.SetLobbyData("GameStarted", "");
        }
Beispiel #4
0
        public void StartGameCountdown()
        {
            // Only the lobby owner can start the match.
            if (!SteamMatches.IsLobbyOwner())
            {
                return;
            }

            // We never set the lobby to unjoinable.
            //SteamMatches.SetLobbyJoinable(false);

            SteamMatches.SetLobbyData("CountDownStarted", "true");
            SteamMatches.SetLobbyData("GameStarted", "");
        }
Beispiel #5
0
        public void OnJoinLobby(bool result)
        {
            LobbyInfo = new LobbyInfo(Program.MaxPlayers);

            if (result)
            {
                Console.WriteLine("Failure joining the lobby.");

                Send("joinFailed");
                SteamMatches.SetLobbyCallbacks(null, null, null, null);

                return;
            }

            BuildMapList();

            if (SteamMatches.IsLobbyOwner())
            {
                GameMapName = null;
                MapLoading  = true;
                SetMap(Maps[0]);
            }

            string lobbyName = SteamMatches.GetLobbyData("name");

            Console.WriteLine("joined lobby {0}", lobbyName);

            IsHost = SteamMatches.IsLobbyOwner();

            SendLobbyData();
            BuildLobbyInfo(joining_player_id: SteamCore.PlayerId());

            SteamP2P.SetOnP2PSessionRequest(OnP2PSessionRequest);
            SteamP2P.SetOnP2PSessionConnectFail(OnP2PSessionConnectFail);

            string game_started = SteamMatches.GetLobbyData("GameStarted");

            if (game_started == "true")
            {
                _StartGame();
            }
        }
Beispiel #6
0
        public void SetMap(string map_name)
        {
            Console.WriteLine("set map to {0}", map_name);

            string full_name = map_name + ".m3n";

            bool skip = false;

            if ((SetMapThread == null || !SetMapThread.IsAlive) && GameMapName == full_name)
            {
                skip = true;
            }

            if (!skip)
            {
                GameMapName = full_name;

                PrevMapThread         = SetMapThread;
                SetMapThread          = new Thread(() => SetMapThreadFunc(GameMapName));
                SetMapThread.Priority = ThreadPriority.Highest;
                SetMapThread.Start();
            }

            if (SteamMatches.IsLobbyOwner())
            {
                string current = SteamMatches.GetLobbyData("MapName");
                string name    = map_name;

                if (name.Length > 0 && name.Contains(".m3n"))
                {
                    name = name.Substring(0, name.Length - 4);
                }

                if (current != map_name)
                {
                    SteamMatches.SetLobbyData("MapName", name);
                    SetLobbyInfo();
                }
            }
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        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();
        }
Beispiel #9
0
        public void BuildArgs()
        {
            if (!SteamMatches.IsLobbyOwner())
            {
                return;
            }

            //"--server                --port 13000 --p 2 --t 1234 --n 1 --map Beset.m3n   --debug --w 1280 --h 720"
            var teams       = new StringBuilder("0000");
            var kingdoms    = new StringBuilder("0000");
            int num_players = 0;
            int i           = 0;

            foreach (var player in LobbyInfo.Players)
            {
                if (!player.Spectator)
                {
                    teams[player.GamePlayer - 1] = player.GameTeam.ToString()[0];
                    kingdoms[i] = player.GamePlayer.ToString()[0];
                    i++;
                }

                if (player.SteamID != 0)
                {
                    num_players++;
                }
            }

            string server = "", users = LobbyInfo.Players.Count.ToString();

            foreach (var player in LobbyInfo.Players)
            {
                if (player.Host)
                {
                    server = player.SteamID.ToString();
                }

                users += ' ' + player.SteamID.ToString();
            }

            string spectators = LobbyInfo.Spectators.Count.ToString();

            foreach (var player in LobbyInfo.Spectators)
            {
                if (player.Host)
                {
                    server = player.SteamID.ToString();
                }

                spectators += ' ' + player.SteamID.ToString();
            }

            foreach (var player in LobbyInfo.Players)
            {
                ConstructArgs(teams, kingdoms, num_players, server, users, spectators, player, spectator: false);
            }

            foreach (var player in LobbyInfo.Spectators)
            {
                player.GameTeam   = 0;
                player.GamePlayer = 0;

                ConstructArgs(teams, kingdoms, num_players, server, users, spectators, player, spectator: true);
            }

            ConstructCommonArgs(teams, kingdoms, num_players, server, users, spectators);
        }