-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase.js
169 lines (156 loc) · 4.99 KB
/
base.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
162
163
164
165
166
167
168
169
const http = require("http");
fs = require("fs");
// ms before a new chunk is sent. The lower, the faster values update.
// The higher, the slower; however, HTML will aggregate more slowly (good).
const BASE_REPEAT_MS = 1200;
const PORT = 1234;
// Init all game data
let cookies = -1; // -1 because the initial req to /cookie will increment this to 0
let store = {
// upgrades: {
// RIF: {
// basePrice: 100,
// owned: 0,
// },
// CTPC: {
// basePrice: 500,
// owned: 0,
// },
// },
items: {
clicker: {
basePrice: 15,
owned: 0,
},
grandma: {
basePrice: 100,
owned: 0,
},
farm: {
basePrice: 1100,
owned: 0,
},
// mine: {
// basePrice: 12000,
// owned: 0,
// },
// factory: 0,
},
};
const auto_clicks = () => {
setTimeout(() => {
cookies +=
store.items.clicker.owned * 0.1 +
store.items.grandma.owned * 1 +
store.items.farm.owned * 8;
auto_clicks();
}, 1000);
};
auto_clicks();
// (TODO) bool which will be used to stop chunk loops once the user quits the game.
let clientConnectionStopped = false;
// Create the http server
http
.createServer((req, res) => {
// Specify which endpoints. We cut off the parameters because we don't use them (and the form submit auto-adds them).
if (req.url.startsWith("/purchase")) {
let pick = req.url.split("-")[1].split("?")[0];
res.writeHead(204);
res.end();
let price = Math.ceil(
store.items[pick].basePrice *
(store.items[pick].owned == 0 ? 1 : store.items[pick].owned * 1.15)
);
if (cookies >= price) {
cookies -= price;
store.items[pick].owned += 1;
}
return;
}
if (req.url.startsWith("/buy")) {
let pick = req.url.split("-")[1].split("?")[0];
res.end(
"<b>" +
pick.charAt(0).toUpperCase() +
pick.slice(1) +
"</b>" +
fs.readFileSync("./buy-item-footer.html") +
pick +
'"></iframe>'
);
return;
}
switch (req.url.split("?")[0]) {
case "/":
res.writeHead(200, { "Content-Type": "text/html" });
res.write(fs.readFileSync("./index.html"));
let loop = 0;
// Set up a loop of chunks, so that we can slowly provide up-to-date information
const loop_update = () => {
setTimeout(() => {
// Add a chunk of updated HTML, along with extra CSS to hide the outdated HTML
res.write(
// LIST OF EXISTING 1CHAR ELS = abgipqsu
// EXPLANATION: This is the "data stream". What it does is
// feed new information into the DOM, including a stylesheet
// to hide the outdated info. Each item has a weird element
// name, to save data transfer.
`<c id="c${loop}">${Math.floor(cookies)}</c><d id="a${loop}">${
store.items.clicker.owned
}</d><e id="b${loop}">${Math.ceil(
store.items.clicker.basePrice *
(store.items.clicker.owned == 0
? 1
: store.items.clicker.owned * 1.15)
)}</e><f id="a${loop}">${
store.items.grandma.owned
}</f><h id="b${loop}">${Math.ceil(
store.items.grandma.basePrice *
(store.items.grandma.owned == 0
? 1
: store.items.grandma.owned * 1.15)
)}</h><l id="a${loop}">${
store.items.farm.owned
}</l><j id="b${loop}">${Math.ceil(
store.items.farm.basePrice *
(store.items.farm.owned == 0
? 1
: store.items.farm.owned * 1.15)
)}</j>
<style>#a${loop - 1},#b${loop - 1},#c${loop - 1},#d${
loop - 1
},#e${loop - 1},#f${loop - 1},#h${loop - 1},#j${
loop - 1
}{display:none}</style>`
);
// increment loop for next iteration to hide
loop++;
// Re-call the same function [TODO: only if the client still is connected]
if (!clientConnectionStopped) loop_update();
}, BASE_REPEAT_MS);
};
loop_update();
break;
case "/cookie":
// Server endpoint used only to count up the number of cookie clicks.
res.writeHead(204);
res.end();
cookies += 1;
break;
case "/cookieButton":
res.writeHead(200);
res.end(fs.readFileSync("./cookieButton.html"));
break;
case "/cookie.webp":
res.writeHead(200);
res.end(fs.readFileSync("./cookie.webp"));
break;
default:
// just try to grab the file requested. [TODO - replace this with a 404 and its dependents with specified endpoints]
res.writeHead(404);
res.end("Not Found");
break;
}
})
.listen(PORT);
console.log("Listening: " + PORT);