public async Task PurchaseLoan(string loanPurchase) { player RequestingPlayer = playerList.First(x => x.ConnectionId == Context.ConnectionId); string purchaseError = null; LoanPurchaseOrPayment playerPurchase = new LoanPurchaseOrPayment(null, null, null, 0); playerPurchase = JsonConvert.DeserializeAnonymousType(loanPurchase, playerPurchase); playerPurchase.DollarAmount = Math.Abs(playerPurchase.DollarAmount); // Check if the player is allowed to make the pruchase bool allowPurchase = true; int MaxLoanAmount = (int)(RequestingPlayer.NetWorth * 0.25); if (playerPurchase.DollarAmount > MaxLoanAmount) { allowPurchase = false; purchaseError = "Requested loan exceeds maximum loan amount."; Console.WriteLine(purchaseError); } else if (playerPurchase.DollarAmount < 100) { allowPurchase = false; purchaseError = "The minimum loan you can purchase is $100."; Console.WriteLine(purchaseError); } if (allowPurchase) { int MinPayment = LoanMinimumPayment(playerPurchase.DollarAmount, 10); Loan newLoan = new Loan(RequestingPlayer.Name, RequestingPlayer.ConnectionId, playerPurchase.DollarAmount, MinPayment, 11, true, 0); Loan newLoan2 = new Loan(RequestingPlayer.Name, RequestingPlayer.ConnectionId, playerPurchase.DollarAmount, MinPayment, 11, true, 0); RequestingPlayer.HasLoan = true; RequestingPlayer.CashOnHand += playerPurchase.DollarAmount; Console.WriteLine(newLoan.PlayerName + "'s balance is " + newLoan.LoanBalance.ToString() + " after purchasing their first loan"); Console.WriteLine("Adding new loan. Player: " + newLoan.PlayerName + " loan balance " + newLoan.LoanBalance.ToString() + " min payment " + newLoan.MinPayment.ToString()); Console.WriteLine("playerPurchase.DollarAmount = " + playerPurchase.DollarAmount.ToString()); AllLoans.Add(newLoan); } await Clients.Caller.SendAsync("Loan Purchased", purchaseError, JsonConvert.SerializeObject(playerList), JsonConvert.SerializeObject(AllLoans)); }
public async Task PayLoan(string playerLoanPaymentToJson) { Console.WriteLine("Initiating loan payment"); player RequestingPlayer = playerList.First(x => x.ConnectionId == Context.ConnectionId); string paymentError = null; LoanPurchaseOrPayment playerPayment = new LoanPurchaseOrPayment(null, null, null, 0); playerPayment = JsonConvert.DeserializeAnonymousType(playerLoanPaymentToJson, playerPayment); Loan playerLoan = AllLoans.First(x => x.ConnectionId == RequestingPlayer.ConnectionId); playerPayment.DollarAmount = Math.Abs(playerPayment.DollarAmount); foreach (player player in playerList) { if (player.ConnectionId == RequestingPlayer.ConnectionId) { if ((playerPayment.DollarAmount >= playerLoan.MinPayment) && (playerPayment.DollarAmount <= player.CashOnHand)) { foreach (Loan loan in AllLoans) { if (loan.ConnectionId == RequestingPlayer.ConnectionId) { loan.PaidThisYear = true; if (playerPayment.DollarAmount < loan.LoanBalance) { Console.WriteLine(loan.PlayerName + " has a loan balance of " + loan.LoanBalance.ToString()); loan.LoanBalance -= playerPayment.DollarAmount; player.CashOnHand -= playerPayment.DollarAmount; // Calculate new minimum payment? loan.MinPayment = LoanMinimumPayment(loan.LoanBalance, (loan.YearsRemaining - 1)); } else if (playerPayment.DollarAmount == loan.LoanBalance) { Console.WriteLine(loan.PlayerName + " has a loan balance of " + loan.LoanBalance.ToString()); loan.LoanBalance -= playerPayment.DollarAmount; player.CashOnHand -= playerPayment.DollarAmount; loan.LoanBalance = 0; loan.MinPayment = 0; //AllLoans.RemoveAll(x => x.ConnectionId == player.ConnectionId); //player.HasLoan = false; } else if (playerPayment.DollarAmount > loan.LoanBalance) { player.CashOnHand -= loan.LoanBalance; loan.LoanBalance = 0; loan.MinPayment = 0; //AllLoans.RemoveAll(x => x.ConnectionId == player.ConnectionId); //player.HasLoan = false; } Console.WriteLine("Calculating new minimum payment. Loan balance: " + loan.LoanBalance + " Years remaining: " + (loan.YearsRemaining - 1).ToString() + " New miniumum: " + loan.MinPayment); Console.WriteLine(loan.PlayerName + " paid " + playerPayment.DollarAmount.ToString() + " toward their loan. New balance is " + loan.LoanBalance.ToString()); } } //playerLoan.PaidThisYear = true; //playerLoan.LoanBalance -= playerPayment.DollarAmount; } else if (playerPayment.DollarAmount < playerLoan.MinPayment) { paymentError = "Payment is below the minimum payment."; foreach (Loan loan in AllLoans) { if (loan.ConnectionId == RequestingPlayer.ConnectionId) { loan.PaidThisYear = false; } } Console.WriteLine("Payment is below the minimum payment " + playerPayment.DollarAmount); } else if (playerPayment.DollarAmount > player.CashOnHand) { paymentError = "You do not have enough money to pay that amount"; foreach (Loan loan in AllLoans) { if (loan.ConnectionId == RequestingPlayer.ConnectionId) { loan.PaidThisYear = false; } } } } } Console.WriteLine("Loan payment complete"); await Clients.Caller.SendAsync("Loan Paid", paymentError, JsonConvert.SerializeObject(AllLoans), JsonConvert.SerializeObject(playerList)); }