summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/othello
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-03-29 14:51:06 +0900
committerGitHub <noreply@github.com>2018-03-29 14:51:06 +0900
commit0b5597c873d2d9d45be94a18e1b74f44d9925185 (patch)
tree8b4dac3a56cf703650c8207f9279028a8560a96b /src/server/api/endpoints/othello
parentoops (diff)
parentResolve conflicts (diff)
downloadmisskey-0b5597c873d2d9d45be94a18e1b74f44d9925185.tar.gz
misskey-0b5597c873d2d9d45be94a18e1b74f44d9925185.tar.bz2
misskey-0b5597c873d2d9d45be94a18e1b74f44d9925185.zip
Merge pull request #1332 from syuilo/pr/1327
Pr/1327
Diffstat (limited to 'src/server/api/endpoints/othello')
-rw-r--r--src/server/api/endpoints/othello/games.ts62
-rw-r--r--src/server/api/endpoints/othello/games/show.ts32
-rw-r--r--src/server/api/endpoints/othello/invitations.ts15
-rw-r--r--src/server/api/endpoints/othello/match.ts95
-rw-r--r--src/server/api/endpoints/othello/match/cancel.ts9
5 files changed, 213 insertions, 0 deletions
diff --git a/src/server/api/endpoints/othello/games.ts b/src/server/api/endpoints/othello/games.ts
new file mode 100644
index 0000000000..37fa384189
--- /dev/null
+++ b/src/server/api/endpoints/othello/games.ts
@@ -0,0 +1,62 @@
+import $ from 'cafy';
+import OthelloGame, { pack } from '../../models/othello-game';
+
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'my' parameter
+ const [my = false, myErr] = $(params.my).optional.boolean().$;
+ if (myErr) return rej('invalid my param');
+
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'sinceId' parameter
+ const [sinceId, sinceIdErr] = $(params.sinceId).optional.id().$;
+ if (sinceIdErr) return rej('invalid sinceId param');
+
+ // Get 'untilId' parameter
+ const [untilId, untilIdErr] = $(params.untilId).optional.id().$;
+ 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 OthelloGame.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/othello/games/show.ts b/src/server/api/endpoints/othello/games/show.ts
new file mode 100644
index 0000000000..f9084682fa
--- /dev/null
+++ b/src/server/api/endpoints/othello/games/show.ts
@@ -0,0 +1,32 @@
+import $ from 'cafy';
+import OthelloGame, { pack } from '../../../models/othello-game';
+import Othello from '../../../../common/othello/core';
+
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'gameId' parameter
+ const [gameId, gameIdErr] = $(params.gameId).id().$;
+ if (gameIdErr) return rej('invalid gameId param');
+
+ const game = await OthelloGame.findOne({ _id: gameId });
+
+ if (game == null) {
+ return rej('game not found');
+ }
+
+ const o = new Othello(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/othello/invitations.ts b/src/server/api/endpoints/othello/invitations.ts
new file mode 100644
index 0000000000..f6e0071a6c
--- /dev/null
+++ b/src/server/api/endpoints/othello/invitations.ts
@@ -0,0 +1,15 @@
+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({
+ 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/othello/match.ts b/src/server/api/endpoints/othello/match.ts
new file mode 100644
index 0000000000..f503c5834c
--- /dev/null
+++ b/src/server/api/endpoints/othello/match.ts
@@ -0,0 +1,95 @@
+import $ from 'cafy';
+import Matching, { pack as packMatching } from '../../models/othello-matching';
+import OthelloGame, { pack as packGame } from '../../models/othello-game';
+import User from '../../models/user';
+import publishUserStream, { publishOthelloStream } from '../../event';
+import { eighteight } from '../../../common/othello/maps';
+
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [childId, childIdErr] = $(params.userId).id().$;
+ 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 OthelloGame.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));
+
+ publishOthelloStream(exist.parentId, 'matched', await packGame(game, exist.parentId));
+
+ const other = await Matching.count({
+ childId: user._id
+ });
+
+ if (other == 0) {
+ publishUserStream(user._id, 'othello_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);
+
+ // 招待
+ publishOthelloStream(child._id, 'invited', packed);
+
+ publishUserStream(child._id, 'othello_invited', packed);
+ }
+});
diff --git a/src/server/api/endpoints/othello/match/cancel.ts b/src/server/api/endpoints/othello/match/cancel.ts
new file mode 100644
index 0000000000..ee0f82a611
--- /dev/null
+++ b/src/server/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({
+ parentId: user._id
+ });
+
+ res();
+});