Example #1
0
        public ActionResult <PlayerViewModel> GetPlayer(int playerId)
        {
            var playerEntity = _playersRepository.GetPlayer(playerId);

            _logger.LogInformation("an elastic search client is instantiated ");

            var mappedPlayerViewModel = _mapper.Map <PlayerViewModel>(playerEntity);

            return(Ok(mappedPlayerViewModel));
        }
Example #2
0
        public async Task <IActionResult> Create(Guid id, AdminActionType adminActionType)
        {
            var playerDto = await _playersRepository.GetPlayer(id);

            if (playerDto == null)
            {
                return(NotFound());
            }

            var adminActionDto       = new AdminActionDto().OfType(adminActionType).WithPlayerDto(playerDto);
            var canCreateAdminAction = await _authorizationService.AuthorizeAsync(User, adminActionDto, AuthPolicies.CreateAdminAction);

            if (!canCreateAdminAction.Succeeded)
            {
                return(Unauthorized());
            }

            if (adminActionType == AdminActionType.TempBan)
            {
                adminActionDto.Expires = DateTime.UtcNow.AddDays(7);
            }

            return(View(adminActionDto));
        }
Example #3
0
        public async Task <IActionResult> GetPlayerChatLog(Guid id)
        {
            var playerDto = await _playersRepository.GetPlayer(id);

            if (playerDto == null)
            {
                return(NotFound());
            }

            var canViewGameChatLog = await _authorizationService.AuthorizeAsync(User, playerDto.GameType, AuthPolicies.ViewGameChatLog);

            if (!canViewGameChatLog.Succeeded)
            {
                return(Unauthorized());
            }

            return(await GetChatLogPrivate(playerDto.GameType, null, playerDto.PlayerId));
        }
