Exemplo n.º 1
0
        public void CreateLobbyAndAddOnePlayerAndSetTwiceLobby()
        {
            bool   hasThrown = false;
            string lobbyName = "toto";

            Connection.StartListening(ConnectionType.TCP, new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0));

            IPEndPoint     lastServerIPEndPoint       = (System.Net.IPEndPoint)Connection.ExistingLocalListenEndPoints(ConnectionType.TCP)[0];
            ConnectionInfo targetServerConnectionInfo = new
                                                        ConnectionInfo(lastServerIPEndPoint.Address.MapToIPv4().ToString(),
                                                                       lastServerIPEndPoint.Port);

            try
            {
                LobbyManager.AddLobby(lobbyName);
                TCPConnection connection = TCPConnection.GetConnection(targetServerConnectionInfo);
                LobbyManager.GetLobby(lobbyName).AddPlayer(connection);
                LobbyManager.GetLobby(lobbyName).AddPlayer(connection);
            }
            catch (Exception)
            {
                hasThrown = true;
            }
            finally
            {
                LobbyManager.DeleteLobby(lobbyName);
            }
            NetworkComms.Shutdown();
            Assert.AreEqual(true, hasThrown);
        }
Exemplo n.º 2
0
        public void CreateLobbyAndGetByName()
        {
            string lobbyName1 = "toto";

            LobbyManager.AddLobby(lobbyName1);
            Assert.AreNotEqual(null, LobbyManager.GetLobby(lobbyName1));
            LobbyManager.DeleteLobby(lobbyName1);
            Assert.AreEqual(null, LobbyManager.GetLobby(lobbyName1));
        }
