コード例 #1
0
ファイル: ServerTest.cs プロジェクト: colholt/Teamtris
        public void EndGameDetection()
        {
            lobbyManager.createLobby(4, "bob", 5, "no", (WebSocketSharp.WebSocket)null, "five");
            EndPacket endPacket = new EndPacket();

            Assert.That(lobbyManager.checkGameEnd(endPacket) == true);
        }
コード例 #2
0
 /**
  * #function LobbyManager::checkGameEnd |
  * @author columbus |
  * @desc determines whether or not the game has ended after receiving valid input from client |
  * @header public bool checkGameEnd(EndPacket ep) |
  * @param EndPacket ep : received EndPacket |
  * @returns bool ep : asserts whether or not the game has ended |
  */
 public bool checkGameEnd(EndPacket ep)
 {
     return(ep != null);
 }
コード例 #3
0
    protected override void OnMessage(MessageEventArgs e)
    {
        // Console.WriteLine(e.Data);
        Packet packet   = JsonConvert.DeserializeObject <Packet>(e.Data);
        string socketID = ID;

        // join packet -- 0
        if (packet.type == Packets.JOIN)
        {
            JoinPacket jPacket = JsonConvert.DeserializeObject <JoinPacket>(packet.data);
            joinLobby(jPacket.lobbyID, jPacket.playerID, jPacket.name, socketID);
        }
        // create packet -- 1
        else if (packet.type == Packets.CREATE)
        {
            try
            {
                Console.WriteLine("making new create packet");
                // Create a lobby with given parameters
                CreatePacket createPacket = JsonConvert.DeserializeObject <CreatePacket>(packet.data);
                createLobby(createPacket.maxPlayers, createPacket.name, createPacket.playerID, socketID, Context.WebSocket);
            }
            catch (NullReferenceException nre)
            {
                Console.WriteLine("hi");
                Console.WriteLine(nre);
            }
        }
        // start packet -- 2
        else if (packet.type == 2)
        {
            startGame(packet);
        }
        else if (packet.type == Packets.PLAYER_INPUT)
        {
            PlayerInputPacket playerInputPacket = JsonConvert.DeserializeObject <PlayerInputPacket>(packet.data);
            Player            currentPlayer     = null;
            foreach (Player player in lobbies[playerInputPacket.lobbyID].players)
            {
                if (player.socketID == ID)
                {
                    currentPlayer = player;
                }
            }
            if (currentPlayer == null)
            {
                // no valid player found in lobby
                return;
            }
            // validate move
            foreach (Player player in lobbies[playerInputPacket.lobbyID].players)
            {
                if (player.socketID != ID)
                {
                    if (player.currentBlockPosition != null)
                    {
                        foreach (int[] pos1 in player.currentBlockPosition)
                        {
                            foreach (int[] pos2 in playerInputPacket.shapeIndices)
                            {
                                if (pos1 == pos2)
                                {
                                    Send("COLLISION");
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            UpdatePacket update = processInput(playerInputPacket);
            if (playerInputPacket.move != "freeze")
            {
                currentPlayer.currentBlockPosition = playerInputPacket.shapeIndices;
                playerInputPacket.playerID         = currentPlayer.id;
                foreach (Player player in lobbies[playerInputPacket.lobbyID].players)
                {
                    if (player.socketID != ID)
                    {
                        Sessions.SendTo(JsonConvert.SerializeObject(update), player.socketID);
                    }
                }
            }
            else
            {
                // remove column is ID # 2
                // remove area is   ID # 3
                // area removes 2 up, 2 left, 2 right, 2 down
                Lobby lobby = lobbies[playerInputPacket.lobbyID];

                update.shapeIndices = playerInputPacket.shapeIndices;
                foreach (int[] pos in playerInputPacket.shapeIndices)
                {
                    // FREEZE
                    lobby.game.board.board[pos[0], pos[1]] = pos[2];
                }

                // Prints prints = new Prints();
                // Console.WriteLine("LOBBY BOARD");
                // prints.PrintMultiDimArr(lobby.game.board.board);

                checkRows(lobby);

                foreach (Player player in lobby.players)
                {
                    if (player.socketID != ID)
                    {
                        Sessions.SendTo(JsonConvert.SerializeObject(update), player.socketID);
                    }
                }
            }
            // on place piece put on board ;GJ
        }
        else if (packet.type == Packets.BOT_UPDATE) // 7
        {
            BotPacket bot = JsonConvert.DeserializeObject <BotPacket>(packet.data);
            if (bot.action == 1)
            {
                lobbies[bot.lobbyID].botCount++;
                lobbies[bot.lobbyID].numPlayers++;
                alertLobby(-2, bot.lobbyID, Packets.ADD_BOT);
            }
            else if (bot.action == 0)
            {
                lobbies[bot.lobbyID].botCount--;
                lobbies[bot.lobbyID].numPlayers--;
                alertLobby(-2, bot.lobbyID, Packets.REMOVE_BOT);
            }
        }
        else if (packet.type == Packets.SHAPE_BLUEPRINT)
        {
            ShapeBlueprintPacket sbp = JsonConvert.DeserializeObject <ShapeBlueprintPacket>(packet.data);
            foreach (Player player in lobbies[sbp.lobbyID].players)
            {
                if (player.socketID != ID)
                {
                    Sessions.SendTo(JsonConvert.SerializeObject(sbp), player.socketID);
                }
                else
                {
                    //player.currentBlock = sbp.shapeBlueprint;
                }
            }
        }
        else if (packet.type == Packets.TEAM_NAME)
        {
            // format the packet to be a scors packet with all the team information
            TeamPacket tPacket = JsonConvert.DeserializeObject <TeamPacket>(packet.data);

            foreach (Player player in lobbies[tPacket.lobbyid].players)
            {
                if (player.socketID != ID)
                {
                    Sessions.SendTo(JsonConvert.SerializeObject(tPacket), player.socketID);
                }
                else
                {
                    //player.currentBlock = sbp.shapeBlueprint;
                }
            }
        }
        else if (packet.type == Packets.GAME_END)
        {
            EndPacket end = JsonConvert.DeserializeObject <EndPacket>(packet.data);
            if (checkGameEnd(end))
            {
                foreach (Player player in lobbies[end.lobbyID].players)
                {
                    Sessions.SendTo(JsonConvert.SerializeObject(end), player.socketID);
                }
            }
        }
        else if (packet.type == Packets.POS_UPDATE)
        {
            PositionPacket pp = JsonConvert.DeserializeObject <PositionPacket>(packet.data);
            foreach (Player player in lobbies[pp.lobbyID].players)
            {
                if (player.socketID == ID)
                {
                    player.currentBlockPosition = pp.shapeIndices;
                }
            }
        }
        else
        {
            Console.WriteLine("bad packet");
            Console.WriteLine(packet.type);
            Send("bad packet!!! :(");
        }
    }