I’m working on a web-based game where a player chooses a type of action, and then a space on which to take that action. Rather than write the web infrastructure first (I’m planning on using express.js) I’m writing the actions first, to see if I can create a nice clean interface.
So for instance, a player chooses ‘Trees’ via request, and the code for what happens when a player selects Trees is
async play(): Promise<void> {
const hex = await game.ask<Hex>(new SelectHex());
game.placeToken(hex, "greenery");
}
I understand how backwards this might be, because game.ask
is now (eventually) responsible for writing the HTTP response, while also owning the Promise
. I’m avoiding trying to write something a little less elegant, like
async play() {
return new SelectHex((hex) => game.placeToken(hex, "greenery"));
}
which doesn’t look so inelegant here, but can be more complicated.
Ideally, the express endpoint that receives the hex will need access to the Promise
, so as to call its resolve
method.
Is this pattern feasible? I realize there’s probably more information necessary, but I’m not sure what else to write.