wip
This commit is contained in:
22
src/api/endpoints/othello/games.ts
Normal file
22
src/api/endpoints/othello/games.ts
Normal file
@@ -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))));
|
||||
});
|
||||
11
src/api/endpoints/othello/invitations.ts
Normal file
11
src/api/endpoints/othello/invitations.ts
Normal file
@@ -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))));
|
||||
});
|
||||
@@ -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));
|
||||
}
|
||||
});
|
||||
|
||||
9
src/api/endpoints/othello/match/cancel.ts
Normal file
9
src/api/endpoints/othello/match/cancel.ts
Normal file
@@ -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();
|
||||
});
|
||||
Reference in New Issue
Block a user