diff --git a/imports/api/methods/joinGame.js b/imports/api/methods/joinGame.js index f498b13..0be8ec1 100644 --- a/imports/api/methods/joinGame.js +++ b/imports/api/methods/joinGame.js @@ -20,6 +20,10 @@ Meteor.methods({ } let roomId = room._id; + if (roomId === undefined) { + throw new Meteor.Error("room-id-undefined"); + } + try { let playerId = Players.insert({ roomId, name }); } catch (e) { diff --git a/imports/api/methods/newGame.js b/imports/api/methods/newGame.js index 378ee8f..0ebbb62 100644 --- a/imports/api/methods/newGame.js +++ b/imports/api/methods/newGame.js @@ -5,17 +5,17 @@ import Rooms from "../collections/Rooms.js"; import Players from "../collections/Players.js"; Meteor.methods({ - "newGame": async ({ name }) => { + "newGame": ({ name }) => { check(name, String); name = name.trim(); - let roomId = null; + let roomId = undefined; let state = "waitingRoom"; // attempt to get a valid room code 10 times let remainingAttempts = 10; let joinCode; - while (roomId === null && remainingAttempts > 0) { + while (roomId === undefined && remainingAttempts > 0) { joinCode = Random.hexString(6).toLowerCase(); let started = new Date(); @@ -26,17 +26,18 @@ Meteor.methods({ } catch (e) { // BulkWriteError if (e.code === 11000) { - remainingAttempts -= 1; } else { console.log("UNCAUGHT", e); } } + remainingAttempts -= 1; } + console.log("ROOM ID", roomId); let playerId = Players.insert({ roomId, name }); Rooms.update(roomId, { $set: { owner: playerId } }); - if (remainingAttempts == 0 && roomId === null) { + if (remainingAttempts == 0 && roomId === undefined) { throw new Meteor.Error("no-more-rooms"); } diff --git a/imports/api/publications/index.js b/imports/api/publications/index.js index 615d60b..c18d786 100644 --- a/imports/api/publications/index.js +++ b/imports/api/publications/index.js @@ -1 +1,2 @@ import "./players"; +import "./rooms"; diff --git a/imports/api/publications/players.js b/imports/api/publications/players.js index 611d227..3bfe2de 100644 --- a/imports/api/publications/players.js +++ b/imports/api/publications/players.js @@ -2,5 +2,8 @@ import { Meteor } from "meteor/meteor"; import Players from "../collections/Players.js"; Meteor.publish("players", (roomId) => { + if (roomId === undefined) { + return []; + } return Players.find({ roomId }); }); diff --git a/imports/api/publications/rooms.js b/imports/api/publications/rooms.js new file mode 100644 index 0000000..5c0100c --- /dev/null +++ b/imports/api/publications/rooms.js @@ -0,0 +1,6 @@ +import { Meteor } from "meteor/meteor"; +import Rooms from "../collections/Rooms.js"; + +Meteor.publish("currentRoom", (roomId) => { + return Rooms.find(roomId); +}); diff --git a/imports/ui/App.vue b/imports/ui/App.vue index b4ea005..c75e4e9 100644 --- a/imports/ui/App.vue +++ b/imports/ui/App.vue @@ -2,8 +2,9 @@
Join code: {{ currentRoom.joinCode.toUpperCase() }}
-Players: {{ players }}
+Join code: {{ currentRoom.joinCode }}
+State: {{ currentRoom.state }} +
Owner: {{ playerNames[currentRoom.owner] }}
++ Players: +