diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-03-07 18:45:16 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-03-07 18:45:16 +0900 |
| commit | 0bca0e8a02e005293663abc4a052ad8f8b2078f3 (patch) | |
| tree | 046e97207c68833a5c57f536312bea0623bc5b44 /src/api | |
| parent | wip (diff) | |
| download | sharkey-0bca0e8a02e005293663abc4a052ad8f8b2078f3.tar.gz sharkey-0bca0e8a02e005293663abc4a052ad8f8b2078f3.tar.bz2 sharkey-0bca0e8a02e005293663abc4a052ad8f8b2078f3.zip | |
wip
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/bot/core.ts | 4 | ||||
| -rw-r--r-- | src/api/endpoints/othello/games.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/othello/match.ts | 1 | ||||
| -rw-r--r-- | src/api/models/othello-game.ts | 4 | ||||
| -rw-r--r-- | src/api/stream/othello-game.ts | 91 |
5 files changed, 62 insertions, 40 deletions
diff --git a/src/api/bot/core.ts b/src/api/bot/core.ts index 0a073a3127..75564b81e8 100644 --- a/src/api/bot/core.ts +++ b/src/api/bot/core.ts @@ -475,8 +475,8 @@ class OthelloContext extends Context { othelloAi('white', this.othello); if (this.othello.getPattern('black').length === 0) { this.bot.clearContext(); - const blackCount = this.othello.board.map(row => row.filter(s => s == 'black').length).reduce((a, b) => a + b); - const whiteCount = this.othello.board.map(row => row.filter(s => s == 'white').length).reduce((a, b) => a + b); + const blackCount = this.othello.board.filter(s => s == 'black').length; + const whiteCount = this.othello.board.filter(s => s == 'white').length; const winner = blackCount == whiteCount ? '引き分け' : blackCount > whiteCount ? '黒の勝ち' : '白の勝ち'; return this.othello.toString() + `\n\n~終了~\n\n黒${blackCount}、白${whiteCount}で${winner}です。`; } else { diff --git a/src/api/endpoints/othello/games.ts b/src/api/endpoints/othello/games.ts index 62388b01d4..da85de1c1f 100644 --- a/src/api/endpoints/othello/games.ts +++ b/src/api/endpoints/othello/games.ts @@ -3,7 +3,7 @@ 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().$; + const [my = false, myErr] = $(params.my).optional.boolean().$; if (myErr) return rej('invalid my param'); const q = my ? { diff --git a/src/api/endpoints/othello/match.ts b/src/api/endpoints/othello/match.ts index 65243a5571..cb094bbc65 100644 --- a/src/api/endpoints/othello/match.ts +++ b/src/api/endpoints/othello/match.ts @@ -34,6 +34,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { 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, + is_ended: false, logs: [] }); diff --git a/src/api/models/othello-game.ts b/src/api/models/othello-game.ts index b9fd94ebc0..d75baf356b 100644 --- a/src/api/models/othello-game.ts +++ b/src/api/models/othello-game.ts @@ -11,6 +11,9 @@ export interface IGame { created_at: Date; black_user_id: mongo.ObjectID; white_user_id: mongo.ObjectID; + turn_user_id: mongo.ObjectID; + is_ended: boolean; + winner_id: mongo.ObjectID; logs: any[]; } @@ -40,6 +43,7 @@ export const pack = ( // Populate user _game.black_user = await packUser(_game.black_user_id, meId); _game.white_user = await packUser(_game.white_user_id, meId); + _game.winner = await packUser(_game.winner_id, meId); resolve(_game); }); diff --git a/src/api/stream/othello-game.ts b/src/api/stream/othello-game.ts index 17cdd3a9e7..59d964777d 100644 --- a/src/api/stream/othello-game.ts +++ b/src/api/stream/othello-game.ts @@ -19,51 +19,68 @@ export default function(request: websocket.request, connection: websocket.connec switch (msg.type) { case 'set': if (msg.pos == null) return; - const pos = msg.pos; + set(msg.pos); + break; + } + }); - const game = await Game.findOne({ _id: gameId }); + async function set(pos) { + const game = await Game.findOne({ _id: gameId }); - const o = new Othello(); + if (game.is_ended) return; - game.logs.forEach(log => { - o.set(log.color, log.pos); - }); + const o = new Othello(); - const myColor = game.black_user_id.equals(user._id) ? 'black' : 'white'; - const opColor = myColor == 'black' ? 'white' : 'black'; + game.logs.forEach(log => { + o.set(log.color, log.pos); + }); - if (!o.canReverse(myColor, pos)) return; - o.set(myColor, pos); + const myColor = game.black_user_id.equals(user._id) ? 'black' : 'white'; + const opColor = myColor == 'black' ? 'white' : 'black'; - let turn; - if (o.getPattern(opColor).length > 0) { - turn = myColor == 'black' ? game.white_user_id : game.black_user_id; - } else { - turn = myColor == 'black' ? game.black_user_id : game.white_user_id; - } + if (!o.canReverse(myColor, pos)) return; + o.set(myColor, pos); - const log = { - at: new Date(), - color: myColor, - pos - }; + let turn; + if (o.getPattern(opColor).length > 0) { + turn = myColor == 'black' ? game.white_user_id : game.black_user_id; + } else if (o.getPattern(myColor).length > 0) { + turn = myColor == 'black' ? game.black_user_id : game.white_user_id; + } else { + turn = null; + } - await Game.update({ - _id: gameId - }, { - $set: { - turn_user_id: turn - }, - $push: { - logs: log - } - }); + const isEnded = turn === null; - publishOthelloGameStream(gameId, 'set', { - color: myColor, - pos - }); - break; + let winner; + if (isEnded) { + const blackCount = o.board.filter(s => s == 'black').length; + const whiteCount = o.board.filter(s => s == 'white').length; + winner = blackCount == whiteCount ? null : blackCount > whiteCount ? game.black_user_id : game.white_user_id; } - }); + + const log = { + at: new Date(), + color: myColor, + pos + }; + + await Game.update({ + _id: gameId + }, { + $set: { + turn_user_id: turn, + is_ended: isEnded, + winner_id: winner + }, + $push: { + logs: log + } + }); + + publishOthelloGameStream(gameId, 'set', { + color: myColor, + pos + }); + } } |