Example #1
0
        public async Task <BetEvent> CloseEventAsync(Guid id, string winOutcome)
        {
            var betClient  = new BetClient(_address);
            var userClient = new UserClient(_address);
            var sumOfMoney = await betClient.SumOfMoneyForEvent(id);

            var winners = new List <Bet>(await betClient.AllWinBetsForBetEventAsync(id, winOutcome)).Select(t => t.UserId).ToHashSet();
            var allBets = await betClient.AllBetsForBetEventAsync(id);

            if (winners.Count != 0)
            {
                var gain = sumOfMoney / winners.Count;
                foreach (var winner in winners)
                {
                    var winnerUser = await userClient.GetAsync(winner);

                    winnerUser.Balance += gain;
                    await userClient.UpdateAsync(winnerUser);
                }
            }
            foreach (var bet in allBets)
            {
                await betClient.DeleteAsync(bet.Id);
            }
            var eventForClosing = await DeleteAsync(id);

            return(eventForClosing);
        }
Example #2
0
        public async Task <Bet[]> AllBetsForUserAsync(int telegramId)
        {
            var userClient = new UserClient(_address);
            var user       = await userClient.GetByTelegramIdAsync(telegramId);

            var allBets = await GetAllAsync();

            return(new List <Bet>(allBets).FindAll(t => t.UserId == user.Id).ToArray());
        }
Example #3
0
        public async Task <Bet> CreateAsync(BetMeta betMeta)
        {
            var betJson       = JsonConvert.SerializeObject(betMeta);
            var stringContent = new StringContent(betJson, Encoding.UTF8, "application/json");
            var httpResponse  = await httpClient.PostAsync($"{_address}/api/Bet", stringContent);

            var newBet     = JsonConvert.DeserializeObject <Bet>(await httpResponse.Content.ReadAsStringAsync());
            var userClient = new UserClient(_address);
            var user       = await userClient.GetAsync(newBet.UserId);

            user.Balance -= newBet.BetSize;
            user          = await userClient.UpdateAsync(user);

            httpResponse.EnsureSuccessStatusCode();
            return(newBet);
        }