public async Task ExecuteAsync(UserMessage message) { var betEventClient = new BetEventClient("http://localhost:27254"); var betClient = new BetClient("http://localhost:27254"); var userClient = new UserClient("http://localhost:27254"); var myUser = await userClient.GetByTelegramIdAsync(message.TelegramMessage.From.Id); if (myUser == null) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Ты еще не зарегистрирован, введи /start, чтобы это сделать"); return; } var userBets = await betClient.AllBetsForUserAsync(message.TelegramMessage.From.Id); var outputMessage = new StringBuilder(); outputMessage.Append($"Информация о профиле:\n" + $"Баланс: {myUser.Balance}\n" + $"Мои ставки:\n"); foreach (var bet in userBets) { var myEvent = await betEventClient.GetAsync(bet.EventId); outputMessage.Append($"Название: {myEvent.Name}\n" + $"Выбранный исход: {bet.Outcome}\n" + $"Сумма ставки: {bet.BetSize}\n" + $"______________\n"); } await client.SendTextMessageAsync(message.TelegramMessage.Chat, outputMessage.ToString()); }
public async Task CRUD_Bet_Test() { var client = new BetClient("http://localhost:27254"); var createdBet = await client.CreateAsync(new BetMeta { BetSize = 19, EventId = Guid.NewGuid(), UserId = Guid.NewGuid(), Outcome = "win" }); var allBets = await client.GetAllAsync(); var anotherCreatedBet = await client.CreateAsync(new BetMeta { BetSize = 4, EventId = Guid.NewGuid(), UserId = Guid.NewGuid(), Outcome = "lose" }); var updatedBet = await client.UpdateAsync(new Bet { Id = createdBet.Id, BetSize = 100, EventId = createdBet.EventId, UserId = createdBet.UserId, Outcome = "lose" }); var newBet = await client.GetAsync(updatedBet.Id); var deletedBet = await client.DeleteAsync(newBet.Id); var allBetsAfterDeletion = await client.GetAllAsync(); Console.WriteLine(allBetsAfterDeletion); }
public async Task ExecuteAsync(UserMessage message) { var state = stateService.GetCurrentState(message.User); var eventClient = new BetEventClient("http://localhost:27254"); var betClient = new BetClient("http://localhost:27254"); var userClient = new UserClient("http://localhost:27254"); if (state is PlaceBetCommandState betState) { switch (betState.State) { case PlaceBetState.EventNumber: var allEvents = await eventClient.GetAllAsync(); int chosenEventNumber; if (!Int32.TryParse(message.Tail, out chosenEventNumber)) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "введите, пожалуйста, номер цифрами"); break; } if (chosenEventNumber < 1 || chosenEventNumber > allEvents.Length) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Введите, пожалуйста, существующий номер события"); break; } var chosenEvent = await eventClient.GetAsync((betState.IdDictionary[chosenEventNumber])); await client.SendTextMessageAsync(message.TelegramMessage.Chat, $"Событие {chosenEventNumber} - {chosenEvent.Name}\n" + $"Исходы:\n" + $"{chosenEvent.Outcomes}\n" + $"Дедлайн: {chosenEvent.BetDeadline}\n" + $"Описание: {chosenEvent.Description}\n" + $"----------------\n" + $" Введите исход, на которую вы хотите сделать ставку"); stateService.SaveState(message.User, new PlaceBetCommandState() { State = PlaceBetState.Outcome, EventId = chosenEvent.Id }); break; case PlaceBetState.Outcome: var chosenOutcome = message.Tail; var myEvent = await eventClient.GetAsync(betState.EventId); var myEventOutcomes = myEvent.Outcomes.Split('\n'); if (!myEventOutcomes.Contains(chosenOutcome)) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Введите, пожалуйста, существующий исход"); break; } await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Введите размер ставки"); stateService.SaveState(message.User, new PlaceBetCommandState() { State = PlaceBetState.BetAmount, EventId = betState.EventId, Outcome = chosenOutcome }); break; case PlaceBetState.BetAmount: int betAmount; if (!Int32.TryParse(message.Tail, out betAmount)) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "введите, пожалуйста, номер цифрами"); break; } var user = await userClient.GetByTelegramIdAsync(message.TelegramMessage.From.Id); var balance = user.Balance; if (betAmount < 1 || balance < betAmount) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Вы ввели некорректный размер ставки, размер ставки не должен превышать ваш баланс"); break; } var bet = await betClient.CreateAsync(new BetMeta { BetSize = betAmount, EventId = betState.EventId, UserId = user.Id, Outcome = betState.Outcome }); //поправить юзер айди await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Ваша ставка сохранена"); stateService.DeleteState(message.User); break; } } else { var allEvents = await eventClient.GetAllAsync(); var allEventsString = new StringBuilder(); var i = 1; var dictionary = new ConcurrentDictionary <int, Guid>(); var overduedEvents = allEvents.Where(oneEvent => oneEvent.BetDeadline < DateTime.Now).ToList(); allEvents = allEvents.Where(val => !overduedEvents.Contains(val)).ToArray(); if (allEvents.Length == 0) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, $"Событий пока нет, чтобы предложить свое событие, напиши /createevent"); return; } foreach (var oneEvent in allEvents) { dictionary[i] = oneEvent.Id; allEventsString.Append($"{i} - {oneEvent.Name}\n" + $"Исходы: \n" + $"{oneEvent.Outcomes}\n" + $"Дедлайн: {oneEvent.BetDeadline}\n" + $"Описание: {oneEvent.Description}\n" + $"----------------\n"); i++; } await client.SendTextMessageAsync(message.TelegramMessage.Chat, $"{allEventsString.ToString()} \n Введите номер события, на которое хотите сделать ставку"); stateService.SaveState(message.User, new PlaceBetCommandState() { State = PlaceBetState.EventNumber, IdDictionary = dictionary }); } }
public async Task ExecuteAsync(UserMessage message) { var state = stateService.GetCurrentState(message.User); var eventClient = new BetEventClient("http://localhost:27254"); var betClient = new BetClient("http://localhost:27254"); if (state is CloseEventCommandState closeState) { switch (closeState.State) { case CloseEventState.EventChoosing: var allEvents = await eventClient.GetAllEventsFromCreatorAsync(message.TelegramMessage.From.Id); int chosenEventNumber; if (!Int32.TryParse(message.Tail, out chosenEventNumber)) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "введите, пожалуйста, номер цифрами"); break; } if (chosenEventNumber < 1 || chosenEventNumber > allEvents.Length) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Введите, пожалуйста, существующий номер ставки"); break; } var chosenEvent = await eventClient.GetAsync((closeState.IdDictionary[chosenEventNumber])); await client.SendTextMessageAsync(message.TelegramMessage.Chat, $"Событие {chosenEventNumber} - {chosenEvent.Name}\n" + $"Исходы:\n" + $"{chosenEvent.Outcomes}\n" + $"Дедлайн: {chosenEvent.BetDeadline}\n" + $"Описание: {chosenEvent.Description}\n" + $"----------------\n" + $" Выберите произошедший исход"); stateService.SaveState(message.User, new CloseEventCommandState() { State = CloseEventState.WinningOutcome, ChosenEventId = chosenEvent.Id }); break; case CloseEventState.WinningOutcome: var chosenOutcome = message.Tail; var myEvent = await eventClient.GetAsync(closeState.ChosenEventId); var myEventOutcomes = myEvent.Outcomes.Split('\n'); if (!myEventOutcomes.Contains(chosenOutcome)) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Введите, пожалуйста, существующий исход"); break; } await eventClient.CloseEventAsync(closeState.ChosenEventId, chosenOutcome); stateService.DeleteState(message.User); await client.SendTextMessageAsync(message.TelegramMessage.Chat, "Событие успешно закрыто"); break; } } else { var allEvents = await eventClient.GetAllEventsFromCreatorAsync(message.TelegramMessage.From.Id); var allEventsString = new StringBuilder(); var i = 1; var dictionary = new ConcurrentDictionary <int, Guid>(); if (allEvents.Length == 0) { await client.SendTextMessageAsync(message.TelegramMessage.Chat, $"Событий пока нет, чтобы предложить свое событие, напиши /createevent"); return; } foreach (var oneEvent in allEvents) { dictionary[i] = oneEvent.Id; allEventsString.Append($"{i} - {oneEvent.Name}\n" + $"Исходы: {oneEvent.Outcomes}\n" + $"Дедлайн: {oneEvent.BetDeadline}\n" + $"Описание: {oneEvent.Description}\n" + $"----------------\n"); i++; } await client.SendTextMessageAsync(message.TelegramMessage.Chat, $"{allEventsString.ToString()} \n Введите номер события, которое хотите закрыть"); stateService.SaveState(message.User, new CloseEventCommandState() { State = CloseEventState.EventChoosing, IdDictionary = dictionary }); } }