Beispiel #1
0
        public static void RemoveFromLobby(Program p, uint connectionId)
        {
            var lobby = Helper.GetLobbyByConnectionId(p, connectionId);

            p.Connections[connectionId].LobbyId = uint.MaxValue;

            //update clients
            foreach (var playerIds in p.Lobbies[lobby.LobbyId].Players)
            {
                var          mem    = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(mem);

                writer.Write((System.Int16) 4);
                writer.Write((System.UInt32)connectionId);
                writer.Flush();

                p.Connections[playerIds].DataSocket.send(mem.ToArray());
            }

            lobby.Players.Remove(connectionId);
            if (lobby.Players.Count == 0)
            {
                DeleteLobby(p, lobby.LobbyId);
            }
            else
            {
                if (lobby.HostId == connectionId)
                {
                    SetHost(p, lobby.Players[0]);
                }
            }

            //send lobbies to player
        }
Beispiel #2
0
 public static void PassHost(Program p, uint connectionId, uint newHostId)
 {
     if (ConnectionState.EnsureConnectionInLobby(p, connectionId))
     {
         var lobby = Helper.GetLobbyByConnectionId(p, connectionId);
         if (lobby.Players.Contains(newHostId))
         {
             SetHost(p, newHostId);
         }
     }
 }
Beispiel #3
0
        public static void StartLobby(Program p, uint connectionId)
        {
            if (ConnectionState.EnsureConnectionInLobby(p, connectionId) && ConnectionState.EnsureConnectionLobbyHost(p, connectionId))
            {
                var lobby = Helper.GetLobbyByConnectionId(p, connectionId);

                var game = new Game(lobby.Players);
                game.GameId = p.GameIdGenerator.Next();
                p.Games.Add(game.GameId, game);

                game.RPCHandler   = new RPCHandler();
                game.GameInstance = new SecretHitlerGame(game.RPCHandler);



                //update clients
                foreach (var playerIds in p.Lobbies[lobby.LobbyId].Players)
                {
                    var          mem    = new MemoryStream();
                    BinaryWriter writer = new BinaryWriter(mem);

                    writer.Write((System.Int16) 5);
                    writer.Write((System.UInt32)game.GameId);
                    writer.Flush();

                    p.Connections[playerIds].DataSocket.send(mem.ToArray());

                    p.Connections[playerIds].LobbyId = uint.MaxValue;
                    p.Connections[playerIds].GameId  = game.GameId;
                }

                game.GameInstance.Init();

                string[] names = new string[p.Lobbies[lobby.LobbyId].Players.Count];
                for (int i = 0; i < p.Lobbies[lobby.LobbyId].Players.Count; i++)
                {
                    names[i] = p.Connections[p.Lobbies[lobby.LobbyId].Players[i]].Username;
                }

                game.GameInstance.OnStart(p.Lobbies[lobby.LobbyId].Players.ToArray(), names);

                DeleteLobby(p, lobby.LobbyId);
            }
        }
Beispiel #4
0
        public static void SetHost(Program p, uint connectionId)
        {
            var lobby = Helper.GetLobbyByConnectionId(p, connectionId);

            p.Lobbies[lobby.LobbyId].HostId = connectionId;

            //update clients
            foreach (var playerIds in p.Lobbies[lobby.LobbyId].Players)
            {
                var          mem    = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(mem);

                writer.Write((System.Int16) 3);
                writer.Write((System.UInt32)connectionId);
                writer.Flush();

                p.Connections[playerIds].DataSocket.send(mem.ToArray());
            }
        }