diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-03-08 17:57:57 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-03-08 17:57:57 +0900 |
| commit | 155012846d8f142515390adb2754b45cdfbb74ef (patch) | |
| tree | 13b237f9464a7df918fee7f35ba6871d08c1ed1c /src/api/stream | |
| parent | Fix bug (diff) | |
| download | sharkey-155012846d8f142515390adb2754b45cdfbb74ef.tar.gz sharkey-155012846d8f142515390adb2754b45cdfbb74ef.tar.bz2 sharkey-155012846d8f142515390adb2754b45cdfbb74ef.zip | |
#1200
Diffstat (limited to 'src/api/stream')
| -rw-r--r-- | src/api/stream/othello-game.ts | 140 |
1 files changed, 116 insertions, 24 deletions
diff --git a/src/api/stream/othello-game.ts b/src/api/stream/othello-game.ts index d086478159..1dcd37efa1 100644 --- a/src/api/stream/othello-game.ts +++ b/src/api/stream/othello-game.ts @@ -1,8 +1,8 @@ import * as websocket from 'websocket'; import * as redis from 'redis'; -import Game from '../models/othello-game'; +import Game, { pack } from '../models/othello-game'; import { publishOthelloGameStream } from '../event'; -import Othello from '../../common/othello'; +import Othello from '../../common/othello/core'; export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { const gameId = request.resourceURL.query.game; @@ -17,6 +17,19 @@ export default function(request: websocket.request, connection: websocket.connec 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 'set': if (msg.pos == null) return; set(msg.pos); @@ -24,38 +37,118 @@ export default function(request: websocket.request, connection: websocket.connec } }); - async function set(pos) { + async function updateSettings(settings) { const game = await Game.findOne({ _id: gameId }); - if (game.is_ended) return; - if (!game.black_user_id.equals(user._id) && !game.white_user_id.equals(user._id)) return; + if (game.is_started) return; + if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return; + if (game.user1_id.equals(user._id) && game.user1_accepted) return; + if (game.user2_id.equals(user._id) && game.user2_accepted) return; - const o = new Othello(); - - game.logs.forEach(log => { - o.set(log.color, log.pos); + await Game.update({ _id: gameId }, { + $set: { + settings + } }); - const myColor = game.black_user_id.equals(user._id) ? 'black' : 'white'; - const opColor = myColor == 'black' ? 'white' : 'black'; + publishOthelloGameStream(gameId, 'update-settings', settings); + } + + async function accept(accept: boolean) { + const game = await Game.findOne({ _id: gameId }); + + if (game.is_started) return; + + let bothAccepted = false; + + if (game.user1_id.equals(user._id)) { + await Game.update({ _id: gameId }, { + $set: { + user1_accepted: accept + } + }); + + publishOthelloGameStream(gameId, 'change-accepts', { + user1: accept, + user2: game.user2_accepted + }); - if (!o.canReverse(myColor, pos)) return; - o.set(myColor, pos); + if (accept && game.user2_accepted) bothAccepted = true; + } else if (game.user2_id.equals(user._id)) { + await Game.update({ _id: gameId }, { + $set: { + user2_accepted: accept + } + }); - 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; + publishOthelloGameStream(gameId, 'change-accepts', { + user1: game.user1_accepted, + user2: accept + }); + + if (accept && game.user1_accepted) bothAccepted = true; } else { - turn = null; + return; + } + + if (bothAccepted) { + // 3秒後、まだacceptされていたらゲーム開始 + setTimeout(async () => { + const freshGame = await Game.findOne({ _id: gameId }); + if (freshGame == null || freshGame.is_started || freshGame.is_ended) return; + + let bw: number; + if (freshGame.settings.bw == 'random') { + bw = Math.random() > 0.5 ? 1 : 2; + } else { + bw = freshGame.settings.bw as number; + } + + await Game.update({ _id: gameId }, { + $set: { + started_at: new Date(), + is_started: true, + black: bw + } + }); + + publishOthelloGameStream(gameId, 'started', await pack(gameId)); + }, 3000); } + } - const isEnded = turn === null; + async function set(pos) { + const game = await Game.findOne({ _id: gameId }); + + if (!game.is_started) return; + if (game.is_ended) return; + if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return; + + const o = new Othello(game.settings.map, { + isLlotheo: game.settings.is_llotheo + }); + + game.logs.forEach(log => { + o.put(log.color, log.pos); + }); + + const myColor = + (game.user1_id.equals(user._id) && game.black == 1) || (game.user2_id.equals(user._id) && game.black == 2) + ? 'black' + : 'white'; + + if (!o.canPut(myColor, pos)) return; + o.put(myColor, pos); let winner; - if (isEnded) { - winner = o.blackCount == o.whiteCount ? null : o.blackCount > o.whiteCount ? game.black_user_id : game.white_user_id; + if (o.isEnded) { + if (o.winner == 'black') { + winner = game.black == 1 ? game.user1_id : game.user2_id; + } else if (o.winner == 'white') { + winner = game.black == 1 ? game.user2_id : game.user1_id; + } else { + winner = null; + } } const log = { @@ -68,8 +161,7 @@ export default function(request: websocket.request, connection: websocket.connec _id: gameId }, { $set: { - turn_user_id: turn, - is_ended: isEnded, + is_ended: o.isEnded, winner_id: winner }, $push: { |