From 50f359c1c3d628efee14bd46cb4350b6b1362907 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Fri, 27 Nov 2020 00:36:02 -0600 Subject: [PATCH] Create new rooms --- imports/api/collections/Links.js | 3 -- imports/api/collections/Links.tests.js | 24 ----------- imports/api/collections/Players.js | 8 ++++ imports/api/collections/Rooms.js | 7 +++- imports/api/fixtures.js | 28 ------------- imports/api/methods/createLink.js | 16 ------- imports/api/methods/createLink.tests.js | 20 --------- imports/api/methods/newGame.js | 39 ++++++++++++++++-- imports/api/publications/index.js | 2 +- imports/api/publications/links.js | 6 --- imports/api/publications/links.tests.js | 22 ---------- imports/ui/App.vue | 12 ++---- imports/ui/components/Hello.vue | 27 ------------ imports/ui/components/Info.vue | 55 ------------------------- imports/ui/components/Lobby.vue | 20 ++++++--- imports/ui/components/WaitingRoom.vue | 12 ++++++ imports/ui/plugins.js | 8 ++-- package-lock.json | 5 +++ package.json | 3 +- 19 files changed, 93 insertions(+), 224 deletions(-) delete mode 100644 imports/api/collections/Links.js delete mode 100644 imports/api/collections/Links.tests.js create mode 100644 imports/api/collections/Players.js delete mode 100644 imports/api/methods/createLink.js delete mode 100644 imports/api/methods/createLink.tests.js delete mode 100644 imports/api/publications/links.js delete mode 100644 imports/api/publications/links.tests.js delete mode 100644 imports/ui/components/Hello.vue delete mode 100644 imports/ui/components/Info.vue create mode 100644 imports/ui/components/WaitingRoom.vue diff --git a/imports/api/collections/Links.js b/imports/api/collections/Links.js deleted file mode 100644 index de6a43c..0000000 --- a/imports/api/collections/Links.js +++ /dev/null @@ -1,3 +0,0 @@ -import { Mongo } from 'meteor/mongo'; - -export default new Mongo.Collection('links'); diff --git a/imports/api/collections/Links.tests.js b/imports/api/collections/Links.tests.js deleted file mode 100644 index ce17851..0000000 --- a/imports/api/collections/Links.tests.js +++ /dev/null @@ -1,24 +0,0 @@ -// Tests for the behavior of the links collection -// -// https://guide.meteor.com/testing.html - -import { Meteor } from 'meteor/meteor'; -import { assert } from 'chai'; -import Links from './Links.js'; - -if (Meteor.isServer) { - describe('links collection', function () { - it('insert correctly', function () { - const linkId = Links.insert({ - title: 'meteor homepage', - url: 'https://www.meteor.com', - }); - const added = Links.find({ _id: linkId }); - const collectionName = added._getCollectionName(); - const count = added.count(); - - assert.equal(collectionName, 'links'); - assert.equal(count, 1); - }); - }); -} diff --git a/imports/api/collections/Players.js b/imports/api/collections/Players.js new file mode 100644 index 0000000..3d306ec --- /dev/null +++ b/imports/api/collections/Players.js @@ -0,0 +1,8 @@ +import { Mongo } from "meteor/mongo"; + +let Players = new Mongo.Collection("players"); + +// let collection = Players.rawCollection(); +// collection.ensureIndex({ joinCode: 1 }, { unique: true }); + +export default Players; diff --git a/imports/api/collections/Rooms.js b/imports/api/collections/Rooms.js index 0bc3cf3..b64830c 100644 --- a/imports/api/collections/Rooms.js +++ b/imports/api/collections/Rooms.js @@ -1,3 +1,8 @@ import { Mongo } from "meteor/mongo"; -export default new Mongo.Collection("rooms"); +let Rooms = new Mongo.Collection("rooms"); + +let collection = Rooms.rawCollection(); +collection.ensureIndex({ joinCode: 1 }, { unique: true }); + +export default Rooms; diff --git a/imports/api/fixtures.js b/imports/api/fixtures.js index f629f5c..0fb5ad6 100644 --- a/imports/api/fixtures.js +++ b/imports/api/fixtures.js @@ -1,32 +1,4 @@ import { Meteor } from 'meteor/meteor'; -import Links from './collections/Links.js'; Meteor.startup(() => { - // if the Links collection is empty - if (Links.find().count() === 0) { - const data = [ - { - title: 'Do the Tutorial', - url: 'https://www.meteor.com/try', - createdAt: new Date(), - }, - { - title: 'Follow the Guide', - url: 'http://guide.meteor.com', - createdAt: new Date(), - }, - { - title: 'Read the Docs', - url: 'https://docs.meteor.com', - createdAt: new Date(), - }, - { - title: 'Discussions', - url: 'https://forums.meteor.com', - createdAt: new Date(), - }, - ]; - - data.forEach(link => Links.insert(link)); - } }); diff --git a/imports/api/methods/createLink.js b/imports/api/methods/createLink.js deleted file mode 100644 index 876f710..0000000 --- a/imports/api/methods/createLink.js +++ /dev/null @@ -1,16 +0,0 @@ -import { Meteor } from 'meteor/meteor'; -import { check } from 'meteor/check'; -import Links from '../collections/Links.js'; - -Meteor.methods({ - 'createLink'(title, url) { - check(url, String); - check(title, String); - - return Links.insert({ - url, - title, - createdAt: new Date(), - }); - }, -}); diff --git a/imports/api/methods/createLink.tests.js b/imports/api/methods/createLink.tests.js deleted file mode 100644 index d899a73..0000000 --- a/imports/api/methods/createLink.tests.js +++ /dev/null @@ -1,20 +0,0 @@ -import { Meteor } from 'meteor/meteor'; -import { assert } from 'chai'; -import Links from '../collections/Links.js'; -import './methods.js'; - -if (Meteor.isServer) { - describe('method: createLink', function () { - beforeEach(function () { - Links.remove({}); - }); - - it('can add a new link', function () { - const addLink = Meteor.server.method_handlers['createLink']; - - addLink.apply({}, ['meteor.com', 'https://www.meteor.com']); - - assert.equal(Links.find().count(), 1); - }); - }); -} diff --git a/imports/api/methods/newGame.js b/imports/api/methods/newGame.js index 2a02f98..da5f9f0 100644 --- a/imports/api/methods/newGame.js +++ b/imports/api/methods/newGame.js @@ -1,9 +1,40 @@ -import { Meteor } from 'meteor/meteor'; -import { check } from 'meteor/check'; +import { Meteor } from "meteor/meteor"; +import { Random } from "meteor/random"; +import { check } from "meteor/check"; +import Rooms from "../collections/Rooms.js"; +import Players from "../collections/Players.js"; Meteor.methods({ - "newGame"({ name }) { + "newGame": async ({ name }) => { check(name, String); - console.log("name", name); + let roomId = null; + let state = "waitingRoom"; + + // attempt to get a valid room code 10 times + let remainingAttempts = 10; + let joinCode; + while (roomId === null && remainingAttempts > 0) { + joinCode = Random.hexString(6); + let started = new Date(); + + try { + let result = Rooms.insert({ joinCode, started, state }); + roomId = result; + break; + } catch (e) { + // BulkWriteError + if (e.code === 11000) { + remainingAttempts -= 1; + } + } + } + + let playerId = Players.insert({ roomId, name }); + + if (remainingAttempts == 0 && roomId === null) { + return "failed"; + } + + return { playerId, roomId, joinCode }; }, }); diff --git a/imports/api/publications/index.js b/imports/api/publications/index.js index d161b58..3588be7 100644 --- a/imports/api/publications/index.js +++ b/imports/api/publications/index.js @@ -1 +1 @@ -import './links' +// import './links' diff --git a/imports/api/publications/links.js b/imports/api/publications/links.js deleted file mode 100644 index f085d75..0000000 --- a/imports/api/publications/links.js +++ /dev/null @@ -1,6 +0,0 @@ -import { Meteor } from 'meteor/meteor'; -import Links from '../collections/Links.js'; - -Meteor.publish('links', function () { - return Links.find(); -}); diff --git a/imports/api/publications/links.tests.js b/imports/api/publications/links.tests.js deleted file mode 100644 index 37f337c..0000000 --- a/imports/api/publications/links.tests.js +++ /dev/null @@ -1,22 +0,0 @@ -import { assert } from 'chai' -import { PublicationCollector } from 'meteor/johanbrook:publication-collector' -import Links from '../collections/Links.js' -import './publications.js' - -describe('Publish links', function () { - beforeEach(function () { - Links.remove({}) - Links.insert({ - title: 'meteor homepage', - url: 'https://www.meteor.com' - }) - }) - - it('sends all links', function (done) { - const collector = new PublicationCollector() - collector.collect('links', (collections) => { - assert.equal(collections.links.length, 1) - done() - }) - }) -}) diff --git a/imports/ui/App.vue b/imports/ui/App.vue index 784629f..16617e5 100644 --- a/imports/ui/App.vue +++ b/imports/ui/App.vue @@ -5,14 +5,10 @@ diff --git a/imports/ui/components/Hello.vue b/imports/ui/components/Hello.vue deleted file mode 100644 index 64947eb..0000000 --- a/imports/ui/components/Hello.vue +++ /dev/null @@ -1,27 +0,0 @@ - - - - - diff --git a/imports/ui/components/Info.vue b/imports/ui/components/Info.vue deleted file mode 100644 index 376d605..0000000 --- a/imports/ui/components/Info.vue +++ /dev/null @@ -1,55 +0,0 @@ - - - - - diff --git a/imports/ui/components/Lobby.vue b/imports/ui/components/Lobby.vue index 601d668..30e7b9b 100644 --- a/imports/ui/components/Lobby.vue +++ b/imports/ui/components/Lobby.vue @@ -3,8 +3,15 @@

