From 1ef66c962a1cea81dee4f5db32cd011feac7de44 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 17 Jun 2018 08:10:54 +0900 Subject: reversi :white_flower: :100: --- src/server/api/endpoints.ts | 10 +- src/server/api/endpoints/othello/games.ts | 62 ----- src/server/api/endpoints/othello/games/show.ts | 32 --- src/server/api/endpoints/othello/invitations.ts | 15 - src/server/api/endpoints/othello/match.ts | 95 ------- src/server/api/endpoints/othello/match/cancel.ts | 9 - src/server/api/endpoints/reversi/games.ts | 62 +++++ src/server/api/endpoints/reversi/games/show.ts | 32 +++ src/server/api/endpoints/reversi/invitations.ts | 15 + src/server/api/endpoints/reversi/match.ts | 95 +++++++ src/server/api/endpoints/reversi/match/cancel.ts | 9 + src/server/api/stream/othello-game.ts | 333 ----------------------- src/server/api/stream/othello.ts | 29 -- src/server/api/stream/reversi-game.ts | 333 +++++++++++++++++++++++ src/server/api/stream/reversi.ts | 29 ++ src/server/api/streaming.ts | 10 +- 16 files changed, 585 insertions(+), 585 deletions(-) delete mode 100644 src/server/api/endpoints/othello/games.ts delete mode 100644 src/server/api/endpoints/othello/games/show.ts delete mode 100644 src/server/api/endpoints/othello/invitations.ts delete mode 100644 src/server/api/endpoints/othello/match.ts delete mode 100644 src/server/api/endpoints/othello/match/cancel.ts create mode 100644 src/server/api/endpoints/reversi/games.ts create mode 100644 src/server/api/endpoints/reversi/games/show.ts create mode 100644 src/server/api/endpoints/reversi/invitations.ts create mode 100644 src/server/api/endpoints/reversi/match.ts create mode 100644 src/server/api/endpoints/reversi/match/cancel.ts delete mode 100644 src/server/api/stream/othello-game.ts delete mode 100644 src/server/api/stream/othello.ts create mode 100644 src/server/api/stream/reversi-game.ts create mode 100644 src/server/api/stream/reversi.ts (limited to 'src/server') diff --git a/src/server/api/endpoints.ts b/src/server/api/endpoints.ts index 5f0a020d6f..a5d13b0237 100644 --- a/src/server/api/endpoints.ts +++ b/src/server/api/endpoints.ts @@ -245,27 +245,27 @@ const endpoints: Endpoint[] = [ }, { - name: 'othello/match', + name: 'reversi/match', withCredential: true }, { - name: 'othello/match/cancel', + name: 'reversi/match/cancel', withCredential: true }, { - name: 'othello/invitations', + name: 'reversi/invitations', withCredential: true }, { - name: 'othello/games', + name: 'reversi/games', withCredential: true }, { - name: 'othello/games/show' + name: 'reversi/games/show' }, { diff --git a/src/server/api/endpoints/othello/games.ts b/src/server/api/endpoints/othello/games.ts deleted file mode 100644 index 2320a34b04..0000000000 --- a/src/server/api/endpoints/othello/games.ts +++ /dev/null @@ -1,62 +0,0 @@ -import $ from 'cafy'; import ID from '../../../../cafy-id'; -import OthelloGame, { pack } from '../../../../models/othello-game'; - -module.exports = (params, user) => 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 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 deleted file mode 100644 index 6b2f5ce137..0000000000 --- a/src/server/api/endpoints/othello/games/show.ts +++ /dev/null @@ -1,32 +0,0 @@ -import $ from 'cafy'; import ID from '../../../../../cafy-id'; -import OthelloGame, { pack } from '../../../../../models/othello-game'; -import Othello from '../../../../../othello/core'; - -module.exports = (params, user) => 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 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 deleted file mode 100644 index 4761537614..0000000000 --- a/src/server/api/endpoints/othello/invitations.ts +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index e70e579755..0000000000 --- a/src/server/api/endpoints/othello/match.ts +++ /dev/null @@ -1,95 +0,0 @@ -import $ from 'cafy'; import ID from '../../../../cafy-id'; -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 '../../../../publishers/stream'; -import { eighteight } from '../../../../othello/maps'; - -module.exports = (params, user) => 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 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 deleted file mode 100644 index 562e691061..0000000000 --- a/src/server/api/endpoints/othello/match/cancel.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Matching from '../../../../../models/othello-matching'; - -module.exports = (params, user) => new Promise(async (res, rej) => { - await Matching.remove({ - parentId: user._id - }); - - res(); -}); diff --git a/src/server/api/endpoints/reversi/games.ts b/src/server/api/endpoints/reversi/games.ts new file mode 100644 index 0000000000..9d879ecf23 --- /dev/null +++ b/src/server/api/endpoints/reversi/games.ts @@ -0,0 +1,62 @@ +import $ from 'cafy'; import ID from '../../../../cafy-id'; +import ReversiGame, { pack } from '../../../../models/reversi-game'; + +module.exports = (params, user) => 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/reversi/games/show.ts b/src/server/api/endpoints/reversi/games/show.ts new file mode 100644 index 0000000000..f32eb23513 --- /dev/null +++ b/src/server/api/endpoints/reversi/games/show.ts @@ -0,0 +1,32 @@ +import $ from 'cafy'; import ID from '../../../../../cafy-id'; +import ReversiGame, { pack } from '../../../../../models/reversi-game'; +import Reversi from '../../../../../reversi/core'; + +module.exports = (params, user) => 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/reversi/invitations.ts b/src/server/api/endpoints/reversi/invitations.ts new file mode 100644 index 0000000000..fc487205a9 --- /dev/null +++ b/src/server/api/endpoints/reversi/invitations.ts @@ -0,0 +1,15 @@ +import Matching, { pack as packMatching } from '../../../../models/reversi-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/reversi/match.ts b/src/server/api/endpoints/reversi/match.ts new file mode 100644 index 0000000000..5a699ddbae --- /dev/null +++ b/src/server/api/endpoints/reversi/match.ts @@ -0,0 +1,95 @@ +import $ from 'cafy'; import ID from '../../../../cafy-id'; +import Matching, { pack as packMatching } from '../../../../models/reversi-matching'; +import ReversiGame, { pack as packGame } from '../../../../models/reversi-game'; +import User from '../../../../models/user'; +import publishUserStream, { publishReversiStream } from '../../../../publishers/stream'; +import { eighteight } from '../../../../reversi/maps'; + +module.exports = (params, user) => 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/reversi/match/cancel.ts b/src/server/api/endpoints/reversi/match/cancel.ts new file mode 100644 index 0000000000..bc8a4cd640 --- /dev/null +++ b/src/server/api/endpoints/reversi/match/cancel.ts @@ -0,0 +1,9 @@ +import Matching from '../../../../../models/reversi-matching'; + +module.exports = (params, user) => new Promise(async (res, rej) => { + await Matching.remove({ + parentId: user._id + }); + + res(); +}); diff --git a/src/server/api/stream/othello-game.ts b/src/server/api/stream/othello-game.ts deleted file mode 100644 index 841e542610..0000000000 --- a/src/server/api/stream/othello-game.ts +++ /dev/null @@ -1,333 +0,0 @@ -import * as websocket from 'websocket'; -import * as redis from 'redis'; -import * as CRC32 from 'crc-32'; -import OthelloGame, { pack } from '../../../models/othello-game'; -import { publishOthelloGameStream } from '../../../publishers/stream'; -import Othello from '../../../othello/core'; -import * as maps from '../../../othello/maps'; -import { ParsedUrlQuery } from 'querystring'; - -export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user?: any): void { - const q = request.resourceURL.query as ParsedUrlQuery; - const gameId = q.game; - - // Subscribe game stream - subscriber.subscribe(`misskey:othello-game-stream:${gameId}`); - subscriber.on('message', (_, data) => { - connection.send(data); - }); - - connection.on('message', async (data) => { - const msg = JSON.parse(data.utf8Data); - - switch (msg.type) { - case 'accept': - accept(true); - break; - - case 'cancel-accept': - accept(false); - break; - - case 'update-settings': - if (msg.settings == null) return; - updateSettings(msg.settings); - break; - - case 'init-form': - if (msg.body == null) return; - initForm(msg.body); - break; - - case 'update-form': - if (msg.id == null || msg.value === undefined) return; - updateForm(msg.id, msg.value); - break; - - case 'message': - if (msg.body == null) return; - message(msg.body); - break; - - case 'set': - if (msg.pos == null) return; - set(msg.pos); - break; - - case 'check': - if (msg.crc32 == null) return; - check(msg.crc32); - break; - } - }); - - async function updateSettings(settings) { - const game = await OthelloGame.findOne({ _id: gameId }); - - if (game.isStarted) return; - if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return; - if (game.user1Id.equals(user._id) && game.user1Accepted) return; - if (game.user2Id.equals(user._id) && game.user2Accepted) return; - - await OthelloGame.update({ _id: gameId }, { - $set: { - settings - } - }); - - publishOthelloGameStream(gameId, 'update-settings', settings); - } - - async function initForm(form) { - const game = await OthelloGame.findOne({ _id: gameId }); - - if (game.isStarted) return; - if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return; - - const set = game.user1Id.equals(user._id) ? { - form1: form - } : { - form2: form - }; - - await OthelloGame.update({ _id: gameId }, { - $set: set - }); - - publishOthelloGameStream(gameId, 'init-form', { - userId: user._id, - form - }); - } - - async function updateForm(id, value) { - const game = await OthelloGame.findOne({ _id: gameId }); - - if (game.isStarted) return; - if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return; - - const form = game.user1Id.equals(user._id) ? game.form2 : game.form1; - - const item = form.find(i => i.id == id); - - if (item == null) return; - - item.value = value; - - const set = game.user1Id.equals(user._id) ? { - form2: form - } : { - form1: form - }; - - await OthelloGame.update({ _id: gameId }, { - $set: set - }); - - publishOthelloGameStream(gameId, 'update-form', { - userId: user._id, - id, - value - }); - } - - async function message(message) { - message.id = Math.random(); - publishOthelloGameStream(gameId, 'message', { - userId: user._id, - message - }); - } - - async function accept(accept: boolean) { - const game = await OthelloGame.findOne({ _id: gameId }); - - if (game.isStarted) return; - - let bothAccepted = false; - - if (game.user1Id.equals(user._id)) { - await OthelloGame.update({ _id: gameId }, { - $set: { - user1Accepted: accept - } - }); - - publishOthelloGameStream(gameId, 'change-accepts', { - user1: accept, - user2: game.user2Accepted - }); - - if (accept && game.user2Accepted) bothAccepted = true; - } else if (game.user2Id.equals(user._id)) { - await OthelloGame.update({ _id: gameId }, { - $set: { - user2Accepted: accept - } - }); - - publishOthelloGameStream(gameId, 'change-accepts', { - user1: game.user1Accepted, - user2: accept - }); - - if (accept && game.user1Accepted) bothAccepted = true; - } else { - return; - } - - if (bothAccepted) { - // 3秒後、まだacceptされていたらゲーム開始 - setTimeout(async () => { - const freshGame = await OthelloGame.findOne({ _id: gameId }); - if (freshGame == null || freshGame.isStarted || freshGame.isEnded) return; - if (!freshGame.user1Accepted || !freshGame.user2Accepted) return; - - let bw: number; - if (freshGame.settings.bw == 'random') { - bw = Math.random() > 0.5 ? 1 : 2; - } else { - bw = freshGame.settings.bw as number; - } - - function getRandomMap() { - const mapCount = Object.entries(maps).length; - const rnd = Math.floor(Math.random() * mapCount); - return Object.entries(maps).find((x, i) => i == rnd)[1].data; - } - - const map = freshGame.settings.map != null ? freshGame.settings.map : getRandomMap(); - - await OthelloGame.update({ _id: gameId }, { - $set: { - startedAt: new Date(), - isStarted: true, - black: bw, - 'settings.map': map - } - }); - - //#region 盤面に最初から石がないなどして始まった瞬間に勝敗が決定する場合があるのでその処理 - const o = new Othello(map, { - isLlotheo: freshGame.settings.isLlotheo, - canPutEverywhere: freshGame.settings.canPutEverywhere, - loopedBoard: freshGame.settings.loopedBoard - }); - - if (o.isEnded) { - let winner; - if (o.winner === true) { - winner = freshGame.black == 1 ? freshGame.user1Id : freshGame.user2Id; - } else if (o.winner === false) { - winner = freshGame.black == 1 ? freshGame.user2Id : freshGame.user1Id; - } else { - winner = null; - } - - await OthelloGame.update({ - _id: gameId - }, { - $set: { - isEnded: true, - winnerId: winner - } - }); - - publishOthelloGameStream(gameId, 'ended', { - winnerId: winner, - game: await pack(gameId, user) - }); - } - //#endregion - - publishOthelloGameStream(gameId, 'started', await pack(gameId, user)); - }, 3000); - } - } - - // 石を打つ - async function set(pos) { - const game = await OthelloGame.findOne({ _id: gameId }); - - if (!game.isStarted) return; - if (game.isEnded) return; - if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return; - - 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 myColor = - (game.user1Id.equals(user._id) && game.black == 1) || (game.user2Id.equals(user._id) && game.black == 2) - ? true - : false; - - if (!o.canPut(myColor, pos)) return; - o.put(myColor, pos); - - let winner; - if (o.isEnded) { - if (o.winner === true) { - winner = game.black == 1 ? game.user1Id : game.user2Id; - } else if (o.winner === false) { - winner = game.black == 1 ? game.user2Id : game.user1Id; - } else { - winner = null; - } - } - - const log = { - at: new Date(), - color: myColor, - pos - }; - - const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString()); - - await OthelloGame.update({ - _id: gameId - }, { - $set: { - crc32, - isEnded: o.isEnded, - winnerId: winner - }, - $push: { - logs: log - } - }); - - publishOthelloGameStream(gameId, 'set', Object.assign(log, { - next: o.turn - })); - - if (o.isEnded) { - publishOthelloGameStream(gameId, 'ended', { - winnerId: winner, - game: await pack(gameId, user) - }); - } - } - - async function check(crc32) { - const game = await OthelloGame.findOne({ _id: gameId }); - - if (!game.isStarted) return; - - // 互換性のため - if (game.crc32 == null) return; - - if (crc32 !== game.crc32) { - connection.send(JSON.stringify({ - type: 'rescue', - body: await pack(game, user) - })); - } - } -} diff --git a/src/server/api/stream/othello.ts b/src/server/api/stream/othello.ts deleted file mode 100644 index fa62b05836..0000000000 --- a/src/server/api/stream/othello.ts +++ /dev/null @@ -1,29 +0,0 @@ -import * as mongo from 'mongodb'; -import * as websocket from 'websocket'; -import * as redis from 'redis'; -import Matching, { pack } from '../../../models/othello-matching'; -import publishUserStream from '../../../publishers/stream'; - -export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { - // Subscribe othello stream - subscriber.subscribe(`misskey:othello-stream:${user._id}`); - subscriber.on('message', (_, data) => { - connection.send(data); - }); - - connection.on('message', async (data) => { - const msg = JSON.parse(data.utf8Data); - - switch (msg.type) { - case 'ping': - if (msg.id == null) return; - const matching = await Matching.findOne({ - parentId: user._id, - childId: new mongo.ObjectID(msg.id) - }); - if (matching == null) return; - publishUserStream(matching.childId, 'othello_invited', await pack(matching, matching.childId)); - break; - } - }); -} diff --git a/src/server/api/stream/reversi-game.ts b/src/server/api/stream/reversi-game.ts new file mode 100644 index 0000000000..63d9b220b0 --- /dev/null +++ b/src/server/api/stream/reversi-game.ts @@ -0,0 +1,333 @@ +import * as websocket from 'websocket'; +import * as redis from 'redis'; +import * as CRC32 from 'crc-32'; +import ReversiGame, { pack } from '../../../models/reversi-game'; +import { publishReversiGameStream } from '../../../publishers/stream'; +import Reversi from '../../../reversi/core'; +import * as maps from '../../../reversi/maps'; +import { ParsedUrlQuery } from 'querystring'; + +export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user?: any): void { + const q = request.resourceURL.query as ParsedUrlQuery; + const gameId = q.game; + + // Subscribe game stream + subscriber.subscribe(`misskey:reversi-game-stream:${gameId}`); + subscriber.on('message', (_, data) => { + connection.send(data); + }); + + connection.on('message', async (data) => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'accept': + accept(true); + break; + + case 'cancel-accept': + accept(false); + break; + + case 'update-settings': + if (msg.settings == null) return; + updateSettings(msg.settings); + break; + + case 'init-form': + if (msg.body == null) return; + initForm(msg.body); + break; + + case 'update-form': + if (msg.id == null || msg.value === undefined) return; + updateForm(msg.id, msg.value); + break; + + case 'message': + if (msg.body == null) return; + message(msg.body); + break; + + case 'set': + if (msg.pos == null) return; + set(msg.pos); + break; + + case 'check': + if (msg.crc32 == null) return; + check(msg.crc32); + break; + } + }); + + async function updateSettings(settings) { + const game = await ReversiGame.findOne({ _id: gameId }); + + if (game.isStarted) return; + if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return; + if (game.user1Id.equals(user._id) && game.user1Accepted) return; + if (game.user2Id.equals(user._id) && game.user2Accepted) return; + + await ReversiGame.update({ _id: gameId }, { + $set: { + settings + } + }); + + publishReversiGameStream(gameId, 'update-settings', settings); + } + + async function initForm(form) { + const game = await ReversiGame.findOne({ _id: gameId }); + + if (game.isStarted) return; + if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return; + + const set = game.user1Id.equals(user._id) ? { + form1: form + } : { + form2: form + }; + + await ReversiGame.update({ _id: gameId }, { + $set: set + }); + + publishReversiGameStream(gameId, 'init-form', { + userId: user._id, + form + }); + } + + async function updateForm(id, value) { + const game = await ReversiGame.findOne({ _id: gameId }); + + if (game.isStarted) return; + if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return; + + const form = game.user1Id.equals(user._id) ? game.form2 : game.form1; + + const item = form.find(i => i.id == id); + + if (item == null) return; + + item.value = value; + + const set = game.user1Id.equals(user._id) ? { + form2: form + } : { + form1: form + }; + + await ReversiGame.update({ _id: gameId }, { + $set: set + }); + + publishReversiGameStream(gameId, 'update-form', { + userId: user._id, + id, + value + }); + } + + async function message(message) { + message.id = Math.random(); + publishReversiGameStream(gameId, 'message', { + userId: user._id, + message + }); + } + + async function accept(accept: boolean) { + const game = await ReversiGame.findOne({ _id: gameId }); + + if (game.isStarted) return; + + let bothAccepted = false; + + if (game.user1Id.equals(user._id)) { + await ReversiGame.update({ _id: gameId }, { + $set: { + user1Accepted: accept + } + }); + + publishReversiGameStream(gameId, 'change-accepts', { + user1: accept, + user2: game.user2Accepted + }); + + if (accept && game.user2Accepted) bothAccepted = true; + } else if (game.user2Id.equals(user._id)) { + await ReversiGame.update({ _id: gameId }, { + $set: { + user2Accepted: accept + } + }); + + publishReversiGameStream(gameId, 'change-accepts', { + user1: game.user1Accepted, + user2: accept + }); + + if (accept && game.user1Accepted) bothAccepted = true; + } else { + return; + } + + if (bothAccepted) { + // 3秒後、まだacceptされていたらゲーム開始 + setTimeout(async () => { + const freshGame = await ReversiGame.findOne({ _id: gameId }); + if (freshGame == null || freshGame.isStarted || freshGame.isEnded) return; + if (!freshGame.user1Accepted || !freshGame.user2Accepted) return; + + let bw: number; + if (freshGame.settings.bw == 'random') { + bw = Math.random() > 0.5 ? 1 : 2; + } else { + bw = freshGame.settings.bw as number; + } + + function getRandomMap() { + const mapCount = Object.entries(maps).length; + const rnd = Math.floor(Math.random() * mapCount); + return Object.entries(maps).find((x, i) => i == rnd)[1].data; + } + + const map = freshGame.settings.map != null ? freshGame.settings.map : getRandomMap(); + + await ReversiGame.update({ _id: gameId }, { + $set: { + startedAt: new Date(), + isStarted: true, + black: bw, + 'settings.map': map + } + }); + + //#region 盤面に最初から石がないなどして始まった瞬間に勝敗が決定する場合があるのでその処理 + const o = new Reversi(map, { + isLlotheo: freshGame.settings.isLlotheo, + canPutEverywhere: freshGame.settings.canPutEverywhere, + loopedBoard: freshGame.settings.loopedBoard + }); + + if (o.isEnded) { + let winner; + if (o.winner === true) { + winner = freshGame.black == 1 ? freshGame.user1Id : freshGame.user2Id; + } else if (o.winner === false) { + winner = freshGame.black == 1 ? freshGame.user2Id : freshGame.user1Id; + } else { + winner = null; + } + + await ReversiGame.update({ + _id: gameId + }, { + $set: { + isEnded: true, + winnerId: winner + } + }); + + publishReversiGameStream(gameId, 'ended', { + winnerId: winner, + game: await pack(gameId, user) + }); + } + //#endregion + + publishReversiGameStream(gameId, 'started', await pack(gameId, user)); + }, 3000); + } + } + + // 石を打つ + async function set(pos) { + const game = await ReversiGame.findOne({ _id: gameId }); + + if (!game.isStarted) return; + if (game.isEnded) return; + if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return; + + 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 myColor = + (game.user1Id.equals(user._id) && game.black == 1) || (game.user2Id.equals(user._id) && game.black == 2) + ? true + : false; + + if (!o.canPut(myColor, pos)) return; + o.put(myColor, pos); + + let winner; + if (o.isEnded) { + if (o.winner === true) { + winner = game.black == 1 ? game.user1Id : game.user2Id; + } else if (o.winner === false) { + winner = game.black == 1 ? game.user2Id : game.user1Id; + } else { + winner = null; + } + } + + const log = { + at: new Date(), + color: myColor, + pos + }; + + const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString()); + + await ReversiGame.update({ + _id: gameId + }, { + $set: { + crc32, + isEnded: o.isEnded, + winnerId: winner + }, + $push: { + logs: log + } + }); + + publishReversiGameStream(gameId, 'set', Object.assign(log, { + next: o.turn + })); + + if (o.isEnded) { + publishReversiGameStream(gameId, 'ended', { + winnerId: winner, + game: await pack(gameId, user) + }); + } + } + + async function check(crc32) { + const game = await ReversiGame.findOne({ _id: gameId }); + + if (!game.isStarted) return; + + // 互換性のため + if (game.crc32 == null) return; + + if (crc32 !== game.crc32) { + connection.send(JSON.stringify({ + type: 'rescue', + body: await pack(game, user) + })); + } + } +} diff --git a/src/server/api/stream/reversi.ts b/src/server/api/stream/reversi.ts new file mode 100644 index 0000000000..35c6167364 --- /dev/null +++ b/src/server/api/stream/reversi.ts @@ -0,0 +1,29 @@ +import * as mongo from 'mongodb'; +import * as websocket from 'websocket'; +import * as redis from 'redis'; +import Matching, { pack } from '../../../models/reversi-matching'; +import publishUserStream from '../../../publishers/stream'; + +export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { + // Subscribe reversi stream + subscriber.subscribe(`misskey:reversi-stream:${user._id}`); + subscriber.on('message', (_, data) => { + connection.send(data); + }); + + connection.on('message', async (data) => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'ping': + if (msg.id == null) return; + const matching = await Matching.findOne({ + parentId: user._id, + childId: new mongo.ObjectID(msg.id) + }); + if (matching == null) return; + publishUserStream(matching.childId, 'reversi_invited', await pack(matching, matching.childId)); + break; + } + }); +} diff --git a/src/server/api/streaming.ts b/src/server/api/streaming.ts index 2d4cfc108f..a3e4ed6f85 100644 --- a/src/server/api/streaming.ts +++ b/src/server/api/streaming.ts @@ -10,8 +10,8 @@ import userListStream from './stream/user-list'; import driveStream from './stream/drive'; import messagingStream from './stream/messaging'; import messagingIndexStream from './stream/messaging-index'; -import othelloGameStream from './stream/othello-game'; -import othelloStream from './stream/othello'; +import reversiGameStream from './stream/reversi-game'; +import reversiStream from './stream/reversi'; import serverStatsStream from './stream/server-stats'; import notesStatsStream from './stream/notes-stats'; import requestsStream from './stream/requests'; @@ -56,8 +56,8 @@ module.exports = (server: http.Server) => { const q = request.resourceURL.query as ParsedUrlQuery; const [user, app] = await authenticate(q.i as string); - if (request.resourceURL.pathname === '/othello-game') { - othelloGameStream(request, connection, subscriber, user); + if (request.resourceURL.pathname === '/reversi-game') { + reversiGameStream(request, connection, subscriber, user); return; } @@ -75,7 +75,7 @@ module.exports = (server: http.Server) => { request.resourceURL.pathname === '/drive' ? driveStream : request.resourceURL.pathname === '/messaging' ? messagingStream : request.resourceURL.pathname === '/messaging-index' ? messagingIndexStream : - request.resourceURL.pathname === '/othello' ? othelloStream : + request.resourceURL.pathname === '/reversi' ? reversiStream : null; if (channel !== null) { -- cgit v1.2.3-freya