Пример #1
0
        public void SendLobbyData()
        {
            var obj = new Dict();

            obj["SteamID"] = SteamCore.PlayerId();
            obj["Maps"]    = Maps;

            string lobby_name = SteamMatches.GetLobbyData("name");
            //string lobby_info = SteamMatches.GetLobbyData("LobbyInfo");
            string lobby_info = Jsonify(GetLobbyInfo());

            if (lobby_info != null && lobby_info.Length > 0 && lobby_name.Length > 0)
            {
                obj["LobbyInfo"]        = lobby_info;
                obj["LobbyName"]        = lobby_name;
                obj["LobbyLoading"]     = false;
                obj["CountDownStarted"] = SteamMatches.GetLobbyData("CountDownStarted");
            }
            else
            {
                obj["LobbyLoading"] = true;
            }

            Send("lobby", obj);
        }
Пример #2
0
        public void _StartGame()
        {
            //var lobby_data = SteamMatches.GetLobbyData("LobbyInfo");
            //var lobby = JsonConvert.DeserializeObject(lobby_data, typeof(LobbyInfo));
            //LobbyInfo = (LobbyInfo)lobby;

            LobbyInfo = GetLobbyInfo();

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

            if (game_started == "true")
            {
                var player = LobbyInfo.Players.Where(match => match.SteamID != 0).First();
                var args   = player.Args + ' ' + LobbyInfo.CommonArgs;

                Program.ParseOptions(args);
                Program.Spectate            = true;
                Program.Server              = false;
                Program.Client              = true;
                Program.StartupPlayerNumber = 0;
            }
            else
            {
                var args = ThisPlayer.Args + ' ' + LobbyInfo.CommonArgs;

                Program.ParseOptions(args);
            }

            SetScenarioToLoad(Program.StartupMap);
            Networking.Start();

            SteamMatches.SetLobbyData("GameStarted", "true");
        }
Пример #3
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());
        }
Пример #4
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();
            }
        }
Пример #5
0
        public void OnFindLobbies(bool result)
        {
            if (result)
            {
                Console.WriteLine("Failure during lobby search.");
                return;
            }

            var obj = new Dict();

            int num_lobbies = SteamMatches.NumLobbies();

            Console.WriteLine("Found {0} lobbies", num_lobbies);
            obj["NumLobbies"] = num_lobbies;

            var lobby_list  = new List <Dict>(num_lobbies);
            var lobby_names = new List <String>(num_lobbies);

            for (int i = 0; i < num_lobbies; i++)
            {
                string lobby_name        = SteamMatches.GetLobbyData(i, "name");
                string game_started      = SteamMatches.GetLobbyData(i, "GameStarted");
                string countdown_started = SteamMatches.GetLobbyData(i, "CountDownStarted");
                if (!lobby_names.Contains(lobby_name) && !(countdown_started == "true" && game_started != "true"))
                {
                    int member_count = SteamMatches.GetLobbyMemberCount(i);
                    int capacity     = SteamMatches.GetLobbyCapacity(i);

                    // Console.WriteLine("lobby {0} name: {1} members: {2}/{3}", i, lobby_name, member_count, capacity);

                    if (capacity <= 0)
                    {
                        continue;
                    }

                    var lobby = new Dict();
                    lobby["Name"]        = lobby_name;
                    lobby["Index"]       = i;
                    lobby["MemberCount"] = member_count;
                    lobby["Capacity"]    = capacity;
                    lobby["GameStarted"] = game_started;

                    lobby["NumPlayers"]    = SteamMatches.GetLobbyData(i, "NumPlayers").MaybeInt();
                    lobby["NumSpectators"] = SteamMatches.GetLobbyData(i, "NumSpectators").MaybeInt();
                    lobby["MaxPlayers"]    = SteamMatches.GetLobbyData(i, "MaxPlayers").MaybeInt();

                    lobby_names.Add(lobby_name);
                    lobby_list.Add(lobby);
                }
            }

            obj["Lobbies"] = lobby_list;
            obj["Online"]  = true;

            Send("lobbies", obj);
        }
Пример #6
0
        public void ResetLobby()
        {
            if (!SteamMatches.IsLobbyOwner())
            {
                return;
            }

            SteamMatches.SetLobbyData("CountDownStarted", "");
            SteamMatches.SetLobbyData("GameStarted", "");
        }
Пример #7
0
        void Test_OnCreateLobby(bool result)
        {
            Console.WriteLine(result);

            string player_name = SteamCore.PlayerName();
            string lobby_name  = string.Format("{0}'s lobby", player_name);

            SteamMatches.SetLobbyData("name", lobby_name);

            SteamMatches.FindLobbies(Test_OnFindLobbies);
        }
Пример #8
0
        public void FindLobbies()
        {
            InTrainingLobby = false;

            if (!SteamCore.SteamIsConnected())
            {
                Offline();
            }

            SteamMatches.FindLobbies(OnFindLobbies);
        }
Пример #9
0
        public void LeaveGame()
        {
            // Use this if you want leaving a game to return you to the lobby,
            // rather than returning you to the main menu.
            //return ReturnToLobby(sender, e);

            LeaveGameNetwork();

            SteamMatches.LeaveLobby();

            ReturnToMainMenu();
        }
Пример #10
0
        public void FindFriendLobbies()
        {
            InTrainingLobby = false;

            if (!SteamCore.SteamIsConnected())
            {
                Offline();
            }

            SteamMatches.SetLobbyCallbacks(null, null, null, () => OnFindLobbies(false));
            SteamMatches.FindFriendLobbies(OnFindLobbies);
        }
