public override void Handle(string command) { switch (_state) { case MainMenuState.MainMenu: if (command == "help" || command == "?") { Game.Instance.SendMessage(_account.Id, "You asked for <#orange>main help<#>."); } else if (command == "money") { Game.Instance.SendMessage(_account.Id, "You asked for <#lightgreen>main $MONEY<#>."); } else if (command == "c") { _connection.RemoveHandler(); _connection.AddHandler <NewCharacterHandler>(); _connection.Handler.Enter(); } else if (command == "p") { _state = MainMenuState.ChoosingCharacter; _script.Call(_script.Globals["printCharacters"]); } break; case MainMenuState.ChoosingCharacter: int idx; if (!int.TryParse(command, out idx)) { if (command == "q") { _state = MainMenuState.MainMenu; _connection.Handler.Enter(); } else { Game.Instance.SendMessage(_account.Id, "Please only enter a number or press \"<#white>q<#>\" to go back."); } break; } AiGEntity chosenCharacter = null; try { chosenCharacter = _characters[idx - 1]; if (chosenCharacter != null) { Game.Instance.SendMessage(_account.Id, $"You chose {chosenCharacter.Name}"); _connection.RemoveHandler(); _connection.AddHandler <GameplayHandler>(); _connection.Handler.Enter(chosenCharacter.Id); } } catch (ArgumentOutOfRangeException) { Game.Instance.SendMessage(_account.Id, "Invalid selection. Please try again."); } break; } }
public AiGEntity CreateEntity(string name = null, Dictionary <string, string> traits = null, int?parent = null) { var e = new Entity { Name = name, Traits = new List <Trait>(), ParentId = parent }; e.Id = _entities.Add(e); var dto = new AiGEntity(e.Id, e.Name); if (traits == null) { return(dto); } foreach (var t in traits) { e.Traits.Add(new Trait { EntityId = e.Id, Name = t.Key, Value = t.Value }); dto.Traits.Add(t.Key, t.Value); } _entities.Update(e); return(dto); }
public List <AiGEntity> LoadCharactersFromAccount(int accountId) { var account = _accounts.GetById(accountId, x => x.Characters); var existing = _entities.Find(x => account.Characters.Select(c => c.EntityId).Contains(x.Id), o => o.Traits, o => o.Components, o => o.Children).ToList(); var retVal = new List <AiGEntity>(); foreach (var entity in existing) { var e = new AiGEntity(entity.Id, entity.Name); foreach (var trait in entity.Traits) { e.Traits.Add(trait.Name, trait.Value); } foreach (var cmp in entity.Components) { ComponentManager.Instance.AssignComponent(e, cmp.Component); } e.Children.AddRange(entity.Children.Select(x => x.Id)); if (entity.Traits.Count != e.Traits.Count) { SaveEntity(e); } retVal.Add(e); } return(retVal); }
public override void Handle(string command) { if (command == "q") { _connection.RemoveHandler(); _connection.AddHandler <MainMenuHandler>(); _connection.Handler.Enter(); return; } switch (_state) { case NewCharState.EnteringName: var name = char.ToUpper(command[0]) + command.Substring(1); _newChar.Name = name; //test persistence ComponentManager.Instance.AssignComponent(_newChar, "health", new AiGTrait("maxHP", "50"), new AiGTrait("currHP", "45")); ComponentManager.Instance.AssignComponent(_newChar, "physical", new AiGTrait("description", $"This is {_newChar.Name}'s description."), new AiGTrait("weight", "175")); Game.Instance.SendMessage(_account.Id, $"Ah, welcome, {name}... 'tis a fine name, indeed.\n"); Game.Instance.SendMessage(_account.Id, _script.Call(_script.Globals["printRaces"]).String); _newChar.Traits.Add("room", "3"); _newChar.Traits.Add("accountId", _account.Id.ToString()); _newChar = Game.Instance.CreatePlayerCharacter(_account.Id, _newChar); _state = NewCharState.ChoosingRace; break; case NewCharState.ChoosingRace: Game.Instance.SendMessage(_account.Id, "Choosing races is hard!"); break; case NewCharState.EnteringDescription: break; } if (command == "help" || command == "?") { Game.Instance.SendMessage(_account.Id, "You asked for <#orange>main help<#>."); } else if (command == "money") { Game.Instance.SendMessage(_account.Id, "You asked for <#lightgreen>main $MONEY<#>."); } }
public AiGEntity CreatePlayerCharacter(int accountId, AiGEntity model) { var e = new Entity { Name = model.Name, Traits = new List <Trait>(model.Traits.GetAll().Select(t => new Trait { Name = t.Name, Value = t.Value })) }; var children = new List <Entity>(); foreach (var c in model.Children) { children.Add(_entities.GetById(c)); } e.Children.AddRange(children); e.Id = _entities.Add(e); var account = _accounts.GetById(accountId); if (account.Characters == null) { account.Characters = new List <Account_Entity> { new Account_Entity { AccountId = accountId, EntityId = e.Id } } } ; else { account.Characters.Add(new Account_Entity { AccountId = accountId, EntityId = e.Id }); } _accounts.Update(account); var newChar = new AiGEntity(e.Id, e.Name); foreach (var t in e.Traits) { newChar.Traits.Add(t.Name, t.Value); } return(newChar); }
public AiGEntity LoadEntityById(int id) { var e = _entities.GetById(id, x => x.Traits, x => x.Children, x => x.Components); var retVal = new AiGEntity(e.Id, e.Name, e.ParentId); foreach (var t in e.Traits) { retVal.Traits.Add(t.Name, t.Value); } foreach (var c in e.Components.Select(x => x.Component)) { ComponentManager.Instance.AssignComponent(retVal, c); } retVal.Children.AddRange(e.Children.Select(x => x.Id)); return(retVal); }
public void AssignComponent(AiGEntity entity, string componentName, params AiGTrait[] defaults) { var script = new Script(); script.Globals["Component"] = typeof(AiGComponent); script.Globals["Trait"] = typeof(AiGTrait); script.Globals["TraitSet"] = typeof(TraitSet); script.Globals["Entity"] = typeof(AiGEntity); script.DoString(_components[componentName]); var args = new Dictionary <string, string>(); foreach (var trait in defaults) { args.Add(trait.Name, trait.Value); } var cmp = (AiGComponent)script.Call(script.Globals["init"], entity, script, args).UserData.Object; entity.Components.Add(cmp); }
public AiGEntity SaveEntity(AiGEntity entity) { var existing = _entities.GetById(entity.Id, x => x.Traits); if (existing == null) { throw new ArgumentNullException("Entity not found. Be sure to CREATE it prior to saving it."); } existing.ParentId = entity.Parent; existing.Traits = entity.Traits.GetAll() .Select(x => new Trait { EntityId = entity.Id, Name = x.Name, Value = x.Value }).ToList(); existing.Components = entity.Components.GetAll() .Select(x => new EntityComponent { EntityId = entity.Id, Component = x.Name }).ToList(); existing.Children = _entities.Find(e => entity.Children.Contains(e.Id)).ToList(); _entities.Update(existing); return(entity); }
public override void Enter(params object[] args) { _state = NewCharState.EnteringName; _newChar = new AiGEntity(-1); Game.Instance.SendMessage(_account.Id, _script.Call(_script.Globals["enter"]).String); }
public AiGEntity CreatePlayerCharacter(int accountId, AiGEntity model) { return(_entityManager.CreatePlayerCharacter(accountId, model)); }