/// <summary> /// Notifica al juez y le muestra su carta negra /// </summary> /// <returns></returns> private async void NotifyJudge() { SingletonBot bot = SingletonBot.Instance; Library.User judge = (Library.User)bot.GetJudge(); Card cardBlack = bot.AskBlackCard(); await Bot.SendTextMessageAsync(judge.ID, "Sos el juez"); await Bot.SendTextMessageAsync(judge.ID, "Tu carta es " + cardBlack.ToString()); }
/// <summary> /// Notifica la carta ganadora a los jugadores /// </summary> /// <param name="card"></param> /// <returns></returns> private async void notifyWin(Card card) { SingletonBot bot = SingletonBot.Instance; IEnumerator <Library.User> listUser = bot.GetListUser(); while (listUser.MoveNext()) { Library.User user = listUser.Current; await Bot.SendTextMessageAsync(user.ID, "El juez seleccionó esta carta :" + card); } }
/// <summary> /// Notifica a todos los jugadores que empezó el juego. /// </summary> /// <returns></returns> private async void NotifyPlayer() { SingletonBot bot = SingletonBot.Instance; IEnumerator <Library.User> listUser = bot.GetListUser(); while (listUser.MoveNext()) { Library.User user = listUser.Current; await Bot.SendTextMessageAsync(user.ID, "Empieza el juego, espera que se repartan las cartas"); } }
/// <summary> /// Muestra a los jugadores que no son juez la carta negra, y sus cartas como opciones de respuesta. /// </summary> /// <returns></returns> private async void NotifyPlayerBlackCardAndWhiteCards() { SingletonBot bot = SingletonBot.Instance; Card blackCard = bot.AskBlackCard(); await Bot.SendTextMessageAsync(userLast.ID, "Carta del juez: " + blackCard.Text); IEnumerator <Card> iteratorCardWhite = userLast.EnumeratorCards(); int i = 1; while (iteratorCardWhite.MoveNext()) { Card card = iteratorCardWhite.Current; await Bot.SendTextMessageAsync(userLast.ID, "Opcion " + i + ": " + card.ToString()); i++; } await Bot.SendTextMessageAsync(userLast.ID, "Seleccione una carta ingresando el número elegido seguido de Opción. Ejemplo: Opción 1"); }
/// <summary> /// Se muestra al juez las opciones de respuestas, y se le pide que seleccione la ganadora. /// </summary> /// <returns></returns> private async void NotifyJudge2() { SingletonBot bot = SingletonBot.Instance; IEnumerator <Card> iteratorCardWhite = bot.AskEnumeratorCardsAnswer(); int i = 1; Library.User judge = (Library.User)bot.GetJudge(); await Bot.SendTextMessageAsync(judge.ID, "Selecciona la carta ganadora. Ejemplo: Opción 1"); while (iteratorCardWhite.MoveNext()) { Card card = iteratorCardWhite.Current; await Bot.SendTextMessageAsync(judge.ID, "Opcion " + i + ": " + card.ToString()); i++; } await Bot.SendTextMessageAsync(judge.ID, "Seleccione una carta: "); }
/// <summary> /// Punto de entrada. /// </summary> public void StartTelegram() { Bot = new TelegramBotClient(Token); var cts = new CancellationTokenSource(); SingletonBot bot = SingletonBot.Instance; bot.CreateGame("configuration.csv", "cards.csv"); // Comenzamos a escuchar mensajes. Esto se hace en otro hilo (en _background_). Bot.StartReceiving( new DefaultUpdateHandler(HandleUpdateAsync, HandleErrorAsync), cts.Token ); Console.WriteLine($"Bot is Up!"); // Esperamos a que el usuario aprete Enter en la consola para terminar el bot. Console.ReadLine(); // Terminamos el bot. cts.Cancel(); }
/// <summary> /// Maneja los mensajes que se envían al bot. /// </summary> /// <param name="message">El mensaje recibido</param> /// <returns></returns> private async Task HandleMessageReceived(Message message) { Console.WriteLine($"Received a message from {message.From.FirstName} saying: {message.Text}"); SingletonBot bot = SingletonBot.Instance; string response = ""; string comand = Analysis(message.Text); switch (comand) { case "/start": response = "Bienvenid@ a Cards Against Programming! ¿Quieres jugar? Responde sí o no"; break; case "no": response = "Te lo pierdes! Esperamos verte pronto."; break; case "si": case "sí": response = "Excelente! Responde cómo quieres llamarte seguido de la palabra alias. Ejemplo: alias Carmen."; break; case "alias": try{ string userName = message.Text.Split(" ")[1]; bot.CreatUser(userName, message.Chat.Id); bool isStart = bot.StartGame(); response = "Bien " + userName + "! Se creó tu alias correctamente."; if (isStart) { NotifyPlayer(); NotifyJudge(); userLast = bot.AskCurrentPlayer(); NotifyPlayerBlackCardAndWhiteCards(); } }catch (UserException e) { response = e.Message; } catch (IndexOutOfRangeException) { response = "Che, te dije alias separado con un espacio"; } catch (Exception) { response = "Sabes que, no tengo ni idea que paso"; } break; case "opcion": case "opción": try { Library.User judge = (Library.User)bot.GetJudge(); int number = Convert.ToInt32(message.Text.Split(" ")[1]) - 1; if (number < 0 || number > 9) { response = "Debes elegir una opción del 1 al 10. Elemplo: Opción 1"; } else { if (bot.isAskNextPlayer()) { userLast = bot.GetUserActually(); Card cardSelect = userLast.GetCard(number); bot.AddAnswer(cardSelect); //Siguiente jugador, si hay muestro sus cartas y la pregunta NotifyPlayerBlackCardAndWhiteCards(); } else { if (message.Chat.Id != judge.ID) { Card cardSelect = userLast.GetCard(number); Console.WriteLine(cardSelect); bot.AddAnswer(cardSelect); //Mostrar las cartas de respuesta de cada jugador NotifyJudge2(); } else //Responde el juez { Card cardWin = bot.CardSelectWhite(number); notifyWin(cardWin); bot.AskWinner(cardWin); NotifyJudge(); userLast = bot.GetUserActually(); NotifyPlayerBlackCardAndWhiteCards(); } } } } catch (FormatException) { response = "Debes elegir un número del 1 al 10. Elemplo: Opción 1"; } break; default: response = "Disculpa, no se qué hacer con ese mensaje!"; break; } // enviamos el texto de respuesta await Bot.SendTextMessageAsync(message.Chat.Id, response); }