Пример #11
0
        public void OnLobbyChatEnter(string message)
        {
            if (Program.GameStarted)
            {
                return;
            }

            if (message != null && message.Length > 0)
            {
                Console.WriteLine("lobby chat message: " + message);
                SteamMatches.SendChatMsg(message);
            }
        }
Пример #12
0
        public void JoinLobby(int lobby)
        {
            InTrainingLobby     = false;
            Program.GameStarted = false;

            if (SteamMatches.InLobby())
            {
                try { SteamMatches.LeaveLobby(); } catch { }
            }
            ;

            SteamMatches.JoinLobby(lobby, OnJoinLobby, OnLobbyChatUpdate, OnLobbyChatMsg, OnLobbyDataUpdate);
        }
Пример #13
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", "");
        }
Пример #14
0
        public static void SetLobbyName()
        {
            string lobby_name = "";

            if (SteamCore.InOfflineMode())
            {
                lobby_name = "Local lobby. Offline.";
            }
            else
            {
                lobby_name = string.Format("{0}'s lobby", SteamCore.PlayerName());
            }

            SteamMatches.SetLobbyData("name", lobby_name);
        }
Пример #15
0
        public void CreateLobby(string lobbyType, bool trainingLobby)
        {
            InTrainingLobby     = trainingLobby;
            Program.GameStarted = false;

            if (!SteamCore.SteamIsConnected())
            {
                SteamCore.SetOfflineMode(true);
            }

            if (SteamMatches.InLobby())
            {
                try { SteamMatches.LeaveLobby(); } catch { }
            }
            ;
            SteamMatches.CreateLobby(OnCreateLobby, StringToLobbyType(lobbyType));
        }
Пример #16
0
        public void OnLobbyDataUpdate()
        {
            Console.WriteLine("lobby data updated");

            if (Program.GameStarted)
            {
                return;
            }

            string map = SteamMatches.GetLobbyData("MapName");

            if (map != null && map.Length > 0)
            {
                SetMap(map);
            }

            SendLobbyData();
        }
Пример #17
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();
            }
        }
Пример #18
0
        public LobbyInfo GetLobbyInfo()
        {
            LobbyInfo info = new LobbyInfo();

            var players = SteamMatches.GetLobbyData("Players");

            info.Players = (List <PlayerLobbyInfo>)JsonConvert.DeserializeObject(players, typeof(List <PlayerLobbyInfo>));

            var spectators = SteamMatches.GetLobbyData("Spectators");

            info.Spectators = (List <PlayerLobbyInfo>)JsonConvert.DeserializeObject(spectators, typeof(List <PlayerLobbyInfo>));

            var game_params = SteamMatches.GetLobbyData("Params");

            info.Params = (GameParameters)JsonConvert.DeserializeObject(game_params, typeof(GameParameters));

            info.CommonArgs = SteamMatches.GetLobbyData("CommonArgs");

            return(info);
        }
Пример #19
0
        void Test_OnFindLobbies(bool result)
        {
            Console.WriteLine(result);

            if (result)
            {
                Console.WriteLine("Failure during lobby search.");
                return;
            }

            int n = SteamMatches.NumLobbies();

            Console.WriteLine("Found {0} lobbies", n);

            for (int i = 0; i < n; i++)
            {
                Console.WriteLine(SteamMatches.GetLobbyData(i, "name"));
            }

            SteamMatches.JoinCreatedLobby(Test_OnJoinLobby, Test_OnLobbyChatUpdate, Test_OnLobbyChatMsg, Test_OnLobbyDataUpdate);
        }
Пример #20
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();
                }
            }
        }
Пример #21
0
        public void OnCreateLobby(bool result)
        {
            if (result)
            {
                Console.WriteLine("Failure during lobby creation.");
                return;
            }

            Program.GameStarted = false;

            SteamMatches.SetLobbyMemberLimit(64);
            SetLobbyName();
            SteamMatches.SetLobbyData("NumPlayers", "1");
            SteamMatches.SetLobbyData("NumSpectators", "0");
            SteamMatches.SetLobbyData("MaxPlayers", "4");

            if (InTrainingLobby)
            {
                SteamMatches.SetLobbyJoinable(false);
            }

            Console.WriteLine("Trying to join the created lobby.");
            SteamMatches.JoinCreatedLobby(OnJoinLobby, OnLobbyChatUpdate, OnLobbyChatMsg, OnLobbyDataUpdate);
        }
Пример #22
0
        public void SendAnnouncement(string message)
        {
            string msg = string.Format("%a{0}", message);

            SteamMatches.SendChatMsg(msg);
        }
Пример #23
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();
        }
Пример #24
0
        public void SelectKingdom(string kingdom)
        {
            string msg = string.Format("%k{0}", kingdom);

            SteamMatches.SendChatMsg(msg);
        }
Пример #25
0
        public void SelectTeam(string team)
        {
            string msg = string.Format("%t{0}", team);

            SteamMatches.SendChatMsg(msg);
        }
Пример #26
0
 public void SetLobbyType(int lobbyType)
 {
     SteamMatches.SetLobbyType(lobbyType);
 }
Пример #27
0
 public void Join()
 {
     SteamMatches.SendChatMsg("%j1");
 }
Пример #28
0
 public void Spectate()
 {
     SteamMatches.SendChatMsg("%j0");
 }
Пример #29
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);
        }
Пример #30
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);
        }