Example #4
0
        public async Task IngestData(GameType gameType, string guid, string username, string ipAddress)
        {
            if (gameType == GameType.Unknown || string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(username))
            {
                _logger.LogWarning("Ingest ignored as it was invalid with gameType '{GameType}', guid '{Guid}', username '{Username}'", gameType, guid, username);
                return;
            }

            if (username == "allies" || username == "axis")
            {
                _logger.LogWarning("Ingest data ignored as username was {Username}", username);
                return;
            }

            _logger.LogDebug("Ingesting gameType '{GameType}', guid '{Guid}', username '{Username}', ipAddress '{IpAddress}'", gameType, guid, username, ipAddress);

            var cachedPlayer = await _playersCacheRepository.GetPlayer(gameType, guid);

            if (cachedPlayer != null)
            {
                _logger.LogDebug("Player {Username} with Guid {Guid} for {GameType} exists in cache", cachedPlayer.Username, cachedPlayer.Guid, cachedPlayer.GameType);

                var playerDto = new PlayerDto
                {
                    PlayerId  = cachedPlayer.PlayerId,
                    GameType  = cachedPlayer.GameType,
                    Username  = cachedPlayer.Username,
                    Guid      = cachedPlayer.Guid,
                    IpAddress = cachedPlayer.IpAddress,
                    FirstSeen = cachedPlayer.FirstSeen,
                    LastSeen  = cachedPlayer.LastSeen
                };

                var update = false;

                if (cachedPlayer.Username != username)
                {
                    playerDto.Username = username;
                    update             = true;
                }

                if (IPAddress.TryParse(ipAddress, out var address))
                {
                    if (cachedPlayer.IpAddress != ipAddress)
                    {
                        playerDto.IpAddress = ipAddress;
                        update = true;
                    }
                }

                if (cachedPlayer.LastSeen < DateTime.UtcNow.AddMinutes(-10))
                {
                    update = true;
                }

                if (update)
                {
                    _logger.LogDebug("Updating database player information for {Guid}", guid);

                    await _playersRepository.UpdatePlayer(playerDto);

                    cachedPlayer.Username  = username;
                    cachedPlayer.IpAddress = ipAddress;
                    cachedPlayer.LastSeen  = DateTime.UtcNow;

                    await _playersCacheRepository.UpdatePlayer(cachedPlayer);
                }
            }
            else
            {
                var player = await _playersRepository.GetPlayer(gameType, guid);

                if (player == null)
                {
                    _logger.LogInformation("Creating new player in the database with username {Username} guid {Guid}", username, guid);

                    var playerDto = new PlayerDto
                    {
                        GameType  = gameType,
                        Username  = username,
                        Guid      = guid,
                        FirstSeen = DateTime.UtcNow,
                        LastSeen  = DateTime.UtcNow
                    };

                    if (IPAddress.TryParse(ipAddress, out var address))
                    {
                        playerDto.IpAddress = ipAddress;
                    }

                    await _playersRepository.CreatePlayer(playerDto);
                }
                else
                {
                    _logger.LogDebug("Adding the player to the cache with guid {Guid}", guid);

                    var playerCacheEntity = new PlayerCacheEntity
                    {
                        PartitionKey = player.GameType.ToString(),
                        RowKey       = player.Guid,
                        PlayerId     = player.PlayerId,
                        GameType     = player.GameType,
                        Username     = player.Username,
                        Guid         = player.Guid,
                        IpAddress    = player.IpAddress,
                        FirstSeen    = player.FirstSeen,
                        LastSeen     = player.LastSeen
                    };

                    await _playersCacheRepository.UpdatePlayer(playerCacheEntity);
                }
            }
        }
        public async Task IngestBanFileDataForGame(GameType gameType, string remoteBanFileData)
        {
            var skipTags = new[] { "[PBBAN]", "[B3BAN]", "[BANSYNC]", "[EXTERNAL]" };

            foreach (var line in remoteBanFileData.Split('\n'))
            {
                if (string.IsNullOrWhiteSpace(line) || skipTags.Any(skipTag => line.Contains(skipTag)))
                {
                    continue;
                }

                ParseLine(line, out var guid, out var name);

                if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(name))
                {
                    continue;
                }

                if (!_guidValidator.IsValid(gameType, guid))
                {
                    _logger.LogWarning($"Could not validate guid {guid} for {gameType}");
                    continue;
                }

                var player = await _playersRepository.GetPlayer(gameType, guid);

                if (player == null)
                {
                    _logger.LogInformation($"BanFileIngest - creating new player {name} with guid {guid} with import ban");

                    await _playersRepository.CreatePlayer(new PlayerDto
                    {
                        GameType = gameType,
                        Username = name,
                        Guid     = guid
                    });

                    player = await _playersRepository.GetPlayer(gameType, guid);

                    var adminActionDto = new AdminActionDto()
                                         .OfType(AdminActionType.Ban)
                                         .WithPlayerDto(player);

                    adminActionDto.Text         = "Imported from server";
                    adminActionDto.ForumTopicId = await _playersForumsClient.CreateTopicForAdminAction(adminActionDto);

                    await _adminActionsRepository.CreateAdminAction(adminActionDto);
                }
                else
                {
                    var adminActions = await _adminActionsRepository.GetAdminActions(new AdminActionsFilterModel
                    {
                        PlayerId = player.PlayerId,
                        Filter   = AdminActionsFilterModel.FilterType.ActiveBans
                    });

                    if (adminActions.Count(aa => aa.Type == AdminActionType.Ban) == 0)
                    {
                        _logger.LogInformation($"BanFileImport - adding import ban to existing player {player.Username} - {player.Guid} ({player.GameType})");

                        var adminActionDto = new AdminActionDto()
                                             .OfType(AdminActionType.Ban)
                                             .WithPlayerDto(player);

                        adminActionDto.Text         = "Imported from server";
                        adminActionDto.ForumTopicId = await _playersForumsClient.CreateTopicForAdminAction(adminActionDto);

                        await _adminActionsRepository.CreateAdminAction(adminActionDto);
                    }
                }
            }
        }
