Fix multiple lobbies interleaving

master
Wilco Kruijer 4 years ago
parent 8482deb7e5
commit 3e514d3857
  1. 13
      backend/src/Lobby.js
  2. 7
      backend/src/index.js
  3. 2
      frontend/src/Wrapper.tsx
  4. 2
      frontend/src/components/NextShot.tsx

@ -23,7 +23,7 @@ module.exports = class Lobby {
this.name = name; this.name = name;
} }
run(ioLobby) { run(io) {
this.running = true; this.running = true;
this.startTime = Date.now(); this.startTime = Date.now();
@ -33,13 +33,12 @@ module.exports = class Lobby {
return; return;
} }
const timestamp = timeline.getIndex(this.timelineIndex); const timestamp = timeline.getIndex(this.timelineIndex);
const nextShot = timeline.getNextShot(this.timelineIndex); const nextShot = timeline.getNextShot(this.timelineIndex);
if (!timestamp) { if (!timestamp) {
// We are done. // We are done.
ioLobby.emit('tick_event', { io.to(this.name + "").emit('tick_event', {
current: this.currentSeconds current: this.currentSeconds
}); });
console.log("Done"); console.log("Done");
@ -49,7 +48,7 @@ module.exports = class Lobby {
console.log("ticking", this.currentSeconds); console.log("ticking", this.currentSeconds);
ioLobby.emit('tick_event', { io.to(this.name + "").emit('tick_event', {
current: this.currentSeconds, current: this.currentSeconds,
next: timestamp, next: timestamp,
nextShot: nextShot nextShot: nextShot
@ -72,14 +71,14 @@ module.exports = class Lobby {
/** /**
* *
* @param ioLobby * @param io
* @param {number} time * @param {number} time
*/ */
seek(ioLobby, time) { seek(io, time) {
this.currentSeconds = time; this.currentSeconds = time;
this.startTime = Date.now() - time * 1000; this.startTime = Date.now() - time * 1000;
this.timelineIndex = timeline.indexForTime(this.currentSeconds); this.timelineIndex = timeline.indexForTime(this.currentSeconds);
ioLobby.emit('seek', time); io.to(this.name + "").emit('seek', time);
} }
/** /**

@ -69,13 +69,12 @@ io.on('connection', socket => {
return; return;
} }
const ioLobby = io.to(lobby.name + ""); io.to(lobby.name + "").emit('started');
ioLobby.emit('started'); lobby.run(io);
lobby.run(ioLobby);
if (typeof time === 'number' && time) { if (typeof time === 'number' && time) {
console.log("Starting at", time); console.log("Starting at", time);
lobby.seek(ioLobby, time); lobby.seek(io, time);
} }
}); });
}); });

@ -35,7 +35,7 @@ const Wrapper = () => {
// Won't play sound so we don't get DOM Interaction errors. // Won't play sound so we don't get DOM Interaction errors.
setWantsToStart(true); setWantsToStart(true);
} }
}, []); }, [noSound]);
useSocket("tick_event", async (tick: Tick) => { useSocket("tick_event", async (tick: Tick) => {
if (!started && wantsToStart) { if (!started && wantsToStart) {

@ -15,6 +15,8 @@ const NextShot = () => {
return; return;
} }
console.log("Tick event from NextShot");
if (fullTime.current === 0) { if (fullTime.current === 0) {
fullTime.current = tick.nextShot.timestamp - tick.current; fullTime.current = tick.nextShot.timestamp - tick.current;
} }

Loading…
Cancel
Save