/// <summary> /// This mehthod task is to intialzie the Inventory Table /// </summary> /// <param name="context"></param> private static void addUserInventory(BattleshipDBContext context) { // If there is any data in inventory table, // remove the data if (context.Inventories.Any()) { var delete = from u in context.Inventories select u; foreach (Inventory use in delete) { context.Inventories.Remove(use); } context.SaveChanges(); } var usersRoles = new Inventory[] { new Inventory { PlayerId = "JohnDoe", Power1 = 0, Power2 = 0, Power3 = 0, Cash = 10000 } }; foreach (Inventory iru in usersRoles) { context.Inventories.Add(iru); } context.SaveChanges(); }
//This calls and randomizes the rocket barrage shot on the gameboard. public async Task <JsonResult> RocketBarrage() { string username = userManager.GetUserName(User); //check if user have enough to use it var databaseUser = battleshipContext.Inventories.Where(i => i.PlayerId == username).FirstOrDefault(); if (databaseUser.Power1 == 0) { return(Json(new { success = false })); } else { // decrement use power count databaseUser.Power1 -= 1; battleshipContext.Update(databaseUser); battleshipContext.SaveChanges(); string[] tenResults = gameService.RocketBarrage(username).Split(" "); return(Json(new { success = true, resultText1 = tenResults[0], col1 = tenResults[1], row1 = tenResults[2], resultText2 = tenResults[3], col2 = tenResults[4], row2 = tenResults[5], resultText3 = tenResults[6], col3 = tenResults[7], row3 = tenResults[8], resultText4 = tenResults[9], col4 = tenResults[10], row4 = tenResults[11], resultText5 = tenResults[12], col5 = tenResults[13], row5 = tenResults[14], resultText6 = tenResults[15], col6 = tenResults[16], row6 = tenResults[17], resultText7 = tenResults[18], col7 = tenResults[19], row7 = tenResults[20], resultText8 = tenResults[21], col8 = tenResults[22], row8 = tenResults[23], resultText9 = tenResults[24], col9 = tenResults[25], row9 = tenResults[26], resultText10 = tenResults[27], col10 = tenResults[28], row10 = tenResults[29], power1Count = databaseUser.Power1 })); } }
//Calculates the purchases by checking user credit. private JsonResult CalculatePurchase(Inventory user, int cost, string powerupType) { // Check if user have enough credit to make a purchase if (user.Cash < cost) { return(Json(new { success = true, resultText = "NOT ENOUGH" })); } // At this point they got enough credit to make purchase int calculatePurchase = user.Cash - cost; // increase quantity count // update user cash if (powerupType == "powerup1") { user.Power1++; user.Cash = calculatePurchase; battleshipContext.Update(user); battleshipContext.SaveChanges(); return(Json(new { success = true, resultText = "PURCHASED_POWER1", powerupCount = user.Power1, userCredit = user.Cash })); } else if (powerupType == "powerup2") { user.Power2++; user.Cash = calculatePurchase; battleshipContext.Update(user); battleshipContext.SaveChanges(); return(Json(new { success = true, resultText = "PURCHASED_POWER2", powerupCount = user.Power2, userCredit = user.Cash })); } else { user.Power3++; user.Cash = calculatePurchase; battleshipContext.Update(user); battleshipContext.SaveChanges(); return(Json(new { success = true, resultText = "PURCHASED_POWER3", powerupCount = user.Power3, userCredit = user.Cash })); } }
/// <summary> /// This mehthod task is to intialzie the Inventory Table /// </summary> /// <param name="context"></param> private static void addUserHighScore(BattleshipDBContext context) { // If there is any data in inventory table, // remove the data if (context.HighScores.Any()) { var delete = from u in context.HighScores select u; foreach (HighScore use in delete) { context.HighScores.Remove(use); } context.SaveChanges(); } var highScore = new HighScore[] { new HighScore { PlayerId = "Jim", AccuracyScore = 100.0, Date_Of_Win = DateTime.Today }, new HighScore { PlayerId = "Eric", AccuracyScore = 99.9, Date_Of_Win = DateTime.Today }, new HighScore { PlayerId = "Tom", AccuracyScore = 25.0, Date_Of_Win = DateTime.Today }, new HighScore { PlayerId = "Chris", AccuracyScore = 75.0, Date_Of_Win = DateTime.Today }, }; foreach (HighScore score in highScore) { context.HighScores.Add(score); } context.SaveChanges(); }
//Adds high score to the database and then updates the page. public async Task <JsonResult> AddHighScore(float score) { string username = userManager.GetUserName(User); HighScore highScore = new HighScore(); highScore.PlayerId = username + DateTime.Now.ToString(); highScore.AccuracyScore = score; highScore.Date_Of_Win = DateTime.Now; _context.HighScores.Add(highScore); _context.SaveChanges(); return(Json(new { success = true })); }