UNPKG

897 BJavaScriptView Raw
1// import Ajv from "ajv";
2// const ajv = new Ajv({ allErrors: true });
3import ZSchema from "z-schema";
4const validator = new ZSchema();
5
6export default function queueReducer(state, action) {
7 if (state.game.queue && state.game.queue.length > 0) {
8 const schema = state.game.queue[0];
9 // const validate = ajv.compile(schema);
10
11 const valid = validator.validate(action, schema);
12 if (!valid) {
13 throw new Error(
14 JSON.stringify({ action, errors: validator.getLastErrors() }, null, 2)
15 );
16 }
17 }
18 if (state.game.nextActions) {
19 return {
20 ...state,
21 game: {
22 ...state.game,
23 queue: state.game.nextActions.map(action => ({
24 type: "object",
25 properties: {
26 type: {
27 type: "string",
28 enum: [action.action.type]
29 }
30 }
31 }))
32 }
33 };
34 }
35
36 return state;
37}