Example #6
0
        public override async Task HandleChatMessage(Guid serverId, string name, string guid, string message, ChatType chatType)
        {
            if (!IsMatchingCommand(message))
            {
                return;
            }

            var server = await _gameServersRepository.GetGameServer(serverId);

            var gameServerStatus = await _gameServerStatusRepository.GetStatus(serverId, TimeSpan.Zero);

            if (gameServerStatus == null)
            {
                _logger.LogWarning("Could not process !like for {name} as the game server status is null for {serverId}", name, serverId);
                return;
            }

            var statusPlayer = gameServerStatus.Players.SingleOrDefault(p => p.Guid == guid);

            if (statusPlayer == null)
            {
                _logger.LogWarning("Could not process !like for {name} as the status player is null for {guid}", name, guid);
                return;
            }

            var databasePlayer = await _playersRepository.GetPlayer(gameServerStatus.GameType, guid);

            if (databasePlayer == null)
            {
                _logger.LogWarning("Could not process !like for {name} as player is null for {guid}", name, guid);
                return;
            }

            var rconClient = _rconClientFactory.CreateInstance(server.GameType, server.ServerId, server.Hostname, server.QueryPort, server.RconPassword);
            var like       = !message.ToLower().Contains("!dislike");

            var mapPopularityDto = await _mapPopularityRepository.GetMapPopularity(gameServerStatus.GameType, gameServerStatus.Map);

            if (mapPopularityDto == null)
            {
                mapPopularityDto = new MapPopularityDto
                {
                    GameType = gameServerStatus.GameType,
                    MapName  = gameServerStatus.Map,
                    MapVotes = new List <MapPopularityVoteDto>
                    {
                        new MapPopularityVoteDto
                        {
                            ServerId    = gameServerStatus.ServerId,
                            ServerName  = gameServerStatus.ServerName,
                            PlayerId    = databasePlayer.PlayerId,
                            PlayerName  = name,
                            ModName     = gameServerStatus.Mod,
                            PlayerCount = gameServerStatus.PlayerCount,
                            Updated     = DateTime.UtcNow,
                            Like        = like
                        }
                    }
                };

                await _mapPopularityRepository.UpdateMapPopularity(mapPopularityDto);
            }
            else
            {
                var existing = mapPopularityDto.MapVotes.SingleOrDefault(mp => mp.PlayerId == databasePlayer.PlayerId && mp.ModName == gameServerStatus.Mod && mp.ServerId == gameServerStatus.ServerId);
                if (existing == null)
                {
                    mapPopularityDto.MapVotes.Add(new MapPopularityVoteDto
                    {
                        ServerId    = gameServerStatus.ServerId,
                        ServerName  = gameServerStatus.ServerName,
                        PlayerId    = databasePlayer.PlayerId,
                        PlayerName  = name,
                        ModName     = gameServerStatus.Mod,
                        PlayerCount = gameServerStatus.PlayerCount,
                        Updated     = DateTime.UtcNow,
                        Like        = like
                    });

                    await _mapPopularityRepository.UpdateMapPopularity(mapPopularityDto);
                }
                else
                {
                    existing.Updated = DateTime.UtcNow;
                    existing.Like    = like;

                    await _mapPopularityRepository.UpdateMapPopularity(mapPopularityDto);
                }
            }

            var globalMessage = $"^6{name} ^2likes ^6this map - thanks for the feedback!";

            if (!like)
            {
                globalMessage = $"^6{name} ^1dislikes ^6this map - thanks for the feedback!";
            }

            var totalLikes    = mapPopularityDto.MapVotes.Count(mv => mv.ServerId == gameServerStatus.ServerId && mv.ModName == gameServerStatus.Mod && mv.Like);
            var totalDislikes = mapPopularityDto.MapVotes.Count(mv => mv.ServerId == gameServerStatus.ServerId && mv.ModName == gameServerStatus.Mod && mv.Like == false);

            var overall = $"^6Overall there are ^2{totalLikes} likes ^6and ^1{totalDislikes} dislikes ^6for this map";

            await rconClient.Say(globalMessage);

            await rconClient.Say(overall);
        }