From 175aa62a7e4a1d8748059e6a8f5acf01a9ffff47 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 9 Jun 2024 10:48:17 +0200 Subject: [PATCH] Extract movementDescriptionMessages() --- web/vessel.js | 68 ++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/web/vessel.js b/web/vessel.js index d41bb5c..7411539 100644 --- a/web/vessel.js +++ b/web/vessel.js @@ -173,37 +173,13 @@ function spawnVessel(vesselId, initialHere, _isRoot) { break; case 'goto': if (!isRootVessel(vessel) || !isLocalVessel(cmd.location)) { - const oldLocation = location.value; + let { outMsg, inMsg } = movementDescriptionMessages(cmd.action, + vessel, + agent, + location.value, + cmd.location); + const id = outMsg ? sendEvent(null, location.value, ... outMsg) : null; location.value = cmd.location; - let outMsg = void 0, inMsg = void 0; - switch (cmd.action) { - case 'initial': - break; - case 'link': - inMsg = [vessel, ` joined the network.`]; - break; - case 'give': - outMsg = inMsg = [agent, ` gave `, _theThing(vessel), ` to `, _theThing(cmd.location), `.`]; - break; - case 'put': - outMsg = inMsg = [agent, ` put `, _theThing(vessel), ` down in `, _thePlace(cmd.location), `.`]; - break; - case 'take': - outMsg = inMsg = [agent, ` picked up `, _theThing(vessel), ` from `, _thePlace(oldLocation), `.`]; - break; - case 'drop': - outMsg = inMsg = [agent, ` dropped `, _theThing(vessel), ` in `, _thePlace(cmd.location), `.`]; - break; - case 'enter': - outMsg = [vessel, ` entered `, _thePlace(cmd.location), `.`]; - inMsg = [vessel, ` left `, _thePlace(oldLocation), ` for `, _thePlace(cmd.location), `.`]; - break; - case 'move': - default: - outMsg = inMsg = [agent, ` moved `, _theThing(vessel), ` from `, _thePlace(oldLocation), ` into `, _thePlace(cmd.location), `.`]; - break; - } - const id = outMsg ? sendEvent(null, oldLocation, ... outMsg) : null; if (inMsg) broadcast(id, ... inMsg); } break; @@ -254,3 +230,35 @@ function spawnVessel(vesselId, initialHere, _isRoot) { } }); } + +function movementDescriptionMessages(action, vessel, agent, oldLocation, newLocation) { + let outMsg = void 0, inMsg = void 0; + switch (action) { + case 'initial': + break; + case 'link': + inMsg = [vessel, ` joined the network.`]; + break; + case 'give': + outMsg = inMsg = [agent, ` gave `, _theThing(vessel), ` to `, _theThing(newLocation), `.`]; + break; + case 'put': + outMsg = inMsg = [agent, ` put `, _theThing(vessel), ` down in `, _thePlace(newLocation), `.`]; + break; + case 'take': + outMsg = inMsg = [agent, ` picked up `, _theThing(vessel), ` from `, _thePlace(oldLocation), `.`]; + break; + case 'drop': + outMsg = inMsg = [agent, ` dropped `, _theThing(vessel), ` in `, _thePlace(newLocation), `.`]; + break; + case 'enter': + outMsg = [vessel, ` entered `, _thePlace(newLocation), `.`]; + inMsg = [vessel, ` left `, _thePlace(oldLocation), ` for `, _thePlace(newLocation), `.`]; + break; + case 'move': + default: + outMsg = inMsg = [agent, ` moved `, _theThing(vessel), ` from `, _thePlace(oldLocation), ` into `, _thePlace(newLocation), `.`]; + break; + } + return { outMsg, inMsg }; +}