public string CreateGuerrilla(guerrilla guerrillaDTO, string name) { try { Guerrilla guerrilla = new Guerrilla(); guerrilla.Name = guerrillaDTO.guerrillaName; guerrilla.Email = guerrillaDTO.email; guerrilla.Faction = guerrillaDTO.faction; guerrilla.Rank = 0; guerrilla.Timestamp = 0; guerrilla.Available = true; _context.Guerrilla.Add(guerrilla); GuerrillaResources oil = new GuerrillaResources { Guerrilla = guerrilla.Name, Quantity = 0, Resource = _oil }; GuerrillaResources money = new GuerrillaResources { Guerrilla = guerrilla.Name, Quantity = 0, Resource = _money }; GuerrillaResources people = new GuerrillaResources { Guerrilla = guerrilla.Name, Quantity = 0, Resource = _people }; _context.GuerrillaResources.Add(oil); _context.GuerrillaResources.Add(money); _context.GuerrillaResources.Add(people); GuerrillaUnits bunker = new GuerrillaUnits { Guerrilla = guerrilla.Name, Quantity = 0, Unit = _bunker }; GuerrillaUnits assault = new GuerrillaUnits { Guerrilla = guerrilla.Name, Quantity = 0, Unit = _assault }; GuerrillaUnits engineer = new GuerrillaUnits { Guerrilla = guerrilla.Name, Quantity = 0, Unit = _engineer }; GuerrillaUnits tank = new GuerrillaUnits { Guerrilla = guerrilla.Name, Quantity = 0, Unit = _tank }; _context.GuerrillaUnits.Add(bunker); _context.GuerrillaUnits.Add(assault); _context.GuerrillaUnits.Add(engineer); _context.GuerrillaUnits.Add(tank); _context.SaveChanges(); return(GetGuerrillaDTO(guerrilla.Name)); } catch { throw; } }
public string BuyUnits(string name, units units) { if (units.buildings == null) { units.buildings = new buildings(); } if (units.army == null) { units.army = new army(); } List <Unit> allUnits = _context.Unit.ToList(); List <GuerrillaResources> guerrillaResources = _context.GuerrillaResources.Where(g => g.Guerrilla.Equals(name)).ToList(); #region calculate costs int oilCost = units.army.assault * allUnits.Where(u => u.Name.Equals(_assault)).Single().Oil + units.army.engineer * allUnits.Where(u => u.Name.Equals(_engineer)).Single().Oil + units.army.tank * allUnits.Where(u => u.Name.Equals(_tank)).Single().Oil + units.buildings.bunker * allUnits.Where(u => u.Name.Equals(_bunker)).Single().Oil; int moneyCost = units.army.assault * allUnits.Where(u => u.Name.Equals(_assault)).Single().Money + units.army.engineer * allUnits.Where(u => u.Name.Equals(_engineer)).Single().Money + units.army.tank * allUnits.Where(u => u.Name.Equals(_tank)).Single().Money + units.buildings.bunker * allUnits.Where(u => u.Name.Equals(_bunker)).Single().Money; int peopleCost = units.army.assault * allUnits.Where(u => u.Name.Equals(_assault)).Single().People + units.army.engineer * allUnits.Where(u => u.Name.Equals(_engineer)).Single().People + units.army.tank * allUnits.Where(u => u.Name.Equals(_tank)).Single().People + units.buildings.bunker * allUnits.Where(u => u.Name.Equals(_bunker)).Single().People; #endregion GuerrillaResources guerrillaOil = guerrillaResources.Where(r => r.Resource.Equals(_oil)).Single(); GuerrillaResources guerrillaMoney = guerrillaResources.Where(r => r.Resource.Equals(_money)).Single(); GuerrillaResources guerrillaPeople = guerrillaResources.Where(r => r.Resource.Equals(_people)).Single(); if ((guerrillaOil.Quantity - oilCost) >= 0 && (guerrillaMoney.Quantity - moneyCost) >= 0 && (guerrillaPeople.Quantity - peopleCost) > 0) { if (units.army.assault > 0) { GuerrillaUnits assaultNewUnits = new GuerrillaUnits(); assaultNewUnits.Guerrilla = name; assaultNewUnits.Unit = _assault; assaultNewUnits.Quantity = units.army.assault; _context.GuerrillaUnits.Update(assaultNewUnits); } if (units.army.engineer > 0) { GuerrillaUnits engineerNewUnits = new GuerrillaUnits(); engineerNewUnits.Guerrilla = name; engineerNewUnits.Unit = _engineer; engineerNewUnits.Quantity = units.army.engineer; _context.GuerrillaUnits.Update(engineerNewUnits); } if (units.army.tank > 0) { GuerrillaUnits tankNewUnits = new GuerrillaUnits(); tankNewUnits.Guerrilla = name; tankNewUnits.Unit = _tank; tankNewUnits.Quantity = units.army.tank; _context.GuerrillaUnits.Update(tankNewUnits); } if (units.buildings.bunker > 0) { GuerrillaUnits bunkerNewUnits = new GuerrillaUnits(); bunkerNewUnits.Guerrilla = name; bunkerNewUnits.Unit = _bunker; bunkerNewUnits.Quantity = units.buildings.bunker; _context.GuerrillaUnits.Add(bunkerNewUnits); } guerrillaOil.Quantity = guerrillaOil.Quantity - oilCost; guerrillaMoney.Quantity = guerrillaMoney.Quantity - moneyCost; guerrillaPeople.Quantity = guerrillaPeople.Quantity - peopleCost; _context.GuerrillaResources.Update(guerrillaOil); _context.SaveChanges(); } return(GetGuerrillaDTO(name)); }