コード例 #1
0
        public void SellOffStocks()
        {
            //get all games that need to be processed
            IList <GameModel> expiredGames = gameDao.GetAllExpiredGames();

            foreach (GameModel game in expiredGames)
            {
                //find all the users within the game
                IList <UserModel> users = userDao.GetUsersByGame(game.Id);

                foreach (UserModel user in users)
                {
                    //get all the stocks the user owns in a particular game
                    IList <OwnedStocksModel> expiredStocks = ownedHelper.GetOwnedStocksByUserAndGame(user.Id, game.Id);

                    foreach (OwnedStocksModel stock in expiredStocks)
                    {
                        TransactionModel finalTransaction = new TransactionModel()
                        {
                            UserId                = user.Id,
                            GameId                = game.Id,
                            StockSymbol           = stock.StockSymbol,
                            NumberOfShares        = stock.NumberOfShares,
                            TransactionSharePrice = stock.CurrentSharePrice,
                            IsPurchase            = false,
                            NetTransactionChange  = stock.NumberOfShares * stock.CurrentSharePrice
                        };
                        //use transactionDAO to sell all of these stocks

                        transactionDao.AddNewTransaction(finalTransaction);

                        //sell all the stocks a user has
                    }
                }
                gameDao.UpdateTransactionsEndGame(game.Id);
            }
        }
コード例 #2
0
        public IActionResult GetOwnedStocks([FromBody] UserAndGameAPIModel apiModel)
        {
            IList <OwnedStocksModel> ownedStocks = ownedHelper.GetOwnedStocksByUserAndGame(userDao.GetUser(apiModel.Username).Id, apiModel.GameId);

            return(new JsonResult(ownedStocks));
        }
コード例 #3
0
        private IList <LeaderboardBalance> BuildLeaderBoardData(int gameId)
        {
            //IList<StockTransaction> allTransactions = transactionDao.GetAllTransactionsByGame(gameId);

            //IDictionary<int, decimal> summedTransactions = new Dictionary<int, decimal>();

            //IList<LeaderboardBalance> result = new List<LeaderboardBalance>();

            //foreach (StockTransaction transaction in allTransactions)
            //{
            //    if (!summedTransactions.ContainsKey(transaction.UserId))
            //    {
            //        summedTransactions.Add(transaction.UserId, 0M);
            //        summedTransactions[transaction.UserId] += transaction.NetValue;
            //    }
            //    else
            //    {
            //        summedTransactions[transaction.UserId] += transaction.NetValue;
            //    }
            //}

            //foreach (KeyValuePair<int, decimal> kvp in summedTransactions)
            //{
            //    LeaderboardBalance newBalance = new LeaderboardBalance()
            //    {
            //        UserName = userDao.GetUser(kvp.Key).Username,
            //        CurrentBalance = kvp.Value
            //    };
            //    result.Add(newBalance);
            //}


            IList <UserModel> users = userDao.GetUsersByGame(gameId);

            IDictionary <UserModel, IList <StockTransaction> > userTransactions = new Dictionary <UserModel, IList <StockTransaction> >();

            foreach (UserModel user in users)
            {
                userTransactions.Add(user, transactionDao.GetTransactionsByGameAndUser(gameId, user.Id));
            }

            //IDictionary<string, decimal> cachedStockPrices = new Dictionary<string, decimal>();

            List <LeaderboardBalance> result = new List <LeaderboardBalance>();


            foreach (KeyValuePair <UserModel, IList <StockTransaction> > kvp in userTransactions)
            {
                LeaderboardBalance balance = new LeaderboardBalance();
                balance.UserId   = kvp.Key.Id;
                balance.UserName = kvp.Key.Username;

                decimal runningBalance = 0.0M;

                foreach (StockTransaction transaction in kvp.Value)
                {
                    runningBalance += transaction.NetValue;
                }

                balance.CurrentBalance = runningBalance;

                IList <OwnedStocksModel> ownedStocks = ownedHelper.GetOwnedStocksByUserAndGame(kvp.Key.Id, gameId);

                decimal runningStockValue = 0.0M;

                foreach (OwnedStocksModel ownedstock in ownedStocks)
                {
                    runningStockValue += ownedstock.CurrentSharePrice * ownedstock.NumberOfShares;
                }

                balance.CurrentStockValue          = runningStockValue;
                balance.CurrentTotalPortfolioValue = balance.CurrentStockValue + balance.CurrentBalance;

                result.Add(balance);
            }

            result.Sort((a, b) => b.CurrentTotalPortfolioValue.CompareTo(a.CurrentTotalPortfolioValue));

            return(result);
        }