public void SellAllStocks(int playerId, List <StockDetail> ownStocks) { list.Clear(); foreach (StockDetail item in ownStocks) { try { int quan = playerService.checkStockQuantity(playerId, item.StockId); AIBuySellDetails itemBuySell = new AIBuySellDetails(); if (quan > 0) { itemBuySell.GameId = curGame.GameId; itemBuySell.PlayerId = playerId; itemBuySell.SectorId = item.SectorId; itemBuySell.Quantity = quan; itemBuySell.Stock = item; itemBuySell.Buy = false; list.Add(itemBuySell); } //playerService.buyStocks(playerID, buyingQuan, stockDetail, item.StartingPrice); } catch (Exception e) { } } }
public void BuyStocksForFirstTime(int playerID) { list.Clear(); List <Stock> stocks = playerService.getAllStocks(); List <Stock> firstThree = new List <Stock>(stocks.OrderBy(c => c.StartingPrice).Take(3)); decimal totPrice = 0; foreach (Stock item in firstThree) { totPrice += item.StartingPrice; } foreach (Stock item in firstThree) { StockDetail stockDetail = new StockDetail(); stockDetail.StockId = item.StockId; stockDetail.StockName = item.StockName; stockDetail.StartingPrice = item.StartingPrice; stockDetail.CurrentPrice = 0; try { int avgPrice = (int)((item.StartingPrice / totPrice) * 100); int quanCategory = CheckQuanAmountForFirstTime(avgPrice); int buyingQuanRange = CalcQuantityForFirstTime(quanCategory); //buying for first time int buyingQuan = (int)(buyingQuanRange / item.StartingPrice); AIBuySellDetails itemBuySell = new AIBuySellDetails(); if (buyingQuan > 0) { itemBuySell.GameId = curGame.GameId; itemBuySell.PlayerId = playerID; itemBuySell.SectorId = item.SectorId; itemBuySell.Quantity = buyingQuan; itemBuySell.Stock = stockDetail; itemBuySell.Buy = true; list.Add(itemBuySell); } //playerService.buyStocks(playerID, buyingQuan, stockDetail, item.StartingPrice); } catch (Exception e) { } } }
public void SetBuySellForAI(List <StockDetail> ownStocks, int playerID) { list.Clear(); CheckHistoryWithCurrent(ownStocks); if (ownStocks != null) { foreach (StockDetail item in ownStocks) { AIBuySellDetails itemBuySell = new AIBuySellDetails(); decimal averagePrice = 0; try { averagePrice = playerService.amountSpentForStocks(playerID, item.StockId); } catch (Exception) { averagePrice = 0; } if (averagePrice > 0) { if (((item.CurrentPrice - averagePrice) >= (decimal)0.2) && item.StockId != shouldntSell) //sell { try { int decidingPrice = (int)((averagePrice * 100) / item.CurrentPrice); int quanCategory = CheckQuanAmount(decidingPrice); int sellingQuan = CalcQuantity(quanCategory, playerService.checkStockQuantity(playerID, item.StockId)); if (quanCategory > 0 && sellingQuan > 0 && curGame != null) { itemBuySell.GameId = curGame.GameId; itemBuySell.PlayerId = playerID; itemBuySell.SectorId = item.SectorId; itemBuySell.Quantity = sellingQuan; itemBuySell.Stock = item; itemBuySell.Buy = false; list.Add(itemBuySell); } //hub.SellStocks(curGame.GameId, playerID, item.SectorId, item, sellingQuan); //playerService.sellStocks(playerID, sellingQuan, item, item.CurrentPrice); } catch (Exception e) { } } else if (((item.CurrentPrice - averagePrice) < (decimal)0.2) && item.StockId != shouldntBuy)//buy { try { int decidingPrice = (int)((item.CurrentPrice * 100) / averagePrice); int quanCategory = CheckQuanAmount(decidingPrice); int buyingQuan = CalcQuantity(quanCategory, playerService.checkStockQuantity(playerID, item.StockId)); if (quanCategory > 0 && buyingQuan > 0) { itemBuySell.GameId = curGame.GameId; itemBuySell.PlayerId = playerID; itemBuySell.SectorId = item.SectorId; itemBuySell.Quantity = buyingQuan; itemBuySell.Stock = item; itemBuySell.Buy = true; list.Add(itemBuySell); } //hub.BuyStocks(curGame.GameId, playerID, item.SectorId, item, buyingQuan); //playerService.buyStocks(playerID, buyingQuan, item, item.CurrentPrice); } catch (Exception e) { } } } } } }