public JsonResult StockListSearch() { var csvData = new List <StockModel>(); var stockNames = new List <string>(); var csvFloder = stockNamesPath; try { // Find all stocks name foreach (var files in Directory.GetFiles(csvFloder)) { FileInfo info = new FileInfo(files); stockNames.Add(info.Name.Replace(".csv", string.Empty)); } // Create json of stocks name foreach (var name in stockNames) { csvData.Add(new StockModel(name.ToUpper())); } } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } //return new ObjectResult<StockModel[]>(csvData.ToArray()); return(JSONHelper.CreateJSONResult(true, csvData)); }
/// <summary> /// Search current stock in portfolio by player name /// </summary> /// <returns>The portfolio</returns> public JsonResult PortfolioSearch() { // TEMP Session["USER_ID"] = "1"; Session["USER_NAME"] = "playerA"; //playerPortfolioPath = string.Format("{0}\\{1}{2}", PLAYER_NAMES_PATH, Session["USER_NAME"], PORTFOLIO_FILE_FORMAT); //playerPath = string.Format("{0}\\{1}.csv", PLAYER_NAMES_PATH, Session["USER_NAME"]); PlayerModel playerData = null; var csvData = new List <StockModel>(); try { // Portfolio data csvData = GetPortfolio(); // Get player data playerData = GetPlayerData(); // Player's stock playerData.Stocks = csvData; } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } return(JSONHelper.CreateJSONResult(true, playerData)); }
public JsonResult ResetTurn() { int turn = 1; var csvFile = turnPath; try { // Reset game turn using (CSVHelper.CsvFileWriter writer = new CSVHelper.CsvFileWriter(csvFile, false)) { // Write csv column writer.AddRow("turn"); // Write turn writer.AddRow(turn.ToString()); } UpdateMarketData(turn); } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } return(JSONHelper.CreateJSONResult(true, turn)); }
public JsonResult CurrentTurn() { int?turn = null; var csvFile = turnPath; try { // Find current turn using (CSVHelper.CsvFileReader reader = new CSVHelper.CsvFileReader(csvFile)) { while (reader.ReadRow(searchAll)) { if (!searchAll.Contains("turn")) { turn = int.Parse(searchAll[0]); } } } } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } return(JSONHelper.CreateJSONResult(true, turn)); }
public JsonResult MarketSearch() { try { return(JSONHelper.CreateJSONResult(true, GetStockMarketByName(null))); } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } }
public JsonResult MarketOpen() { try { ChangeMarketStatus(1); } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } return(JSONHelper.CreateJSONResult(true, true)); }
public JsonResult NextTurn() { int?turn = null; var csvFile = turnPath; try { // Get current game turn using (CSVHelper.CsvFileReader reader = new CSVHelper.CsvFileReader(csvFile)) { while (reader.ReadRow(searchAll)) { if (!searchAll.Contains("turn")) { turn = int.Parse(searchAll[0]) + 1; } } } // Update game turn if (turn.HasValue) { using (CSVHelper.CsvFileWriter writer = new CSVHelper.CsvFileWriter(csvFile, false)) { // Write csv column writer.AddRow("turn"); // Write turn data writer.AddRow(turn.Value.ToString()); } UpdateMarketData(turn.Value); } } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } return(JSONHelper.CreateJSONResult(true, turn)); }
public JsonResult BuyStock(int id, string name, int vol) { // Check if user already logged in //if (Session["username"] == null) // return; // Check Buy/Sell volumns if less than 0 return error if (vol < 1) { return(JSONHelper.CreateJSONResult(false, "The volumns cannot less than 100.")); } var row = new List <string>(); decimal cost = 0; StockModel stock = null; try { // Read stocks data (price, dividend) at current turn stock = GetStockMarketByName(name).First(); // Calculate stock cost cost = stock.Price * vol; // Apply commission cost cost += cost * COMMISSION_RATE; // Find player data var player = GetPlayerData(); // Check player cash // If cash not enough return error if (player.Cash - cost < 0) { return(JSONHelper.CreateJSONResult(false, "Not enough cash")); } // Read player portfolio var playerPortfolio = GetPortfolio(); // Stock info var newStock = GetStockMarketByName(name).First(); // Add new stock to player protfolio playerPortfolioPath = string.Format("{0}\\{1}{2}", PLAYER_NAMES_PATH, Session["USER_NAME"], PORTFOLIO_FILE_FORMAT); using (CSVHelper.CsvFileWriter writer = new CSVHelper.CsvFileWriter(playerPortfolioPath)) { writer.AddRow(string.Format("{0},{1},{2},{3},{4},{5}", id, name.ToLower(), newStock.Price, 0.00, vol, Session["USER_ID"])); } // Update player cash //var player = GetPlayerData(name); player.Cash -= cost; // Update Portfolio value var total = 0m; foreach (var item in playerPortfolio) { total += item.Price * item.Vol; } player.Portfolio = player.Cash + total; // Write new player data SavePlayerData(player); } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } return(JSONHelper.CreateJSONResult(true, "Trade success")); }
/// <summary> /// Player action to sell stock in portfolio /// </summary> /// <param name="id">The stock id</param> /// <param name="name">The stock name</param> /// <param name="vol">The stock volumns</param> /// <returns></returns> public JsonResult SellStock(int id, string name, int vol) { // Check if user already logged in //if (Session["username"] == null) // return; // Check Buy/Sell volumns if less than 0 return error if (vol < 1) { return(JSONHelper.CreateJSONResult(false, "The volumns cannot less than 100.")); } var amount = 0m; var row = new List <string>(); var stockMarket = GetStockMarketByName(name).First(); // Get player portfolio every record var portfolio = GetPortfolio(false); // Find all stocks in portfolio with the same name var stocks = portfolio.Where(p => p.Name.ToLower().Equals(name.ToLower())).OrderBy(p => p.Price).ToList(); // If sell volumns are more than existing stock return error if (stocks.Sum(p => p.Vol) - vol < 0) { return(JSONHelper.CreateJSONResult(false, "Not enough stock for sell")); } // Sell stock foreach (var item in stocks) { // If sell volumns are more than exists volumns, delete record if (item.Vol - vol < 0) { // Cash gain from selling stock amount += stockMarket.Price * item.Vol; vol -= item.Vol; item.Vol = 0; } // There are volumns left after sell else { // Cash gain from selling stock amount += stockMarket.Price * vol; item.Vol -= vol; vol = 0; break; } } // Apply commission rate when sell amount -= amount * COMMISSION_RATE; // Update player data var player = GetPlayerData(); player.Cash += amount; player.Portfolio = portfolio.Sum(p => p.Price * p.Vol); // Save new player data SavePlayerData(player); // Update portfolio playerPortfolioPath = string.Format("{0}\\{1}{2}", PLAYER_NAMES_PATH, Session["USER_NAME"], PORTFOLIO_FILE_FORMAT); using (CSVHelper.CsvFileWriter writer = new CSVHelper.CsvFileWriter(playerPortfolioPath, false)) { // Write file header writer.AddRow("id,name,price,dividend,volumn,playerid"); // Update stocks in portfolio which have volumns more than 0 foreach (var item in portfolio) { if (item.Vol > 0) { writer.AddRow(string.Format("{0},{1},{2},{3},{4},{5}", 0, item.Name.ToLower(), item.Price, item.Dividend, item.Vol, Session["USER_ID"])); } } } return(JSONHelper.CreateJSONResult(true, "Trade success")); }
public JsonResult ChangeStockInMarket(string stockName, bool isAdd) { var csvData = new List <StockModel>(); var csvFile = marketDataPath; try { if (isAdd) { // Add new stock to market using (CSVHelper.CsvFileWriter writer = new CSVHelper.CsvFileWriter(csvFile)) { writer.AddRow(string.Format("0,{0},0,0", stockName.ToLower())); } } else { // Get all stock in market var market = GetMarketStocks(); // Remove stock from market var stockToRemove = market.Where(p => p.Name.Equals(stockName.ToUpper())).First(); market.Remove(stockToRemove); // Rewrite market data using (CSVHelper.CsvFileWriter writer = new CSVHelper.CsvFileWriter(csvFile, false)) { // Add market csv header writer.AddRow("id,name,price,dividend"); foreach (var item in market) { writer.AddRow(string.Format("0,{0},{1},{2}", item.Name, item.Price, item.Dividend)); } } // Read stocks data (price, dividend) at current turn //using (CSVHelper.CsvFileReader reader = new CSVHelper.CsvFileReader(csvFile)) //{ // while (reader.ReadRow(searchAll)) // { // if (!searchAll.Contains("id")) // { // // Create stock model data from csv // csvData.Add(new StockModel(int.Parse(searchAll[0]), searchAll[1].ToUpper(), decimal.Parse(searchAll[2]), decimal.Parse(searchAll[3]))); // } // } //} } // Find current turn int?turn = null; csvFile = turnPath; using (CSVHelper.CsvFileReader reader = new CSVHelper.CsvFileReader(csvFile)) { while (reader.ReadRow(searchAll)) { if (!searchAll.Contains("turn")) { turn = int.Parse(searchAll[0]); } } } //Update market value if (turn.HasValue) { UpdateMarketData(turn.Value); csvData = GetMarketStocks(); } } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } return(JSONHelper.CreateJSONResult(true, csvData)); }
public JsonResult PlayersSearch() { var csvData = new List <PlayerModel>(); var csvFloder = System.Web.HttpContext.Current.Server.MapPath("~/App_Data") + "\\" + ConfigurationManager.AppSettings["players"]; try { // Get player info foreach (var files in Directory.GetFiles(csvFloder)) { FileInfo info = new FileInfo(files); var fileName = Path.GetFileName(info.FullName); // Skip player account info if (fileName.Equals("player.csv")) { continue; } // Player's portfolio path var csvFile = string.Format("{0}\\{1}", csvFloder, fileName); // Read player's portfolio data using (CSVHelper.CsvFileReader reader = new CSVHelper.CsvFileReader(csvFile)) { if (fileName.ToLower().Contains("stock")) { var stocks = new List <StockModel>(); var playerId = 0; while (reader.ReadRow(searchAll)) { if (!searchAll[0].ToLower().Equals("id")) { stocks.Add(new StockModel(int.Parse(searchAll[0]), searchAll[1].ToUpper(), decimal.Parse(searchAll[2]), decimal.Parse(searchAll[3]), int.Parse(searchAll[4]))); playerId = int.Parse(searchAll[5]); } } csvData.Where(p => p.ID == playerId).First().Stocks = stocks; } else { while (reader.ReadRow(searchAll)) { if (!searchAll[0].ToLower().Equals("id")) { var player = new PlayerModel(int.Parse(searchAll[0]), searchAll[1], decimal.Parse(searchAll[2]), decimal.Parse(searchAll[3])); csvData.Add(player); } } } } } } catch (Exception ex) { return(JSONHelper.CreateJSONResult(false, ex)); } return(JSONHelper.CreateJSONResult(true, csvData)); }