return withLock(sid, () => {
switch (action) {
case 'view': {
const state = loadOrStart(sid, game);
const rows = rowsFor(sid, game, state);
writeJson(stateFile(sid, gid), state); // keeps the survey for this turn
return send(res, 200, rows);
}
case 'cmd': { // Search
play(sid, game, q('q'));
const state = loadOrStart(sid, game);
state.lastKey = null;
const rows = rowsFor(sid, game, state);
writeJson(stateFile(sid, gid), state);
return send(res, 200, rows);
}
case 'do': {
// A row was opened. Rows carry the turn they were made for, so opening
// the same row again shows the same answer instead of playing it twice.
const key = `${q('t')}|${q('c')}`;
const state = loadOrStart(sid, game);
if (state.lastKey === key) {
const last = state.history[state.history.length - 1];
return send(res, 200, entryHtml(game, last, 'Shown again. Type a command with Search to repeat it.'), 'text/html; charset=utf-8');
}
const entry = play(sid, game, q('c'));
const after = loadOrStart(sid, game);
after.lastKey = key;
writeJson(stateFile(sid, gid), after);
const stale = Number(q('t')) !== state.turn;
return send(res, 200, entryHtml(game, entry, stale ? 'Older command list: go Back and pick the game again to refresh it.' : ''), 'text/html; charset=utf-8');
}