42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
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({
|
|
"joinGame": async ({ code, name }) => {
|
|
check(code, String);
|
|
check(name, String);
|
|
name = name.trim();
|
|
|
|
let room = Rooms.findOne({ joinCode: code.toLowerCase() });
|
|
if (room === undefined) {
|
|
throw new Meteor.Error("room-not-found");
|
|
}
|
|
|
|
if (room.state !== "waitingRoom") {
|
|
throw new Meteor.Error("room-already-playing");
|
|
}
|
|
|
|
let roomId = room._id;
|
|
if (roomId === undefined) {
|
|
throw new Meteor.Error("room-id-undefined");
|
|
}
|
|
|
|
try {
|
|
let playerId = Players.insert({ roomId, name });
|
|
} catch (e) {
|
|
if (e.code === 11000) {
|
|
throw new Meteor.Error("name-collision");
|
|
}
|
|
}
|
|
|
|
let joinCode = room.joinCode;
|
|
let players = {};
|
|
Players.find({ roomId }).forEach((doc) => {
|
|
players[doc._id] = doc.name;
|
|
});
|
|
return { joinCode, players, roomId };
|
|
}
|
|
});
|