Skip to content
Snippets Groups Projects
Commit e30aeed9 authored by Pregmodder's avatar Pregmodder
Browse files

Merge branch 'policy-js' into 'pregmod-master'

Convert policies screen to use function, LF review.

See merge request pregmodfan/fc-pregmod!7089
parents 4a7304cc 424399b8
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,8 @@ App.Art = {};
App.Corporate = {};
App.Data = {};
App.Data.HeroSlaves = {};
App.Data.Policies = {};
App.Data.Policies.Selection = {};
App.Data.Weather = {};
App.Debug = {};
App.Desc = {};
......
This diff is collapsed.
......@@ -106,3 +106,195 @@ globalThis.policies = (function() {
function cost() { return 5000; }
})();
/**
* @param {string} category
* @returns {JQuery}
*/
globalThis.policy = function(category) {
const frag = new DocumentFragment;
for (let policyVariable in App.Data.Policies.Selection[category]) {
frag.append(policyElement(policyVariable));
}
return jQuery(`#${category}`).empty().append(frag);
/**
* @param {string} policyVariable
* @returns {node} el
*/
function policyElement(policyVariable) {
let el = document.createElement("p");
let div;
let span;
let link;
const policyValue = _.get(V, policyVariable);
const policyObject = App.Data.Policies.Selection[category][policyVariable];
if (policyValue === 0) {
// apply
for (let i = 0; i < policyObject.length; i++) {
const p = policyObject[i];
const enable = p.enable || 1;
if (p.hasOwnProperty("requirements") && p.requirements() === false) {
continue;
}
div = document.createElement("div");
span = document.createElement("span");
// title
span.style.fontWeight = "bold";
if (p.hasOwnProperty("titleClass")) {
span.classList.add(p.titleClass);
}
span.append(p.title);
div.append(span);
div.append(`: `);
// Description text
div.append(p.text);
div.append(` `);
// link
if (!(p.hasOwnProperty("hide") && p.hide.button === 1)) {
if (p.hasOwnProperty("requirements")) {
const req = p.requirements();
if (req === true) {
div.append(implement(p, enable));
} else {
link = App.UI.DOM.disabledLink("Implement", [`You do not meet the requirments, or passed a conflicting policy already`]);
link.style.color = "white";
div.append(link);
}
} else {
div.append(implement(p, enable));
}
}
el.append(div);
}
} else {
// repeal
let i = 0;
for (const pol in policyObject) {
if (policyObject[pol].hasOwnProperty("enable") && policyObject[pol].enable === policyValue) {
i = pol;
break;
}
}
const p = policyObject[i];
if (p.hasOwnProperty("hide")) {
if (p.hide.ifActivated === 1) {
return el;
}
}
let title;
if (p.hasOwnProperty("activatedTitle")) {
title = p.activatedTitle;
} else {
title = p.title;
}
let text;
if (p.hasOwnProperty("activatedText")) {
text = p.activatedText;
} else {
text = p.text;
}
div = document.createElement("div");
span = document.createElement("span");
// title
span.style.fontWeight = "bold";
span.append(title);
div.append(span);
div.append(`: `);
// Description text
div.append(text);
div.append(` `);
// link
div.append(repeal(p));
el.append(div);
}
return el;
/**
* @param {*} p The data object that describes the policy being considered.
* @returns {node} Link to repeal.
*/
function repeal(p) {
const frag = new DocumentFragment;
let check = canAfford();
if (!(p.hasOwnProperty("hide") && p.hide.button === 1)) {
if (check === true) {
link = App.UI.DOM.link(
"Repeal",
() => {
_.set(V, policyVariable, 0);
applyCost();
if (p.hasOwnProperty("onRepeal")) {
p.onRepeal();
}
policy(category);
}
);
link.style.color = "yellow";
} else {
link = App.UI.DOM.disabledLink("Repeal", [`You do not have enough ${check}`]);
link.style.color = "red";
}
frag.append(link);
}
if (p.hasOwnProperty("activatedNote")) {
frag.append(App.UI.DOM.makeElement("div", p.activatedNote, ["note", "indent"]));
}
return frag;
}
/**
* @param {*} p The data object that describes the policy being considered.
* @param {number|string} enable value to set the policy to in order to switch it on.
* @returns {node} Link to implement.
*/
function implement(p, enable) {
let check = canAfford();
const frag = new DocumentFragment;
if (check === true) {
link = App.UI.DOM.link(
"Implement",
() => {
_.set(V, policyVariable, enable);
applyCost();
if (p.hasOwnProperty("onImplementation")) {
p.onImplementation();
}
policy(category);
}
);
link.style.color = "green";
} else {
link = App.UI.DOM.disabledLink("Implement", [`You do not have enough ${check}`]);
link.style.color = "red";
}
frag.append(link);
if (p.hasOwnProperty("note")) {
frag.append(App.UI.DOM.makeElement("div", p.note, ["note", "indent"]));
}
return frag;
}
function canAfford() {
if (V.cash < 5000) {
return "cash";
} else if (V.rep < 1000 && !["EducationPolicies"].includes(category)) {
return "reputation";
}
return true;
}
function applyCost() {
cashX(-5000, "policies");
if (!["EducationPolicies"].includes(category)) {
repX(-1000, "policies");
}
}
}
};
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment