-
Notifications
You must be signed in to change notification settings - Fork 1
/
trading_game.html
220 lines (208 loc) · 7.63 KB
/
trading_game.html
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="jatloesmile-hd.png">
<title>Trading Game</title>
<style>
.side{
display: inline;
}
body,p,button {
font-family: 'Monospace Typewriter', monospace;
font-size: 15px;
}
button {
margin-top: 5px;
margin-bottom: 5px;
}
#left {
width: 50%;
float: left;
margin-top: -18px;
}
#right {
margin-left: 50%;
width: 50%;
}
#refreshButton {
margin-top: 0px;
}
</style>
</head>
<body>
<p>V1.10</p>
<div>
<div id="left">
<p style="font-size:18px;"><b>Inventory</b></p>
<div id="inv_displays"></div>
<br>
<button id="export" onclick="exportToClipboard()">Export Save to Clipboard</button>
</div>
<div id="right">
<p style="font-size:18px;" class="side"><b>Trades</b></p>
<button id="refreshButton" onclick="updateTrades()">Refresh</button>
<div id="trades"></div>
</div>
</div>
</body>
<script>
function funnyRound(x) {
return Math.round(x*1000)/1000;
}
//Basic setup
let MAXNUM = 3;
let DEFAULT_VALUES = [1,1];
for (let i = 0; i < 24; i++) DEFAULT_VALUES.push(DEFAULT_VALUES[DEFAULT_VALUES.length-1] + DEFAULT_VALUES[DEFAULT_VALUES.length-2]);
let LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
let inventory = [10,10];
for (let i = 0; i < 24; i++) inventory.push(0);
let trade_rates = [];
for (let i = 0; i < 26; i++) {
let stuff = [];
for (let j = 0; j < 26; j++) stuff.push(funnyRound(1.01*DEFAULT_VALUES[i]/DEFAULT_VALUES[j]));
trade_rates.push(stuff);
}
window.addEventListener("load", (event) => {
for (let i = 0; i < 26; i++) {
let newString = LETTERS[i] + ": " + make_string(inventory[i]);
let newThing = document.createTextNode(newString);
let newThingy = document.createElement("p");
newThingy.id = LETTERS[i] + "_display";
newThingy.appendChild(newThing);
if (i >= MAXNUM) newThingy.style = "display:none;";
document.getElementById("inv_displays").appendChild(newThingy);
}
for (let i = 0; i < 26; i++) {
for (let j = 0; j < 26; j++) {
if (i == j) continue;
let newString = LETTERS[i] + " -> " + LETTERS[j] + ": " + make_string(trade_rates[i][j]);
let newThing = document.createTextNode(newString);
let newThingy = document.createElement("button");
newThingy.id = LETTERS[i] + LETTERS[j] + "_trade";
newThingy.addEventListener("click", function(e) {
trade(i,j);
}, false);
newThingy.addEventListener("contextmenu", function(e) {
e.preventDefault();
tradeAll(i,j);
return false;
}, false);
newThingy.appendChild(newThing);
let newFunny = document.createElement("div");
newFunny.appendChild(newThingy);
newFunny.id = LETTERS[i] + LETTERS[j] + "_container";
newFunny.style = "display:none;"
document.getElementById("trades").appendChild(newFunny);
}
}
updateTrades();
});
function exportToClipboard() {
navigator.clipboard.writeText("inventory = [" + inventory + "]; MAXNUM = " + MAXNUM + "; updateTrades();");
}
function setButtonsDisabled(shouldBeDisabled) {
document.getElementById("refreshButton").disabled = shouldBeDisabled;
// for (let i = 0; i < 26; i++) {
// for (let j = 0; j < 26; j++) {
// if (i == j) continue;
// document.getElementById(LETTERS[i]+LETTERS[j]+"_trade").disabled = shouldBeDisabled;
// }
// }
}
function updateStuff() {
for (let i = 0; i < 26; i++) {
document.getElementById(LETTERS[i]+"_display").innerHTML = LETTERS[i] + ": " + make_string(inventory[i]);
if (i < MAXNUM) document.getElementById(LETTERS[i]+"_display").style.display = "";
else document.getElementById(LETTERS[i]+"_display").style.display = "none";
}
for (let i = 0; i < 26; i++) {
for (let j = 0; j < 26; j++) {
if (i == j) continue;
document.getElementById(LETTERS[i]+LETTERS[j]+"_trade").innerHTML = LETTERS[i] + " -> " + LETTERS[j] + ": " + make_string(trade_rates[i][j]);
}
}
}
// setInterval(updateTrades,10000);
function updateTrades() {
for (let i = 0; i < 26; i++) {
for (let j = 0; j < 26; j++) {
trade_rates[i][j] *= Math.random()*0.1+0.95;
}
}
updateStuff();
for (let i = 0; i < 26; i++) {
for (let j = 0; j < 26; j++) {
if (i == j) continue;
document.getElementById(LETTERS[i]+LETTERS[j]+"_container").style.display = "none";
}
}
let sobad = [-1];
for (let aa = 0; aa < Math.min(MAXNUM*(MAXNUM-1)/2,MAXNUM); aa++) {
let curr = -1;
let i = 0;
let j = 0;
while (sobad.includes(curr)) {
i = Math.floor(MAXNUM*Math.random());
j = Math.floor(MAXNUM*Math.random());
while (i == j) j = Math.floor(MAXNUM*Math.random());
curr = i*(10**12)+j;
}
document.getElementById(LETTERS[i]+LETTERS[j]+"_container").style.display = "";
sobad.push(i*(10**12)+j);
sobad.push(j*(10**12)+i);
}
for (let i = 0; i < 26; i++) {
for (let j = 0; j < 26; j++) {
if (i == j) continue;
document.getElementById(LETTERS[i]+LETTERS[j]+"_trade").innerHTML = LETTERS[i] + " -> " + LETTERS[j] + ": " + make_string(trade_rates[i][j]);
}
}
setButtonsDisabled(true);
setTimeout(() => {
setButtonsDisabled(false);
}, 500);
}
//Hotkeys, keycodes are here https://www.toptal.com/developers/keycode
document.onkeyup = function(e) {
if (e.which == 82) {
// R
if (!document.getElementById("refreshButton").disabled) updateTrades();
}
};
function trade(a,b) {
if (inventory[a] <= 0) return;
let tradeAmt = 1;
if (inventory[a] < 1) tradeAmt = inventory[a];
inventory[a] -= tradeAmt;
inventory[b] += tradeAmt*trade_rates[a][b];
if ((a == MAXNUM - 1 || b == MAXNUM - 1) && MAXNUM < 26) MAXNUM++;
updateStuff();
}
function tradeAll(a,b) {
if (inventory[a] <= 0) return;
let tradeAmt = inventory[a];
inventory[a] -= tradeAmt;
inventory[b] += tradeAmt*trade_rates[a][b];
if ((a == MAXNUM - 1 || b == MAXNUM - 1) && MAXNUM < 26) MAXNUM++;
updateStuff();
}
//This function turns numbers into nice numbers(make_string(1234567)=1.234e6)
function make_string(number) {
if(number<1000000) {
return number.toFixed(6);
} else if(isFinite(number)) {
let power = Math.floor(Math.log10(number));
number = number/Math.pow(10,power);
return number.toFixed(6) + "e" + String(power);
} else if(isNaN(number)) {
return "NaN";
} else {
return "Infinity";
}
}
//Log function because I can't find it anywhere
function log(a,b) {
return Math.round(Math.log(b) / Math.log(a));
}
</script>
</html>