-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
161 lines (137 loc) · 6.67 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function formatBigInt(value, cutby = 0, format = null) {
if (value == null) {
console.log('Value was null, cannot format.');
return 'FORMAT error';
}
let ret = value.toString();
//cut if necessary
if (cutby != 0) {
if (ret.length > cutby) {
ret = ret.substr(0, ret.length - cutby);
} else {
ret = '0';
}
// Add decimals for small values
if (value < 1000n * BigInt(cutby)) {
ret += settings.DecimalPoint + value.toString().slice(-1 * cutby).padStart(cutby, '0');
}
}
// format tousands
ret = ret.replace(/\B(?=(\d{3})+(?!\d))/g, settings['TousandPoint']);
// format with currency etc.
if (format != null) {
ret = format.replace('n', ret);
}
//console.log('Format ' + value +' to ' + ret);
return ret;
}
function jsonparse(stringed) {
try {
return JSON.parse(stringed, (key, value) => {
if (typeof value === "string" && /^\d+n$/.test(value)) {
return BigInt(value.substr(0, value.length - 1));
}
return value;
});
} catch (e) {
error('Invalid File');
return null;
}
}
function jsonify(json) {
return JSON.stringify(json, (key, value) => typeof value === "bigint" ? value.toString() + "n" : value);
}
function updateVisibility() {
limits.forEach(limit => {
if (limit.LimitLow < save.Nails) {
let allow = true;
if ('Other' in limit) { // loop over other restrictions
obj = limit['Other'];
Object.keys(obj).forEach(key => {
let k = save[key];
let k2 = limit.Other[key];
if (save[key] < limit.Other[key]) {
allow = false;
} // endif
}); // end foreach
} // end check other
if (allow) {
if (document.getElementById(limit.Show) != null) {
//console.log('unlock: '+ limit.Show);
if (document.getElementById(limit.Show).className == 'hidden') {
document.getElementById(limit.Show).classList.remove('hidden');
}
document.getElementById(limit.Show).style.display = "inline";
} else {
console.log('Element to unlock does not exist: ' + limit.Show);
}
} //end allow
} // end nails
}); //end foreach
}
function updateView() {
// settings
document.getElementById('TousandPoint').value = settings.TousandPoint;
document.getElementById('DecimalPoint').value = settings.DecimalPoint;
document.getElementById('Currency').value = settings.Currency;
document.getElementById('FormatExample').innerHTML = formatBigInt(123456711, 2, settings.Currency);
if (settings.Pause) document.getElementById('Pause').innerHTML = '▶ Unpause';
else document.getElementById('Pause').innerHTML = '❚❚ Pause';
//main elements
document.getElementById('Money').innerHTML = formatBigInt(save.Money, 2, settings.Currency);
document.getElementById('NailsTotal').innerHTML = formatBigInt(save.Nails);
document.getElementById('NailsProduction').innerHTML = formatBigInt(save.NailsPerTick);
document.getElementById('StorageGarageCount').innerHTML = formatBigInt(save.StorageGarage);
document.getElementById('NailsInStorage').innerHTML = formatBigInt(save.NailsInStorage);
document.getElementById('SteelbarsNo').innerHTML = formatBigInt(save.SteelbarsByK, 3);
document.getElementById('SteelBarCost').innerHTML = formatBigInt(save.SteelbarCost, 2, settings.Currency);
let storagecap = getStorageCap();
document.getElementById('NailsInStoragePercent').innerHTML = save.NailsInStorage * 100n / storagecap;
document.getElementById('SteelbarsPercent').innerHTML = save.SteelbarsByK * 100n / storagecap;
if((save.SteelbarsByK + save.NailsInStorage) * 100n / storagecap > 90) {
document.getElementById('StorageTotal').classList.add('warn');
} else {
document.getElementById('StorageTotal').classList.remove('warn');
}
if((save.NailsPerTick * 60n) > save.SteelbarsByK) {
document.getElementById('SteelbarsNo').classList.add('warn');
} else {
document.getElementById('SteelbarsNo').classList.remove('warn');
}
document.getElementById('StorageTotal').innerHTML = (save.SteelbarsByK + save.NailsInStorage) * 100n / storagecap;
document.getElementById('StorageCap').innerHTML = formatBigInt(storagecap);
document.getElementById('Price').innerHTML = formatBigInt(save.Price, 2, settings.Currency);
document.getElementById('Demand').innerHTML = formatBigInt(save.Demand, 0);
document.getElementById('AutoPressNo').innerHTML = formatBigInt(save.AutoPress, 0);
document.getElementById('AutoPressPrice').innerHTML = formatBigInt(save.AutoPressPrice, 2, settings.Currency);
document.getElementById('SalesRepCount').innerHTML = formatBigInt(save.SalesReps);
document.getElementById('SalesRepCost').innerHTML = formatBigInt(getSalesRepCost(), 2, settings.Currency);
document.getElementById('Time').innerHTML = time[save.Time].Name;
document.getElementById('Weather').innerHTML = weather[save.Weather].Name;
document.getElementById('Tick').innerHTML = formatBigInt(save.Tick);
document.getElementById('PowerConsumption').innerHTML = formatBigInt(save.PowerConsumed, 0, settings.PowerFormat);
document.getElementById('PowerCost').innerHTML = formatBigInt(save.PowerCost, 2, settings.Currency);
document.getElementById('PowerProduced').innerHTML = formatBigInt(save.PowerProduced, 0, settings.PowerFormat);
document.getElementById('PowerStored').innerHTML = formatBigInt(save.PowerStored, 0, settings.PowerFormat);
document.getElementById('PowerStoreCap').innerHTML = formatBigInt(save.PowerStoreCap, 0, settings.PowerFormat);
if(save.PowerConsumed > save.PowerProduced) {
document.getElementById('PowerProduced').classList.add('warn');
} else {
document.getElementById('PowerProduced').classList.remove('warn');
}
if(save.PowerStored < (save.PowerConsumed)) {
document.getElementById('PowerStored').classList.add('warn');
} else {
document.getElementById('PowerStored').classList.remove('warn');
}
getSalesRepCost()
console.log("did run updateView");
}
function getRandom(min = 0, max = 100) {
return Math.floor(Math.random() * (max - min)) + min;
}
function BinaryRandom(probability = 0.1) {
let rand = Math.random();
console.log('Random: ' + rand + ', Result: ' + (rand < probability));
return rand < probability;
}