2022-06-24 13:37:03 +00:00
|
|
|
import { IllegalAction } from "./illegalAction.js";
|
|
|
|
import { roomManager } from "../roomManager.js";
|
|
|
|
import { io } from "./io.js";
|
|
|
|
io.on('connection', function (client) {
|
|
|
|
connected(client);
|
|
|
|
});
|
|
|
|
function connected(client) {
|
|
|
|
client.sync = function () {
|
|
|
|
const tempWorld = client.game.world.obfuscate();
|
|
|
|
io.to(client.game.id).emit('sync', { world: tempWorld });
|
|
|
|
};
|
|
|
|
client.sendMeta = function () {
|
|
|
|
const roomID = client.game.id;
|
|
|
|
const roomPlayers = client.game.players; // Dont send client id's to everyone, TODO
|
|
|
|
const metadata = { players: roomPlayers, roomID: roomID };
|
|
|
|
io.to(client.game.id).emit('metadata', metadata);
|
|
|
|
const id = client.game.id;
|
|
|
|
client.emit('message', id);
|
|
|
|
};
|
|
|
|
client.game = "";
|
|
|
|
client.player = "";
|
|
|
|
client.on('disconnect', function () {
|
|
|
|
if (!roomManager.getAllPlayerIDs().includes(client.id))
|
|
|
|
return new IllegalAction(client.id, 22);
|
|
|
|
client.game.removePlayerByID(client.id);
|
|
|
|
client.sendMeta();
|
|
|
|
});
|
|
|
|
client.on('joinGame', function (data) {
|
2022-06-24 14:34:25 +00:00
|
|
|
const info = roomManager.playerJoinGame(data.room, data.name, data.color, client.id);
|
2022-06-24 13:37:03 +00:00
|
|
|
client.join(info[0].id);
|
|
|
|
client.game = info[0];
|
|
|
|
client.player = info[1];
|
|
|
|
client.sync();
|
|
|
|
client.emit('inGame', true);
|
|
|
|
client.sendMeta();
|
|
|
|
});
|
|
|
|
client.on('createGame', function (data) {
|
|
|
|
const info = roomManager.playerCreateGame(data.options, data.name, data.color, client.id);
|
|
|
|
client.join(info[0].id);
|
|
|
|
client.game = info[0];
|
|
|
|
client.player = info[1];
|
|
|
|
client.emit('inGame', true);
|
|
|
|
client.sync();
|
|
|
|
client.sendMeta();
|
|
|
|
});
|
|
|
|
client.on('leaveGame', function (data) {
|
|
|
|
client.game.removePlayerByID(client.id);
|
|
|
|
});
|
|
|
|
client.on('clickCanvas', function (data) {
|
|
|
|
if (!roomManager.getAllPlayerIDs().includes(client.id))
|
|
|
|
return new IllegalAction(client.id, 1);
|
|
|
|
client.game.world.click(data.tilePosition[0], data.tilePosition[1], data.mode, client.player);
|
|
|
|
client.sync();
|
|
|
|
});
|
|
|
|
}
|
2022-06-24 16:29:05 +00:00
|
|
|
//# sourceMappingURL=netcode.js.map
|