/// <summary> /// Send the current online players /// <para>Current playerlist is retrieved from the database (which is updated by the CSGOTelegramSync app) /// It is possible to not use the sync app and get the players directly from the gameserver through this function. /// But when the gameserver is busy or doesn't have the specs to process it fast, the performance issues/feedback in /// Telegram when requesting this information will be awful and confusing to the user. /// With the sync app you can be certain at this point to always have a playerlist available and deliver it fast to /// the Telegram user. /// PS: If you're not using the sync app, it's advised to use a local player cache since retrieving the playerlist often needs a few retries</para></summary> /// <param name="chatId">Unique identifier for the target chat</param> /// <param name="userName">Username for the target user</param> /// <returns></returns> public async Task SendCurrentPlayers(long chatId, string userName) { string playerMessage = $"<b>{Config.MainTitle}\r\n\r\nOnline players\r\n</b>"; List <PlayerInfoRecord> playerList = new List <PlayerInfoRecord>(); using (var mc = new CSGODataContext()) { playerList = mc.PlayerInfos.ToList(); } await SendBotTypingAction(chatId); await Task.Delay(1000); playerMessage = $"<b>{Config.MainTitle}</b>\r\n\r\n<b>Online players\r\n\r\n</b>"; foreach (PlayerInfoRecord currentPlayer in playerList) { if (currentPlayer.Name.Trim() != "") { string currentName = currentPlayer.Name; string cleanName = WebUtility.HtmlEncode(currentName); playerMessage += $"{cleanName}\r\n"; } } playerMessage += $"\r\n\r\n<code>Updated {GetDifference(playerList[0].Date)} minute(s) ago</code>"; var t = await SendMessage(chatId, playerMessage); }
/// <summary> /// Send the current online clan members /// <para>Checking the clancache is always called first to determine if it needs to be updated. /// <param name="chatId">Unique identifier for the target chat</param> /// <param name="userName">Username for the target user</param> /// <returns></returns> public async Task SendCurrentClanPlayers(long chatId, string userName) { string playerMessage = $"<b>{Config.MainTitle}\r\n\r\nOnline clan members\r\n</b>"; List <PlayerInfoRecord> playerList = new List <PlayerInfoRecord>(); using (var mc = new CSGODataContext()) { playerList = mc.PlayerInfos.ToList(); } await SendBotTypingAction(chatId); await Task.Delay(1000); playerMessage = $"<b>{Config.MainTitle}</b>\r\n\r\n<b>Online clan members</b>\r\n\r\n"; CheckClanCache(); if (ClanCache.Count > 0) { foreach (Clan currentClan in ClanCache) { playerMessage += $"<b>{currentClan.Name}\r\n</b>"; List <string> currentClanPlayerNames = SteamApiHelper.GetPlayerSummaries(currentClan.Members); List <String> currentOnlineClanMembers = currentClanPlayerNames.Where(clanmembername => playerList.Any(player => player.Name != "" && player.Name.ToLower().Equals(clanmembername.ToLower()))).ToList(); if (currentOnlineClanMembers.Count > 0) { foreach (String onlineClanMember in currentOnlineClanMembers) { playerMessage += $"{HtmlEncode(onlineClanMember)}\r\n"; } } else { playerMessage += "No players online\r\n"; } playerMessage += "\r\n"; } } else { playerMessage += "No clan information available"; } playerMessage += $"\r\n<code>Updated {GetDifference(playerList[0].Date)} minute(s) ago</code>"; var t = await SendMessage(chatId, playerMessage); }
/// <summary> /// Send the current online admins /// </summary> /// <para>Players can only be retrieved/identified from the gameserver with their steam displaynames. /// So we always need to get the current displaynames from the admins through the steamwebapi first /// to be able to identify them from the gameserver playerlist</para> /// <param name="chatId">Unique identifier for the target chat</param> /// <param name="userName">Username for the target user</param> /// <returns></returns> public async Task SendCurrentAdmins(long chatId, string userName) { string adminMessage = $"<b>{Config.MainTitle}\r\n\r\nAdmins\r\n</b>"; List <PlayerInfoRecord> playerList = new List <PlayerInfoRecord>(); using (var mc = new CSGODataContext()) { playerList = mc.PlayerInfos.ToList(); } await SendBotTypingAction(chatId); await Task.Delay(1000); adminMessage = $"<b>{Config.MainTitle}</b>\r\n\r\n<b>Online Admins\r\n</b>"; List <String> AdminNames = SteamApiHelper.GetPlayerSummaries(Config.Admins); int adminCount = 0; foreach (PlayerInfoRecord currentPlayer in playerList) { string cleanName = currentPlayer.Name; if (cleanName != "") { if (AdminNames.Any(member => cleanName.ToLower().Equals(member.ToLower()))) { adminMessage += $"{HtmlEncode(cleanName)}\r\n"; adminCount++; } } } if (adminCount == 0) { adminMessage += "No admins online"; } adminMessage += $"\r\n\r\n<code>Updated {GetDifference(playerList[0].Date)} minute(s) ago</code>"; var t = await SendMessage(chatId, adminMessage); }
/// <summary> /// Main Telegram Sync Task /// <para>Current playerlist is requested from the server and added to the database</para> /// </summary> /// <returns></returns> static async Task Run() { ServerHelper serverHelper = new ServerHelper(); Config.LogSync("CSGO Telegram Sync Online..."); while (true) { try { Config.LogSync("*** START playerlist cache refresh run ***"); DateTime StartTime = DateTime.Now; List <PlayerInfo> playerList = serverHelper.GetPlayersForced(ForceRetries); if (playerList.Count > 0) { using (var mc = new CSGODataContext()) { List <PlayerInfoRecord> currentPlayerRecords = mc.PlayerInfos.ToList(); mc.PlayerInfos.RemoveRange(currentPlayerRecords); mc.SaveChanges(); foreach (PlayerInfo currentPlayer in playerList) { if (currentPlayer.Name.Length > 0) { PlayerInfoRecord tempPlayerRecord = new PlayerInfoRecord { Name = currentPlayer.Name, Date = DateTime.Now, Time = currentPlayer.Time }; mc.PlayerInfos.Add(tempPlayerRecord); } } mc.SaveChanges(); Config.LogSync($"{playerList.Count} players added to database"); Config.LogSync("*** END playerlist cache refresh run ***"); } } DateTime EndTime = DateTime.Now; TimeSpan span = EndTime - StartTime; int timeRunning = (int)span.TotalMilliseconds; int newDelay = 0; if (timeRunning > RefreshDelay) { newDelay = 1000; } else { newDelay = RefreshDelay - timeRunning; } Config.LogSync($"*** SLEEP {newDelay} milliseconds ***"); await Task.Delay(newDelay); } catch (Exception ex) { Config.LogSync("FATAL ERROR!"); Config.LogSync($"Error Message: {ex.Message}"); Restart(); } } }