Joining games works!
This commit is contained in:
parent
f5fdd499e0
commit
43065902ae
8 changed files with 58 additions and 22 deletions
|
@ -20,6 +20,10 @@ Meteor.methods({
|
||||||
}
|
}
|
||||||
|
|
||||||
let roomId = room._id;
|
let roomId = room._id;
|
||||||
|
if (roomId === undefined) {
|
||||||
|
throw new Meteor.Error("room-id-undefined");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let playerId = Players.insert({ roomId, name });
|
let playerId = Players.insert({ roomId, name });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -5,17 +5,17 @@ import Rooms from "../collections/Rooms.js";
|
||||||
import Players from "../collections/Players.js";
|
import Players from "../collections/Players.js";
|
||||||
|
|
||||||
Meteor.methods({
|
Meteor.methods({
|
||||||
"newGame": async ({ name }) => {
|
"newGame": ({ name }) => {
|
||||||
check(name, String);
|
check(name, String);
|
||||||
name = name.trim();
|
name = name.trim();
|
||||||
|
|
||||||
let roomId = null;
|
let roomId = undefined;
|
||||||
let state = "waitingRoom";
|
let state = "waitingRoom";
|
||||||
|
|
||||||
// attempt to get a valid room code 10 times
|
// attempt to get a valid room code 10 times
|
||||||
let remainingAttempts = 10;
|
let remainingAttempts = 10;
|
||||||
let joinCode;
|
let joinCode;
|
||||||
while (roomId === null && remainingAttempts > 0) {
|
while (roomId === undefined && remainingAttempts > 0) {
|
||||||
joinCode = Random.hexString(6).toLowerCase();
|
joinCode = Random.hexString(6).toLowerCase();
|
||||||
let started = new Date();
|
let started = new Date();
|
||||||
|
|
||||||
|
@ -26,17 +26,18 @@ Meteor.methods({
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// BulkWriteError
|
// BulkWriteError
|
||||||
if (e.code === 11000) {
|
if (e.code === 11000) {
|
||||||
remainingAttempts -= 1;
|
|
||||||
} else {
|
} else {
|
||||||
console.log("UNCAUGHT", e);
|
console.log("UNCAUGHT", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
remainingAttempts -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("ROOM ID", roomId);
|
||||||
let playerId = Players.insert({ roomId, name });
|
let playerId = Players.insert({ roomId, name });
|
||||||
Rooms.update(roomId, { $set: { owner: playerId } });
|
Rooms.update(roomId, { $set: { owner: playerId } });
|
||||||
|
|
||||||
if (remainingAttempts == 0 && roomId === null) {
|
if (remainingAttempts == 0 && roomId === undefined) {
|
||||||
throw new Meteor.Error("no-more-rooms");
|
throw new Meteor.Error("no-more-rooms");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
import "./players";
|
import "./players";
|
||||||
|
import "./rooms";
|
||||||
|
|
|
@ -2,5 +2,8 @@ import { Meteor } from "meteor/meteor";
|
||||||
import Players from "../collections/Players.js";
|
import Players from "../collections/Players.js";
|
||||||
|
|
||||||
Meteor.publish("players", (roomId) => {
|
Meteor.publish("players", (roomId) => {
|
||||||
|
if (roomId === undefined) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
return Players.find({ roomId });
|
return Players.find({ roomId });
|
||||||
});
|
});
|
||||||
|
|
6
imports/api/publications/rooms.js
Normal file
6
imports/api/publications/rooms.js
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { Meteor } from "meteor/meteor";
|
||||||
|
import Rooms from "../collections/Rooms.js";
|
||||||
|
|
||||||
|
Meteor.publish("currentRoom", (roomId) => {
|
||||||
|
return Rooms.find(roomId);
|
||||||
|
});
|
|
@ -2,8 +2,9 @@
|
||||||
<div>
|
<div>
|
||||||
<component
|
<component
|
||||||
v-bind:is="whatScreen"
|
v-bind:is="whatScreen"
|
||||||
v-bind:currentRoom="currentRoom"
|
v-bind:roomInfo="roomInfo"
|
||||||
v-bind:players="players"
|
v-bind:players="players"
|
||||||
|
v-bind:currentRoom="currentRoom"
|
||||||
v-on:newGame="newGame"
|
v-on:newGame="newGame"
|
||||||
v-on:joinGame="joinGame"
|
v-on:joinGame="joinGame"
|
||||||
></component>
|
></component>
|
||||||
|
@ -14,6 +15,7 @@
|
||||||
import Lobby from "./components/Lobby.vue"
|
import Lobby from "./components/Lobby.vue"
|
||||||
import WaitingRoom from "./components/WaitingRoom.vue"
|
import WaitingRoom from "./components/WaitingRoom.vue"
|
||||||
import Players from "../api/collections/Players";
|
import Players from "../api/collections/Players";
|
||||||
|
import Rooms from "../api/collections/Rooms";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
|
@ -22,36 +24,38 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
meteor: {
|
meteor: {
|
||||||
$subscribe: {
|
|
||||||
players: [],
|
|
||||||
},
|
|
||||||
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() {
|
data() {
|
||||||
return {
|
return {
|
||||||
currentRoom: null,
|
roomInfo: null,
|
||||||
whatScreen: Lobby,
|
whatScreen: Lobby,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
newGame: function(args) {
|
newGame: function(args) {
|
||||||
this.currentRoom = args;
|
this.roomInfo = args;
|
||||||
this.whatScreen = WaitingRoom;
|
this.whatScreen = WaitingRoom;
|
||||||
Meteor.subscribe("players", args.roomId, () => {
|
Meteor.subscribe("players", args.roomId);
|
||||||
console.log("UPDATE", arguments);
|
Meteor.subscribe("currentRoom", args.roomId);
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
joinGame: function(args) {
|
joinGame: function(args) {
|
||||||
this.currentRoom = args;
|
this.roomInfo = args;
|
||||||
this.whatScreen = WaitingRoom;
|
this.whatScreen = WaitingRoom;
|
||||||
Meteor.subscribe("players", args.roomId, () => {
|
Meteor.subscribe("players", args.roomId);
|
||||||
console.log("UPDATE", arguments);
|
Meteor.subscribe("currentRoom", args.roomId);
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,6 +59,7 @@ export default {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
let name = this.newGameName;
|
let name = this.newGameName;
|
||||||
Meteor.call("newGame", { name }, (err, res) => {
|
Meteor.call("newGame", { name }, (err, res) => {
|
||||||
|
console.log(err, res);
|
||||||
if (err !== undefined) {
|
if (err !== undefined) {
|
||||||
this.errorMessage = err.message;
|
this.errorMessage = err.message;
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
|
|
|
@ -1,14 +1,30 @@
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<h1>Waiting for players...</h1>
|
<h1>Waiting for players...</h1>
|
||||||
<p>Join code: {{ currentRoom.joinCode.toUpperCase() }}</p>
|
<p>Join code: {{ currentRoom.joinCode }}</p>
|
||||||
<p>Players: {{ players }}</p>
|
<p>State: {{ currentRoom.state }}
|
||||||
|
<p>Owner: {{ playerNames[currentRoom.owner] }}</p>
|
||||||
|
<p>
|
||||||
|
Players:
|
||||||
|
<ul>
|
||||||
|
<li v-for="player in players" v-bind:key="player._id">{{ player.name }}</li>
|
||||||
|
</ul>
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
export default {
|
export default {
|
||||||
props: ["currentRoom", "players"],
|
props: ["currentRoom", "players", "roomInfo"],
|
||||||
|
computed: {
|
||||||
|
playerNames: function() {
|
||||||
|
let names = new Map();
|
||||||
|
for (let player of this.players) {
|
||||||
|
names[player._id] = player.name;
|
||||||
|
}
|
||||||
|
return names;
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue