UNPKG

6.27 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _regenerator = require("babel-runtime/regenerator");
8
9var _regenerator2 = _interopRequireDefault(_regenerator);
10
11exports.default = createGameMiddleware;
12
13var _util = require("@cardcore/util");
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
18
19function createGameMiddleware(gameActions, clientActions) {
20 var clientFetch = clientActions.clientFetch,
21 clientPoll = clientActions.clientPoll,
22 clientNext = clientActions.clientNext;
23
24
25 return function gameMiddleware(store) {
26 var _this = this;
27
28 return function (next) {
29 return function () {
30 var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee(action) {
31 var prevState, me, prevHash, nextState, nextHash, signedAction, res;
32 return _regenerator2.default.wrap(function _callee$(_context) {
33 while (1) {
34 switch (_context.prev = _context.next) {
35 case 0:
36 if (!(typeof action === "function")) {
37 _context.next = 2;
38 break;
39 }
40
41 return _context.abrupt("return", action(store.dispatch, store.getState));
42
43 case 2:
44 if (gameActions[action.type]) {
45 _context.next = 4;
46 break;
47 }
48
49 return _context.abrupt("return", Promise.resolve(next(action)));
50
51 case 4:
52
53 // Okay, it's a game action. Great! Let's handle some special cases first.
54
55 // Special case: we're loading the state from the server. Pass through.
56
57 // Resolve the action.
58 prevState = store.getState();
59 me = prevState.client.keys.id;
60 prevHash = null;
61 // Special case until we have an actual blockchain to build on
62
63 if (action.type !== gameActions.CREATE_GAME) {
64 prevHash = (0, _util.hashState)(prevState);
65 }
66
67 if (action[_util.REMOTE_ACTION]) {
68 _context.next = 12;
69 break;
70 }
71
72 action = Object.assign({}, action, {
73 agent: me,
74 prev: prevHash
75 });
76 _context.next = 14;
77 break;
78
79 case 12:
80 if (!(action.prev !== prevHash)) {
81 _context.next = 14;
82 break;
83 }
84
85 throw new Error("hash mismatch, " + action.next + " !== " + nextHash);
86
87 case 14:
88 next(action);
89 nextState = store.getState();
90 nextHash = (0, _util.hashState)(nextState);
91
92 if (!action[_util.REMOTE_ACTION]) {
93 _context.next = 22;
94 break;
95 }
96
97 if (!(action.next !== nextHash)) {
98 _context.next = 20;
99 break;
100 }
101
102 throw new Error("hash mismatch, " + action.next + " !== " + nextHash);
103
104 case 20:
105 _context.next = 23;
106 break;
107
108 case 22:
109 action = Object.assign({}, action, {
110 next: nextHash
111 });
112
113 case 23:
114 if (action[_util.REMOTE_ACTION]) {
115 _context.next = 43;
116 break;
117 }
118
119 signedAction = _util.Keys.signAction(prevState, action);
120 res = void 0;
121 _context.prev = 26;
122 _context.next = 29;
123 return store.dispatch(clientFetch("/" + encodeURIComponent(nextHash), {
124 method: "POST",
125 body: signedAction,
126 headers: {
127 "content-type": "application/json"
128 }
129 }));
130
131 case 29:
132 res = _context.sent;
133 _context.next = 35;
134 break;
135
136 case 32:
137 _context.prev = 32;
138 _context.t0 = _context["catch"](26);
139 throw new Error("Failed to create an action. We should probably handle this error.");
140
141 case 35:
142 if (res.ok) {
143 _context.next = 41;
144 break;
145 }
146
147 _context.t1 = Error;
148 _context.next = 39;
149 return res.text();
150
151 case 39:
152 _context.t2 = _context.sent;
153 throw new _context.t1(_context.t2);
154
155 case 41:
156 _context.next = 45;
157 break;
158
159 case 43:
160 if (_util.Keys.verifyAction(prevState, action)) {
161 _context.next = 45;
162 break;
163 }
164
165 throw new Error("Remote action failed to verify: " + JSON.stringify(action));
166
167 case 45:
168 _context.next = 47;
169 return store.dispatch(clientNext());
170
171 case 47:
172 store.dispatch(clientPoll());
173
174 case 48:
175 case "end":
176 return _context.stop();
177 }
178 }
179 }, _callee, _this, [[26, 32]]);
180 }));
181
182 return function (_x) {
183 return _ref.apply(this, arguments);
184 };
185 }();
186 };
187 };
188}
189//# sourceMappingURL=game-middleware.js.map
\No newline at end of file