142 lines
3.0 KiB
JavaScript
142 lines
3.0 KiB
JavaScript
const fetch = require('node-fetch');
|
|
const WebSocket = require('ws');
|
|
|
|
const version = '0.0.1';
|
|
|
|
if (process.argv.length < 5) {
|
|
console.error(`
|
|
usage: npm start SERVER GAME-ID USER
|
|
|
|
For example:
|
|
|
|
npm start https://nuc.ketrenos.com:3000/ketr.ketran robot-wars ai-1
|
|
|
|
`);
|
|
process.exit(-1);
|
|
}
|
|
|
|
const server = process.argv[2];
|
|
const gameId = process.argv[3];
|
|
let session = undefined;
|
|
const user = process.argv[4];
|
|
|
|
const game = {};
|
|
|
|
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
|
|
|
|
const error = (e) => {
|
|
console.log(`ws - error`, e);
|
|
}
|
|
|
|
const connect = async () => {
|
|
let loc = new URL(server), new_uri;
|
|
const res = await fetch(`${server}/api/v1/games`, {
|
|
method: 'GET',
|
|
cache: 'no-cache',
|
|
credentials: 'same-origin', /* include cookies */
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
if (!res) {
|
|
throw new Error(`Unable to connect to ${server}`);
|
|
}
|
|
|
|
const { player } = JSON.parse(await res.text());
|
|
console.log(`Connecting to ${server} as ${player}`);
|
|
|
|
if (loc.protocol === "https:") {
|
|
new_uri = "wss";
|
|
} else {
|
|
new_uri = "ws";
|
|
}
|
|
new_uri = `${new_uri}://${loc.host}/ketr.ketran/api/v1/games/ws/${gameId}`;
|
|
const ws = new WebSocket(new_uri,
|
|
[],
|
|
{
|
|
'headers': {
|
|
'Cookie': `player=${player}`
|
|
}
|
|
});
|
|
return new Promise((resolve, reject) => {
|
|
const headers = (e) => {
|
|
console.log(`ws - headers`);
|
|
};
|
|
|
|
const open = (e) => {
|
|
console.log(`ws - open`);
|
|
resolve(ws);
|
|
};
|
|
|
|
const close = (e) => {
|
|
console.log(`ws - close`);
|
|
};
|
|
|
|
ws.on('open', open);
|
|
ws.on('headers', headers);
|
|
ws.on('close', close);
|
|
ws.on('error', error);
|
|
ws.on('message', (data) => { message(ws, data) });
|
|
});
|
|
};
|
|
|
|
const message = (ws, data) => {
|
|
data = JSON.parse(data);
|
|
switch (data.type) {
|
|
case 'game-update':
|
|
Object.assign(game, data.update);
|
|
if (game.name === '') {
|
|
ws.send(JSON.stringify({ type: 'player-name', name: user }));
|
|
}
|
|
if (game.state === 'lobby' && game.unselected.indexOf(user) !== -1) {
|
|
const slots = [];
|
|
for (let color in game.players) {
|
|
if (game.players[color].status === 'Not active') {
|
|
slots.push(color);
|
|
}
|
|
}
|
|
if (slots.length !== 0) {
|
|
const index = Math.floor(Math.random() * slots.length);
|
|
console.log(`Requesting to play as ${slots[index]}.`);
|
|
game.unselected = game.unselected.filter(
|
|
color => color === slots[index]);
|
|
ws.send(JSON.stringify({
|
|
type: 'set',
|
|
field: 'color',
|
|
value: slots[index]
|
|
}));
|
|
ws.send(JSON.stringify({
|
|
type: 'chat',
|
|
message: `Woohoo! Robot AI ${version} is alive!`
|
|
}));
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'ping':
|
|
break;
|
|
|
|
default:
|
|
console.log(data);
|
|
break;
|
|
}
|
|
}
|
|
|
|
const ai = async (ws) => {
|
|
ws.send(JSON.stringify({
|
|
type: 'game-update'
|
|
}));
|
|
}
|
|
|
|
connect().then((ws) => {
|
|
ai(ws).then(() => {
|
|
})
|
|
.catch((error) => {
|
|
console.error(error);
|
|
ws.close();
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.error(error);
|
|
});
|