Exemplo n.º 3
0
        public void CreateTwoLobbiesSameName()
        {
            string lobbyName1 = "toto";
            string lobbyName2 = lobbyName1;
            bool   hasThrown  = false;

            try
            {
                LobbyManager.AddLobby(lobbyName1);
                LobbyManager.AddLobby(lobbyName2);
            }
            catch (Exception)
            {
                hasThrown = true;
            }
            finally
            {
                LobbyManager.DeleteLobby(lobbyName1);
                LobbyManager.DeleteLobby(lobbyName2);
            }
            Assert.AreEqual(true, hasThrown);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handle the specified header, connection and message.
        /// </summary>
        /// <returns>The handler.</returns>
        /// <param name="header">Header.</param>
        /// <param name="connection">Connection.</param>
        /// <param name="message">Message.</param>
        private static void Handler(PacketHeader header, Connection connection, string message)
        {
            var connectInfos = ConnectionManager.Get(connection);
            var pseudo       = connectInfos.Pseudo;

            Console.WriteLine("Player " +
                              pseudo +
                              " selected lobby " + message);
            // Check if requested lobby exists.
            // I.  If it exists, try to join it.
            //       1) If it is full -> send error back to client and ask for another lobby
            //       2) Else -> Unregister this handler, respond to client, and switch to lobby room
            // II. If it does not exist, create lobby, respond to client, and switch to lobby room

            var lobby = LobbyManager.GetLobby(message);

            if (lobby == null)
            {
                // Create lobby
                Console.WriteLine("Player " + pseudo + " created lobby " + message);
                LobbyManager.AddLobby(message);
                lobby = LobbyManager.GetLobby(message);
            }
            try
            {
                lobby.AddPlayer(connection);
                connectInfos.Lobby = lobby;
                Unregister(connection);
                LobbyRoom.Register(connection);
                connection.SendObject("LobbyInfo", "Joined Lobby " + message + ": there are " + lobby.NbPlayers + " players.");
                NetworkGame.Register(connection);
            }
            catch (Exception e)
            {
                connection.SendObject("LobbyError", e.Message);
            }
        }
Exemplo n.º 5
0
        public async Task Test([Remainder] string arg = "")
        {
            SocketUser author = Context.Message.Author;

            string[] args = arg.Split(' '); //! Seperate multiple args

            var embed = new EmbedBuilder();

            embed.WithTitle("Lobby Message:");
            embed.WithColor(new Color(0, 255, 0));

            if (args[0].ToLower() == "create" || args[0].ToLower() == "new")
            {
                Lobby createdLobby = new Lobby
                {
                    Owner          = author,
                    PlayerCapacity = 10,
                    Players        = new List <SocketUser>()
                };
                LobbyManager.AddPlayer(createdLobby, author);

                try
                {
                    if (args[1] != null)
                    {
                        LobbyManager.ChangeTitle(args[1], author);
                    }
                }
                catch (IndexOutOfRangeException)
                {
                    LobbyManager.ChangeTitle($"{author.Username}'s lobby", author);
                }

                List <SocketUser> additionalPlayers = null;
                if (Context.Message.MentionedUsers.Count >= 1)
                {
                    additionalPlayers = new List <SocketUser>();
                    foreach (SocketUser su in Context.Message.MentionedUsers)
                    {
                        additionalPlayers.Add(su);
                    }
                }

                try
                {
                    if (args[2] != null)
                    {
                        LobbyManager.SetCapacity(author, Convert.ToInt32(args[2]));
                    }
                    embed.WithDescription($"Created lobby: {createdLobby.GameTitle}");
                    embed.WithFooter($"{createdLobby.CurrentPlayers} / {createdLobby.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                }
                catch
                {
                    LobbyManager.SetCapacity(author, 10);
                    embed.WithDescription($"Created lobby: {createdLobby.GameTitle}");
                    embed.WithFooter($"{createdLobby.CurrentPlayers} / {createdLobby.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                }
            }
            if (args[0].ToLower() == "join")
            {
                if (Context.Message.MentionedUsers.Count >= 1)
                {
                    try
                    {
                        Lobby target = LobbyManager.FindLobby(Context.Message.MentionedUsers.FirstOrDefault <SocketUser>());
                        LobbyManager.AddPlayer(target, author);

                        embed.WithDescription($"{author.Username} joined {target.Owner.Username}'s lobby: {target.GameTitle}");
                        embed.AddInlineField("In-lobby players:", target.Players);
                        embed.WithFooter($"{target.CurrentPlayers} / {target.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                    }
                    catch (NullReferenceException)
                    {
                        await Context.Channel.SendMessageAsync("No lobby exists! Creating new lobby.");

                        List <SocketUser> additionalPlayers = null;

                        if (Context.Message.MentionedUsers.Count >= 1)
                        {
                            additionalPlayers = new List <SocketUser>();
                            foreach (SocketUser su in Context.Message.MentionedUsers)
                            {
                                additionalPlayers.Add(su);
                            }
                        }

                        LobbyManager.AddLobby(author, $"{author.Username}'s lobby", additionalPlayers);
                        Lobby target = LobbyManager.FindLobby(author);

                        embed.WithDescription($"{author.Username} created a lobby.");
                        embed.AddInlineField("In-lobby players:", target.Players);
                        embed.WithFooter($"{target.CurrentPlayers} / {target.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                    }
                }
                else
                {
                    try
                    {
                        Lobby target = LobbyManager.FindLobby(args[1]);
                        LobbyManager.AddPlayer(target, author);

                        embed.WithDescription($"{author.Username} joined {target.Owner.Username}'s lobby: {target.GameTitle}");
                        embed.AddInlineField("In-lobby players:", target.Players);
                        embed.WithFooter($"{target.CurrentPlayers} / {target.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                    }
                    catch (NullReferenceException)
                    {
                        await Context.Channel.SendMessageAsync("No lobby exists! Creating new lobby.");

                        List <SocketUser> additionalPlayers = null;

                        if (Context.Message.MentionedUsers.Count >= 1)
                        {
                            additionalPlayers = new List <SocketUser>();
                            foreach (SocketUser su in Context.Message.MentionedUsers)
                            {
                                additionalPlayers.Add(su);
                            }
                        }

                        LobbyManager.AddLobby(author, args[1], additionalPlayers);
                        Lobby target = LobbyManager.FindLobby(author);

                        embed.WithDescription($"{author.Username} created a lobby.");
                        embed.AddInlineField("In-lobby players:", target.Players);
                        embed.WithFooter($"{target.CurrentPlayers} / {target.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                    }
                }
            }
            if (args[0].ToLower() == "leave" || args[0].ToLower() == "exit")
            {
                try
                {
                    Lobby target = LobbyManager.FindLobby(author);
                    LobbyManager.RemovePlayer(target, author);

                    embed.WithDescription($"{author.Username} left {target.Owner.Username}'s lobby: {target.GameTitle}");
                    embed.AddInlineField("In-lobby players:", target.Players);
                    embed.WithFooter($"{target.CurrentPlayers} / {target.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                }
                catch (NullReferenceException)
                {
                    await Context.Channel.SendMessageAsync("You're not in a lobby!");
                }
            }
            if (args[0].ToLower() == "edit" || args[0].ToLower() == "change")
            {
                Lobby target = LobbyManager.FindLobby(author);
                if (args[1].ToLower() == "cap" || args[1].ToLower() == "capacity")
                {
                    LobbyManager.SetCapacity(author, Convert.ToInt32(args[2]));

                    embed.WithDescription($"{author.Username} changed his lobby's capacity to: {target.PlayerCapacity}");
                    embed.AddInlineField("In-lobby players:", target.Players);
                    embed.WithFooter($"{target.CurrentPlayers} / {target.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                }
                else if (args[1].ToLower() == "name" || args[1].ToLower() == "title")
                {
                    LobbyManager.ChangeTitle(args[2], author);

                    embed.WithDescription($"{author.Username} changed his lobby's title to: {target.GameTitle}");
                    embed.AddInlineField("In-lobby players:", target.Players);
                    embed.WithFooter($"{target.CurrentPlayers} / {target.PlayerCapacity} users", "https://i.imgur.com/9Y0vuCO.png");
                }
            }
            await Context.Channel.SendMessageAsync("", false, embed);
        }