Example #1
0
        /*
         * Lobby deleted
         */
        private void RemoveLobby(Player p)
        {
            LobbyServerSide l = lobbies[p.Lobby.Id];

            lobbies.Remove(p.Lobby.Id);
            Console.WriteLine(string.Format("Lobby deleted: {0}", l.Name));
        }
Example #2
0
        /*
         * Called when a new player readies up
         */
        private void HandlePlayerReady(Player p, IPEndPoint endPoint)
        {
            LobbyServerSide l = lobbies[p.Lobby.Id];

            l.Players[p.LobbyPos].GameState = p.GameState;
            Console.WriteLine("    " + l.Players[p.LobbyPos].Name + " has readied up.");

            int readies = 0;

            foreach (Player player in l.Players)
            {
                if (player != null && player.GameState == GameState.LobbyReady)
                {
                    readies++;
                }
            }
            Console.WriteLine(string.Format("{0} out of {1} players are ready in {2}.", readies, MaxPlayersPerLobby, l.Name));

            if (readies == MaxPlayersPerLobby)
            {
                foreach (Player player in l.Players)
                {
                    player.GameState = GameState.GameStarted;
                }
                Console.WriteLine(l.Name + " is going to start.");

                SendGameStartMessage(l);
            }
            SendPlayersInLobby(l, p);
        }
Example #3
0
        /*
         * Player chooses an existing lobby to join and sends its ID here
         * Everything from the lobby is then sent back
         */
        private void JoinExistingLobby(Player p, IPEndPoint endPoint)
        {
            string          playerJson = "null";
            LobbyServerSide l          = lobbies[p.Lobby.Id];
            bool            isJoinable = CountPlayersInLobby(l) < MaxPlayersPerLobby;

            if (isJoinable)
            {
                int pos = 0;
                for (int i = 0; i < l.Players.Length; i++)
                {
                    if (l.Players[i] == null)
                    {
                        pos = i;
                        break;
                    }
                }


                l.Players[pos]        = p;
                p.Lobby.Name          = l.Name;
                p.Lobby.PlayerCount   = CountPlayersInLobby(l);
                p.LobbyPos            = pos;
                p.Lobby.MulticastIP   = l.EndPoint.Address.ToString();
                p.Lobby.MulticastPort = l.EndPoint.Port;
                p.GameState           = GameState.LobbyUnready;

                playerJson = JsonConvert.SerializeObject(p);
                Console.WriteLine(p.Name + " has joined " + l.Name);
            }
            byte[] msg = Encoding.ASCII.GetBytes(playerJson);

            server.Send(msg, msg.Length, endPoint);
        }
Example #4
0
        /*
         * Called when a new player readies up
         */
        private void HandlePlayerUnready(Player p)
        {
            LobbyServerSide l = lobbies[p.Lobby.Id];

            l.Players[p.LobbyPos].GameState = p.GameState;

            SendPlayersInLobby(l, p);
            Console.WriteLine("    " + l.Players[p.LobbyPos].Name + " is not ready.");
        }
Example #5
0
        private void SendGameStartMessage(LobbyServerSide l)
        {
            Message msg = new Message();

            msg.MessageType = MessageType.GameStart;
            string msgJson = JsonConvert.SerializeObject(msg);

            byte[] msgBytes = Encoding.ASCII.GetBytes(msgJson);
            l.UdpLobby.Send(msgBytes, msgBytes.Length, l.EndPoint);
        }
Example #6
0
        private void SyncPlayersInLobby(LobbyServerSide l, Player p)
        {
            foreach (Message m in p.Messages)
            {
                string messageJson = JsonConvert.SerializeObject(m);

                byte[] msg = Encoding.ASCII.GetBytes(messageJson);
                l.UdpLobby.Send(msg, msg.Length, l.EndPoint);
            }
            p.Messages.Clear();
        }
Example #7
0
        /*
         * Sends all players info to multicast group
         */
        private void SendPlayersInLobby(LobbyServerSide l, Player p)
        {
            Message message = new Message();

            message.MessageType = MessageType.LobbyStatus;
            message.Description = JsonConvert.SerializeObject(l.Players);
            string messageJson = JsonConvert.SerializeObject(message);

            byte[] msg = Encoding.ASCII.GetBytes(messageJson);
            l.UdpLobby.Send(msg, msg.Length, l.EndPoint);
        }
Example #8
0
        private int CountPlayersInLobby(LobbyServerSide l)
        {
            int count = 0;

            for (int i = 0; i < l.Players.Length; i++)
            {
                if (l.Players[i] != null)
                {
                    count++;
                }
            }
            return(count);
        }
Example #9
0
        /*
         * Player removed from server
         * ifthere's no one left lobby is deleted else message is sent to the remaining players
         */
        private void RemovePlayer(Player p, IPEndPoint endPoint)
        {
            LobbyServerSide l = lobbies[p.Lobby.Id];

            l.Players[p.LobbyPos] = null;
            Console.WriteLine("Player: " + p.Name + " left");

            if (CountPlayersInLobby(l) > 0)
            {
                SendPlayersInLobby(l, p);
                Console.WriteLine("    " + p.Name + " left the lobby.");
            }
            else
            {
                RemoveLobby(p);
            }
        }
Example #10
0
        /*
         * Lobby creation from owner player
         * Owner player sends lobby's name
         */
        private void NewLobby(Player p, IPEndPoint endPoint)
        {
            LobbyServerSide newLobby = new LobbyServerSide(p.Lobby.Name);

            p.Lobby.Id            = Guid.NewGuid();
            p.Lobby.MulticastIP   = GetNextAdress();
            p.Lobby.MulticastPort = multicastPort;

            newLobby.UdpLobby = new UdpClient();
            newLobby.EndPoint = new IPEndPoint(IPAddress.Parse(p.Lobby.MulticastIP), p.Lobby.MulticastPort);
            newLobby.UdpLobby.JoinMulticastGroup(newLobby.EndPoint.Address);

            lobbies.Add(p.Lobby.Id, newLobby);
            Console.WriteLine(string.Format("New lobby created: {0}, {1} listenning in {2}:{3}", newLobby.Name, p.Lobby.Id, p.Lobby.MulticastIP, p.Lobby.MulticastPort));
            p.GameState = GameState.LobbyConnecting;

            JoinCreatedLobby(p, endPoint);
        }
Example #11
0
        /*
         * Called when a new player joins a lobby
         */
        private void HandlePlayerJoinLobby(Player p)
        {
            LobbyServerSide l = lobbies[p.Lobby.Id];

            SendPlayersInLobby(l, p);
        }