-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag1NodeScript.mjs
151 lines (133 loc) · 3.95 KB
/
flag1NodeScript.mjs
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
import fetch from 'node-fetch';
import io from 'socket.io-client';
function assert(condition, message) {
if (!condition) {
throw new Error(message ?? "Assertion failed");
}
}
async function main() {
// const domain = 'localhost:8080'; // 'ap.amongst.chal.pwni.ng'; //
const domain = 'ap.amongst.chal.pwni.ng'; // 'ap.amongst.chal.pwni.ng'; //
assert(process.argv.length === 3, "Usage: node test.mjs <roomId>");
let roomId = process.argv[2];
let res = await fetch('http://' + domain + '/api/info/' + roomId);
let props = await res.json();
console.log(props)
const url = `ws://${props.host || domain.split(':').shift()}:${props.port}`;
const socket = io(url, {
transports: ["websocket"],
query: {
game: roomId,
name: "Special"
}
});
await new Promise((resolve, reject) => {
socket.on('connect', resolve);
socket.on('error', reject);
});
console.log('hi')
setInterval(() => socket.emit("ping", () => null), 2000);
let game = null;
let localTick = 0;
let time = 0;
let syncEvents = [];
function updateTick(flush = false) {
let nt = Date.now();
let delta = Math.round((nt - time) / 50);
if (flush && delta < 1) delta = 1;
localTick += delta;
time = nt;
}
function waitForSync() {
return new Promise(resolve => {
syncEvents.push(resolve);
});
}
socket.on('sync', (sync) => {
game = sync;
console.log('sync', game.self.id);
localTick = game.nextPlayableTick + 10;
time = Date.now();
for (let event of syncEvents) event();
syncEvents = [];
});
socket.on('update', (update) => {
if (update.tick >= localTick) {
localTick = update.tick + 5;
time = Date.now();
}
});
socket.emit('join');
console.log('dd');
async function delay(time) {
await new Promise(resolve => {
setTimeout(resolve, time);
});
}
function sendAction(action) {
updateTick(true);
socket.emit("action", {
syncId: game.id,
tick: Math.round(localTick),
action: action
});
}
async function moveToS(x, idx) {
let limit = 0.5; // + 1e-8;
while (game.self.location[idx] !== x) {
let delta = Math.min(limit, Math.max(-limit, x - game.self.location[idx]));
let deltas = [0, 0];
deltas[idx] = delta;
sendAction({
delta: deltas
});
game.self.location[idx] += delta;
console.log(game.self.location, deltas);
await delay(50);
}
}
async function moveTo(x, y, orderSwap = false) {
if (orderSwap) {
await moveToS(y, 1);
await moveToS(x, 0);
} else {
await moveToS(x, 0);
await moveToS(y, 1);
}
}
socket.on("hello", (data) => {
console.log(data);
});
let times = 5;
while (true) {
await waitForSync();
if (game.level.map !== 'dropship') break;
await moveTo(27.5, 9, true);
console.log('spec');
times--;
if (times === 0) {
socket.emit('requestSync');
times = 5;
}
socket.emit('special');
await delay(200);
}
console.log('enter');
await delay(4000);
for (let i = 0; i < (1000 / 50 * 3); i++) {
sendAction({
delta: [0, 0],
bonusAction: {
kind: "Interact",
system: 4,
device: "emergency-button"
}
})
await delay(50);
}
while (true) {
socket.emit('special');
await delay(1000);
}
}
main();