public static void Start(Update u, string[] args) { if (u.Message.Chat.Type == ChatType.Private && u.Message.From != null) { using (var db = new WWContext()) { var p = GetDBPlayer(u.Message.From.Id, db); if (p == null) { var usr = u.Message.From; p = new Player { UserName = usr.Username, Name = (usr.FirstName + " " + usr.LastName).Trim(), TelegramId = usr.Id, Language = "English" }; db.Players.Add(p); db.SaveChanges(); p = GetDBPlayer(u.Message.From.Id, db); } #if RELEASE p.HasPM = true; #elif RELEASE2 p.HasPM2 = true; #elif BETA p.HasDebugPM = true; #endif db.SaveChanges(); if (String.IsNullOrEmpty(args[1])) { var msg = $"Hi there! I'm @{Bot.Me.Username}, and I moderate games of Werewolf." + $"\nJoin the main group @werewolfgame, or to find a group to play in, you can use /grouplist." + $"\nFor role information, use /rolelist." + $"\nIf you want to set your default language, use /setlang." + $"\nBe sure to stop by <a href=\"https://telegram.me/greywolfsupport\">Werewolf Support</a> for any questions, and subscribe to @greywolfdev for updates from the developer." + $"\nMore infomation can be found <a href=\"https://www.tgwerewolf.com/?referrer=start\">here</a>!"; Bot.Send(msg, u.Message.Chat.Id); return; } if (args[1] == "donatetg") { GetDonationInfo(m: u.Message); return; } if (args[1] == "xsolla") { GetXsollaLink(m: u.Message); return; } if (args[1].StartsWith("join") && args[1].Length == 48) // 4 "join" + 22 node id + 22 game id { //okay, they are joining a game. string nodeid = ""; string gameid = ""; Models.Node node = null; Models.GameInfo game = null; ChatMember chatmember = null; try { nodeid = args[1].Substring(4, 22); gameid = args[1].Substring(26, 22); // check that they aren't ingame in another group node = GetPlayerNode(u.Message.From.Id); if (node != null) { game = node.Games.ToList().FirstOrDefault(x => x.Users.Contains(u.Message.From.Id)); if (game == null) { game = node.Games.ToList().FirstOrDefault(x => x.Users.Contains(u.Message.From.Id)); } if (game == null) { game = node.Games.ToList().FirstOrDefault(x => x.Users.Contains(u.Message.From.Id)); } if (game != null) { if (node.ClientId != nodeid || game.Guid != gameid) { // they are in game in another group, can't join here var pl = db.Players.FirstOrDefault(x => x.TelegramId == u.Message.From.Id); Send(GetLocaleString("AlreadyInGame", pl?.Language ?? "English", game.ChatGroup.ToBold()), u.Message.Chat.Id); return; } else { // do nothing, they are in the game, they are just being spammy return; } } } node = null; game = null; //first get the node where to search for the game for (var i = 0; i < 3; i++) { node = Bot.Nodes.ToList().FirstOrDefault(x => x.ClientId == nodeid); if (node != null) { break; } } if (node == null) { //log it //Bot.Send($"{u.Message.From.Id} (@{u.Message.From.Username ?? ""}) didn't find node with guid {n.ToString()} while attempting to play in {g.ToString()}", -1001098399855); return; } //we have the node, get the game for (var i = 0; i < 5; i++) { game = node.Games.ToList().FirstOrDefault(x => x.Guid == gameid); if (game != null) { break; } } if (game == null) { //log it //Bot.Send($"{u.Message.From.Id} (@{u.Message.From.Username ?? ""}) found node with guid {n.ToString()} but not the game {g.ToString()}", -1001098399855); return; } //ok we got the game, now join //make sure they are member try { chatmember = Bot.Api.GetChatMemberAsync(game.GroupId, u.Message.From.Id).Result; } catch { return; } if (chatmember == null) // if we fail to determine their chatmember status, just let them try again { return; } if (chatmember.Status == ChatMemberStatus.Left || chatmember.Status == ChatMemberStatus.Kicked || (chatmember.Status == ChatMemberStatus.Restricted && !chatmember.CanSendMessages)) { Bot.Send( GetLocaleString("NotMember", GetLanguage(u.Message.From.Id), game.ChatGroup.ToBold()), u.Message.Chat.Id); return; } game.AddPlayer(u, gameid); return; } catch (AggregateException e) { var ex = e.InnerExceptions[0]; while (ex.InnerException != null) { ex = ex.InnerException; } Send(ex.Message, u.Message.Chat.Id); Send($"Error in START:\n" + $"{u.Message.Text}\n" + $"Node: {nodeid}\n" + $"Game: {gameid}\n" + $"Found Node: {node?.ClientId}\n" + $"Found Game: {game?.Guid}\n" + $"Chat Member Status: {chatmember?.Status.ToString() ?? "NULL"}\n" + $"{ex.Message}\n{ex.StackTrace}", Settings.ErrorGroup); } catch (Exception ex) { while (ex.InnerException != null) { ex = ex.InnerException; } Send(ex.Message, u.Message.Chat.Id); Send($"Error in START:\n" + $"{u.Message.Text}\n" + $"Node: {nodeid}\n" + $"Game: {gameid}\n" + $"Found Node: {node?.ClientId}\n" + $"Found Game: {game?.Guid}\n" + $"Chat Member Status: {chatmember?.Status.ToString() ?? "NULL"}\n" + $"{ex.Message}\n{ex.StackTrace}", Settings.ErrorGroup); } } } } }
public static void Start(Update u, string[] args) { if (u.Message.Chat.Type == ChatType.Private && u.Message.From != null) { using (var db = new WWContext()) { var p = GetDBPlayer(u.Message.From.Id, db); if (p == null) { var usr = u.Message.From; p = new Player { UserName = usr.Username, Name = (usr.FirstName + " " + usr.LastName).Trim(), TelegramId = usr.Id, Language = "English" }; db.Players.Add(p); db.SaveChanges(); p = GetDBPlayer(u.Message.From.Id, db); } #if RELEASE p.HasPM = true; #elif RELEASE2 p.HasPM2 = true; #elif BETA || DEBUG p.HasDebugPM = true; #endif db.SaveChanges(); if (String.IsNullOrEmpty(args[1])) { var msg = $"Olá! Sou @{Bot.Me.Username}, e gerencio jogos de Werewolf." + $"\nJunte-se ao grupo principal @BRMarvelWW, ou para encontrar um grupo para jogar, você pode usar /grouplist." + $"\nPara informações de papéis, use /rolelist." + $"\nSe quer definir sua língua principal, use /setlang." + $"\nSe tiver alguma dúvida ou pergunta venha até Werewolf Zion Suporte (@WerewolfZionSuporte)!"; Bot.Send(msg, u.Message.Chat.Id); return; } if (args[1] == "donatetg") { GetDonationInfo(m: u.Message); return; } //okay, they are joining a game. string[] argsSplit = args[1].Split('_'); var nodeid = argsSplit[0]; var gameid = argsSplit[1]; //try to get the guid of the game they want to join int n, g; long gid = 0; if (!(int.TryParse(nodeid, out n) && int.TryParse(gameid, out g))) { return; } //first get the node where to search for the game Models.Node node = null; for (var i = 0; i < 3; i++) { node = Bot.Nodes.FirstOrDefault(x => x.ClientId == n); if (node != null) { break; } } if (node == null) { //log it //Bot.Send($"{u.Message.From.Id} (@{u.Message.From.Username ?? ""}) didn't find node with guid {n.ToString()} while attempting to play in {g.ToString()}", -1001098399855); return; } //we have the node, get the game Models.GameInfo game = null; for (var i = 0; i < 5; i++) { game = node.Games.FirstOrDefault(x => x.Guid == g); if (game != null) { break; } } if (game == null) { //log it //Bot.Send($"{u.Message.From.Id} (@{u.Message.From.Username ?? ""}) found node with guid {n.ToString()} but not the game {g.ToString()}", -1001098399855); return; } //ok we got the game, now join //make sure they are member var status = Bot.Api.GetChatMemberAsync(game.GroupId, u.Message.From.Id).Result.Status; if (status == ChatMemberStatus.Left || status == ChatMemberStatus.Kicked) { Bot.Send(GetLocaleString("NotMember", GetLanguage(u.Message.From.Id), game.ChatGroup.ToBold()), u.Message.Chat.Id); return; } //if (game?.Users.Contains(u.Message.From.Id) ?? false) //{ // if (game.GroupId != gid) // { // //player is already in a game (in another group), and alive // var grp = db.Groups.FirstOrDefault(x => x.GroupId == gid); // Send(GetLocaleString("AlreadyInGame", grp?.Language ?? "English", game.ChatGroup.ToBold()), gid); // return; // } // else // { // //do nothing, player is in the game, in that group, they are just being spammy // return; // } //} game.AddPlayer(u); return; } } }
//TODO this needs to be an event public static void NodeConnected(Node n) { #if DEBUG Api.SendTextMessage(Settings.MainChatId, $"Node connected with guid {n.ClientId}"); #endif }
public static void Start(Update u, string[] args) { if (u.Message.Chat.Type == ChatType.Private && u.Message.From != null) { using (var db = new WWContext()) { var p = GetDBPlayer(u.Message.From.Id, db); if (p == null) { var usr = u.Message.From; p = new Player { UserName = usr.Username, Name = (usr.FirstName + " " + usr.LastName).Trim(), TelegramId = usr.Id, Language = "English" }; db.Players.Add(p); db.SaveChanges(); p = GetDBPlayer(u.Message.From.Id, db); } #if RELEASE p.HasPM = true; #elif RELEASE2 p.HasPM2 = true; #elif BETA p.HasDebugPM = true; #endif db.SaveChanges(); if (String.IsNullOrEmpty(args[1])) { var msg = $"Hi there! I'm @{Bot.Me.Username}, and I moderate games of Werewolf." + $"\nJoin the main group @werewolfgame, or to find a group to play in, you can use /grouplist." + $"\nFor role information, use /rolelist." + $"\nIf you want to set your default language, use /setlang." + $"\nBe sure to stop by <a href=\"https://telegram.me/werewolfsupport\">Werewolf Support</a> for any questions, and subscribe to @werewolfdev for updates from the developer." + $"\nMore infomation can be found <a href=\"https://www.tgwerewolf.com/?referrer=start\">here</a>!"; Bot.Send(msg, u.Message.Chat.Id); return; } if (args[1] == "donatetg") { GetDonationInfo(m: u.Message); return; } //okay, they are joining a game. string nodeid = ""; string gameid = ""; Models.Node node = null; Models.GameInfo game = null; ChatMemberStatus status = ChatMemberStatus.Creator; try { nodeid = args[1].Substring(0, 32); gameid = args[1].Substring(32); //try to get the guid of the game they want to join Guid g, n; if (!(Guid.TryParse(nodeid, out n) && Guid.TryParse(gameid, out g))) { return; } //first get the node where to search for the game for (var i = 0; i < 3; i++) { node = Bot.Nodes.ToList().FirstOrDefault(x => x.ClientId == n); if (node != null) { break; } } if (node == null) { //log it //Bot.Send($"{u.Message.From.Id} (@{u.Message.From.Username ?? ""}) didn't find node with guid {n.ToString()} while attempting to play in {g.ToString()}", -1001098399855); return; } //we have the node, get the game for (var i = 0; i < 5; i++) { game = node.Games.ToList().FirstOrDefault(x => x.Guid == g); if (game != null) { break; } } if (game == null) { //log it //Bot.Send($"{u.Message.From.Id} (@{u.Message.From.Username ?? ""}) found node with guid {n.ToString()} but not the game {g.ToString()}", -1001098399855); return; } //ok we got the game, now join //make sure they are member status = Bot.Api.GetChatMemberAsync(game.GroupId, u.Message.From.Id).Result.Status; if (status == ChatMemberStatus.Left || status == ChatMemberStatus.Kicked) { Bot.Send( GetLocaleString("NotMember", GetLanguage(u.Message.From.Id), game.ChatGroup.ToBold()), u.Message.Chat.Id); return; } game.AddPlayer(u); return; } catch (AggregateException e) { var ex = e.InnerExceptions[0]; while (ex.InnerException != null) { ex = ex.InnerException; } Send(ex.Message, u.Message.Chat.Id); Send($"Error in START:\n" + $"{u.Message.Text}\n" + $"Node: {nodeid}\n" + $"Game: {gameid}\n" + $"Found Node: {node?.ClientId}\n" + $"Found Game: {game?.Guid}\n" + $"Chat Member Status: {status}\n" + $"{ex.Message}\n{ex.StackTrace}", Settings.ErrorGroup); } catch (Exception ex) { while (ex.InnerException != null) { ex = ex.InnerException; } Send(ex.Message, u.Message.Chat.Id); Send($"Error in START:\n" + $"{u.Message.Text}\n" + $"Node: {nodeid}\n" + $"Game: {gameid}\n" + $"Found Node: {node?.ClientId}\n" + $"Found Game: {game?.Guid}\n" + $"Chat Member Status: {status}\n" + $"{ex.Message}\n{ex.StackTrace}", Settings.ErrorGroup); } } } }
private static void ServerOnDataReceived(object sender, Message message) { try { var messages = message.MessageString.Split('\u0013'); foreach (var msg in messages) { if (String.IsNullOrWhiteSpace(msg) || String.IsNullOrWhiteSpace(msg.Replace("\0", ""))) continue; dynamic m = JsonConvert.DeserializeObject(msg); string t = m.JType?.ToString(); if (t != null) { Node node; switch (t) { case "ClientRegistrationInfo": var cri = JsonConvert.DeserializeObject<ClientRegistrationInfo>(msg); //validate the client if (cri.Secret == Settings.TcpSecret) { //we can register var n = new Node {ClientId = cri.ClientId, TcpClient = message.TcpClient}; Bot.Nodes.Add(n); Bot.NodeConnected(n); //n.Broadcast("Registered"); Program.Log($"Client registered: {cri.ClientId}"); } break; case "NodeInfo": var ni = JsonConvert.DeserializeObject<NodeInfo>(msg); node = Bot.Nodes.FirstOrDefault(x => x.ClientId == ni.ClientId); if (node == null) { node = new Node {ClientId = ni.ClientId, TcpClient = message.TcpClient}; Bot.Nodes.Add(node); Bot.NodeConnected(node); } node.CurrentGames = ni.CurrentGames; node.CurrentPlayers = ni.CurrentPlayers; node.DuplicateGamesRemoved = ni.DuplicateGamesRemoved; node.ThreadCount = ni.ThreadCount; node.TotalGames = ni.TotalGames; node.TotalPlayers = ni.TotalPlayers; node.Uptime = ni.Uptime; node.Games = ni.Games; node.Version = ni.Version; node.ShuttingDown = ni.ShuttingDown; if (ni.Version.Contains("5984.20648")) node.ShuttingDown = true; break; case "GameEndInfo": var gei = JsonConvert.DeserializeObject<GameEndInfo>(msg); node = Bot.Nodes.FirstOrDefault(x => x.ClientId == gei.ClientId); node?.EndGame(gei); break; } } } } catch (Exception e) { Program.Log($"Error in message received: {e.Message}\n{message.MessageString}", true); } }