//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 })); } }