public async Task UpdateMapPopularity(MapPopularityDto mapPopularityDto)
        {
            var mapPopularityEntity = new MapPopularityEntity(mapPopularityDto);

            var operation = TableOperation.InsertOrMerge(mapPopularityEntity);
            await _mapPopularityTable.ExecuteAsync(operation);
        }
        public async Task <MapPopularityDto> GetMapPopularity(GameType gameType, string mapName)
        {
            var tableOperation = TableOperation.Retrieve <MapPopularityEntity>(gameType.ToString(), mapName);
            var result         = await _mapPopularityTable.ExecuteAsync(tableOperation);

            if (result.HttpStatusCode == 404)
            {
                return(null);
            }

            var mapPopularity = (MapPopularityEntity)result.Result;

            var mapPopularityDto = new MapPopularityDto
            {
                GameType = gameType,
                MapName  = mapPopularity.RowKey,
                MapVotes = mapPopularity.MapVotes
            };

            return(mapPopularityDto);
        }
Esempio n. 3
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);
        }
 public MapPopularityEntity(MapPopularityDto model)
 {
     RowKey       = model.MapName;
     PartitionKey = model.GameType.ToString();
     MapVotes     = model.MapVotes;
 }