-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_requests.js
156 lines (133 loc) · 4.57 KB
/
simulate_requests.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
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const https = require('https');
// Constants
const ENDPOINT = "https://ds-api.tonk.gg";
const PLAYER_IDS = [0,1,2,3,4,5,6,7,8,9].map((n) => 'staticplayerid' + n);
// const PLAYER_IDS = [0].map((n) => 'staticplayerid' + n);
const NUM_WORKERS = 10;
function httpRequest(url, options = { method: 'GET', body: null, headers: {} }) {
return new Promise((resolve, reject) => {
const req = https.request(url, options, (res) => {
let responseBody = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
responseBody += chunk;
});
res.on('end', () => {
resolve({ statusCode: res.statusCode, headers: res.headers, body: responseBody });
});
});
req.on('error', (err) => {
reject(err);
});
if (options.body) {
req.write(options.body);
}
req.end();
});
}
// Function to create a new worker thread with unique player ID
function createWorker(playerId) {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, { workerData: { playerId } });
worker.on('message', (msg) => {
console.log('Main thread received message from worker:', msg);
});
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
} else {
resolve();
}
});
});
}
async function registerPlayer(id, mobileUnitId, displayName, hash, secret) {
var raw = JSON.stringify({
id: id,
mobile_unit_id: mobileUnitId,
display_name: displayName
})
var requestOptions = {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: raw
};
try {
// let response = await httpRequest(`${ENDPOINT}/player/${id}?secret_key=${secret}&onchain_hash=${hash}`, requestOptions)
let response = await httpRequest(`${ENDPOINT}/player/${id}`, requestOptions)
// let text = await response.text();
} catch (e) {
console.log(e);
}
}
async function main() {
try {
// Here, instead of registering a single player, you might want to register each player with a unique ID.
// If registering is a common action for all players regardless of their IDs, you might want to loop over the PLAYER_IDS array and register each one.
PLAYER_IDS.forEach((playerId, i) => {
registerPlayer(playerId, i, playerId, 'someHash', 'someSecret');
})
// Spawn worker threads with unique player IDs
const workers = PLAYER_IDS.map(playerId => createWorker(playerId));
// Wait for all workers to be done
await Promise.all(workers);
} catch (error) {
console.error('Error in main function:', error);
}
}
async function getTask(playerId) {
try {
let response = await httpRequest(`${ENDPOINT}/task?player_id=${playerId}&secret_key=fff`);
// let text = await response.text();
// return JSON.parse(text);
} catch (e) {
console.log(e);
}
}
async function getGame() {
try {
let response = await httpRequest(`${ENDPOINT}/game`);
// let raw = await response.text();
return JSON.parse(response.body);
} catch (e) {
console.log(e);
return (`{ "status": "GameServerDown" }`)
}
}
async function getPlayer(id) {
try {
let response = await httpRequest(`${ENDPOINT}/player/${id}`)
// let raw = await response.text();
// return JSON.parse(raw);
} catch (e) {
console.log(e);
}
}
// If we're in the main thread, start the main function
if (isMainThread) {
main();
} else {
// This is the worker thread part. It will be executed in separate threads
const tick = async () => {
try {
// Execute the tasks every 2 seconds
const player = await getPlayer(workerData.playerId); // using the unique player ID passed to the worker
const game = await getGame();
if (game.status = "Tasks") {
const task = await getTask(workerData.playerId);
}
// Send some data to the main thread if needed
// parentPort.postMessage({ player, game, task });
} catch (e) {
console.error('Error in worker thread:', e);
}
// Schedule the next tick
setTimeout(tick, 2000);
};
// Start the tick function
tick();
}