private TelegramUserContext GetContextByMessage(SingleBot bot, Message message) { var contextKey = GetUserKey(bot, message); return(contexts.AddOrUpdate(contextKey, userKey => { var newContext = createUserContextFn(bot); newContext.UserKey = userKey; newContext.Message = message; newContext.Contexts = contexts; newContext.State = new State(); log.Info($"UserContext {contextKey} created"); return newContext; }, (k, oldContext) => { oldContext.Message = message; oldContext.CallbackQuery = null; return oldContext; })); }
private TelegramUserContext GetContextByCallback(SingleBot bot, CallbackQuery query) { var context = GetContextByMessage(bot, query.Message); context.CallbackQuery = query; return(context); }
public void Start() { Bots = settings.Bots.Select(botSettings => { var bot = new SingleBot() { Name = botSettings.Name, ContentFolder = botSettings.Content, }; bot.Settings = botSettings; bot.BotMapFile = botSettings.BotMapFile; bot.BotConfig = File.ReadAllText(botSettings.BotMapFile, Encoding.UTF8); bot.Map = bot.BotConfig.ToBotMap(); if (botSettings.ProxyHost.IsNullOrEmpty()) { bot.Client = new TelegramBotClient(botSettings.BotToken); } else { var proxy = new WebProxy(botSettings.ProxyHost); bot.Client = new TelegramBotClient(botSettings.BotToken, proxy); } bot.Client.OnMessage += async(o, a) => { try { if (!IsActual(a.Message)) { return; } if (a.Message.Text.IsNotNullOrEmpty()) { GetContextByMessage(bot, a.Message).Maestro.Type(a.Message.Text); } if (a.Message.Photo != null) { var file = await bot.Client.GetFileAsync(a.Message.Photo.Last().FileId); var fileName = settings.GetBotFile(bot.Settings.BotToken, file.FilePath); GetContextByMessage(bot, a.Message).Maestro.Photo(fileName); } } catch (Exception e) { log.Exception(e); } }; bot.Client.OnCallbackQuery += (o, a) => { try { if (IsActual(a.CallbackQuery.Message)) { GetContextByCallback(bot, a.CallbackQuery).Maestro.Command(a.CallbackQuery.Data); } } catch (Exception e) { log.Exception(e); } }; bot.Client.OnReceiveGeneralError += (o, a) => log.Exception(a.Exception); bot.Client.OnReceiveError += (o, a) => log.Exception(a.ApiRequestException); bot.Client.StartReceiving(); log.Info($"Start listening bot {botSettings.Name}"); return(bot); }).ToArray(); }
private string GetUserKey(SingleBot bot, Message message) => $"{message.Chat.Username}{bot.Name}";
public void stateUpdate() { while (true) { Thread.Sleep(1000); // tick rate foreach (string lobbyID in lobbies.Keys.ToList()) { Lobby lobby = lobbies[lobbyID]; if (lobby.lobbyState == LobbyState.PLAYING) { if (lobby.bot != null) { Bot bot = lobby.bot; Console.WriteLine("making bot move"); Board modifiedBoard = new Board(lobby.game.board.height, lobby.game.board.width); for (int i = 0; i < lobby.game.board.height; i++) { for (int j = 0; j < lobby.game.board.width; j++) { if (lobby.game.board.board[i, j] >= 1) { // Console.WriteLine("THE INDEX IS " + i + " " + j + " " + lobby.game.board.board[i, j]); modifiedBoard.board[i, j] = 1; } else { modifiedBoard.board[i, j] = 0; } } } Prints botInfoPrinter = new Prints(); // Console.WriteLine("BEFORE BOT BOARD"); // botInfoPrinter.PrintMultiDimArr(modifiedBoard.board); allBlocks[0].RemoveAt(0); allBlocks[0].Add(new Block(randomPiece.GenerateRandomPiece(), 1)); allBlocks[1].RemoveAt(0); allBlocks[1].Add(new Block(randomPiece.GenerateRandomPiece(), 1)); allBlocks[2].RemoveAt(0); allBlocks[2].Add(new Block(randomPiece.GenerateRandomPiece(), 1)); List <Tuple <int, int> > bob; try { SingleBot singleBot = new SingleBot(); List <List <Tuple <int, int> > > allBobs = singleBot.GetMove(modifiedBoard, allBlocks); bob = allBobs[0]; if (bot is DoubleBot || bot is TripleBot) { Console.WriteLine("I AM A DOUBLE BOT!!"); int[,] newBoard = new int[modifiedBoard.board.GetLength(0), modifiedBoard.board.GetLength(1)]; for (int i = 0; i < modifiedBoard.board.GetLength(0); i++) { for (int j = 0; j < modifiedBoard.board.GetLength(1); j++) { newBoard[i, j] = modifiedBoard.board[i, j]; } } foreach (Tuple <int, int> dot in bob) { newBoard[dot.Item1, dot.Item2] = 1; } Board nBoard = new Board(newBoard.GetLength(0), newBoard.GetLength(1)); nBoard.board = newBoard; List <List <Tuple <int, int> > > newBobs = singleBot.GetMove(nBoard, allBlocks); if (newBobs != null) { bob.AddRange(newBobs[0]); } } } catch (Exception e) { bob = null; } // List<List<Tuple<int, int>>> allBobs = bot.GetMove(modifiedBoard, allBlocks); // List<Tuple<int, int>> bob = allBobs[0]; if (bob == null) { Console.WriteLine("no place to place piece"); return; } else { Console.WriteLine("bot move?!@#\n\n\n"); bool moveValid = true; // foreach (Tuple<int, int> tup in bob) // { // foreach (Player player in lobby.players) // { // if (player.currentBlockPosition != null) // { // for (int i = 0; i < player.currentBlockPosition.Length; i++) // { // if (tup.Item1 == player.currentBlockPosition[i][0] && tup.Item2 == player.currentBlockPosition[i][1]) // { // moveValid = false; // } // } // } // } // } if (moveValid) { foreach (Tuple <int, int> tup in bob) { lobby.game.board.board[tup.Item1, tup.Item2] = 1; } } } } // update board for (int j = 0; j < lobby.players.Count; j++) { if (lobby.players[j].currentBlock == null) { // spawn block // lobby.players[j].currentBlock = new Block(data, 5); // lobby.players[j].currentBlockPosition = new Tuple<int, int>(5, 5); } else { if (checkCollision(lobby.players[j], lobby.game.board)) { // place block // set player's current block to null } // block falls 1 space // lobby.players[j].currentBlockPosition = new Tuple<int, int>(lobby.players[j].currentBlockPosition.Item1 - 1, lobby.players[j].currentBlockPosition.Item2); } } lobby.game.current_time += 1; // send game state to all players in lobby for (int j = 0; j < lobby.players.Count; j++) { lobby.players[j].webSocket.Send(JsonConvert.SerializeObject(lobby.game)); } } } } }