summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-03-11 07:07:17 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-03-11 07:07:22 +0900
commit98f09ad16c8dbdfedfb1a517203f84e584fb0bee (patch)
treeba136e84adb52a83ea6d5786d0f2e4232799d7a9 /src/api
parentMerge pull request #1233 from syuilo/othello-lack-of-black-map (diff)
downloadsharkey-98f09ad16c8dbdfedfb1a517203f84e584fb0bee.tar.gz
sharkey-98f09ad16c8dbdfedfb1a517203f84e584fb0bee.tar.bz2
sharkey-98f09ad16c8dbdfedfb1a517203f84e584fb0bee.zip
:v:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/models/othello-game.ts2
-rw-r--r--src/api/stream/othello-game.ts76
2 files changed, 78 insertions, 0 deletions
diff --git a/src/api/models/othello-game.ts b/src/api/models/othello-game.ts
index a8c3025108..ab90cffa44 100644
--- a/src/api/models/othello-game.ts
+++ b/src/api/models/othello-game.ts
@@ -33,6 +33,8 @@ export interface IGame {
can_put_everywhere: boolean;
looped_board: boolean;
};
+ form1: any;
+ form2: any;
}
/**
diff --git a/src/api/stream/othello-game.ts b/src/api/stream/othello-game.ts
index 5f61f0cc2c..888c599338 100644
--- a/src/api/stream/othello-game.ts
+++ b/src/api/stream/othello-game.ts
@@ -31,6 +31,21 @@ export default function(request: websocket.request, connection: websocket.connec
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);
@@ -55,6 +70,67 @@ export default function(request: websocket.request, connection: websocket.connec
publishOthelloGameStream(gameId, 'update-settings', settings);
}
+ async function initForm(form) {
+ const game = await Game.findOne({ _id: gameId });
+
+ if (game.is_started) return;
+ if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return;
+
+ const set = game.user1_id.equals(user._id) ? {
+ form1: form
+ } : {
+ form2: form
+ };
+
+ await Game.update({ _id: gameId }, {
+ $set: set
+ });
+
+ publishOthelloGameStream(gameId, 'init-form', {
+ user_id: user._id,
+ form
+ });
+ }
+
+ async function updateForm(id, value) {
+ const game = await Game.findOne({ _id: gameId });
+
+ if (game.is_started) return;
+ if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return;
+
+ const form = game.user1_id.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.user1_id.equals(user._id) ? {
+ form2: form
+ } : {
+ form1: form
+ };
+
+ await Game.update({ _id: gameId }, {
+ $set: set
+ });
+
+ publishOthelloGameStream(gameId, 'update-form', {
+ user_id: user._id,
+ id,
+ value
+ });
+ }
+
+ async function message(message) {
+ message.id = Math.random();
+ publishOthelloGameStream(gameId, 'message', {
+ user_id: user._id,
+ message
+ });
+ }
+
async function accept(accept: boolean) {
const game = await Game.findOne({ _id: gameId });