diff --git a/scripts/main.js b/scripts/main.js index 1aba262..bd501d1 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -2,13 +2,13 @@ import { system, world } from "@minecraft/server"; import utils from "utils"; import { websocket } from "@minecraft/server-net"; import { InitPlayer } from "entities/player"; -import { interval, roomCode } from "properties"; +import { roomCode } from "properties"; class LagoonAddons { constructor() { this.timer = 0; world.afterEvents.worldLoad.subscribe(() => system.run(() => this.initEarlyExecution())); } - async initEarlyExecution() { + async connect() { this.ws = await websocket.connect('wss://lagoon.under-scape.com/ws/rooms/' + roomCode + '/plugins/proximity'); this.ws.afterEvents.message.subscribe((event) => { try { @@ -26,9 +26,17 @@ class LagoonAddons { } }); this.ws.afterEvents.close.subscribe((event) => { - utils.logStaff(`§cConnexion à lagoon fermé ! (${event})`); - console.log(`Connexion à lagoon fermé ! (${event})`); + utils.logStaff(`§cConnection to Lagoon closed. (${event})`); + console.log(`Connection to Lagoon closed. (${event})`); + utils.logStaff(`§eReconnecting in 5 seconds.`); + console.log(`Reconnecting in 5 seconds.`); + system.runTimeout(() => { + this.connect(); + }, 20 * 5); }); + } + async initEarlyExecution() { + await this.connect(); world.getAllPlayers().forEach((p) => InitPlayer(p)); world.afterEvents.playerSpawn.subscribe((e) => { system.runTimeout(() => { @@ -40,23 +48,20 @@ class LagoonAddons { console.log('Lagoon loaded !'); } async clock() { - this.timer++; - // 1 seconde - if (this.timer % interval === 0) { - if (this.ws.isOpen) { - const players = world.getAllPlayers().filter((p) => p.initialized).map((p) => { - return { - ...p.location, - code: p.lagoonCode, - muted: p.hasTag('muted') - }; - }); - this.ws.send(JSON.stringify({ - type: 'positions', - uuid: utils.makeid(100), - data: players - })); - } + this.timer += 20; + if (this.ws.isOpen) { + const players = world.getAllPlayers().filter((p) => p.initialized).map((p) => { + return { + ...p.location, + code: p.lagoonCode, + muted: p.hasTag('muted') + }; + }); + this.ws.send(JSON.stringify({ + type: 'positions', + uuid: utils.makeid(100), + data: players + })); } } } diff --git a/src/main.ts b/src/main.ts index aa86dbb..2d1e3c1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,31 +9,42 @@ class LagoonAddons { private ws!: WebSocketClient; public timer: number = 0; - constructor () { + constructor() { world.afterEvents.worldLoad.subscribe(() => system.run(() => this.initEarlyExecution())); } - async initEarlyExecution() { - this.ws = await websocket.connect('wss://lagoon.under-scape.com/ws/rooms/'+roomCode+'/plugins/proximity'); + async connect() { + this.ws = await websocket.connect('wss://lagoon.under-scape.com/ws/rooms/' + roomCode + '/plugins/proximity'); + this.ws.afterEvents.message.subscribe((event) => { try { const payload = JSON.parse(event.message); const type = payload.type; const data = payload.data; - if(type == 'status' && data == 'connected !') { + if (type == 'status' && data == 'connected !') { utils.logStaff('§aLagoon connected !'); console.log('Lagoon connected !') } } catch (error) { - utils.logStaff('ERROR With Payload : '+event.message); - console.log('ERROR With Payload : '+event.message) + utils.logStaff('ERROR With Payload : ' + event.message); + console.log('ERROR With Payload : ' + event.message) } }); this.ws.afterEvents.close.subscribe((event) => { - utils.logStaff(`§cConnexion à lagoon fermé ! (${event})`); - console.log(`Connexion à lagoon fermé ! (${event})`) + utils.logStaff(`§cConnection to Lagoon closed. (${event})`); + console.log(`Connection to Lagoon closed. (${event})`); + + utils.logStaff(`§eReconnecting in 5 seconds.`); + console.log(`Reconnecting in 5 seconds.`); + system.runTimeout(() => { + this.connect(); + }, 20 * 5); }); + } + + async initEarlyExecution() { + await this.connect(); world.getAllPlayers().forEach((p) => InitPlayer(p)); world.afterEvents.playerSpawn.subscribe((e) => { @@ -50,27 +61,24 @@ class LagoonAddons { async clock() { - this.timer++; + this.timer += 20; - // 1 seconde - if (this.timer % interval === 0) { - - if (this.ws.isOpen) { - const players: {x: number, y: number, z: number, code: string, muted: boolean}[] = world.getAllPlayers().filter((p) => p.initialized).map((p) => { - return { - ...p.location, - code: p.lagoonCode, - muted: p.hasTag('muted') - } - }); + if (this.ws.isOpen) { + const players: { x: number, y: number, z: number, code: string, muted: boolean }[] = world.getAllPlayers().filter((p) => p.initialized).map((p) => { + return { + ...p.location, + code: p.lagoonCode, + muted: p.hasTag('muted') + } + }); - this.ws.send(JSON.stringify({ - type: 'positions', - uuid: utils.makeid(100), - data: players - })); - } + this.ws.send(JSON.stringify({ + type: 'positions', + uuid: utils.makeid(100), + data: players + })); } + } }