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 @@
@@ -14,6 +15,7 @@ import Lobby from "./components/Lobby.vue" import WaitingRoom from "./components/WaitingRoom.vue" import Players from "../api/collections/Players"; +import Rooms from "../api/collections/Rooms"; export default { components: { @@ -22,36 +24,38 @@ export default { }, meteor: { - $subscribe: { - players: [], - }, players() { - return Players.find({}); + if (this.roomInfo === null) + return []; + return Players.find({ roomId: this.roomInfo.roomId }); + }, + currentRoom() { + if (this.roomInfo === null) + return null; + return Rooms.findOne(this.roomInfo.roomId); }, }, data() { return { - currentRoom: null, + roomInfo: null, whatScreen: Lobby, }; }, methods: { newGame: function(args) { - this.currentRoom = args; + this.roomInfo = args; this.whatScreen = WaitingRoom; - Meteor.subscribe("players", args.roomId, () => { - console.log("UPDATE", arguments); - }); + Meteor.subscribe("players", args.roomId); + Meteor.subscribe("currentRoom", args.roomId); }, joinGame: function(args) { - this.currentRoom = args; + this.roomInfo = args; this.whatScreen = WaitingRoom; - Meteor.subscribe("players", args.roomId, () => { - console.log("UPDATE", arguments); - }); + Meteor.subscribe("players", args.roomId); + Meteor.subscribe("currentRoom", args.roomId); }, } } diff --git a/imports/ui/components/Lobby.vue b/imports/ui/components/Lobby.vue index 977ceb9..a4275c8 100644 --- a/imports/ui/components/Lobby.vue +++ b/imports/ui/components/Lobby.vue @@ -59,6 +59,7 @@ export default { this.loading = true; let name = this.newGameName; Meteor.call("newGame", { name }, (err, res) => { + console.log(err, res); if (err !== undefined) { this.errorMessage = err.message; this.loading = false; diff --git a/imports/ui/components/WaitingRoom.vue b/imports/ui/components/WaitingRoom.vue index 7b52bdb..db1b06e 100644 --- a/imports/ui/components/WaitingRoom.vue +++ b/imports/ui/components/WaitingRoom.vue @@ -1,14 +1,30 @@