continues
This commit is contained in:
1 parent
b1ffc5205b
commit
20acd333b9
4 files changed
+55
-4
No files matched your search
+38
-2
@@ -65,6 +65,7 @@ function getQuestionPage(question: Question, urls: QuestionURLS){
|
||||
<div class="field">
|
||||
<div class="control is-expanded">
|
||||
<input class="input" type="text" placeholder="Answer" name="answer"></input>
|
||||
<input type="hidden" name="rendedingTimeStamp" value={Date.now()} ></input>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field is-grouped">
|
||||
@@ -144,14 +145,49 @@ function getUrls(ctx): QuestionURLS{
|
||||
return {nextQuestionUrl, validationUrl, answerQuestionUrl};
|
||||
}
|
||||
|
||||
apiRouter.post("/api/v1/random-questions/:seed/:idx/validate", async (ctx)=> {
|
||||
const answer = (await ctx.request.body().value).get("answer");
|
||||
async function persistAnswer(ctx, form): Promise<void>{
|
||||
const answer = form.get("answer");
|
||||
const rendedingTimeStamp = form.get("rendedingTimeStamp");
|
||||
const answeredTimestamp = Date.now();
|
||||
const kv = await Deno.openKv();
|
||||
const q = getQuestion(ctx);
|
||||
|
||||
const username = ctx.request.headers.get('session');
|
||||
const data = {
|
||||
ip: ctx.request.ip,
|
||||
url: ctx.request.url.toString(),
|
||||
date: Date.now(),
|
||||
domain: ctx.request.url.hostname,
|
||||
hasRightResponse: isValidAnswer(q, answer),
|
||||
answer,
|
||||
rendedingTimeStamp,
|
||||
answeredTimestamp
|
||||
}
|
||||
const baseKey = ["answer", q[1].id, username];
|
||||
for(let i = 0 ; i < 1000; i++){
|
||||
const key = baseKey.concat([i]);
|
||||
const res = await kv.atomic()
|
||||
.check({ key, versionstamp: null }) // `null` versionstamps mean 'no value'
|
||||
.set(key, data)
|
||||
.commit();
|
||||
if (res.ok) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
apiRouter.post("/api/v1/random-questions/:seed/:idx/validate", async (ctx)=> {
|
||||
const form = (await ctx.request.body().value);
|
||||
const answer = form.get("answer");
|
||||
const q = getQuestion(ctx);
|
||||
|
||||
if(isValidAnswer(q, answer)){
|
||||
processValidAnswer(ctx, getUrls(ctx).nextQuestionUrl)
|
||||
}else{
|
||||
processInvalidAnswer(ctx, getUrls(ctx).nextQuestionUrl)
|
||||
}
|
||||
await persistAnswer(ctx, form);
|
||||
|
||||
})
|
||||
|
||||
apiRouter.get("/api/v1/random-questions/:seed/:idx/answer", (ctx)=> {
|
||||
|
||||
Reference in new issue
Block a user