Actually get both sides to update

This commit is contained in:
Michael Zhang 2020-11-27 01:34:12 -06:00
parent 4ba0f326bd
commit f5fdd499e0
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
8 changed files with 44 additions and 12 deletions

View file

@ -2,7 +2,12 @@ import { Mongo } from "meteor/mongo";
let Players = new Mongo.Collection("players"); let Players = new Mongo.Collection("players");
// TODO: there's probably a better way to do this
try {
let collection = Players.rawCollection(); let collection = Players.rawCollection();
collection.ensureIndex({ name: 1, roomId: 1, }, { unique: true }); collection.ensureIndex({ name: 1, roomId: 1, }, { unique: true });
} catch (e) {
}
export default Players; export default Players;

View file

@ -2,7 +2,11 @@ import { Mongo } from "meteor/mongo";
let Rooms = new Mongo.Collection("rooms"); let Rooms = new Mongo.Collection("rooms");
try {
let collection = Rooms.rawCollection(); let collection = Rooms.rawCollection();
collection.ensureIndex({ joinCode: 1 }, { unique: true }); collection.ensureIndex({ joinCode: 1 }, { unique: true });
} catch (e) {
}
export default Rooms; export default Rooms;

View file

@ -10,7 +10,7 @@ Meteor.methods({
check(name, String); check(name, String);
name = name.trim(); name = name.trim();
let room = Rooms.findOne({ joinCode: code }); let room = Rooms.findOne({ joinCode: code.toLowerCase() });
if (room === undefined) { if (room === undefined) {
throw new Meteor.Error("room-not-found"); throw new Meteor.Error("room-not-found");
} }

View file

@ -16,7 +16,7 @@ Meteor.methods({
let remainingAttempts = 10; let remainingAttempts = 10;
let joinCode; let joinCode;
while (roomId === null && remainingAttempts > 0) { while (roomId === null && remainingAttempts > 0) {
joinCode = Random.hexString(6); joinCode = Random.hexString(6).toLowerCase();
let started = new Date(); let started = new Date();
try { try {

View file

@ -1 +1 @@
// import './links' import "./players";

View file

@ -0,0 +1,6 @@
import { Meteor } from "meteor/meteor";
import Players from "../collections/Players.js";
Meteor.publish("players", (roomId) => {
return Players.find({ roomId });
});

View file

@ -2,7 +2,8 @@
<div> <div>
<component <component
v-bind:is="whatScreen" v-bind:is="whatScreen"
v-bind="currentRoom" v-bind:currentRoom="currentRoom"
v-bind:players="players"
v-on:newGame="newGame" v-on:newGame="newGame"
v-on:joinGame="joinGame" v-on:joinGame="joinGame"
></component> ></component>
@ -10,8 +11,9 @@
</template> </template>
<script> <script>
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";
export default { export default {
components: { components: {
@ -19,6 +21,15 @@ export default {
WaitingRoom, WaitingRoom,
}, },
meteor: {
$subscribe: {
players: [],
},
players() {
return Players.find({});
},
},
data() { data() {
return { return {
currentRoom: null, currentRoom: null,
@ -30,11 +41,17 @@ export default {
newGame: function(args) { newGame: function(args) {
this.currentRoom = args; this.currentRoom = args;
this.whatScreen = WaitingRoom; this.whatScreen = WaitingRoom;
Meteor.subscribe("players", args.roomId, () => {
console.log("UPDATE", arguments);
});
}, },
joinGame: function(args) { joinGame: function(args) {
this.currentRoom = args; this.currentRoom = args;
this.whatScreen = WaitingRoom; this.whatScreen = WaitingRoom;
Meteor.subscribe("players", args.roomId, () => {
console.log("UPDATE", arguments);
});
}, },
} }
} }

View file

@ -1,14 +1,14 @@
<template> <template>
<div> <div>
<h1>Waiting for players...</h1> <h1>Waiting for players...</h1>
<p>Join code: {{ joinCode.toUpperCase() }}</p> <p>Join code: {{ currentRoom.joinCode.toUpperCase() }}</p>
<p>Players: {{ players }}</p> <p>Players: {{ players }}</p>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
props: ["joinCode", "players"], props: ["currentRoom", "players"],
} }
</script> </script>