Пример #1
0
    public void createLobby(int maxPlayers, string name, int id, string socketID, WebSocketSharp.WebSocket socketContext, string token = "-1")
    {
        int playerID = 1;

        if (token == "-1")
        {
            token = getToken();
        }
        // initialize a new lobby, player, and list of players
        Lobby  newLobby  = new Lobby(token, 4); // hard coded max players
        Player newPlayer = new Player(1, name, socketID, socketContext);

        newLobby.players = new List <Player>();

        newLobby.players.Add(newPlayer);
        lobbies[newLobby.id] = newLobby;

        // Create a packet to confirm creation of new lobby
        ConfirmationPacket confirmationPacket = new ConfirmationPacket();

        confirmationPacket.lobbyID  = newLobby.id;
        confirmationPacket.playerID = playerID;
        Send(JsonConvert.SerializeObject(confirmationPacket));

        Console.WriteLine(maxPlayers + name + id);
        Console.WriteLine("sending new lobby");
    }
Пример #2
0
    public LobbyInfoPacket joinLobby(string lobbyID, int playerID, string name, string socketID)
    {
        lobbyID = lobbyID.ToLower();
        Lobby lobby;

        if (lobbies == null)
        {
            Console.WriteLine("lobbies is null");
        }
        if (lobbies.ContainsKey(lobbyID))
        {
            lobby = lobbies[lobbyID];
            if (lobby.numPlayers < lobby.maxPlayers)
            {
                if (lobby.lobbyState == LobbyState.PLAYING && lobby.game.current_time > 5)
                {
                    return((LobbyInfoPacket)null);
                }
                int newPlayerID = lobby.players.Count + 1;
                lobby.numPlayers += 1;
                Player newPlayer = new Player(newPlayerID, name, socketID, Context.WebSocket);
                lobby.players.Add(newPlayer);
                // send message to user of lobby id
                ConfirmationPacket confirmationPacket = new ConfirmationPacket();
                confirmationPacket.lobbyID  = lobbyID;
                confirmationPacket.playerID = newPlayerID;
                alertLobby(playerID, lobbyID, Packets.UPDATE);
                LobbyInfoPacket lobbyInfoPacket = new LobbyInfoPacket();
                lobbyInfoPacket.lobbyID    = lobby.id;
                lobbyInfoPacket.players    = lobby.players;
                lobbyInfoPacket.lobbyID    = lobbyID;
                lobbyInfoPacket.maxPlayers = lobby.maxPlayers;
                lobbyInfoPacket.dataType   = Packets.UPDATE;
                lobbyInfoPacket.numBots    = lobby.botCount;
                Send(JsonConvert.SerializeObject(lobbyInfoPacket));
                Send(JsonConvert.SerializeObject(confirmationPacket, Formatting.Indented, new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                }));
                return(lobbyInfoPacket);
            }
            else
            {
                if (lobby.botCount > 0 && lobby.lobbyState != LobbyState.PLAYING)
                {
                    lobbies[lobbyID].botCount--;
                    lobbies[lobbyID].numPlayers--;
                    alertLobby(-2, lobbyID, Packets.REMOVE_BOT);
                    joinLobby(lobbyID, playerID, name, socketID);
                }
            }
        }
        else if (lobbyID == "testing")
        {
            lobby = lobbies["five"];
            lobby.players.Add(new Player(1, "bob", "no", (WebSocketSharp.WebSocket)null));
            return(alertLobby(0, "testing", Packets.UPDATE));
        }
        else
        {
            //send message invalid ID
            Send("bad");
        }
        return(null);
    }