Exemple #1
0
        // Once paging is active, classic commands are not processed anymore
        // Valid commands are (Enter), (N)ext, (P)revious, (Q)uit, (A)ll
        private void HandlePaging(PlayingClient playingClient, string command)
        {
            string lowerCommand = command.ToLowerInvariant();

            if (command == string.Empty || "next".StartsWith(lowerCommand)) // <Enter> or Next -> send next page
            {
                // Pages are always sent immediately asynchronously, don't use ProcessOutput even if in synchronous mode
                string nextPage = playingClient.Paging.GetNextPage(25); // TODO: configurable line count
                playingClient.Client.WriteData(nextPage);
                if (playingClient.Paging.HasPageLeft)                   // page left, send page instructions (no prompt)
                {
                    playingClient.Client.WriteData(ServerOptions.PagingInstructions);
                }
                else // no more page -> normal mode
                {
                    playingClient.Paging.Clear();
                    playingClient.Client.WriteData(playingClient.Player.Prompt);
                }
            }
            else if ("previous".StartsWith(lowerCommand))
            {
                string previousPage = playingClient.Paging.GetPreviousPage(25); // TODO: configurable line count
                playingClient.Client.WriteData(previousPage);
                if (playingClient.Paging.HasPageLeft)                           // page left, send page instructions (no prompt)
                {
                    playingClient.Client.WriteData(ServerOptions.PagingInstructions);
                }
                else // no more page -> normal mode
                {
                    playingClient.Paging.Clear();
                    playingClient.Client.WriteData(playingClient.Player.Prompt);
                }
            }
            else if ("quit".StartsWith(lowerCommand))
            {
                playingClient.Paging.Clear();
                playingClient.Client.WriteData(Environment.NewLine);
                playingClient.Client.WriteData(playingClient.Player.Prompt);
            }
            else if ("all".StartsWith(lowerCommand))
            {
                string remaining = playingClient.Paging.GetRemaining();
                playingClient.Paging.Clear();
                playingClient.Client.WriteData(remaining);
                playingClient.Client.WriteData(playingClient.Player.Prompt);
            }
        }
Exemple #2
0
        public IAdmin AddAdmin(IClient client, string name)
        {
            IAdmin admin = new Admin.Admin(Guid.NewGuid(), name);

            admin.SendData      += PlayerOnSendData;
            admin.PageData      += PlayerOnPageData;
            client.DataReceived += ClientPlayingOnDataReceived;
            PlayingClient playingClient = new PlayingClient
            {
                Client = client,
                Player = admin
            };

            lock (_playingClientLockObject)
            {
                _players.TryAdd(admin, playingClient);
                _clients.TryAdd(client, playingClient);
            }

            admin.Send("Welcome master {0}", name);

            return(admin);
        }
Exemple #3
0
        // TODO: remove
        // TEST PURPOSE
        public IPlayer AddPlayer(IClient client, string name)
        {
            IPlayer player = new Player.Player(Guid.NewGuid(), name);

            player.SendData     += PlayerOnSendData;
            player.PageData     += PlayerOnPageData;
            client.DataReceived += ClientPlayingOnDataReceived;
            PlayingClient playingClient = new PlayingClient
            {
                Client = client,
                Player = player
            };

            lock (_playingClientLockObject)
            {
                _players.TryAdd(player, playingClient);
                _clients.TryAdd(client, playingClient);
            }

            player.Send("Welcome {0}", name);

            return(player);
        }
Exemple #4
0
        private void LoginStateMachineOnLoginSuccessful(IClient client, string username, bool isAdmin, bool isNewPlayer)
        {
            Log.Default.WriteLine(LogLevels.Info, "LoginStateMachineOnLoginSuccessful");

            IPlayer playerOrAdmin = null;
            // if same user is already connected, remove old client and link new client to old player
            KeyValuePair <IPlayer, PlayingClient> previousPlayerPair;

            lock (_playingClientLockObject)
            {
                previousPlayerPair = _players.FirstOrDefault(x => x.Key.Name == username);
            }
            if (previousPlayerPair.Key != null)
            {
                Log.Default.WriteLine(LogLevels.Info, "Player was already connected, disconnect previous client and reuse player");

                // Save player
                playerOrAdmin = previousPlayerPair.Key; // TODO: pause client ????
                // Remove client and player from players/clients
                lock (_playingClientLockObject)
                {
                    PlayingClient oldPlayingClient;
                    bool          removed = _players.TryRemove(playerOrAdmin, out oldPlayingClient);
                    if (removed)
                    {
                        _clients.TryRemove(oldPlayingClient.Client, out oldPlayingClient);
                    }
                    // !!! PlayingClient removed from both collection must be equal
                }

                // Disconnect previous client
                previousPlayerPair.Value.Client.WriteData("Reconnecting on another client!!");
                previousPlayerPair.Value.Client.DataReceived -= ClientPlayingOnDataReceived;
                previousPlayerPair.Value.Client.Disconnect();

                Wiznet($"{username} has reconnected.", WiznetFlags.Logins);

                // Welcome back
                client.WriteData("Reconnecting to Mud.Net!!" + Environment.NewLine);
            }
            else
            {
                Wiznet($"{username} has connected.", WiznetFlags.Logins);

                // Welcome
                client.WriteData("Welcome to Mud.Net!!" + Environment.NewLine);
            }
            // TODO: if new player, avatar creation state machine
            if (isNewPlayer)
            {
            }

            // Remove login state machine
            LoginStateMachine loginStateMachine;

            _loginInClients.TryRemove(client, out loginStateMachine);
            if (loginStateMachine != null)
            {
                loginStateMachine.LoginFailed     -= LoginStateMachineOnLoginFailed;
                loginStateMachine.LoginSuccessful -= LoginStateMachineOnLoginSuccessful;
            }
            else
            {
                Log.Default.WriteLine(LogLevels.Error, "LoginStateMachineOnLoginSuccessful: LoginStateMachine not found for a client!!!");
            }

            // Remove login handlers
            client.DataReceived -= ClientLoginOnDataReceived;
            // Add playing handlers
            client.DataReceived += ClientPlayingOnDataReceived;

            bool loadPlayerOrAdmin = false;

            // Create a new player/admin only if not reconnecting
            if (playerOrAdmin == null)
            {
                playerOrAdmin = isAdmin
                    ? new Admin.Admin(Guid.NewGuid(), username)
                    : new Player.Player(Guid.NewGuid(), username);
                //
                playerOrAdmin.SendData += PlayerOnSendData;
                playerOrAdmin.PageData += PlayerOnPageData;
                loadPlayerOrAdmin       = true;
            }
            //
            PlayingClient newPlayingClient = new PlayingClient
            {
                Client = client,
                Player = playerOrAdmin
            };

            lock (_playingClientLockObject)
            {
                _players.TryAdd(playerOrAdmin, newPlayingClient);
                _clients.TryAdd(client, newPlayingClient);
            }

            // Save if isNewPlayer
            if (isNewPlayer)
            {
                playerOrAdmin.Save();
            }

            // Prompt
            client.WriteData(playerOrAdmin.Prompt);

            // Load player/admin (if needed)
            if (loadPlayerOrAdmin)
            {
                playerOrAdmin.Load(username);
            }
        }