-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API for submitting game results (does not yet trigger reconcil…
…iation).
- Loading branch information
Showing
3 changed files
with
138 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import httpErrors from 'http-errors' | ||
import { getUserGameRecord, setReportedResults } from '../models/games-users' | ||
import createThrottle from '../throttle/create-throttle' | ||
import throttleMiddleware from '../throttle/middleware' | ||
|
||
const throttle = createThrottle('gamesResults', { | ||
rate: 10, | ||
burst: 30, | ||
window: 60000, | ||
}) | ||
|
||
export default function (router) { | ||
router.post( | ||
'/:gameId', | ||
throttleMiddleware(throttle, ctx => ctx.ip), | ||
submitGameResults, | ||
) | ||
} | ||
|
||
// TODO(tec27): clients also need to report the assigned races for each player | ||
async function submitGameResults(ctx, next) { | ||
const { gameId } = ctx.params | ||
const { userId, resultCode, results } = ctx.request.body | ||
|
||
if (userId == null) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tec27
Author
Member
|
||
throw new httpErrors.BadRequest('userId must be specified') | ||
} else if (!resultCode) { | ||
throw new httpErrors.BadRequest('resultCode must be specified') | ||
} else if (!results) { | ||
throw new httpErrors.BadRequest('results must be specified') | ||
} else if (!Array.isArray(results) || !results.length) { | ||
throw new httpErrors.BadRequest('results must be a non-empty array') | ||
} | ||
|
||
// TODO(tec27): Check that the results only contain players that are actually in the game | ||
for (const result of results) { | ||
if ( | ||
!Array.isArray(result) || | ||
result.length !== 2 || | ||
isNaN(result[1]) || | ||
result[1] < 0 || | ||
result[1] > 3 | ||
) { | ||
throw new httpErrors.BadRequest('results are incorrectly formatted') | ||
} | ||
} | ||
|
||
const gameRecord = await getUserGameRecord(userId, gameId) | ||
if (!gameRecord || gameRecord.resultCode !== resultCode) { | ||
// TODO(tec27): Should we be giving this info to clients? Should we be giving *more* info? | ||
throw new httpErrors.NotFound('no matching game found') | ||
} | ||
|
||
if (gameRecord.reportedResults) { | ||
throw new httpErrors.Conflict('results already reported') | ||
} | ||
|
||
await setReportedResults({ userId, gameId, reportedResults: results, reportedAt: new Date() }) | ||
|
||
ctx.status = 204 | ||
|
||
// TODO(tec27): check if this game now has complete results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Btw, I read this comment by Dan Abramov about loose checks causing engine deoptimizations while randomly browsing the React repo a while ago: facebook/react#12534 (comment)
Not sure if by "engine" they meant some internal React engine, or the JS engine in general. In either case, I feel like being more specific (or actually less specific since we can just check for a
falsy
value here?) wouldn't hurt.