Skip to content

Commit

Permalink
Add auto start to front end #231
Browse files Browse the repository at this point in the history
  • Loading branch information
tisboyo committed Sep 5, 2021
1 parent 067f7b0 commit 8a675d1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
8 changes: 7 additions & 1 deletion web/routes/trivia.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ async def trivia_get_question(
return JSONResponse(jsonret)


# @router.post("/trivia/start")
@router.post("/trivia/start")
async def trivia_start(request: Request, key: str = Depends(check_valid_api_key(level=AuthLevel.admin))):
logger.info("Trivia started")
await send_command_to_bot("trivia", ["start"])
return Response(status_code=204)


@router.post("/trivia/end")
async def trivia_end(request: Request, key: str = Depends(check_valid_api_key(level=AuthLevel.admin))):
logger.info("Trivia ended")
Expand Down
2 changes: 1 addition & 1 deletion web/static_files/trivia/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

</head>

<body>
<body onload="startTrivia('start');">

<div id="timer">
<h1></h1>
Expand Down
25 changes: 20 additions & 5 deletions web/static_files/trivia/new.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
(function () {
//Not a great coder look at all my globals
var ActivePoll,
TriviaQuestion,
ChoiceCount,
SubMultiplier;

TriviaQuestion;

// Graciously stolen from sitepoint
// https://www.sitepoint.com/get-url-parameters-with-javascript/
Expand Down Expand Up @@ -67,7 +64,16 @@
return obj;
}


var endTriviaNotified = false
document.addEventListener("visibilitychange", function () {
if (document.visibilityState === 'hidden') {
if (!endTriviaNotified) {
var params = getAllUrlParams()
endTriviaNotified = true;
navigator.sendBeacon('/trivia/end?key=' + params.key);
}
}
});

window.onload = async () => {

Expand Down Expand Up @@ -257,3 +263,12 @@
};
};
})();

function startTrivia(url) {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const key = urlParams.get('key')
if (document.visibilityState === 'visible') {
navigator.sendBeacon('/trivia/start?key=' + key);
}
}

0 comments on commit 8a675d1

Please sign in to comment.