From 161fd4afab323ca6bf491def473f84bb7557b481 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 7 Mar 2018 17:48:32 +0900 Subject: wip --- src/api/endpoints/othello/games.ts | 22 ++++++++++++++++++++++ src/api/endpoints/othello/invitations.ts | 11 +++++++++++ src/api/endpoints/othello/match.ts | 22 +++++++++------------- src/api/endpoints/othello/match/cancel.ts | 9 +++++++++ 4 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 src/api/endpoints/othello/games.ts create mode 100644 src/api/endpoints/othello/invitations.ts create mode 100644 src/api/endpoints/othello/match/cancel.ts (limited to 'src/api/endpoints/othello') diff --git a/src/api/endpoints/othello/games.ts b/src/api/endpoints/othello/games.ts new file mode 100644 index 0000000000..62388b01d4 --- /dev/null +++ b/src/api/endpoints/othello/games.ts @@ -0,0 +1,22 @@ +import $ from 'cafy'; +import Game, { pack } from '../../models/othello-game'; + +module.exports = (params, user) => new Promise(async (res, rej) => { + // Get 'my' parameter + const [my = false, myErr] = $(params.my).boolean().$; + if (myErr) return rej('invalid my param'); + + const q = my ? { + $or: [{ + black_user_id: user._id + }, { + white_user_id: user._id + }] + } : {}; + + // Fetch games + const games = await Game.find(q); + + // Reponse + res(Promise.all(games.map(async (g) => await pack(g, user)))); +}); diff --git a/src/api/endpoints/othello/invitations.ts b/src/api/endpoints/othello/invitations.ts new file mode 100644 index 0000000000..f462ef0bf9 --- /dev/null +++ b/src/api/endpoints/othello/invitations.ts @@ -0,0 +1,11 @@ +import Matching, { pack as packMatching } from '../../models/othello-matching'; + +module.exports = (params, user) => new Promise(async (res, rej) => { + // Find session + const invitations = await Matching.find({ + child_id: user._id + }); + + // Reponse + res(Promise.all(invitations.map(async (i) => await packMatching(i, user)))); +}); diff --git a/src/api/endpoints/othello/match.ts b/src/api/endpoints/othello/match.ts index 2dc22d11f9..65243a5571 100644 --- a/src/api/endpoints/othello/match.ts +++ b/src/api/endpoints/othello/match.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; -import Matching from '../../models/othello-matchig'; -import Game, { pack } from '../../models/othello-game'; +import Matching, { pack as packMatching } from '../../models/othello-matching'; +import Game, { pack as packGame } from '../../models/othello-game'; import User from '../../models/user'; import { publishOthelloStream } from '../../event'; @@ -33,17 +33,14 @@ module.exports = (params, user) => new Promise(async (res, rej) => { created_at: new Date(), black_user_id: parentIsBlack ? exist.parent_id : user._id, white_user_id: parentIsBlack ? user._id : exist.parent_id, + turn_user_id: parentIsBlack ? exist.parent_id : user._id, logs: [] }); - const packedGame = await pack(game); - // Reponse - res(packedGame); + res(await packGame(game, user)); - publishOthelloStream(exist.parent_id, 'matched', { - game - }); + publishOthelloStream(exist.parent_id, 'matched', await packGame(game, exist.parent_id)); } else { // Fetch child const child = await User.findOne({ @@ -64,17 +61,16 @@ module.exports = (params, user) => new Promise(async (res, rej) => { }); // セッションを作成 - await Matching.insert({ + const matching = await Matching.insert({ + created_at: new Date(), parent_id: user._id, child_id: child._id }); // Reponse - res(204); + res(); // 招待 - publishOthelloStream(child._id, 'invited', { - user_id: user._id - }); + publishOthelloStream(child._id, 'invited', await packMatching(matching, child)); } }); diff --git a/src/api/endpoints/othello/match/cancel.ts b/src/api/endpoints/othello/match/cancel.ts new file mode 100644 index 0000000000..6f751ef835 --- /dev/null +++ b/src/api/endpoints/othello/match/cancel.ts @@ -0,0 +1,9 @@ +import Matching from '../../../models/othello-matching'; + +module.exports = (params, user) => new Promise(async (res, rej) => { + await Matching.remove({ + parent_id: user._id + }); + + res(); +}); -- cgit v1.2.3-freya