Allow start at time

master
Wilco Kruijer 5 years ago
parent 490c09c618
commit d66f172b5d
  1. 20
      backend/src/Lobby.js
  2. 17
      backend/src/index.js
  3. 33
      frontend/package-lock.json
  4. 1
      frontend/package.json
  5. 2
      frontend/src/App.tsx
  6. 2
      frontend/src/components/Feed.tsx
  7. 32
      frontend/src/components/Lobby.tsx
  8. 7
      frontend/src/util/socket.ts

@ -17,7 +17,7 @@ module.exports = class Lobby {
timelineIndex = 0; timelineIndex = 0;
// For debugging purposes // For debugging purposes
speedFactor = 100; speedFactor = 1;
constructor(name) { constructor(name) {
this.name = name; this.name = name;
@ -68,16 +68,24 @@ module.exports = class Lobby {
}; };
if (false) { if (false) {
this.currentSeconds = 500; this.seek(ioLobby, 500);
this.startTime = Date.now() - this.currentSeconds * 1000;
this.timelineIndex = timeline.indexForTime(this.currentSeconds);
console.log("idx", this.timelineIndex);
ioLobby.emit('seek', this.currentSeconds);
} }
doTick(); doTick();
} }
/**
*
* @param ioLobby
* @param {number} time
*/
seek(ioLobby, time) {
this.currentSeconds = time;
this.startTime = Date.now() - time * 1000;
this.timelineIndex = timeline.indexForTime(this.currentSeconds);
ioLobby.emit('seek', time);
}
/** /**
* *
* @returns {boolean} * @returns {boolean}

@ -1,7 +1,7 @@
const service = require("./service.js"); const service = require("./service.js");
const state = require("./state.js"); const state = require("./state.js");
const { io } = require('./app.js'); const {io} = require('./app.js');
io.on('connection', socket => { io.on('connection', socket => {
const socketId = socket.id; const socketId = socket.id;
@ -20,7 +20,7 @@ io.on('connection', socket => {
socket.on('join_lobby', (lobbyId, callback) => { socket.on('join_lobby', (lobbyId, callback) => {
if (!callback || typeof callback !== 'function') { if (!callback || typeof callback !== 'function') {
console.error("Callback not a function."); console.error("Join: Callback not a function.");
return return
} }
@ -52,7 +52,7 @@ io.on('connection', socket => {
socket.on('lobby_info', callback => { socket.on('lobby_info', callback => {
if (!callback || typeof callback !== 'function') { if (!callback || typeof callback !== 'function') {
console.error("Callback not a function."); console.error("Lobby info: Callback not a function.");
return return
} }
@ -64,7 +64,7 @@ io.on('connection', socket => {
}); });
}); });
socket.on('request_start', () => { socket.on('request_start', (time = null) => {
console.log('request start', socket.rooms); console.log('request start', socket.rooms);
const lobby = service.getUserLobby(socketId); const lobby = service.getUserLobby(socketId);
@ -73,8 +73,13 @@ io.on('connection', socket => {
return; return;
} }
const ioLobby = io.to(lobby.name + "") const ioLobby = io.to(lobby.name + "");
ioLobby.emit('started'); ioLobby.emit('started');
lobby.run(ioLobby); lobby.run(ioLobby);
})
if (typeof time === 'number' && time) {
console.log("Starting at", time);
lobby.seek(ioLobby, time);
}
});
}); });

@ -11445,11 +11445,6 @@
"resolved": "https://registry.npmjs.org/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz", "resolved": "https://registry.npmjs.org/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz",
"integrity": "sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==" "integrity": "sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA=="
}, },
"react-radial-progress-indicator": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/react-radial-progress-indicator/-/react-radial-progress-indicator-1.4.1.tgz",
"integrity": "sha512-4FYrCtpqshAdVdKWPo+CmIVU5fALidzKrFNIB3S98JgXruts22jiqS3n0JFLW5mlYqTXUqzOQNH1m2jyWgnnzg=="
},
"react-scripts": { "react-scripts": {
"version": "3.3.1", "version": "3.3.1",
"resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-3.3.1.tgz", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-3.3.1.tgz",
@ -12165,6 +12160,26 @@
"resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-2.1.2.tgz", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-2.1.2.tgz",
"integrity": "sha512-rs9OggEUF0V4jUSecXazOYsLfu7OGK2qIn3c7IPBiffz32XniEp/TX9Xmc9LQfK2nQ2QKHvZ2oygKUGU0lG4jQ==" "integrity": "sha512-rs9OggEUF0V4jUSecXazOYsLfu7OGK2qIn3c7IPBiffz32XniEp/TX9Xmc9LQfK2nQ2QKHvZ2oygKUGU0lG4jQ=="
}, },
"serialize-query-params": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/serialize-query-params/-/serialize-query-params-0.3.0.tgz",
"integrity": "sha512-gtarsAa5B7L+v2mxbm0zTq8WjqwjgYHZmLsyvxGnlSfW2bWkokV0QKzRxLCGHF2aDqbQYCCNaj1/NP6DqbcEKQ==",
"requires": {
"query-string": "^5.0.0"
},
"dependencies": {
"query-string": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/query-string/-/query-string-5.1.1.tgz",
"integrity": "sha512-gjWOsm2SoGlgLEdAGt7a6slVOk9mGiXmPFMqrEhLQ68rhQuBnpfs3+EmlvqKyxnCo9/PPlF+9MtY02S1aFg+Jw==",
"requires": {
"decode-uri-component": "^0.2.0",
"object-assign": "^4.1.0",
"strict-uri-encode": "^1.0.0"
}
}
}
},
"serve-index": { "serve-index": {
"version": "1.9.1", "version": "1.9.1",
"resolved": "https://registry.npmjs.org/serve-index/-/serve-index-1.9.1.tgz", "resolved": "https://registry.npmjs.org/serve-index/-/serve-index-1.9.1.tgz",
@ -13641,6 +13656,14 @@
"resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz", "resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz",
"integrity": "sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==" "integrity": "sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ=="
}, },
"use-query-params": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/use-query-params/-/use-query-params-0.6.0.tgz",
"integrity": "sha512-AEZm0+TTYuV3/l/IC+Iyqkvol1muXWJd2hlgY2erFESvtZNIpU4BrsTo6jnOBFd/t6rTOQuFjKSmW/+TAy8mzQ==",
"requires": {
"serialize-query-params": "^0.3.0"
}
},
"use-socketio": { "use-socketio": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/use-socketio/-/use-socketio-2.0.0.tgz", "resolved": "https://registry.npmjs.org/use-socketio/-/use-socketio-2.0.0.tgz",

@ -17,6 +17,7 @@
"socket.io-client": "^2.3.0", "socket.io-client": "^2.3.0",
"sscaffold-css": "^0.1.0", "sscaffold-css": "^0.1.0",
"typescript": "^3.7.5", "typescript": "^3.7.5",
"use-query-params": "^0.6.0",
"use-socketio": "^2.0.0" "use-socketio": "^2.0.0"
}, },
"scripts": { "scripts": {

@ -13,6 +13,7 @@ const App = () => {
const [started, setStarted] = useState(false); const [started, setStarted] = useState(false);
const player = useRef(new Audio("centurion.m4a")); const player = useRef(new Audio("centurion.m4a"));
useEffect(() => { useEffect(() => {
socket.on('started', async () => { socket.on('started', async () => {
setStarted(true); setStarted(true);
@ -34,7 +35,6 @@ const App = () => {
}); });
return () => { return () => {
socket.off("started"); socket.off("started");
socket.off("seek"); socket.off("seek");

@ -36,7 +36,7 @@ const Feed = () => {
} }
// @ts-ignore // @ts-ignore
setFeedItems(newItems); // setFeedItems(newItems);
} }
}); });

@ -3,10 +3,12 @@ import {Card, Col, InputNumber, Row} from "antd"
import socket, {emit} from "../util/socket"; import socket, {emit} from "../util/socket";
import "../css/lobby.css"; import "../css/lobby.css";
import beer from "../img/beer.png" import beer from "../img/beer.png"
import {NumberParam, useQueryParam} from 'use-query-params';
const Lobby = () => { const Lobby = () => {
const [lobbyId, setLobbyId] = useState<number | undefined>(undefined); const [lobbyId, setLobbyId] = useQueryParam('lobby', NumberParam);
const [isLeader, setIsLeader] = useState(false); const [isLeader, setIsLeader] = useState(false);
const [seekTime, setSeekTime] = useState(0);
const [userCount, setUserCount] = useState(0); const [userCount, setUserCount] = useState(0);
async function onChangeLobbyInput(i: any) { async function onChangeLobbyInput(i: any) {
@ -19,6 +21,13 @@ const Lobby = () => {
useEffect(() => { useEffect(() => {
socket.on('welcome', async (obj: any) => { socket.on('welcome', async (obj: any) => {
if (lobbyId) {
// lobbyId is already defined, this means we have a queryparam set.
await onChangeLobbyInput(lobbyId);
return;
}
console.log("Got welcome", lobbyId);
setLobbyId(obj.lobby.name); setLobbyId(obj.lobby.name);
setIsLeader(socket.id === obj.lobby.leaderId); setIsLeader(socket.id === obj.lobby.leaderId);
setUserCount(obj.lobby.users?.length || 0); setUserCount(obj.lobby.users?.length || 0);
@ -38,6 +47,14 @@ const Lobby = () => {
wrapper(); wrapper();
}, []); }, []);
const seekItem = (
<span>
Start bij: <InputNumber size="small" min={0} max={60000} value={seekTime}
onChange={time => setSeekTime(time || 0)}/>
sec.
</span>
);
return ( return (
<div className="lobby"> <div className="lobby">
<Row> <Row>
@ -54,16 +71,19 @@ const Lobby = () => {
</Row> </Row>
<Row> <Row>
<Col className="control" span={8} offset={8}> <Col className="control" span={8} offset={8}>
<Card title={`Lobby setup (${lobbyId}):`} actions={ isLeader ? [ <Card title={`Lobby setup (${lobbyId}):`} actions={isLeader ? [
<span onClick={async () => {await emit('request_start')}}>Start</span>, <span onClick={async () => {
]: []}> await emit('request_start', seekTime)
}}>Start</span>,
] : []}>
<span> <span>
Huidige lobby <InputNumber size="small" min={100} max={100000} value={lobbyId} Huidige lobby <InputNumber size="small" min={100} max={100000} value={lobbyId}
onChange={onChangeLobbyInput}/> onChange={onChangeLobbyInput}/>
</span> </span>
<p>Er zijn {userCount} gebruiker(s) aanwezig.</p> <p>Er zijn {userCount} gebruiker(s) aanwezig.</p>
<p> {isLeader ? 'Jij bent de baas.' : 'Wachten op de baas...'}</p> <p> {isLeader ? 'Jij bent de baas.' : 'Wachten op de baas...'} </p>
{isLeader ? seekItem : ''}
</Card> </Card>
</Col> </Col>

@ -8,7 +8,6 @@ export default socket;
* Promisify emit. * Promisify emit.
* @param event * @param event
* @param arg * @param arg
* @param callback
*/ */
export function emit(event: string, arg: any = null) { export function emit(event: string, arg: any = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -20,10 +19,10 @@ export function emit(event: string, arg: any = null) {
resolve(res); resolve(res);
}; };
if (arg) { if (arg === null || typeof arg === 'undefined') {
socket.emit(event, arg, cb);
} else {
socket.emit(event, cb); socket.emit(event, cb);
} else {
socket.emit(event, arg, cb);
} }
}) })

Loading…
Cancel
Save