LOBBY

- - + +

or

@@ -23,15 +30,18 @@ export default { data() { return { + loading: false, newGameName: "", }; }, methods: { - newGame: (evt) => { + newGame: function (evt) { + loading = true; let name = this.newGameName; - console.log("name", name); - Meteor.call("newGame", { name }); + Meteor.call("newGame", { name }, (err, res) => { + console.log(err, res); + }); } } } diff --git a/imports/ui/components/WaitingRoom.vue b/imports/ui/components/WaitingRoom.vue new file mode 100644 index 0000000..283df85 --- /dev/null +++ b/imports/ui/components/WaitingRoom.vue @@ -0,0 +1,12 @@ + + + + + diff --git a/imports/ui/plugins.js b/imports/ui/plugins.js index eb976c9..eff6717 100644 --- a/imports/ui/plugins.js +++ b/imports/ui/plugins.js @@ -1,4 +1,6 @@ -import Vue from 'vue' -import VueMeteorTracker from 'vue-meteor-tracker' +import Vue from "vue"; +import Vuex from "vuex"; +import VueMeteorTracker from "vue-meteor-tracker"; -Vue.use(VueMeteorTracker) +Vue.use(VueMeteorTracker); +Vue.use(Vuex); diff --git a/package-lock.json b/package-lock.json index 5730d6e..db1c6c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -594,6 +594,11 @@ "requires": { "lodash.omit": "^4.5.0" } + }, + "vuex": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/vuex/-/vuex-3.6.0.tgz", + "integrity": "sha512-W74OO2vCJPs9/YjNjW8lLbj+jzT24waTo2KShI8jLvJW8OaIkgb3wuAMA7D+ZiUxDOx3ubwSZTaJBip9G8a3aQ==" } } } diff --git a/package.json b/package.json index 78f186e..f6c30f6 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "@babel/runtime": "^7.11.2", "meteor-node-stubs": "^1.0.1", "vue": "^2.6.12", - "vue-meteor-tracker": "^2.0.0-beta.5" + "vue-meteor-tracker": "^2.0.0-beta.5", + "vuex": "^3.6.0" }, "meteor": { "mainModule": {