private void ProcessLobbiesCommand(ulong channelId, string name) { try { var channelName = Channels[channelId]; if (_config.PrivateChat == channelName) { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbiesCommandNotAllowed, name), false); } else { var lobbies = LobbyStates.Where(l => _config.Games.Any(g => g.Chat == channelName && g.Maps.Any(m => m.MapId == l.CustomMapName))); if (lobbies.Count() == 0) { DotaClient.QueueChatMessage(channelId, String.Format(_config.HostingNoLobbiesMessage, name), false); } else { foreach (var lobby in lobbies) { var region = _config.ServerRegions.Where(r => r.ServerId == lobby.ServerId).SingleOrDefault(); var game = _config.Games.Where(g => g.Maps.Any(m => m.MapId == lobby.CustomMapName)).SingleOrDefault(); var lobbyName = game.Name; if (game.Maps.Count > 1) { lobbyName += " " + game.Maps.Where(m => m.MapId == lobby.CustomMapName).FirstOrDefault().Name; } if (lobby.LobbyId != 0) { DotaClient.QueueChatMessage(channelId, String.Format(_config.HostingLobby, lobbyName, region.LongName), true); DotaClient.QueueLobbyShare(channelId, lobby.LobbyId, game.CustomGameMode, true); } else { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyQueuedMessage, lobbyName, region.LongName), true); } } } } } catch (Exception e) { _logger.Error("Error while processing !lobbies command", e); } }
private void ProcessLobbyCommand(ulong channelId, ulong accountId, string name, string text) { try { var commandParams = text.Split(' '); var channelName = Channels[channelId]; var game = _config.Games.Where(m => m.Chat.ToLower() == channelName.ToLower()).SingleOrDefault(); if (channelName == _config.PrivateChat) { DotaClient.QueueChatMessage(channelId, String.Format("{0}, requesting a lobby in this channel is not allowed, please request a lobby in custom game's public channel.", name, GetValidServerRegions()), false); } else if (commandParams.Length < 2) { DotaClient.QueueChatMessage(channelId, String.Format("{0}, please specify server region. Regions: {1}.", name, GetValidServerRegions()), false); if (game.Maps.Count > 1) { DotaClient.QueueChatMessage(channelId, String.Format(_config.SpecifyMapMessage, name, GetValidMaps(game), _config.ServerRegions.FirstOrDefault().ShortName, game.Maps.FirstOrDefault().ChatId), false); } } else { var region = _config.ServerRegions.Where(sr => sr.ShortName == commandParams[1].Trim().ToLower()).SingleOrDefault(); if (region == null) { DotaClient.QueueChatMessage(channelId, String.Format("{0}, invalid server region. Valid regions: {1}.", name, GetValidServerRegions()), false); } else { string mapName = null; if (commandParams.Length > 2 && game.Maps.Count > 1) { mapName = commandParams[2]; } Map map = null; bool mapOk = true; if (mapName == null) { map = game.Maps.OrderBy(x => Guid.NewGuid()).FirstOrDefault(); } else { map = game.Maps.Where(m => m.ChatId == mapName.Trim().ToLower()).FirstOrDefault(); if (map == null && game.Maps.Count > 1) { mapOk = false; DotaClient.QueueChatMessage(channelId, String.Format(_config.MapNotFoundMessage, name, GetValidMaps(game)), false); } } if (mapOk) { using (IRepository repository = new Repository(_dbOptions)) { var banned = repository.Bans.Any(b => b.Account.AccountId == accountId.ToString() && b.Expires > DateTime.UtcNow); if (!banned || _config.Admins.Contains(accountId)) { try { var timedoutLobbies = LobbyStates.Where(ls => ls.Created.AddMinutes(_config.Games.Where(g => g.Maps.Any(m => m.MapId == ls.CustomMapName)).SingleOrDefault().LobbyTimeout + 1) < DateTime.UtcNow); foreach (var timedoutLobby in timedoutLobbies.ToList()) { _logger.Info("Removed timed out lobby - Game: " + timedoutLobby.CustomMapName + " Server ID: " + timedoutLobby.CustomMapName); LobbyStates.Remove(timedoutLobby); } } catch (Exception e) { _logger.Error("Error while checking timed out lobbies", e); } var myExistingLobby = LobbyStates.Where(ls => ls.RequesterAccountId == accountId).FirstOrDefault(); if (myExistingLobby != null) { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyRequesterHasHost, name, _config.Games.Where(g => g.Maps.Any(m => m.MapId == myExistingLobby.CustomMapName)).SingleOrDefault().Name), false); } else { var existingLobby = LobbyStates.Where(ls => game.Maps.Any(m => m.MapId == ls.CustomMapName) && ls.ServerId == region.ServerId).FirstOrDefault(); if (existingLobby != null) { if (existingLobby.LobbyId == 0) { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyAlreadyQueued, name, region.LongName), false); } else { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyAlreadyHosted, name, region.LongName), true); DotaClient.QueueLobbyShare(channelId, existingLobby.LobbyId, game.CustomGameMode, true); } } else { if (LobbyStates.Count < _config.LobbyBotsPool.Count) { DotaClient.QueueChatMessage(channelId, String.Format("{0}, hosting your lobby, just a sec...", name), false); game.ServerId = region.ServerId; game.RequestedBy = accountId; _logger.Info("Queue lobby - Game: " + game.Name + " - Map: " + map.Name + " - Server ID: " + game.ServerId); OutboundMessages.Enqueue(new Message() { Id = MessageIds.Out.CREATE_LOBBY, Payload = JsonConvert.SerializeObject(new LobbyHost() { Game = game, Map = map }) }); lock (LobbyStates) { //Just in case should something here be multithreaded LobbyStates.Add(new LobbyState() { CustomMapName = map.MapId, ServerId = game.ServerId, RequesterAccountId = accountId, Created = DateTime.UtcNow }); } } else { DotaClient.QueueChatMessage(channelId, String.Format(_config.BusyMessage, name), false); } } } } else if (banned) { DotaClient.QueueChatMessage(channelId, String.Format(_config.LobbyRequesterBanned, name), false); } } } } } } catch (Exception e) { _logger.Error(String.Format("Error while processing !host command: {0}", text), e); } }