UNPKG

kraft

Version:

A mysterious command line game with lots of secrets

79 lines (56 loc) 2.11 kB
const Lenses = require(__base+'helpers/Lenses'); const Flow = require(__base+'helpers/Flow'); const Output = require(__base+'helpers/Output'); const pActionType = Lenses.create(['player', 'action', 'type']); const pActionEnd = Lenses.create(['player', 'action', 'end']); const pActionStart = Lenses.create(['player', 'action', 'start']); const pActionArguments = Lenses.create(['player', 'action', 'arguments']); const pResource = (resource) => Lenses.create(['player', 'resources', resource]); const pLocation = Lenses.create(['player', 'location']); const pBuilding = (location, building) => Lenses.create(['player', 'buildings', location, building]); const getCurrentMillis = () => new Date().getTime(); const setActionIdle = () => { pActionType.set("idle"); pActionStart.set(undefined); pActionEnd.set(undefined); pActionArguments.set(undefined); }; const checkIfCraftingIsDone = () => { if ( pActionType.equals("crafting") && pActionEnd.view() <= getCurrentMillis() ) { const item = pActionArguments.view(); pResource(item).add(1); setActionIdle(); Output.afterSave(Lenses.config(), "** You completed crafting 1 ".green, item.green ); } }; const checkIfTravellingIsDone = () => { if ( pActionType.equals("travelling") && pActionEnd.view() <= getCurrentMillis() ) { const location = pActionArguments.view(); pLocation.set(location); setActionIdle(); Output.afterSave(Lenses.config(), "** You arrived at ", location.green ); } }; const checkIfBuildingIsDone = () => { if ( pActionType.equals("building") && pActionEnd.view() <= getCurrentMillis() ) { const building = pActionArguments.view(); pBuilding(pLocation.view(), building).set({}); setActionIdle(); Output.afterSave(Lenses.config(), "** You have finished building ", building.green); } }; exports.checkCurrentAction = () => { checkIfCraftingIsDone(); checkIfTravellingIsDone(); checkIfBuildingIsDone(); Flow.next(); };