Example #1
0
        private async void SendMovementDetailsToPlayersInLocationAsync(CharacterPositionUpdateDetails positionDetails, int wmId, bool isOnWorldMap, int parentObjectId)
        {
            this.SendingMovementDetailsInProgress = true;

            await Task.Factory.StartNew
            (
                () =>
            {
                List <CharacterData> charactersInLocationList = _characterInfo.GetCharactersByWorldLocation(wmId, isOnWorldMap, parentObjectId);
                PlayerDetails player = null;

                foreach (CharacterData charData in charactersInLocationList)
                {
                    if (charData.AccId < 0)
                    {
                        continue;
                    }

                    player = _playerHandler.GetPlayerByCurrentCharId(charData.CharId);
                    if (player == null)
                    {
                        continue;
                    }

                    CommandHandler.Send(new CharPositionUpdateCmdBuilder(positionDetails), player);
                }
            }
            );

            this.SendingMovementDetailsInProgress = false;
        }
        private async void ExecuteAsync(PlayerDetails playerDetails)
        {
            await Task.Factory.StartNew
            (
                () =>
            {
                lock (_dataLock)
                {
                    try
                    {
                        if (_cmdElements.Length < 1 || !_cmdElements[0].Equals(_keyWord, GlobalData.InputDataStringComparison))
                        {
                            throw new Exception($"command replacement error [{_cmdInfo.CommandTxt}]");
                        }
                        else
                        {
                            CharacterData mainCharacter = _characterInfo.GetCharacterById(playerDetails.CharId);
                            if (mainCharacter == null)
                            {
                                throw new Exception($"cannot get main character with char_id [{playerDetails.CharId}]");
                            }

                            int wmId           = mainCharacter.WmId;
                            int parentObjectId = mainCharacter.ParentObjectId;
                            bool isOnWorldMap  = mainCharacter.IsOnWorldMap;

                            int charIdFilter = -1;
                            if (_cmdElements.Length == 2 && !String.IsNullOrWhiteSpace(_cmdElements[1]))
                            {
                                Int32.TryParse(_cmdElements[1], out charIdFilter);
                            }

                            List <CharacterData> charsList = _characterInfo.GetCharactersByWorldLocation(wmId, isOnWorldMap, parentObjectId);

                            if (charIdFilter < 0)
                            {
                                foreach (CharacterData character in charsList)
                                {
                                    CommandHandler.Send(new LocalCharacterDetailsCmdBuilder(false, character), playerDetails);
                                }

                                CommandHandler.Send(new LocalCharacterDetailsCmdBuilder(true), playerDetails);
                            }
                            else
                            {
                                foreach (CharacterData character in charsList)
                                {
                                    if (character.CharId == charIdFilter)
                                    {
                                        CommandHandler.Send
                                        (
                                            new LocalCharacterDetailsCmdBuilder(false, character, LocalCharacterDetailsCmdBuilder.Action.OnRequestSpawn),
                                            playerDetails
                                        );
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        CommandHandler.Send(new InfoCmdBuilder("Internal server error (cannot get local characters)!"), playerDetails);
                        _logger.UpdateLog($"Location characters getting error for TCP client ID [{playerDetails.TcpClientId}]: {exception.Message}");
                    }
                }
            }
            );
        }
Example #3
0
        private async void HandleMessagesAsync()
        {
            _logger.UpdateLog("Chat handler - handling started");

            int charId;
            ChatMessageDetails details;
            List <Tuple <int, ChatMessageDetails> > tempMsgList = new List <Tuple <int, ChatMessageDetails> >();
            PlayerDetails        senderPlayer;
            CharacterData        senderCharacter;
            PlayerDetails        receiverPlayer;
            CharacterData        receiverCharacter;
            List <CharacterData> receiverCharacterList = new List <CharacterData>();
            double distance = 0;

            do
            {
                await Task.Factory.StartNew(() => Thread.Sleep(_timeIntervalMs));

                if (!_handlingInProgress)
                {
                    break;
                }

                //TEMP LIST CREATION
                lock (_chatMsgLock)
                {
                    if (_charIdMsgList.Count > 0)
                    {
                        foreach (Tuple <int, ChatMessageDetails> msgData in _charIdMsgList)
                        {
                            charId = msgData.Item1;
                            if (charId < 0)
                            {
                                continue;
                            }

                            tempMsgList.Add(Tuple.Create(charId, (ChatMessageDetails)msgData.Item2.Clone()));
                        }

                        _charIdMsgList.Clear();
                    }
                }

                //TEMP LIST HANDLING

                foreach (Tuple <int, ChatMessageDetails> msgData in tempMsgList)
                {
                    charId  = msgData.Item1;
                    details = msgData.Item2;

                    senderPlayer = await Task.Factory.StartNew(() => _playerHandler.GetPlayerByCurrentCharId(charId));

                    if (senderPlayer == null)
                    {
                        _logger.UpdateLog($"Chat handling - cannot find sender player by char. ID [{charId}]");
                        continue;
                    }

                    senderCharacter = await _characterInfo.GetCharacterByIdTaskStart(charId);

                    if (senderCharacter == null)
                    {
                        _logger.UpdateLog($"Chat handling - cannot find sender character by char. ID [{charId}]");
                        continue;
                    }

                    if (details.IsPrivate)
                    {
                        #region Private message
                        //PRIVATE MESSAGE

                        receiverCharacter = await _characterInfo.GetCharacterByNameTaskStart(details.To);

                        if (receiverCharacter == null)
                        {
                            CommandHandler.Send
                            (
                                new ChatMessageCmdBuilder
                                (
                                    new ChatMessageDetails()
                            {
                                IsPrivate = true,
                                From      = "SERVER",
                                To        = senderCharacter.Name,
                                Message   = $"Character [{details.To}] doesn't exist!"
                            }
                                ),
                                senderPlayer
                            );
                            continue;
                        }

                        if (receiverCharacter.GetDbData().IsNpc)
                        {
                            CommandHandler.Send
                            (
                                new ChatMessageCmdBuilder
                                (
                                    new ChatMessageDetails()
                            {
                                IsPrivate = true,
                                From      = "SERVER",
                                To        = senderCharacter.Name,
                                Message   = $"Cannot send message to NPC [{details.To}]"
                            }
                                ),
                                senderPlayer
                            );
                            continue;
                        }

                        receiverPlayer = _playerHandler.GetPlayerByCurrentCharId(receiverCharacter.CharId);
                        if (receiverPlayer == null)
                        {
                            CommandHandler.Send
                            (
                                new ChatMessageCmdBuilder
                                (
                                    new ChatMessageDetails()
                            {
                                IsPrivate = true,
                                From      = "SERVER",
                                To        = senderCharacter.Name,
                                Message   = $"Character's [{details.To}] player is not logged in!"
                            }
                                ),
                                senderPlayer
                            );
                            continue;
                        }

                        //============== success

                        CommandHandler.Send //to receiver
                        (
                            new ChatMessageCmdBuilder
                            (
                                new ChatMessageDetails()
                        {
                            IsPrivate = true,
                            From      = senderCharacter.Name,
                            To        = receiverCharacter.Name,
                            Message   = details.Message
                        }
                            ),
                            receiverPlayer
                        );

                        CommandHandler.Send //to sender
                        (
                            new ChatMessageCmdBuilder
                            (
                                new ChatMessageDetails()
                        {
                            IsPrivate = true,
                            From      = senderCharacter.Name,
                            To        = receiverCharacter.Name,
                            Message   = details.Message
                        }
                            ),
                            senderPlayer
                        );

                        #endregion
                    }
                    else
                    {
                        #region Public message
                        //PUBLIC MESSAGE
                        if (senderCharacter.IsOnWorldMap)
                        {
                            CommandHandler.Send(new InfoCmdBuilder("Cannot send public message on world map!"), senderPlayer);
                            continue;
                        }

                        await Task.Factory.StartNew
                        (
                            () =>
                        {
                            receiverCharacterList = _characterInfo.GetCharactersByWorldLocation(senderCharacter.WmId, senderCharacter.IsOnWorldMap, senderCharacter.ParentObjectId);
                            foreach (CharacterData receiverCharData in receiverCharacterList)
                            {
                                distance = Measure.GetDistanceBetweenPoints(senderCharacter.CurrentLoc, receiverCharData.CurrentLoc);
                                if (distance > _globalChatMaxDistance)
                                {
                                    continue;
                                }

                                receiverPlayer = _playerHandler.GetPlayerByCurrentCharId(receiverCharData.CharId);
                                if (receiverPlayer == null)
                                {
                                    continue;
                                }

                                CommandHandler.Send
                                (
                                    new ChatMessageCmdBuilder
                                    (
                                        new ChatMessageDetails()
                                {
                                    IsPrivate = false,
                                    From      = senderCharacter.Name,
                                    To        = String.Empty,
                                    Message   = details.Message
                                }
                                    ),
                                    receiverPlayer
                                );
                            }
                        }
                        );

                        receiverCharacterList.Clear();

                        #endregion
                    }
                }

                tempMsgList.Clear();
            }while (_handlingInProgress);

            _logger.UpdateLog("Chat handler - handling stopped!");
        }