summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/games
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-07-07 19:01:33 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-07-07 19:01:33 +0900
commit865fd25af1cb2d9d1fd9752795f36482226d36a7 (patch)
tree75045ec1b717ef4a5708aff1c85443427a1a7376 /src/server/api/endpoints/games
parent:v: (diff)
downloadmisskey-865fd25af1cb2d9d1fd9752795f36482226d36a7.tar.gz
misskey-865fd25af1cb2d9d1fd9752795f36482226d36a7.tar.bz2
misskey-865fd25af1cb2d9d1fd9752795f36482226d36a7.zip
Refactorijg
Diffstat (limited to 'src/server/api/endpoints/games')
-rw-r--r--src/server/api/endpoints/games/reversi/games.ts63
-rw-r--r--src/server/api/endpoints/games/reversi/games/show.ts33
-rw-r--r--src/server/api/endpoints/games/reversi/invitations.ts16
-rw-r--r--src/server/api/endpoints/games/reversi/match.ts95
-rw-r--r--src/server/api/endpoints/games/reversi/match/cancel.ts10
5 files changed, 217 insertions, 0 deletions
diff --git a/src/server/api/endpoints/games/reversi/games.ts b/src/server/api/endpoints/games/reversi/games.ts
new file mode 100644
index 0000000000..585e833ef1
--- /dev/null
+++ b/src/server/api/endpoints/games/reversi/games.ts
@@ -0,0 +1,63 @@
+import $ from 'cafy'; import ID from '../../../../../cafy-id';
+import ReversiGame, { pack } from '../../../../../models/games/reversi/game';
+import { ILocalUser } from '../../../../../models/user';
+
+export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
+ // Get 'my' parameter
+ const [my = false, myErr] = $.bool.optional.get(params.my);
+ if (myErr) return rej('invalid my param');
+
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit);
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'sinceId' parameter
+ const [sinceId, sinceIdErr] = $.type(ID).optional.get(params.sinceId);
+ if (sinceIdErr) return rej('invalid sinceId param');
+
+ // Get 'untilId' parameter
+ const [untilId, untilIdErr] = $.type(ID).optional.get(params.untilId);
+ if (untilIdErr) return rej('invalid untilId param');
+
+ // Check if both of sinceId and untilId is specified
+ if (sinceId && untilId) {
+ return rej('cannot set sinceId and untilId');
+ }
+
+ const q: any = my ? {
+ isStarted: true,
+ $or: [{
+ user1Id: user._id
+ }, {
+ user2Id: user._id
+ }]
+ } : {
+ isStarted: true
+ };
+
+ const sort = {
+ _id: -1
+ };
+
+ if (sinceId) {
+ sort._id = 1;
+ q._id = {
+ $gt: sinceId
+ };
+ } else if (untilId) {
+ q._id = {
+ $lt: untilId
+ };
+ }
+
+ // Fetch games
+ const games = await ReversiGame.find(q, {
+ sort,
+ limit
+ });
+
+ // Reponse
+ res(Promise.all(games.map(async (g) => await pack(g, user, {
+ detail: false
+ }))));
+});
diff --git a/src/server/api/endpoints/games/reversi/games/show.ts b/src/server/api/endpoints/games/reversi/games/show.ts
new file mode 100644
index 0000000000..62c37f8e0a
--- /dev/null
+++ b/src/server/api/endpoints/games/reversi/games/show.ts
@@ -0,0 +1,33 @@
+import $ from 'cafy'; import ID from '../../../../../../cafy-id';
+import ReversiGame, { pack } from '../../../../../../models/games/reversi/game';
+import Reversi from '../../../../../../games/reversi/core';
+import { ILocalUser } from '../../../../../../models/user';
+
+export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
+ // Get 'gameId' parameter
+ const [gameId, gameIdErr] = $.type(ID).get(params.gameId);
+ if (gameIdErr) return rej('invalid gameId param');
+
+ const game = await ReversiGame.findOne({ _id: gameId });
+
+ if (game == null) {
+ return rej('game not found');
+ }
+
+ const o = new Reversi(game.settings.map, {
+ isLlotheo: game.settings.isLlotheo,
+ canPutEverywhere: game.settings.canPutEverywhere,
+ loopedBoard: game.settings.loopedBoard
+ });
+
+ game.logs.forEach(log => {
+ o.put(log.color, log.pos);
+ });
+
+ const packed = await pack(game, user);
+
+ res(Object.assign({
+ board: o.board,
+ turn: o.turn
+ }, packed));
+});
diff --git a/src/server/api/endpoints/games/reversi/invitations.ts b/src/server/api/endpoints/games/reversi/invitations.ts
new file mode 100644
index 0000000000..c6d0ecfa73
--- /dev/null
+++ b/src/server/api/endpoints/games/reversi/invitations.ts
@@ -0,0 +1,16 @@
+import Matching, { pack as packMatching } from '../../../../../models/games/reversi/matching';
+import { ILocalUser } from '../../../../../models/user';
+
+export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
+ // Find session
+ const invitations = await Matching.find({
+ childId: user._id
+ }, {
+ sort: {
+ _id: -1
+ }
+ });
+
+ // Reponse
+ res(Promise.all(invitations.map(async (i) => await packMatching(i, user))));
+});
diff --git a/src/server/api/endpoints/games/reversi/match.ts b/src/server/api/endpoints/games/reversi/match.ts
new file mode 100644
index 0000000000..77c014143a
--- /dev/null
+++ b/src/server/api/endpoints/games/reversi/match.ts
@@ -0,0 +1,95 @@
+import $ from 'cafy'; import ID from '../../../../../cafy-id';
+import Matching, { pack as packMatching } from '../../../../../models/games/reversi/matching';
+import ReversiGame, { pack as packGame } from '../../../../../models/games/reversi/game';
+import User, { ILocalUser } from '../../../../../models/user';
+import publishUserStream, { publishReversiStream } from '../../../../../publishers/stream';
+import { eighteight } from '../../../../../games/reversi/maps';
+
+export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [childId, childIdErr] = $.type(ID).get(params.userId);
+ if (childIdErr) return rej('invalid userId param');
+
+ // Myself
+ if (childId.equals(user._id)) {
+ return rej('invalid userId param');
+ }
+
+ // Find session
+ const exist = await Matching.findOne({
+ parentId: childId,
+ childId: user._id
+ });
+
+ if (exist) {
+ // Destroy session
+ Matching.remove({
+ _id: exist._id
+ });
+
+ // Create game
+ const game = await ReversiGame.insert({
+ createdAt: new Date(),
+ user1Id: exist.parentId,
+ user2Id: user._id,
+ user1Accepted: false,
+ user2Accepted: false,
+ isStarted: false,
+ isEnded: false,
+ logs: [],
+ settings: {
+ map: eighteight.data,
+ bw: 'random',
+ isLlotheo: false
+ }
+ });
+
+ // Reponse
+ res(await packGame(game, user));
+
+ publishReversiStream(exist.parentId, 'matched', await packGame(game, exist.parentId));
+
+ const other = await Matching.count({
+ childId: user._id
+ });
+
+ if (other == 0) {
+ publishUserStream(user._id, 'reversi_no_invites');
+ }
+ } else {
+ // Fetch child
+ const child = await User.findOne({
+ _id: childId
+ }, {
+ fields: {
+ _id: true
+ }
+ });
+
+ if (child === null) {
+ return rej('user not found');
+ }
+
+ // 以前のセッションはすべて削除しておく
+ await Matching.remove({
+ parentId: user._id
+ });
+
+ // セッションを作成
+ const matching = await Matching.insert({
+ createdAt: new Date(),
+ parentId: user._id,
+ childId: child._id
+ });
+
+ // Reponse
+ res();
+
+ const packed = await packMatching(matching, child);
+
+ // 招待
+ publishReversiStream(child._id, 'invited', packed);
+
+ publishUserStream(child._id, 'reversi_invited', packed);
+ }
+});
diff --git a/src/server/api/endpoints/games/reversi/match/cancel.ts b/src/server/api/endpoints/games/reversi/match/cancel.ts
new file mode 100644
index 0000000000..84cd1ff119
--- /dev/null
+++ b/src/server/api/endpoints/games/reversi/match/cancel.ts
@@ -0,0 +1,10 @@
+import Matching from '../../../../../../models/games/reversi/matching';
+import { ILocalUser } from '../../../../../../models/user';
+
+export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
+ await Matching.remove({
+ parentId: user._id
+ });
+
+ res();
+});