public static Dictionary <ObjectGame.ObjcetIds, PlainBmpTexture> CreateGameObjectsTextures(Size aTextureSize, ShaderProgram aProgram) { Dictionary <ObjectGame.ObjcetIds, GameObjectType> objTypeList = ObjectGame.GetObjTypes(); Dictionary <ObjectGame.ObjcetIds, PlainBmpTexture> objTextureList = new Dictionary <ObjectGame.ObjcetIds, PlainBmpTexture>(); SolidBrush tempBrush; Bitmap tempBmp; ObjMaterial tempMaterial; foreach (KeyValuePair <ObjectGame.ObjcetIds, GameObjectType> keyValuePair in objTypeList) { PlainBmpTexture tempBmpTexture = new PlainBmpTexture(keyValuePair.Value.Name) { Color = keyValuePair.Value.Color }; tempBrush = new SolidBrush(tempBmpTexture.Color); tempBmp = BitmapHelper.CreatBitamp(aTextureSize, tempBrush); tempBmpTexture.TextureBmp = tempBmp; tempMaterial = new ObjMaterial(aProgram) { DiffuseMap = new Texture(tempBmp) }; tempBmpTexture.Material = tempMaterial; objTextureList.Add(keyValuePair.Key, tempBmpTexture); } return(objTextureList); }
/// <summary> /// Поиск номера игрока, который должен ходить следующим /// </summary> /// <param name="objectGame">Объект активной игры</param> /// <returns>Номер игрока</returns> private int FindSelectedUser(ObjectGame objectGame) { var count = 0; var member = -1; var gameInfo = (GameInfo)objectGame.Info; for (var i = 0; i < objectGame.Users.Count; i++) { var user = (UserInfo)objectGame.Users[i].UserInGame; for (var j = 0; j < user.Card.Count; j++) { var card = user.Card[j]; if (card[0] == gameInfo.MainSuit) { if (int.Parse(Change(card.Substring(1, card.Length - 1))) > count) { member = i; count = int.Parse(Change(card.Substring(1, card.Length - 1))); } } } } return(member); }
/// <summary> /// Досрочный выход из игры /// </summary> /// <param name="message"> Сообщение пользователя </param> /// <param name="objectGame"> Информация по активной игре </param> public async void Exit(Message message, ObjectGame objectGame) { var id = message.From.Id; var info = (GameInfo)objectGame.Info; var hidden = info.NumberIsHidden; GameBotOnline.Turn[1].RemoveAt(objectGame.NumberInTurnGame); await Program.Bot.SendTextMessageAsync(id, "Вы вышли из игры\n" + "А было загадано число: " + hidden, replyMarkup : Keyboards.Keyboard); }
/// <summary> /// Переадресация сообщения на другого пользователя , с кем связан этот пользователь. /// </summary> /// <param name="message">Сообщение, которое нужно обработать.</param> /// <param name="infoGame">Информация по активной игре. </param> public async Task SendMessage(Message message, ObjectGame infoGame) { string stranger; if (message.From.Id.ToString() == infoGame.Users[0].Id) { stranger = infoGame.Users[1].Id; } else { stranger = infoGame.Users[0].Id; } await Program.Bot.SendTextMessageAsync(stranger, message.Text); }
/// <summary> /// Генерация текста сообщения для пользователей (подробности игры) /// </summary> /// <param name="objectGame">Объект активной игры</param> /// <returns>Текст для пользователей</returns> private string InlineFullGame(ObjectGame objectGame) { var info = (GameInfo)objectGame.Info; var userCard = new string[objectGame.Users.Count]; for (var i = 0; i < objectGame.Users.Count; i++) { var userInGame = (UserInfo)objectGame.Users[i].UserInGame; userCard[i] += i == info.NumUserSelect ? $"❗{objectGame.Users[i].Name}: x" : $"ᅠ{objectGame.Users[i].Name}: x"; userCard[i] += userInGame.Card.Count + "🃏"; } string result = null; result += "ᅠᅠИгра \"Дурак\"\n\n"; foreach (var str in userCard) { result += str + "\n"; } result += "\n"; result += "Cтол:\n"; for (var i = 0; i < info.Now.Count; i++) { if (i % 2 == 1) { result += info.Now[i] != "-1" ? info.Now[i] : "🃏"; result += " "; } else { result += info.Now[i]; } } result += $"\nКолода: 🃏x{info.Deck.Count} | Козырь: {info.MainSuit}"; if (info.Stage == 2) { result += $"\n\n❌Пасанули - {info.StagePass}/{objectGame.Users.Count - 1} игроков ❌"; } return(result); }
/// <summary> /// Обновление карт на руках у игроков /// </summary> /// <param name="game">Объект активной игры</param> /// <returns></returns> private void UpdateDeck(ObjectGame game) { var gameInfo = (GameInfo)game.Info; var numberUser = gameInfo.NumUserSelect - 1; for (var i = numberUser; i > numberUser - gameInfo.CountUser; i--) { var j = i >= 0 ? i : i + gameInfo.CountUser; var userSelected = (UserInfo)game.Users[j].UserInGame; while (userSelected.Card.Count < 6 && gameInfo.Deck.Count > 0) { userSelected.Card.Add(gameInfo.Deck[0]); gameInfo.Deck.RemoveAt(0); } game.Users[j].UserInGame = userSelected; } game.Info = gameInfo; }
/// <summary> /// Создание новой сессии/игры /// </summary> /// <param name="newGame">Объект игры</param> /// <param name="numberGameInTurn">Номер игры в очереди ожидания/></param> private static async void NewSession(ObjectGame newGame, int numberGameInTurn) { var game = new GameInfo(numberGameInTurn) { CountUser = newGame.CountPlayer }; var cards = new Durak().GeneratedCards(); for (var i = 0; i < game.CountUser; i++) { var myCards = new List <string>(); for (var j = 0; j < 6; j++) { myCards.Add(cards[0]); cards.RemoveAt(0); } var user = new UserInfo { Card = myCards }; newGame.Users[i].UserInGame = user; } game.Deck = cards; game.MainSuit = cards[cards.Count - 1][0]; game.Stage = 0; game.Now = new List <string>(); newGame.NumberGame = 3; newGame.Info = game; game.NumUserSelect = Math.Max(0, new Durak().FindSelectedUser(newGame)); newGame.Info = game; GameBotOnline.Turn[NumberGame][newGame.NumberInTurnGame] = newGame; // UPDATE CARD var result = new Durak().InlineFullGame(GameBotOnline.Turn[NumberGame][newGame.NumberInTurnGame]); await new Durak().SendMessage(GameBotOnline.Turn[NumberGame][newGame.NumberInTurnGame], result); }
private void ProcessingMotion(Message message, ObjectGame objectGame, GameInfo gameInfo, int numberUser, UserInfo usersInfo, int numberCard) { switch (gameInfo.Stage) { //пользователи ходят case 0: FirstMotion(message, gameInfo, numberUser, usersInfo, numberCard); break; //пользователи отбиваются case 1: // SecondMotion(message, gameInfo, numberUser, usersInfo, numberCard); break; ////пользователи подкидывают case 2: ThirdthMotion(message, objectGame, gameInfo, numberUser, usersInfo, numberCard); break; } }
/// <summary> /// Выход из игры досрочно /// </summary> /// <param name="userId"> UserId пользователя, который выходит из игры</param> /// <param name="objectGame">Объект игры,из которой пользователь выходит</param> public override void Quit(Message userId, ObjectGame objectGame) { var numberUser = -1; if (objectGame.CountPlayer > 2) { for (var i = 0; i < objectGame.CountPlayer; i++) { if (userId.Chat.Id.ToString() == objectGame.Users[i].Id) { numberUser = i; } } } base.Quit(userId, objectGame); if (numberUser != -1) { var info = (GameInfo)objectGame.Info; if (info.NumUserSelect >= numberUser) { info.NumUserSelect--; } } }
private void Awake() { instance = this; objectGame = new ObjectGame("1", "windows", "0", "Action", "", "1", ""); }
/// <summary> /// Метод, реализующий логику игры Быки и Коровы /// </summary> /// <param name="message"> Сообщение пользователья</param> /// <param name="dataGame"> Информация по игре пользователя</param> /// <returns></returns> public async Task InGame(Message message, ObjectGame dataGame) { var mesText = message.Text.ToLower(); var id = message.From.Id; byte bull = 0; byte cow = 0; var info = (GameInfo)dataGame.Info; var hidden = info.NumberIsHidden; var number = new int[10]; var badRequest = true; if (mesText.Length == 5) { for (var i = 0; i < mesText.Length; i++) { badRequest = int.TryParse(mesText[i].ToString(), out var index); if (!badRequest) { break; } if (number[index] != 0) { badRequest = false; break; } number[index] = i + 1; } } else { badRequest = false; } if (!badRequest) { await BadRequest(id); return; } info.Motion++; GameBotOnline.Turn[1][dataGame.NumberInTurnGame].Info = info; for (var i = 0; i < mesText.Length; i++) { var index = Convert.ToInt32(hidden[i].ToString()); if (number[index] == 0) { continue; } if (number[index] == i + 1) { bull++; } else { cow++; } } if (bull == 5) { GameBotOnline.Turn[1].RemoveAt(dataGame.NumberInTurnGame); await Program.Bot.SendTextMessageAsync(id, "Поздравляю! Вы выиграли!\n" + $"Количество шагов: {info.Motion}\n" + "Чтобы снова сыграть , выберите \"Быкануть\".", replyMarkup : Keyboards.Keyboard); } else { await Program.Bot.SendTextMessageAsync(id, $"Коров: {cow}\n" + $"Быков: {bull}\n" + $"Шаг: {info.Motion}"); } }
/// <summary> /// Обработка хода игрока /// </summary> /// <param name="message">Сообщение пользователя</param> /// <param name="objectGame">Подробности игры</param> /// <returns></returns> public async Task Motion(Message message, ObjectGame objectGame) { await Program.Bot.DeleteMessageAsync(message.Chat.Id.ToString(), message.MessageId); var numberUser = -1; var number = objectGame.NumberInTurnGame; var gameInfo = (GameInfo)objectGame.Info; var stage = gameInfo.Stage; User you = null; for (var i = 0; i < objectGame.Users.Count; i++) { if (objectGame.Users[i].Id == message.From.Id.ToString()) { if (i != gameInfo.NumUserSelect && gameInfo.Stage == 0 || i == gameInfo.NumUserSelect && gameInfo.Stage == 1 || i == (gameInfo.NumUserSelect + 1) % gameInfo.CountUser && (gameInfo.Stage == 0 || gameInfo.Stage == 2)) { await Program.Bot.SendTextMessageAsync(message.From.Id, "Не ваш ход!"); return; } numberUser = i; you = objectGame.Users[i]; break; } } if (you == null) { return; } var usersInfo = (UserInfo)you.UserInGame; var numberCard = -1; for (var i = 0; i < usersInfo.Card.Count; i++) { if (usersInfo.Card[i] == message.Text) { numberCard = i; break; } } ProcessingMotion(message, objectGame, gameInfo, numberUser, usersInfo, numberCard); var game = (GameInfo)objectGame.Info; var flag = false; for (var i = 0; i < objectGame.Users.Count; i++) { var user = (UserInfo)objectGame.Users[i].UserInGame; if (game.Deck.Count == 0 && user.Card.Count == 0) { await Program.Bot.SendTextMessageAsync(objectGame.Users[i].Id, "Поздравляем! Вы победили :)", replyMarkup : Keyboards.Keyboard); game.CountUser--; if (you.Id == objectGame.Users[i].Id) { flag = true; } if (game.NumUserSelect >= i) { game.NumUserSelect--; } objectGame.Info = game; new Durak().DeleteMessage(objectGame.Users[i].Id, int.Parse(objectGame.Users[i].MessageId), 5000); objectGame.Users.RemoveAt(i); if (objectGame.Users.Count == 1) { await Program.Bot.SendTextMessageAsync(objectGame.Users[0].Id, "Упс, увы , вы проиграли :(\n Сыграйте ещё раз.", replyMarkup : Keyboards.Keyboard); new Durak().DeleteMessage(objectGame.Users[0].Id, int.Parse(objectGame.Users[0].MessageId), 3000); for (var j = number + 1; j < GameBotOnline.Turn[NumberGame].Count; j++) { GameBotOnline.Turn[NumberGame][i].NumberInTurnGame--; } GameBotOnline.Turn[NumberGame].RemoveAt(number); return; } } } if (!flag) { objectGame.Users[numberUser].UserInGame = usersInfo; } GameBotOnline.Turn[NumberGame][number] = objectGame; if (game.Stage == 0 && stage == 2) { UpdateDeck(GameBotOnline.Turn[NumberGame][number]); var info = (GameInfo)GameBotOnline.Turn[NumberGame][number].Info; info.Take = false; } if (game.Stage == 2 && stage == 1 || game.Stage == 0 && stage == 2) { for (var i = 0; i < GameBotOnline.Turn[NumberGame][number].CountPlayer; i++) { var us = (UserInfo)GameBotOnline.Turn[NumberGame][number].Users[i].UserInGame; us.Pass = false; GameBotOnline.Turn[NumberGame][number].Users[i].UserInGame = us; } } var result = InlineFullGame(GameBotOnline.Turn[NumberGame][number]); await SendMessage(GameBotOnline.Turn[NumberGame][number], result); }
/// <summary> /// Отправка сообщения пользователям относительно их хода /// </summary> /// <param name="objectGame">Объект активной игры</param> /// <param name="message">Основной текст для всех пользователей</param> private async Task SendMessage(ObjectGame objectGame, string message) { var info = (GameInfo)objectGame.Info; var keyboard = new ReplyKeyboardMarkup { OneTimeKeyboard = true, ResizeKeyboard = true }; for (var i = 0; i < objectGame.Users.Count; i++) { var user = (UserInfo)objectGame.Users[i].UserInGame; user.Card.Sort(); var keyboards = new KeyboardButton[user.Card.Count / 4 + Math.Min(user.Card.Count % 4, 1) + 1][]; for (var q = 0; q < user.Card.Count / 4 + Math.Min(user.Card.Count % 4, 1); q++) { var buttonLine = new List <KeyboardButton>(); for (var j = q * 4; j < user.Card.Count && j < q * 4 + 4; j++) { buttonLine.Add(new KeyboardButton(user.Card[j])); } keyboards[q] = buttonLine.ToArray(); } if (i == (info.NumUserSelect + 1) % info.CountUser) { keyboards[user.Card.Count / 4 + Math.Min(user.Card.Count % 4, 1)] = new[] { new KeyboardButton("Беру!") } } ; else { keyboards[user.Card.Count / 4 + Math.Min(user.Card.Count % 4, 1)] = new[] { new KeyboardButton("Пас!") } }; keyboard.Keyboard = keyboards; switch (info.Stage) { case 0: if (info.NumUserSelect == i) { message += "\n\nВы должны походить или нажать \"Пас!\":"; } else if ((info.NumUserSelect + 1) % info.CountUser == i) { message += "\n\nОжидайте, вы будете отбиваться:"; } else { message += "\n\nОжидайте, вы будете подкидывать:"; } break; case 1: if ((info.NumUserSelect + 1) % info.CountUser == i) { message += "\n\nВы должны отбиваться:"; } else { message += "\n\nОжидайте,вы будете подкидывать:"; } break; case 2: if ((info.NumUserSelect + 1) % info.CountUser == i) { message += "\n\nВам подкидывают:"; } else if ((info.NumUserSelect + 1) % info.CountUser == i) { message += "\n\nОжидайте, вам подкидывают :"; } else { message += "\n\nВы можете сейчас подкинуть или нажать \"Пас!\" :"; } break; } if (objectGame.Users[i].MessageId == null) { var mes = await Program.Bot.SendTextMessageAsync(objectGame.Users[i].Id, message, replyMarkup : keyboard); objectGame.Users[i].MessageId = mes.MessageId.ToString(); } else { var mes = await Program.Bot.SendTextMessageAsync(objectGame.Users[i].Id, message, replyMarkup : keyboard); await Program.Bot.DeleteMessageAsync(objectGame.Users[i].Id, int.Parse(objectGame.Users[i].MessageId)); objectGame.Users[i].MessageId = mes.MessageId.ToString(); } objectGame.Users[i].UserInGame = user; } objectGame.Info = info; }
/// <summary> /// Обработка хода, в случае, если игроки "подкидывают" /// </summary> /// <param name="message"></param> /// <param name="objectGame"></param> /// <param name="gameInfo"></param> /// <param name="numberUser"></param> /// <param name="usersInfo"></param> /// <param name="numberCard"></param> private static void ThirdthMotion(Message message, ObjectGame objectGame, GameInfo gameInfo, int numberUser, UserInfo usersInfo, int numberCard) { if (gameInfo.Stage == 2 && numberUser != (gameInfo.NumUserSelect + 1) % gameInfo.CountUser) { if (message.Text.ToLower() == "пас!") { switch (usersInfo.Pass) { case true: break; case false: gameInfo.StagePass++; usersInfo.Pass = true; if (gameInfo.StagePass == gameInfo.CountUser - 1 && gameInfo.Take) { gameInfo.Stage = 0; var user = (UserInfo)objectGame. Users[(gameInfo.NumUserSelect + 1) % gameInfo.CountUser].UserInGame; for (var i = 0; i < gameInfo.Now.Count; i++) { if (gameInfo.Now[i] != "-1") { user.Card.Add(gameInfo.Now[i]); } } gameInfo.Now = new List <string>(); objectGame.Users[(gameInfo.NumUserSelect + 1) % gameInfo.CountUser]. UserInGame = user; gameInfo.NumUserSelect = (gameInfo.NumUserSelect + 2) % gameInfo.CountUser; } var j = 1; while (j < gameInfo.Now.Count && gameInfo.Now[j] != "-1") { j += 2; } if (gameInfo.StagePass == gameInfo.CountUser - 1 && !gameInfo.Take && j > gameInfo.Now.Count) { gameInfo.Now = new List <string>(); gameInfo.Stage = 0; gameInfo.NumUserSelect = (gameInfo.NumUserSelect + 1) % gameInfo.CountUser; } if (gameInfo.StagePass == gameInfo.CountUser - 1 && !gameInfo.Take && j <= gameInfo.Now.Count) { gameInfo.Stage = 1; } break; } } else if (numberCard != -1) { var countCards = 0; var contains = false; var card = usersInfo.Card[numberCard]; for (var i = 0; i < gameInfo.Now.Count; i++) { if (gameInfo.Now[i] == "-1") { countCards++; } if (gameInfo.Now[i][1] == card[1]) { contains = true; } } var user = (UserInfo)objectGame.Users[gameInfo.NumUserSelect].UserInGame; if (countCards < user.Card.Count && contains) { gameInfo.Now.Add(card); gameInfo.Now.Add("-1"); usersInfo.Card.RemoveAt(numberCard); } else if (countCards + 1 >= user.Card.Count) { gameInfo.Stage = 2; } objectGame.Users[gameInfo.NumUserSelect].UserInGame = user; } } }