Exemplo n.º 1
0
 public void handleErrorResponse(ResponseStatus status, IIncommingMessage rawMsg)
 {
     if (status != ResponseStatus.Success)
     {
         Debug.LogError(rawMsg.AsString());
         Events.Fire(EventNames.ShowDialogBox, DialogBoxData.CreateError(rawMsg.AsString()));
     }
 }
Exemplo n.º 2
0
 public bool serverResponseSuccess(ResponseStatus status, IIncommingMessage rawMsg)
 {
     if (status != ResponseStatus.Success)
     {
         Events.Fire(EventNames.ShowDialogBox, DialogBoxData.CreateError(rawMsg.AsString("Unkown error")));
         Debug.LogError(status + " - " + rawMsg.AsString("Unkown error"));
         return(false);
     }
     return(true);
 }
Exemplo n.º 3
0
 private static void handleRoundStarted(IIncommingMessage rawMsg)
 {
     if (ClientUIOverlord.currentState == ClientUIStates.PlayingGame && isInTournament)
     {
         ClientUIStateManager.requestGotoState(ClientUIStates.GameLobby, () => {
             NewGameCreator.handleJoinPreGame(GameInfoType.PreGame, rawMsg.AsString());
         });
     }
     else
     {
         NewGameCreator.handleJoinPreGame(GameInfoType.PreGame, rawMsg.AsString());
     }
 }
Exemplo n.º 4
0
        protected virtual void HandleLeaveChannel(IIncommingMessage message)
        {
            var chatUser = message.Peer.GetExtension <ChatUserExtension>();

            if (chatUser == null)
            {
                message.Respond("Chat cannot identify you", ResponseStatus.Unauthorized);
                return;
            }

            var channelName = message.AsString().ToLower();

            ChatChannel channel;

            Channels.TryGetValue(channelName, out channel);

            if (channel == null)
            {
                message.Respond("This channel does not exist", ResponseStatus.Failed);
                return;
            }

            channel.RemoveUser(chatUser);

            if (SetLastChannelAsLocal && chatUser.CurrentChannels.Count == 1)
            {
                chatUser.DefaultChannel = chatUser.CurrentChannels.First();
            }

            message.Respond(ResponseStatus.Success);
        }
        private void handleStartPreGame(IIncommingMessage rawMsg)
        {
            PreGame targetGame;
            string  roomID   = rawMsg.AsString();
            string  errorMsg = "";

            if (findGame(roomID, out targetGame, rawMsg) == false)
            {
                return;
            }
            else if (targetGame.canGameStart() == false)
            {
                errorMsg = "Not all players are ready";
            }
            else if (targetGame.isAdmin(rawMsg.Peer) == false)
            {
                errorMsg = "Only the Admin can start the game";
            }

            if (string.IsNullOrEmpty(errorMsg) == false)
            {
                rawMsg.Respond(errorMsg, ResponseStatus.Failed);
                return;
            }

            startGame(targetGame, rawMsg);
        }
Exemplo n.º 6
0
        protected virtual void HandleGetCompletionData(IIncommingMessage message)
        {
            var       spawnId = message.AsString();
            SpawnTask task;

            SpawnTasks.TryGetValue(spawnId, out task);

            if (task == null)
            {
                message.Respond("Invalid request", ResponseStatus.Failed);
                return;
            }

            /*
             * if (task.Requester != message.Peer){
             * message.Respond("You're not the requester", ResponseStatus.Unauthorized);
             * return;
             * }
             */

            if (task.FinalizationPacket == null)
            {
                message.Respond("Task has no completion data", ResponseStatus.Failed);
                return;
            }

            // Respond with data (dictionary of strings)
            message.Respond(task.FinalizationPacket.FinalizationData.ToBytes(), ResponseStatus.Success);
        }
Exemplo n.º 7
0
        protected virtual void OnGetUsersInChannelRequestHandler(IIncommingMessage message)
        {
            string responseMsg = string.Empty;

            var chatUser = message.Peer.GetExtension <ChatUserPeerExtension>();

            if (chatUser == null)
            {
                responseMsg = "Chat cannot identify you";

                logger.Error(responseMsg);

                message.Respond(responseMsg, ResponseStatus.Unauthorized);
                return;
            }

            var channelName = message.AsString();
            var channel     = GetOrCreateChannel(channelName);

            if (channel == null)
            {
                responseMsg = "This channel is forbidden";

                logger.Error(responseMsg);

                // There's no such channel
                message.Respond(responseMsg, ResponseStatus.Failed);
                return;
            }

            var users = channel.Users.Select(u => u.Username);

            message.Respond(users.ToBytes(), ResponseStatus.Success);
        }
