public static void MoveContractsToActive(ApplicationDbContext db, DataCache data) { var newContracts = false; var exclude = data.CorporationContracts.Select(cc => cc.Contract.Name).ToList(); foreach (var c in data.ContractsInMemory.Where(c => !exclude.Contains(c.Name))) { //HasAttributeGreaterThan if (c.TriggerCondition.Condition == Constants.Constants.HasAttributeGreaterThan) { var attribute = data.Corporation.GetType().GetProperty(c.TriggerCondition.Attribute); if (Convert.ToInt32(attribute.GetValue(data.Corporation)) > Convert.ToInt32(c.TriggerCondition.Value)) { var schemaContract = db.Contracts.FirstOrDefault(con => con.Name == c.Name); var active = new CorporationContract() { Active = true, Accepted = false, Complete = false, Contract = schemaContract, Corporation = data.Corporation }; db.CorporationContracts.Add(active); newContracts = true; } } } if (newContracts) LogService.Log(db, data.Corporation, "New contracts are available."); }
public IActionResult EndTurn(ClientActionModel clientModel) { var newModel = ActionModel.NewActionModel(db, User.Identity.Name); var data = new DataCache(db, newModel.User, newModel.Corporation); newModel.DataCache = data; ActionService.PerformActions(db, clientModel, data); ActionService.ResolveState(db, clientModel, data); data.RefreshCache(); newModel.BuildViewItems(db); return Json(newModel); }
// GET: Action public IActionResult Index(ActionModel actionModel) { var model = ActionModel.NewActionModel(db, User.Identity.Name); var data = new DataCache(db, model.User, model.Corporation); data.RefreshCache(); model.DataCache = data; if (model.Corporation.TurnCount > 1) { model.Messages = LogService.GetLogsByTurn(db, model.Corporation, model.Corporation.TurnCount - 1); } data.RefreshCache(); model.BuildViewItems(db); return View(model); }
public static void SaveDataCache(HttpContext context, DataCache cache) { context.Session.SetString(context.User.Identity.Name + "DataCache", JsonConvert.SerializeObject(cache)); }
public static void PerformActions(ApplicationDbContext db, ClientActionModel model, DataCache data) { var actionCounter = new Dictionary<string, int>(); actionCounter[Constants.Constants.ActionTypeResearch] = 0; string currentObjectName = ""; if (string.IsNullOrEmpty(model.Actions)) { LogService.Log(db, data.Corporation, "You made no actions the previous turn."); return; } var actions = model.Actions.Split('|'); foreach (var actionType in actions) { var currentActionDict = new Dictionary<string, string>(); var actionItems = actionType.Split(';'); foreach (var a in actionItems) { var parameters = a.Split('='); if (string.IsNullOrEmpty(parameters[0])) continue; currentActionDict[parameters[0]] = parameters[1]; } //Handle action types--- if (currentActionDict.ContainsKey(Constants.Constants.ActionType)) { if (currentActionDict[Constants.Constants.ActionType] == Constants.Constants.ActionTypeResearch) actionCounter[Constants.Constants.ActionTypeResearch]++; } //Handle actions--- if (currentActionDict.ContainsKey(Constants.Constants.Action)) { var specifierSplit = currentActionDict[Constants.Constants.Action].Split(':'); currentObjectName = specifierSplit[1]; //#LearnResearch if (specifierSplit[0] == Constants.Constants.LearnResearch) { var researchNode = db.ResearchNodes.FirstOrDefault(n => n.Name == currentObjectName); if (currentActionDict[Constants.Constants.ActionParameter] == Constants.Constants.LearnByCash) //Research is bought with cash { var node = data.ResearchNodes.FirstOrDefault(n => n.Name == currentObjectName); if (node == null) LogService.Log(db, data.Corporation, "ERROR: Attempting to add node " + currentObjectName + " that does not exist."); else { if (node.CashCost > data.Corporation.Cash) LogService.Log(db, data.Corporation, "Error: Not enough cash to purchase research node " + currentObjectName); else { data.Corporation.Cash -= node.CashCost; var lrn = new LearnedResearchNode { Corporation = data.Corporation, ResearchNode = researchNode }; db.Add(lrn); LogService.Log(db, data.Corporation, "Research " + currentObjectName + " purchased."); } } } else if (currentActionDict[Constants.Constants.ActionParameter] == Constants.Constants.SetActiveResearch) //Research is set to active { data.ActiveResearchNodes.ForEach(n => n.Active = false); var active = data.ActiveResearchNodes.FirstOrDefault(n => n.ResearchNode.Name == currentObjectName); if (active != null) //If not null, we have already started researching it, so set to false { active.Active = true; var toRemove = data.ActiveResearchNodes.Where(n => n.Id != active.Id); db.Remove(toRemove); } else { active = new ActiveResearchNode(); active.ResearchNode = researchNode; active.Corporation = data.Corporation; active.Active = true; db.Add(active); } LogService.Log(db, data.Corporation, "Set " + currentObjectName + " to active research. "); } var validated = data.LearnableResearchNodeNames.Contains(currentObjectName); if (!validated) { LogService.Log(db, data.Corporation, "Error: Already learned node " + currentObjectName); } } //#CancelActiveResearch else if (specifierSplit[0] == Constants.Constants.CancelActiveResearch) { if (!data.ActiveResearchNodesSchemaNodes.Select(n => n.Name).Contains(specifierSplit[1])) { LogService.Log(db, data.Corporation, "Cannot cancel research for " + specifierSplit[1] + ". It is not currently being researched."); continue; } var nodeToCancel = db.ActiveResearchNodes.FirstOrDefault( n => n.Corporation.Id == data.Corporation.Id && n.ResearchNode.Name == specifierSplit[1]); if (nodeToCancel == null) { LogService.Log(db, data.Corporation, specifierSplit[1] + " is not a valid research node name."); continue; } db.Remove(nodeToCancel); LogService.Log(db, data.Corporation, "Canceled research for " + specifierSplit[1] + "."); } //#HirePerson else if (specifierSplit[0] == Constants.Constants.HirePerson) { var alreadyHired = CorporationPerson.GetPeopleByHiredStatus(true, data.CorporationPersons).ToList(); var entity = data.CorporationPersons.FirstOrDefault(cp => cp.Person.Name == currentObjectName); if (entity == null) { LogService.Log(db, data.Corporation, "Cannot find entity with name: " + currentObjectName + "!"); continue; } if (alreadyHired.Select(h => h.Person.Name).Contains(currentObjectName)) { LogService.Log(db, data.Corporation, "Already hired " + currentObjectName + "! Action could not be completed."); continue; } if ((data.Corporation.Cash + entity.Person.SigningBonus) < entity.Person.TurnSalary) { LogService.Log(db, data.Corporation, "Not enough funds to hire " + entity.Person.Name + "!"); continue; } entity.Hired = true; data.Corporation.Cash -= entity.Person.SigningBonus; data.Corporation.PublicInterest += entity.Person.Celebrity ? Constants.Constants.CelebrityBonus : 0; data.Corporation.Reputation += entity.Person.Celebrity ? Constants.Constants.CelebrityBonus : 0; data.Corporation.RD += entity.Person.Intelligence; data.Corporation.Readiness += entity.Person.Experience; data.Corporation.BusinessMultiplier += (double)entity.Person.Business / 100; LogService.Log(db, data.Corporation, "Hired " + currentObjectName + " as " + entity.Person.Position + " for $" + entity.Person.TurnSalary + " salary and a signing bonus of $" + entity.Person.SigningBonus + "."); } //#FirePerson else if (specifierSplit[0] == Constants.Constants.FirePerson) { var alreadyHired = CorporationPerson.GetPeopleByHiredStatus(true, data.CorporationPersons).ToList(); var entity = data.CorporationPersons.FirstOrDefault(cp => cp.Person.Name == currentObjectName); if (entity == null) { LogService.Log(db, data.Corporation, "Cannot find entity with name: " + currentObjectName + "!"); continue; } if (!alreadyHired.Select(h => h.Person.Name).Contains(currentObjectName)) { LogService.Log(db, data.Corporation, "Cannot fire " + currentObjectName + " because they are not hired!"); continue; } entity.Hired = false; data.Corporation.Cash -= entity.Person.SeverancePayout; data.Corporation.PublicInterest -= entity.Person.Celebrity ? Constants.Constants.CelebrityBonus : 0; data.Corporation.Reputation -= entity.Person.Celebrity ? Constants.Constants.CelebrityBonus : 0; data.Corporation.RD -= entity.Person.Intelligence; data.Corporation.Readiness -= entity.Person.Experience; data.Corporation.BusinessMultiplier -= (double)entity.Person.Business / 100; LogService.Log(db, data.Corporation, "Fired " + currentObjectName + ", saving $" + entity.Person.TurnSalary + " per turn, with a severance payout of $" + entity.Person.SeverancePayout + "."); } if (specifierSplit[0] == Constants.Constants.StartContract) { var contractName = specifierSplit[1]; var contract = data.CorporationContracts.FirstOrDefault(c => c.Contract.Name == contractName); if (contract == null || contract.Accepted || !contract.Active || contract.Complete) { LogService.Log(db, data.Corporation, "Unable to begin contract " + contractName + "."); continue; } LogService.Log(db, data.Corporation, "Accepted contract " + contractName + "."); contract.Accepted = true; contract.Active = true; contract.Complete = false; contract.NodeNumber = 1; } if (specifierSplit[0] == Constants.Constants.AdvanceContract) { var contractName = specifierSplit[1]; var corpContract = data.CorporationContracts.FirstOrDefault(cc => cc.Contract.Name == contractName); var memContract = data.ContractsInMemory.FirstOrDefault(mc => mc.Name == contractName); if (corpContract == null || memContract == null || !corpContract.Active || !corpContract.Accepted || corpContract.Complete) { LogService.Log(db, data.Corporation, "Cannot advance contract " + contractName + "."); continue; } var node = memContract.ContractNodes.FirstOrDefault(cn => cn.NodeNumber == corpContract.NodeNumber); if (node == null) { LogService.Log(db, data.Corporation, "Invalid contract node identifier. Cannot advance contract state for " + contractName + "."); continue; } var param = currentActionDict[Constants.Constants.ActionParameter]; var option = node.ContractOptions.FirstOrDefault(o => o.OptionCommand == param); if (option == null) { LogService.Log(db, data.Corporation, "Invalid contract option parameter. Cannot advance contract " + contractName + "."); continue; } if (option.NextNode == Constants.Constants.Complete) { LogService.Log(db, data.Corporation, "Completed contract " + contractName + "."); corpContract.Complete = true; corpContract.Active = false; continue; } else if (option.NextNode == Constants.Constants.Fail) { LogService.Log(db, data.Corporation, "Failed contract " + contractName + "."); corpContract.Complete = false; corpContract.Active = false; continue; } else { //The quest isn't being completed or failed at this point, //so change the current contract node to the advanced state. corpContract.NodeNumber = Convert.ToInt32(option.NextNode); } } } } db.SaveChanges(); }
public static void ResolveState(ApplicationDbContext db, ClientActionModel model, DataCache data) { data.RefreshCache(); //Advance research node states var nodes = data.ActiveResearchNodes.Where(n => n.Corporation.Id == data.Corporation.Id).ToList(); nodes.ForEach(n => { n.RDInvested += data.Corporation.RD; if (n.RDInvested >= n.ResearchNode.RDCost) { db.LearnedResearchNodes.Add(new LearnedResearchNode() { Corporation = data.Corporation, ResearchNode = n.ResearchNode, }); db.ActiveResearchNodes.Remove(n); LogService.Log(db, data.Corporation, "Learned research node " + n.ResearchNode.Name); } }); //Take out salaries var hired = CorporationPerson.GetPeopleByHiredStatus(true, data.CorporationPersons); foreach (var p in hired) { data.Corporation.Cash -= p.Person.TurnSalary; if (data.Corporation.Cash < 0) LogService.Log(db, data.Corporation, "You are unabel to pay your employees! When they don't get paid, their happiness drops, and they might quit."); } //Resolve Contracts ContractService.MoveContractsToActive(db, data); data.Corporation.TurnCount++; db.SaveChanges(); }