Start game logic

This commit is contained in:
Michael Zhang 2020-11-27 02:44:06 -06:00
parent 8ed8449b11
commit cdd78585b8
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 32 additions and 14 deletions

View file

@ -24,8 +24,10 @@ Meteor.methods({
throw new Meteor.Error("room-id-undefined"); throw new Meteor.Error("room-id-undefined");
} }
let level = 2;
let playerId;
try { try {
let playerId = Players.insert({ roomId, name }); playerId = Players.insert({ roomId, name, level });
} catch (e) { } catch (e) {
if (e.code === 11000) { if (e.code === 11000) {
throw new Meteor.Error("name-collision"); throw new Meteor.Error("name-collision");
@ -33,10 +35,6 @@ Meteor.methods({
} }
let joinCode = room.joinCode; let joinCode = room.joinCode;
let players = {}; return { joinCode, playerId, roomId };
Players.find({ roomId }).forEach((doc) => {
players[doc._id] = doc.name;
});
return { joinCode, players, roomId };
} }
}); });

View file

@ -37,15 +37,14 @@ Meteor.methods({
} }
console.log("ROOM ID", roomId); console.log("ROOM ID", roomId);
let playerId = Players.insert({ roomId, name }); let level = 2;
let playerId = Players.insert({ roomId, name, level });
Rooms.update(roomId, { $set: { owner: playerId } }); Rooms.update(roomId, { $set: { owner: playerId } });
if (remainingAttempts == 0 && roomId === undefined) { if (remainingAttempts == 0 && roomId === undefined) {
throw new Meteor.Error("no-more-rooms"); throw new Meteor.Error("no-more-rooms");
} }
let players = {}; return { playerId, roomId, joinCode };
players[playerId] = name;
return { players, roomId, joinCode };
}, },
}); });

View file

@ -2,21 +2,29 @@
<div> <div>
<h1>Waiting for players...</h1> <h1>Waiting for players...</h1>
<p>Join code: {{ currentRoom.joinCode }}</p> <p>Join code: {{ currentRoom.joinCode }}</p>
<p>State: {{ currentRoom.state }}
<p>Owner: {{ playerNames[currentRoom.owner] }}</p> <p>Owner: {{ playerNames[currentRoom.owner] }}</p>
<p>Rules: <br /><pre>{{ JSON.stringify(currentRoom.ruleset, null, 2) }}</pre></p> <p>Info: <br /><pre>{{ JSON.stringify(roomInfo, null, 2) }}</pre></p>
<p>Room: <br /><pre>{{ JSON.stringify(currentRoom, null, 2) }}</pre></p>
<p> <p>
Players: Players:
<ul> <ul>
<li v-for="player in players" v-bind:key="player._id">{{ player.name }}</li> <li v-for="player in players" v-bind:key="player._id">
{{ player.name }} (level {{ player.level }})
</li>
</ul> </ul>
</p> </p>
<p><button v-if="isOwner" :disabled="!canStart">Start Game</button></p>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
props: ["currentRoom", "players", "roomInfo"], props: [
"currentRoom",
"players",
"roomInfo",
],
computed: { computed: {
playerNames: function() { playerNames: function() {
let names = new Map(); let names = new Map();
@ -25,6 +33,19 @@ export default {
} }
return names; return names;
}, },
isOwner: function() {
return this.currentRoom.owner === this.roomInfo.playerId;
},
canStart: function() {
let val =
// only the owner can start the game
this.isOwner
// TODO: actually calculate number of players based on decks?
&& this.players.length === 4;
return val;
},
}, },
} }
</script> </script>