Beispiel #1
0
        public PaymentAudit RecordPayment(int positivePlayerId, int negativePlayerId, decimal amount)
        {
            var positivePlayer = _cardsContext.Players.Include(x => x.MatchesParticipatedIn).ThenInclude(x => x.Match).FirstOrDefault(x => x.PlayerId == positivePlayerId);
            var negativePlayer = _cardsContext.Players.Include(x => x.MatchesParticipatedIn).ThenInclude(x => x.Match).FirstOrDefault(x => x.PlayerId == negativePlayerId);

            if (positivePlayer == null)
            {
                throw new ArgumentException("This player doesn't exist", nameof(positivePlayerId));
            }
            if (negativePlayer == null)
            {
                throw new ArgumentException("This player doesn't exist", nameof(negativePlayerId));
            }
            Log.Logger.Information($"Recording payment of {amount:C2} from {negativePlayerId} to {positivePlayerId}");

            var match = CreateSettlementMatch(positivePlayer, negativePlayer, amount);
            var audit = new PaymentAudit
            {
                AmountTransferred = amount,
                NegativePlayerId  = negativePlayer.PlayerId,
                NegativePlayer    = negativePlayer,
                SettleMatch       = match,
                PaymentDate       = DateTime.Now,
                PositivePlayerId  = positivePlayer.PlayerId,
                PositivePlayer    = positivePlayer,
                SettleMatchId     = match.MatchId
            };

            _cardsContext.PaymentAudits.Add(audit);
            _cardsContext.SaveChanges();
            return(audit);
        }
Beispiel #2
0
        public int StartCashGame(CashGameCreateModel model)
        {
            try
            {
                if (model == null)
                {
                    return(-1);
                }
                var trans = _context.Database.BeginTransaction();
                var game  = new CashGame
                {
                    GameName    = "Cash Poker",
                    Stakes      = model.Stakes,
                    TimeStarted = DateTime.Now
                };
                _context.CashGames.Add(game);
                _context.SaveChanges();
                foreach (var player in model.StartingPlayers)
                {
                    var party = new CashGameParty
                    {
                        AmountStaked     = 0m,
                        PlayerId         = player,
                        GameId           = game.CashGameId,
                        IsPlayerFinished = false
                    };
                    _context.CashGameParties.Add(party);
                }

                if (_httpContext.HttpContext != null)
                {
                    var userId = _httpContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
                    var audit  = new MatchAudit
                    {
                        AuditDate = DateTime.Now,
                        MatchId   = game.CashGameId,
                        UserId    = userId,
                        Type      = AuditType.CreateMatch
                    };
                    _context.MatchAudits.Add(audit);
                }

                _context.SaveChanges();
                trans.Commit();
                return(game.CashGameId);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Log.Error(e, "Error occured while saving a new match");
                return(-1);
            }
        }
Beispiel #3
0
        private Match CreateSettlementMatch(Player boardPlayer, Player inactivePlayer, decimal amount)
        {
            var settlementGame = _context.Games.Find(999);

            if (settlementGame == null)
            {
                Log.Logger.Error("Failed to seed database correctly, no settlement game type found");
                return(null);
            }

            var trans = _context.Database.BeginTransaction();
            var match = new Match
            {
                StartTime     = DateTime.Now,
                EndTime       = DateTime.Now,
                IsSettleMatch = true,
                IsResolved    = true,
                EntranceFee   = amount,
                Archived      = false,
                GameId        = settlementGame.GameId,
            };

            _context.Matches.Add(match);
            _context.SaveChanges();
            var winningParty = new Participant
            {
                IsResolved = true,
                IsWinner   = true,
                NetResult  = -amount,
                Archived   = false,
                PlayerId   = inactivePlayer.PlayerId,
                MatchId    = match.MatchId //Winning party gets the amount removed from their position as they have been paid.
            };
            var losingParty = new Participant
            {
                IsResolved = true,
                IsWinner   = false,
                NetResult  = amount,
                Archived   = false,
                PlayerId   = boardPlayer.PlayerId,
                MatchId    = match.MatchId
            };

            _context.Participants.Add(winningParty);
            _context.Participants.Add(losingParty);
            _context.SaveChanges();
            trans.Commit();
            trans.Dispose();
            return(match);
        }
Beispiel #4
0
        public void AddPlayer(PlayerViewModel player)
        {
            var model = new Player
            {
                UserName           = player.UserName,
                EmailAddress       = player.EmailAddress,
                Archived           = false,
                HasAdminPermission = player.HasAdminPermission,
                RealName           = player.RealName,
                LastPaid           = DateTime.Now
            };

            _context.Players.Add(model);
            _context.SaveChanges();
            _analytic.TrackEvent("Added Player", model.PlayerId.ToString(CultureInfo.InvariantCulture));
        }
Beispiel #5
0
        public IActionResult Login([FromBody] AuthenticationViewModel authModel)
        {
            if (authModel == null)
            {
                return(BadRequest());
            }
            if (string.IsNullOrWhiteSpace(authModel.UserToken))
            {
                return(BadRequest());
            }
            var model = _context.AppleAuthUsers.Where(x => x.DateArchived == null).FirstOrDefault(x => x.AppleAuthCode == authModel.UserToken);

            if (model == null)
            {
                _logger.LogWarning($"New AppleId SignIn - {authModel.FullName}");
                var authUser = new AppleAuthUser
                {
                    ApiKey          = Guid.NewGuid(),
                    AppleAuthCode   = authModel.UserToken,
                    EmailAddress    = authModel.EmailAddress,
                    FullName        = authModel.FullName,
                    DateArchived    = null,
                    IsAdmin         = false,
                    IsAllowedAccess = false
                };
                _context.AppleAuthUsers.Add(authUser);
                _context.SaveChanges();
                return(StatusCode(402, authUser.ApiKey));
            }

            if (model.IsAllowedAccess)
            {
                return(Ok(model.ApiKey));
            }
            return(StatusCode((int)HttpStatusCode.PaymentRequired)); //Use payment required as short hand for awaiting activation
        }
        public void AddPlayers(int matchId, List <int> playerIds)
        {
            var match = _context.Matches.Find(matchId);

            foreach (var player in playerIds)
            {
                var pl = _context.Players.Find(player);
                if (pl == null)
                {
                    throw new PlayerNotFoundException();
                }
                var participant = new Participant
                {
                    Match    = match,
                    MatchId  = match.MatchId,
                    PlayerId = pl.PlayerId
                };
                _context.Participants.Add(participant);
            }
            var userId = _httpContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var audit  = new MatchAudit
            {
                AuditDate = DateTime.Now,
                MatchId   = match.MatchId,
                UserId    = userId,
                Type      = AuditType.AddParty
            };

            _context.MatchAudits.Add(audit);

            match.NumberOfPlayers += playerIds.Count;
            _context.SaveChanges();
        }