// asynchroní metoda pro načtení objednávek public static async Task <List <CharactersDO> > GetCharactersAsync( string playerName) { using (fantasyDbEntities context = new fantasyDbEntities()) { // return obstarávající select a join tabulek pro získání dat return(await context.Character .Where(x => x.playername == playerName) .Select(x => new CharactersDO() { CharID = x.idchar, PlayerName = x.playername, CharName = x.charname, CharClass = x.charclass, CharBio = x.charbio, CharHp = x.charhp, CharXp = x.charxp, CharMp = x.charmp, CharLevel = x.charlevel, CharStr = x.strength, CharAgi = x.agility, CharDurability = x.chardurability, CharInt = x.intelligence, }) .ToListAsync()); } }
public ActionResult EditCharacter(CharModel model, int id) { ModelState.Clear(); if (ModelState.IsValid) { try { using (fantasyDbEntities context = new fantasyDbEntities()) { Character character = new Character(); character = context.Character.Find(id); // kontrola, zda string hodnoty nejsou null if (model.CharName != null) { character.charname = model.CharName; } if (model.CharBio != null) { character.charbio = model.CharBio; } if (model.CharClass != null) { character.charclass = model.CharClass; } character.charlevel = model.CharLevel; character.charhp = model.CharHp; character.charmp = model.CharMp; character.strength = model.CharStr; character.chardurability = model.CharDurability; character.idchar = id; try { // uložení všech změněných hodnot context.Entry(character).State = EntityState.Modified; context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { var error = ex.ToString(); return(View()); } } return(RedirectToAction("Characters")); } catch (Exception exception) { var error = exception.ToString(); return(View()); } } return(View()); }
public ActionResult NewCharacter(CharModel model) { if (ModelState.IsValid) { using (fantasyDbEntities context = new fantasyDbEntities()) { // vytvoření nové entity postavy Character player = new Character(); player.charname = model.CharName; player.charbio = model.CharBio; // hardcoded starting values player.charhp = 20; player.charlevel = 1; player.charmp = 10; player.charxp = 0; player.strength = 5; player.agility = 5; player.chardurability = 5; player.intelligence = 5; // pokud uživatel nevybrat žádnou profesi if (model.CharClass == null) { // nastaví profesi za uživatele player.charclass = "Warrior"; } else { player.charclass = model.CharClass; } player.playername = User.Identity.GetUserName(); // přidání a uložení nové postavy context.Character.Add(player); context.SaveChanges(); // automatické založení inventáře pro každou novou postavu Inventory inventory = new Inventory(); inventory.maxspace = 10; inventory.freespace = 10; inventory.ownerid = player.idchar; context.Inventory.Add(inventory); context.SaveChanges(); } } else { return(RedirectToAction("Characters")); } return(RedirectToAction("Characters")); }
public static async Task <List <BeastTypeDO> > GetBeastTypeAsync() { using (fantasyDbEntities context = new fantasyDbEntities()) { return(await context.BeastType .Select(x => new BeastTypeDO() { Idbeasttype = x.idbeasttype, Name = x.name }) .ToListAsync()); } }
public static async Task <List <TownsDO> > GetTownsAsync() { using (fantasyDbEntities context = new fantasyDbEntities()) { return(await context.Town .Select(x => new TownsDO() { TownID = x.idtown, Name = x.name }) .ToListAsync()); } }
public async Task <ActionResult> NewBeast(BeastModel model) { List <BeastTypeDO> beastType = await BeastTypeDO.GetBeastTypeAsync(); // hardcoded - povoluje přidat bestie pouze mimo města List <LocationsDO> locations = await LocationsDO.GetLocationsAsync(0); ViewBag.beastType = beastType .Select(x => new SelectListItem() { Text = x.Name, Value = x.Idbeasttype.ToString() }) .ToList(); ViewBag.Location = locations .Select(x => new SelectListItem() { Text = x.Name, Value = x.LocationID.ToString() }) .ToList(); if (ModelState.IsValid) { if (model.BeastName != null && model.BeastBio != null) { using (fantasyDbEntities context = new fantasyDbEntities()) { // Vytvoření nové bestie a načtení hodnot zvolených uživatelem Bestiary beast = new Bestiary(); beast.name = model.BeastName; beast.bio = model.BeastBio; beast.hp = model.BeastHp; beast.attack = model.BeastAttack; beast.defense = model.BeastDefense; beast.locationid = model.BeastLocation; beast.beasttypeid = model.BeastTypeID; // Přidání řádku tabulky a uložení změn context.Bestiary.Add(beast); context.SaveChanges(); } } } return(RedirectToAction("Bestiary")); }
public static async Task <List <InventoryDO> > GetInventoryAsync(int charid) { using (fantasyDbEntities context = new fantasyDbEntities()) { return(await context.Inventory .Where(inventory => inventory.ownerid == charid) .Select(x => new InventoryDO() { MaxSpace = x.maxspace, FreeSpace = x.freespace }) .ToListAsync()); } }
// zobrazení stránky Inventory - využívá zaslané ID hráče public ActionResult Inventory(int id) { using (fantasyDbEntities context = new fantasyDbEntities()) { var inventory = context.Inventory.Where(x => x.ownerid == id).FirstOrDefault(); // vytvoření InventoryModelu a načtení aktuálních hodnot InventoryModel model = new InventoryModel(); model.OwnerID = inventory.ownerid; model.MaxSpace = inventory.maxspace; model.FreeSpace = inventory.freespace; return(View(model)); } }
// zobrazení stránky EditBeast public ActionResult EditBeast(int id) { using (fantasyDbEntities context = new fantasyDbEntities()) { var beast = context.Bestiary.Where(x => x.idbeast == id).FirstOrDefault(); // vytvoření instance BeastModelu a načtení aktuálních hodnot BeastModel model = new BeastModel(); model.BeastID = id; model.BeastName = beast.name; model.BeastBio = beast.bio; model.BeastTypeID = beast.beasttypeid; return(View(model)); } }
public ActionResult EditBeast(BeastModel model, int id) { ModelState.Clear(); if (ModelState.IsValid) { try { using (fantasyDbEntities context = new fantasyDbEntities()) { Bestiary beast = new Bestiary(); beast = context.Bestiary.Find(id); beast.idbeast = id; beast.beasttypeid = model.BeastTypeID; // kontrola stringových hodnot - nezměněné hodnoty jsou NULL if (model.BeastName != null) { beast.name = model.BeastName; } if (model.BeastBio != null) { beast.bio = model.BeastBio; } try { context.Entry(beast).State = EntityState.Modified; context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { var error = ex.ToString(); return(View()); } } return(RedirectToAction("Bestiary")); } catch (Exception exception) { var error = exception.ToString(); return(View()); } } return(View()); }
public static async Task <List <LocationsDO> > GetLocationsAsync(int townid) { using (fantasyDbEntities context = new fantasyDbEntities()) { return(await context.Location .Where(location => location.townid == townid) .Join(context.Town, location => location.townid, // cizí klíč z tab objednávek town => town.idtown, // prim. klíč z tab zaměstnanců (location, town) => new LocationsDO() { LocationID = location.idlocation, Name = location.name, TownName = town.name, }) .ToListAsync()); } }
// zobrazení stránky EditCharacter - zasílající zvolené ID hráče public ActionResult EditCharacter(int id) { using (fantasyDbEntities context = new fantasyDbEntities()) { var character = context.Character.Where(x => x.idchar == id).FirstOrDefault(); // vytvoření instance CharModelu a načtení všech aktuálních hodnot CharModel model = new CharModel(); model.CharID = id; model.CharName = character.charname; model.CharBio = character.charbio; model.CharClass = character.charclass; model.CharLevel = character.charlevel; model.CharHp = character.charhp; model.CharMp = character.charmp; model.CharStr = (int)character.strength; model.CharDurability = (int)character.chardurability; return(View(model)); } }
// asynchroní metoda pro načtení objednávek public static async Task <List <BeastsDO> > GetBeastsAsync( int idbeasttype) { using (fantasyDbEntities context = new fantasyDbEntities()) { // return obstarávající select a join tabulek pro získání dat return(await context.Bestiary .Where(bestiary => bestiary.beasttypeid == idbeasttype) .Join(context.BeastType, bestiary => bestiary.beasttypeid, // cizí klíč z tab objednávek beastType => beastType.idbeasttype, // prim. klíč z tab zaměstnanců (bestiary, beastType) => new BeastsDO() { Idbeast = bestiary.idbeast, Name = bestiary.name, Attack = bestiary.attack, Defense = bestiary.defense, BeastTypeName = beastType.name, Bio = bestiary.bio, }) .ToListAsync()); } }
public ActionResult Inventory(InventoryModel model, string submitButton) { ModelState.Clear(); // switch kontrolující, jaké z tlačítek bylo stlačeno switch (submitButton) { case "AddItem": // hráč se rozhodl přidat předmět if (model.FreeSpace > 1) { model.FreeSpace -= 1; } break; case "RemoveItem": // hráč se rozhodl odebrat předmět if (model.FreeSpace < model.MaxSpace) { model.FreeSpace += 1; } break; case "IncreaseSize": // hráč se rozhodl rozšířit inventář model.MaxSpace += 1; model.FreeSpace += 1; break; case "DecreaseSize": // hráč se rozhodl zmenšit inventář if (model.MaxSpace > 1 && model.FreeSpace > 1) { model.MaxSpace -= 1; model.FreeSpace -= 1; } break; default: // vrácení v případě, že byl zaslán POST požadavek bez kliknutí na tlačítka return(View()); } if (ModelState.IsValid) { try { using (fantasyDbEntities context = new fantasyDbEntities()) { // Vytvoření entity inventory a načtení hodnot z modelu Inventory inventory = new Inventory(); inventory = context.Inventory.Find(model.OwnerID); inventory.freespace = model.FreeSpace; inventory.maxspace = model.MaxSpace; try { // uložení změn do db context.Entry(inventory).State = EntityState.Modified; context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { var error = ex.ToString(); return(View()); } } return(View(model)); } catch (Exception exception) { var error = exception.ToString(); return(View()); } } return(View()); }