Exemplo n.º 8
0
        protected virtual void HandleSetDefaultChannel(IIncommingMessage message)
        {
            var chatUser = message.Peer.GetExtension <ChatUserExtension>();

            if (chatUser == null)
            {
                message.Respond("Chat cannot identify you", ResponseStatus.Unauthorized);
                return;
            }

            var channelName = message.AsString();

            var channel = GetOrCreateChannel(channelName);

            if (channel == null)
            {
                // There's no such channel
                message.Respond("This channel is forbidden", ResponseStatus.Failed);
                return;
            }

            // Add user to channel
            channel.AddUser(chatUser);

            // Set the property of default chat channel
            chatUser.DefaultChannel = channel;

            // Respond with a "success" status
            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Handles password reset request
        /// </summary>
        /// <param name="message"></param>
        protected virtual void HandlePasswordResetRequest(IIncommingMessage message)
        {
            var email = message.AsString();
            var db    = Msf.Server.DbAccessors.GetAccessor <IAuthDatabase>();

            var account = db.GetAccountByEmail(email);

            if (account == null)
            {
                message.Respond("No such e-mail in the system", ResponseStatus.Unauthorized);
                return;
            }

            var code = Msf.Helper.CreateRandomString(4);

            db.SavePasswordResetCode(account, code);

            if (!Mailer.SendMail(account.Email, "Password Reset Code", string.Format(PasswordResetCode, code)))
            {
                message.Respond("Couldn't send an activation code to your e-mail");
                return;
            }

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 10
0
        private void HandleLobbyMasterChangeMsg(IIncommingMessage message)
        {
            var masterUsername = message.AsString();

            Data.GameMaster = masterUsername;
            Listener?.OnMasterChanged(masterUsername);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Handles a request from user to join a lobby
        /// </summary>
        /// <param name="message"></param>
        protected virtual void HandleJoinLobby(IIncommingMessage message)
        {
            var user = GetOrCreateLobbiesExtension(message.Peer);

            if (user.CurrentLobby != null)
            {
                message.Respond("You're already in a lobby", ResponseStatus.Failed);
                return;
            }

            var lobbyId = message.AsString();

            ILobby lobby;

            Lobbies.TryGetValue(lobbyId, out lobby);

            if (lobby == null)
            {
                message.Respond("Lobby was not found", ResponseStatus.Failed);
                return;
            }

            string error;

            if (!lobby.AddPlayer(user, out error))
            {
                message.Respond(error ?? "Failed to add player to lobby", ResponseStatus.Failed);
                return;
            }

            var data = lobby.GenerateLobbyData(user);

            message.Respond(data, ResponseStatus.Success);
        }
Exemplo n.º 12
0
        protected virtual void HandlePermissionLevelRequest(IIncommingMessage message)
        {
            var key = message.AsString();

            var extension = message.Peer.GetExtension <PeerSecurityExtension>();

            var currentLevel = extension.PermissionLevel;
            var newLevel     = currentLevel;

            var permissionClaimed = false;

            foreach (var entry in Permissions)
            {
                if (entry.Key == key)
                {
                    newLevel          = entry.PermissionLevel;
                    permissionClaimed = true;
                }
            }

            extension.PermissionLevel = newLevel;

            if (!permissionClaimed && !string.IsNullOrEmpty(key))
            {
                // If we didn't claim a permission
                message.Respond("Invalid permission key", ResponseStatus.Unauthorized);
                return;
            }

            message.Respond(newLevel, ResponseStatus.Success);
        }
Exemplo n.º 13
0
        protected virtual void HandleAesKeyRequest(IIncommingMessage message)
        {
            var extension = message.Peer.GetExtension <PeerSecurityExtension>();

            var encryptedKey = extension.AesKeyEncrypted;

            if (encryptedKey != null)
            {
                // There's already a key generated
                message.Respond(encryptedKey, ResponseStatus.Success);
                return;
            }

            // Generate a random key
            var aesKey = Msf.Helper.CreateRandomString(8);

            var clientsPublicKeyXml = message.AsString();

            // Deserialize public key
            var sr = new System.IO.StringReader(clientsPublicKeyXml);
            var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
            var clientsPublicKey = (RSAParameters)xs.Deserialize(sr);

            using (var csp = new RSACryptoServiceProvider()){
                csp.ImportParameters(clientsPublicKey);
                var encryptedAes = csp.Encrypt(Encoding.Unicode.GetBytes(aesKey), false);

                // Save keys for later use
                extension.AesKeyEncrypted = encryptedAes;
                extension.AesKey          = aesKey;

                message.Respond(encryptedAes, ResponseStatus.Success);
            }
        }
Exemplo n.º 14
0
        protected virtual void HandleJoinChannel(IIncommingMessage message)
        {
            var chatUser = message.Peer.GetExtension <ChatUserExtension>();

            if (chatUser == null)
            {
                message.Respond("Chat cannot identify you", ResponseStatus.Unauthorized);
                return;
            }

            var channelName = message.AsString();

            var channel = GetOrCreateChannel(channelName);

            if (channel == null)
            {
                // There's no such channel
                message.Respond("This channel is forbidden", ResponseStatus.Failed);
                return;
            }

            if (!channel.AddUser(chatUser))
            {
                message.Respond("Failed to join a channel", ResponseStatus.Failed);
                return;
            }

            if (SetFirstChannelAsLocal && chatUser.CurrentChannels.Count == 1)
            {
                chatUser.DefaultChannel = channel;
            }

            message.Respond(ResponseStatus.Success);
        }
        protected virtual void HandleAesKeyRequest(IIncommingMessage message)
        {
            var extension = message.Peer.GetExtension <PeerSecurityExtension>();

            var encryptedKey = extension.AesKeyEncrypted;

            if (encryptedKey != null)
            {
                // There's already a key generated
                message.Respond(encryptedKey, ResponseStatus.Success);
                return;
            }

            // Generate a random key
            var aesKey = Msf.Helper.CreateRandomString(8);

            var clientsPublicKey = JsonUtility.FromJson <RSAParameters>(message.AsString());

            using (var csp = new RSACryptoServiceProvider())
            {
                csp.ImportParameters(clientsPublicKey);
                var encryptedAes = csp.Encrypt(Encoding.Unicode.GetBytes(aesKey), false);

                // Save keys for later use
                extension.AesKeyEncrypted = encryptedAes;
                extension.AesKey          = aesKey;

                message.Respond(encryptedAes, ResponseStatus.Success);
            }
        }
Exemplo n.º 16
0
        private void HandleLobbyStatusTextChangeMsg(IIncommingMessage message)
        {
            var text = message.AsString();

            Data.StatusText = text;

            Listener?.OnLobbyStatusTextChanged(text);
        }
Exemplo n.º 17
0
 public static void handleJoinedTournament(ResponseStatus status, IIncommingMessage rawMsg)
 {
     if (Msf.Helper.serverResponseSuccess(status, rawMsg) == false)
     {
         return;
     }
     ClientUIStateManager.requestGotoState(ClientUIStates.PreTournament);
     tournamentID = rawMsg.AsString();
 }
        private void handlePlayerLeft(IIncommingMessage rawMsg)
        {
            PreGame targetGame;

            if (findGame(rawMsg.AsString(), out targetGame, rawMsg) && targetGame.containsPeer(rawMsg.Peer))
            {
                targetGame.peerLeft(rawMsg.Peer);
            }
        }
Exemplo n.º 19
0
        private void HandleLobbyMasterChangeMsg(IIncommingMessage message)
        {
            var masterUsername = message.AsString();

            if (_listener != null)
            {
                _listener.OnMasterChanged(masterUsername);
            }
        }
Exemplo n.º 20
0
 private static void handleTournamentClosed(IIncommingMessage rawMsg)
 {
     if (rawMsg.AsString() != tournamentID)
     {
         return;
     }
     Debug.LogError("Tournament forcefully Closed");
     ClientUIStateManager.requestGotoState(ClientUIStates.GameLobby);
     isInTournament = false;
     tournamentID   = "";
 }
        private void handleRestartGame(IIncommingMessage rawMsg)
        {
            if (activeGames.ContainsKey(rawMsg.AsString()) == false)
            {
                rawMsg.Respond("Could not find matching game", ResponseStatus.Error);
                return;
            }
            PreGame targetGame = activeGames[rawMsg.AsString()];

            if (targetGame.containsPeer(rawMsg.Peer) == false || targetGame.specs.canRestart == false)
            {
                return;
            }

            targetGame.updatePeerReady(rawMsg.Peer, true);
            if (targetGame.canGameStart())
            {
                startGame(targetGame, rawMsg);
            }
        }
Exemplo n.º 22
0
 private void handleCreatedGameResponse(ResponseStatus status, IIncommingMessage rawMsg)
 {
     if (Msf.Helper.serverResponseSuccess(status, rawMsg) == false)
     {
         return;
     }
     joinLobbyGame(new GamesListUiItem()
     {
         roomType = GameInfoType.PreGame, GameId = rawMsg.AsString()
     });
 }
Exemplo n.º 23
0
        /// <summary>
        /// Handles e-mail confirmation request
        /// </summary>
        /// <param name="message"></param>
        protected virtual void HandleEmailConfirmation(IIncommingMessage message)
        {
            var code = message.AsString();

            var extension = message.Peer.GetExtension <IUserExtension>();

            if (extension == null || extension.AccountData == null)
            {
                message.Respond("Invalid session", ResponseStatus.Unauthorized);
                return;
            }

            if (extension.AccountData.IsGuest)
            {
                message.Respond("Guests cannot confirm e-mails", ResponseStatus.Unauthorized);
                return;
            }

            if (extension.AccountData.IsEmailConfirmed)
            {
                // We still need to respond with "success" in case
                // response is handled somehow on the client
                message.Respond("Your email is already confirmed",
                                ResponseStatus.Success);
                return;
            }

            var db = Msf.Server.DbAccessors.GetAccessor <IAuthDatabase>();

            db.GetEmailConfirmationCode(extension.AccountData.Email, requiredCode =>
            {
                if (requiredCode != code)
                {
                    message.Respond("Invalid activation code", ResponseStatus.Error);
                    return;
                }

                // Confirm e-mail
                extension.AccountData.IsEmailConfirmed = true;

                // Update account
                db.UpdateAccount(extension.AccountData, () => { });

                // Respond with success
                message.Respond(ResponseStatus.Success);

                // Invoke the event
                if (EmailConfirmed != null)
                {
                    EmailConfirmed.Invoke(extension.AccountData);
                }
            });
        }
Exemplo n.º 24
0
        private void HandleLobbyMemberLeftMsg(IIncommingMessage message)
        {
            var username = message.AsString();

            Members.TryGetValue(username, out var member);

            if (member == null)
            {
                return;
            }

            Listener?.OnMemberLeft(member);
        }
        private void handleTournamentStarted(ResponseStatus status, IIncommingMessage msg)
        {
            if (status != ResponseStatus.Success)
            {
                Debug.LogError(msg.AsString());
                return;
            }

            TournamentInfoMsg specMsg = msg.Deserialize <TournamentInfoMsg>();

            AdminUIManager.requestGotoState(ClientUI.ClientUIStates.PlayingTournament, () => {
                AdminRunningTournamentManager.onTournamentStarted(specMsg);
            });
        }
Exemplo n.º 26
0
        public void HandleChatMessage(LobbyMember member, IIncommingMessage message)
        {
            var text = message.AsString();

            var messagePacket = new LobbyChatPacket()
            {
                Message = text,
                Sender  = member.Username
            };

            var msg = MessageHelper.Create((short)MsfMessageCodes.LobbyChatMessage, messagePacket.ToBytes());

            Broadcast(msg);
        }
Exemplo n.º 27
0
        protected virtual void OnJoinChannelRequestHandler(IIncommingMessage message)
        {
            string responseMsg = string.Empty;

            var chatUser = message.Peer.GetExtension <ChatUserPeerExtension>();

            if (chatUser == null)
            {
                responseMsg = "Chat cannot identify you";

                logger.Error(responseMsg);

                message.Respond(responseMsg, ResponseStatus.Unauthorized);
                return;
            }

            var channelName = message.AsString();

            var channel = GetOrCreateChannel(channelName);

            if (channel == null)
            {
                responseMsg = "This channel is forbidden";

                logger.Error(responseMsg);

                // There's no such channel
                message.Respond(responseMsg, ResponseStatus.Failed);
                return;
            }

            if (!channel.AddUser(chatUser))
            {
                responseMsg = "Failed to join a channel";

                logger.Error(responseMsg);

                message.Respond(responseMsg, ResponseStatus.Failed);
                return;
            }

            if (setFirstChannelAsLocal && chatUser.CurrentChannels.Count == 1)
            {
                chatUser.DefaultChannel = channel;
            }

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 28
0
        private void OnSaveProfileResponseCallback(ResponseStatus status, IIncommingMessage response)
        {
            Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

            if(status == ResponseStatus.Success)
            {
                OnProfileSavedEvent?.Invoke();

                logger.Debug("Your profile is successfuly updated and saved");
            }
            else
            {
                Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(response.AsString()));
                logger.Error(response.AsString());
            }
        }
Exemplo n.º 29
0
        protected virtual void HandleGetLobbyInfo(IIncommingMessage message)
        {
            var lobbyId = message.AsString();

            ILobby lobby;

            Lobbies.TryGetValue(lobbyId, out lobby);

            if (lobby == null)
            {
                message.Respond("Lobby not found", ResponseStatus.Failed);
                return;
            }

            message.Respond(lobby.GenerateLobbyData(), ResponseStatus.Success);
        }
Exemplo n.º 30
0
        protected virtual void HandleProcessStarted(IIncommingMessage message)
        {
            var spawnId = message.AsString();

            SpawnTask task;

            SpawnTasks.TryGetValue(spawnId, out task);

            if (task == null)
            {
                return;
            }

            task.OnProcessStarted();
            task.Spawner.OnProcessStarted();
        }