/* * protected override void OnModelCreating(ModelBuilder modelBuilder) * { * base.OnModelCreating(modelBuilder); * modelBuilder.Entity<Door>() * .HasOne(x => x.TargetRoom); * } */ public void Seed(TextEngineContext context) { Room r = new Room(); r.Name = "default"; r.Description = "default"; context.Rooms.Add(r); context.Inventories.Add(r.Inventory); context.SaveChanges(); }
public static Room ByID(TextEngineContext context, long ID) { return(context.Rooms .Where(r => r.RoomID == ID) .Include(r => r.Doors) .Include(r => r.Inventory) .Include(r => r.Monsters).ThenInclude(m => m.MonsterType) .Include(r => r.Monsters).ThenInclude(m => m.Inventory) .Include(r => r.Monsters).ThenInclude(m => m.Inventory).ThenInclude(i => i.Items) .FirstOrDefault()); }
public static Character GetInstance(TextEngineContext context) { return(context.Characters .Include(c => c.Inventory).ThenInclude(i => i.Items) .Include(c => c.CurrentRoom).ThenInclude(r => r.Doors) .Include(c => c.CurrentRoom).ThenInclude(r => r.Inventory).ThenInclude(i => i.Items) .Include(c => c.CurrentRoom).ThenInclude(r => r.Monsters).ThenInclude(m => m.MonsterType) .Include(c => c.CurrentRoom).ThenInclude(r => r.Monsters).ThenInclude(m => m.Inventory) .Include(c => c.CurrentRoom).ThenInclude(r => r.Monsters).ThenInclude(m => m.Inventory).ThenInclude(i => i.Items) .FirstOrDefault()); }
public static Item FromID(TextEngineContext context, string itemID) { Item item = null; long itemIDLong; bool validID = long.TryParse(itemID, out itemIDLong); if (validID) { item = context.Items .Include(i => i.Inventory) .Where(i => i.ItemID == itemIDLong) .FirstOrDefault(); } return(item); }
public void Die() { Utility.Print(ConsoleColor.DarkRed, "The " + this.MonsterType.Name + " collapses."); Utility.Print(ConsoleColor.Yellow, "The " + this.MonsterType.Name + " drops " + string.Join(", ", this.Inventory.Items)); using (var context = new TextEngineContext()) { foreach (Item localItem in this.Inventory.Items) { var item = context.Items.First(i => i.ItemID == localItem.ItemID); item.Inventory = Room.ByID(context, Program.character.CurrentRoom.RoomID).Inventory; } context.SaveChanges(); Program.RefreshGameState(context); } }
// this method only does anything when a new game is started public void SetInitialRoom(TextEngineContext context) { // set the default room to the first room in the list // TODO: maybe change this to 0, 0 if (this.CurrentRoom == null) { // TODO: how to make the model do this automatically? // ThenInclude: https://github.com/aspnet/EntityFramework/wiki/Design-Meeting-Notes:-January-8,-2015 var rooms = from r in context.Rooms .Include(r => r.Doors).ThenInclude(d => d.TargetRoom) // this syntax was found in a bug fix .Include(r => r.Inventory) select r; this.CurrentRoom = rooms.First(); } }
public void Move(Direction direction) { Door door = null; using (var context = new TextEngineContext()) { door = (from d in context.Doors .Include(d => d.TargetRoom).ThenInclude(r => r.Doors) .Include(d => d.DoorItemConditions).ThenInclude(c => c.Item).ThenInclude(i => i.Inventory) .Where(d => d.SourceRoom.RoomID == Program.character.CurrentRoom.RoomID && d.Direction == direction) select d).FirstOrDefault(); if (door == null) { Utility.Print(ConsoleColor.Yellow, "There doesn't appear to be an exit in that direction."); return; } } // are there item conditions? bool itemConditionsMet = true; foreach (var cond in door.DoorItemConditions) { if (Program.character.Inventory.Items.FirstOrDefault(x => x.ItemID == cond.Item.ItemID) == null) { Utility.Print(ConsoleColor.Yellow, "You must possess the " + cond.Item.Name + " to unlock this door!"); itemConditionsMet = false; } else { Utility.Print(ConsoleColor.Yellow, "You successfully unlock the door using the " + cond.Item.Name + "!"); Console.WriteLine(); } } if (!itemConditionsMet) { return; } /* * Room TargetRoom = door == null ? null : door.TargetRoom; * * if (TargetRoom == null) * { * var defaultColor = Console.ForegroundColor; * Console.ForegroundColor = ConsoleColor.Yellow; * Console.WriteLine("There is no exit in that direction!"); * Console.ForegroundColor = defaultColor; * return; * } */ Program.character.CurrentRoom = door.TargetRoom; using (var context = new TextEngineContext()) { Character c = Character.GetInstance(context); Room room = Room.ByID(context, door.TargetRoom.RoomID); c.CurrentRoom = room; context.SaveChanges(); Program.RefreshGameState(context); } Program.character.CurrentRoom.Look(); }
public static Door FromDir(TextEngineContext context, string dir) { Direction direction = Utility.StringToDirection(dir); return(context.Doors.FirstOrDefault(x => x.Direction == direction && x.SourceRoom.RoomID == Program.character.CurrentRoom.RoomID)); }