UNPKG

5.62 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/game-action-middleware.js"],"names":["createGameActionMiddleware","actions","actionMap","Object","keys","forEach","value","key","camelCase","split","map","word","toUpperCase","slice","toLowerCase","join","actionCreator","checkActionAllowed","state","action","type","game","turn","sender","agent","client","id","nextActions","length","queueUser","playerId","Error","allowedActions","gameActionMiddleware","store","getState","dispatch","loadingState","_fromQueue","_dispatchedFromQueue","next"],"mappings":";;;;;kBASwBA,0B;AATxB;;;;;;;;;AASe,SAASA,0BAAT,CAAoCC,OAApC,EAA6C;AAC1D,MAAMC,YAAY,EAAlB;AACAC,SAAOC,IAAP,CAAYH,OAAZ,EAAqBI,OAArB,CAA6B,eAAO;AAClC,QAAMC,QAAQL,QAAQM,GAAR,CAAd;AACA,QAAI,OAAOD,KAAP,KAAiB,QAAjB,IAA6BC,QAAQD,KAAzC,EAAgD;AAC9C;AACD;AACD,QAAME,YAAYF,MACfG,KADe,CACT,GADS,EAEfC,GAFe,CAEX;AAAA,aAAQC,KAAK,CAAL,EAAQC,WAAR,KAAwBD,KAAKE,KAAL,CAAW,CAAX,EAAcC,WAAd,EAAhC;AAAA,KAFW,EAGfC,IAHe,CAGV,EAHU,CAAlB;AAIA,QAAMC,gBAAgBR,UAAU,CAAV,EAAaM,WAAb,KAA6BN,UAAUK,KAAV,CAAgB,CAAhB,CAAnD;AACA,QAAIZ,QAAQe,aAAR,CAAJ,EAA4B;AAC1Bd,gBAAUI,KAAV,IAAmBL,QAAQe,aAAR,CAAnB;AACD;AACF,GAbD;;AAeA;;;AAGA,WAASC,kBAAT,CAA4BC,KAA5B,EAAmCC,MAAnC,EAA2C;AACzC;AACA,QAAI,CAAClB,QAAQkB,OAAOC,IAAf,CAAL,EAA2B;AACzB;AACD;AACD;AACA,QAAI,CAACF,MAAMG,IAAP,IAAe,CAACH,MAAMG,IAAN,CAAWC,IAA/B,EAAqC;AACnC;AACD;AACD,QAAMC,SAASJ,OAAOK,KAAP,IAAgBN,MAA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tB,EAAkC;AAChC,iBAAOA,OAAOiB,QAAP,EAAiBD,QAAjB,CAAP;AACD;AACD,eAAOK,KAAKrB,MAAL,CAAP;AACD,OAvBD;AAwBD,KAzBD;AA0BD,GA3BD;AA4BD","file":"game-action-middleware.js","sourcesContent":["/**\n * So we have lots of actions that come to us from the nextActions queue in the reducer. Some of\n * them can be dispatched as-is, but many of them require us to run an action creator function to\n * e.g. decrypt or encrypt something as is appropriate. This middleware detects such actions and\n * sees if we have a cooresponding action based on a simple naming convention.\n *\n * actions.SEED_RNG_DECRYPT => actions.seedRngDecryptAction\n */\n\nexport default function createGameActionMiddleware(actions) {\n const actionMap = {};\n Object.keys(actions).forEach(key => {\n const value = actions[key];\n if (typeof value !== \"string\" || key !== value) {\n return;\n }\n const camelCase = value\n .split(\"_\")\n .map(word => word[0].toUpperCase() + word.slice(1).toLowerCase())\n .join(\"\");\n const actionCreator = camelCase[0].toLowerCase() + camelCase.slice(1);\n if (actions[actionCreator]) {\n actionMap[value] = actions[actionCreator];\n }\n });\n\n /**\n * two kinds of actions are allowed - you can either be a human that did an action on your turn or you can be a computer handling the most recent action in nextactions\n */\n function checkActionAllowed(state, action) {\n // non-game actions are always allowed\n if (!actions[action.type]) {\n return;\n }\n // also if we don't have a turn order yet, nbd\n if (!state.game || !state.game.turn) {\n return;\n }\n const sender = action.agent || state.client.keys.id;\n if (state.game.nextActions.length > 0) {\n const queueUser = state.game.nextActions[0].playerId;\n if (sender !== queueUser) {\n throw new Error(\n `queue wants user ${queueUser} but user ${sender} acted`\n );\n }\n }\n // no queue, check if it's an allowed action\n else if (state.game.allowedActions[action.type] !== true) {\n throw new Error(\n `${sender} attempted to do non-allowed action ${action.type}`\n );\n } else if (state.game.turn !== sender) {\n throw new Error(\n `${sender} acted out of turn; it's ${state.game.turn}'s turn`\n );\n }\n }\n\n return function gameActionMiddleware(store) {\n return next => {\n return action => {\n const state = store.getState();\n let { dispatch, getState } = store;\n checkActionAllowed(state, action);\n if (\n !state.client.loadingState &&\n actionMap[action.type] &&\n action._fromQueue &&\n !action._dispatchedFromQueue\n ) {\n action = actionMap[action.type](action);\n dispatch = action =>\n store.dispatch({\n ...action,\n _fromQueue: true,\n _dispatchedFromQueue: true\n });\n }\n // implement thunk\n if (typeof action === \"function\") {\n return action(dispatch, getState);\n }\n return next(action);\n };\n };\n };\n}\n"]}
\No newline at end of file