Reputation: 23
I'm creating a game using MVC 4. There is a crime feature and I just can't work it out. I've tried a lot of things but since I'm new on MVC4 I just can't figure it out.
I have created a radiobutton list and each different option should give varying result. This is the code so far:
Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiveGangs.Models;
namespace FiveGangs.Controllers.Game {
[Authorize]
public class CrimesController : Controller {
//
// GET: /Crimes/
[HttpGet]
public ActionResult Index() {
return View();
}
//
// POST: /Crimes/
[HttpPost]
public ActionResult Index(LocalCrimeModel.Crime crimeType) {
//1 is de beste crime, 10 de slechste
ViewBag.Message = "You commited the crime!";
var db = new FGEntities();
var g = db.UserGangster.FirstOrDefault(p => p.UserProfile.UserName == User.Identity.Name);
g.GansterStatistic.CrimesSuccess++;
g.Experience += 1000;
g.Cash += 100;
db.SaveChanges();
return View();
}
}
}
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FiveGangs.Models
{
public class LocalCrimeModel
{
public enum Crime
{
Crime01 = 1,
Crime02 = 2,
Crime03 = 3,
Crime04 = 4,
Crime05 = 5,
Crime06 = 6,
Crime07 = 7,
Crime08 = 8,
Crime09 = 9,
Crime10 = 10,
}
public Crime SelectedCrime { get; set; }
}
}
View:
@using FiveGangs.Models
@model FiveGangs.Models.LocalCrimeModel
<!DOCTYPE html>
<html>
<head>
<title>Crimes</title>
</head>
<body>
<div>
<h2>Crimes</h2>
@if (ViewBag.Message != null) {
<p class="warning">@ViewBag.Message</p>
}
@using (Html.BeginForm("Index")) {
@Html.RadioButton("crimetype", LocalCrimeModel.Crime.Crime01, false)@: Rob the local bank<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime02, false)@: Rob the local casino<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime03, false)@: Rob a jewelry store<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime04, false)@: Rob the local café<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime05, false)@: Steal casino chips from the local casino<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime06, false)@: Rob the local theatre<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime07, false)@: Rob the local supermarket<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime08, false)@: Rob a laundry service<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime09, false)@: Empty a parking meter<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime10, false)@: Pickpocket someone<br /><br />
<input class="btn btn-large btn-primary" type="submit" value="Commit crime!" />
}
</div>
</body>
</html>
So far I can successfully execute a crime, and experience and money gets added to the database. I'm stuck on varying the "payouts" per crime. Any help is appreciated, I've been googling for 2 days without result.
Thanks
Upvotes: 2
Views: 182
Reputation: 8156
Are you looking for the switch
language construct?
You can just write:
switch (crimeType)
{
case LocalCrimeModel.Crime.Crime01:
g.Experience += 1000;
g.Cash += 100;
break;
case LocalCrimeModel.Crime.Crime02:
g.Experience += 2000;
g.Cash += 200;
break;
case LocalCrimeModel.Crime.Crime03:
g.Experience += 3000;
g.Cash += 300;
break;
case LocalCrimeModel.Crime.Crime04:
g.Experience += 4000;
g.Cash += 400;
break;
...
default:
break;
}
Can't you?
EDIT:
On the other hand this kind of hard-coded logic is not really scalable, crimes should not be an enum
, but a List<Crime>
populated form database instead. A Crime
should be like:
public class LocalCrimeModel
{
public class Crime
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Cash { get; set; }
public int Experience { get; set; }
}
public string Message { get; set; }
public IList<Crime> Crimes { get; set; }
public int SelectedCrimeID { get; set; }
public Crime SelectedCrime
{
get
{
if (this.Crimes != null)
{
return this.Crimes.FirstOrDefault(o => o.ID == this.SelectedCrimeID);
}
else
{
return null;
}
}
}
}
CrimesController can be just like:
public class CrimesController : Controller
{
public ActionResult Index()
{
var model = new LocalCrimeModel();
model.Crimes = GetCrimes(); // THIS SHOULD BE A RESULT OF A DATABASE QUERY
return View(model);
}
[HttpPost]
public ActionResult Index(LocalCrimeModel model)
{
if (model != null)
{
model = new LocalCrimeModel(); // just to be sure
}
model.Crimes = GetCrimes(); // USE THE SAME QUERY HERE
if (model != null && model.SelectedCrime != null)
{
var db = new FGEntities();
var g = db.UserGangster.FirstOrDefault(p => p.UserProfile.UserName == User.Identity.Name);
g.GansterStatistic.CrimesSuccess++;
g.Experience += model.SelectedCrime.Experience;
g.Cash += model.SelectedCrime.Cash;
db.SaveChanges();
model.Message = "You commited the crime!";
}
else
{
model.Message = "Crime does not exist!";
}
return View(model);
}
}
Also your view could be like:
@using FiveGangs.Models
@model FiveGangs.Models.LocalCrimeModel
<!DOCTYPE html>
<html>
<head>
<title>Crimes</title>
</head>
<body>
<div>
<h2>Crimes</h2>
@if (model.Message != null)
{
<p class="warning">@model.Message</p>
}
@using (Html.BeginForm())
{
foreach (var crime in Model.Crimes)
{
<text>@Html.RadioButton("SelectedCrimeID", crime.ID, false): @crime.Name<br /></text>
}
<br />
<input class="btn btn-large btn-primary" type="submit" value="Commit crime!" />
}
</div>
</body>
</html>
Upvotes: 2
Reputation: 56429
You should have a Crime
table in your database, that way you can store your payouts against each crime.
On another note, would it not be better to change all those Radio Buttons to a drop down list.
Upvotes: 1