summaryrefslogtreecommitdiff
path: root/src/api/endpoints/othello
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-03-07 17:48:32 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-03-07 17:48:32 +0900
commit161fd4afab323ca6bf491def473f84bb7557b481 (patch)
tree4c8a2215dc9d3e3b817bbd82ca9b8f88fb0a0420 /src/api/endpoints/othello
parentwip (diff)
downloadsharkey-161fd4afab323ca6bf491def473f84bb7557b481.tar.gz
sharkey-161fd4afab323ca6bf491def473f84bb7557b481.tar.bz2
sharkey-161fd4afab323ca6bf491def473f84bb7557b481.zip
wip
Diffstat (limited to 'src/api/endpoints/othello')
-rw-r--r--src/api/endpoints/othello/games.ts22
-rw-r--r--src/api/endpoints/othello/invitations.ts11
-rw-r--r--src/api/endpoints/othello/match.ts22
-rw-r--r--src/api/endpoints/othello/match/cancel.ts9
4 files changed, 51 insertions, 13 deletions
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();
+});