Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Trivia Stats & Add Trivia Game Default #181

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions api/createConsent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ module.exports = function(app) {
});
}else{

const secretKey = process.env.REACT_APP_GAME_RECAPTCHA_SECRET_KEY_V3;
const Recap_url = `https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${token}`;
const recaptchaV3 = await axios.get(Recap_url).then((resp) => resp.data);

const {success, challenge_ts, hostname, score, action} = recaptchaV3;
await db.consent.create({"user_id": user_id, "ip_hash" :ip_hash, "universe": universe, locale: "en", "url": url, "score_bot": score}).catch(err => {
console.error(err);
res.status(500).json({
success: false

const secretKey = process.env.REACT_APP_GAME_RECAPTCHA_SECRET_KEY_V3;
const Recap_url = `https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${token}`;
const recaptchaV3 = await axios.get(Recap_url).then((resp) => resp.data);

const {success, challenge_ts, hostname, score, action} = recaptchaV3;
// console.log("score score",score);

await db.consent.create({"user_id": user_id, "ip_hash" :ip_hash, "universe": universe, locale: "en", "url": url, "score_bot": score}).catch(err => {
console.error(err);
res.status(500).json({
success: false
});
});
});
}


res.status(200).json({
success: true
});
Expand Down
135 changes: 132 additions & 3 deletions api/getAllTriviaScore.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,141 @@

function convertTZ(date, tzString) {
return new Date(
(typeof date === "string" ? new Date(date) : date).toLocaleString(
"en-US",
{ timeZone: tzString }
)
);
}

function consecutiveGames(array) {
let counter = 0;
for (let i = 0; i < (array.length-1); i++) {
if ((array[i] + 1) === array[i + 1]) {
counter++;
} else {
return counter;
}
}
return counter;
}

function maxConsecutiveGames(array) {
let maxCounter = 0;
let counter = 0;
for (let i = 0; i < (array.length-1); i++) {
if ((array[i] + 1) === array[i + 1]) {
counter++;
} else {
if (counter > maxCounter) {
maxCounter = counter;
}
counter = 0;
}
}
if (counter > maxCounter) {
return counter;
}
return maxCounter;
}

module.exports = function(app) {

const {db} = app.settings;

app.get("/api/getAllTriviaScore", async(req, res) => {
app.post("/api/getAllTriviaScore", async(req, res) => {

const {user_id} = req.body;
const allScores = await db.trivia_score.findAll({where: {"user_id": user_id}});

const get_trivia_score = await db.trivia_score.findAll();
res.status(200).json(get_trivia_score);
var myStatistics = {
total: 0,
correct: 0,
numberDays: 0,
numberAttempts: 0,
currentStreak: 0,
maxStreak: 0,
numberWins: 0,
game_ids : [],
win_game_ids : [],
games : []
}

allScores.sort((a, b) => a.createdAt - b.createdAt);

if (allScores.length > 0) {

var game_id = 0;
allScores.forEach((element,index) => {

myStatistics.total = myStatistics.total + 1;

if (index === 0) {

myStatistics.games[game_id] = {
id: game_id,
correct: 0,
played: 0
}

}else{
const difference =
+convertTZ(new Date(element.createdAt)) -
+convertTZ(new Date(allScores[index-1].createdAt));
if (difference > 16000){
// differetGame, nextGame
game_id = game_id + 1;
myStatistics.games[game_id] = {
id: game_id,
correct: 0,
played:0
}
}
}

if (element.current_answer_option === element.correct_answer_option){
myStatistics.correct = myStatistics.correct + 1;

myStatistics.games[game_id].correct = myStatistics.games[game_id].correct + 1;
}
myStatistics.games[game_id].played = myStatistics.games[game_id].played + 1;

myStatistics.game_ids.push(element.game_share_id);
});


myStatistics.games.forEach((element,index) => {
if (element.correct >= 10){
myStatistics.numberWins = myStatistics.numberWins = 1;
myStatistics.win_game_ids.push(element.id);
}
if (element.played > 9){
myStatistics.numberAttempts = myStatistics.numberAttempts + 1;
}
if (myStatistics.games[index].correct === 0){
myStatistics.games[index].score = myStatistics.games[index].correct;
}else{
if (myStatistics.games[index].correct > 10){
myStatistics.games[index].score = 100;
}else{
myStatistics.games[index].score = parseInt(100*(myStatistics.games[index].correct/10));
}

}

});
myStatistics.games.sort((a, b) => b.score - a.score);
myStatistics.bestScore = myStatistics.games[0].score;
myStatistics.numberDays = Array.from(new Set(myStatistics.game_ids)).length;

const gamesU = Array.from(new Set(myStatistics.win_game_ids));
gamesU.sort((a, b) => b - a);
myStatistics.currentStreak = consecutiveGames(gamesU);
myStatistics.maxStreak = maxConsecutiveGames(gamesU)


}
return res.json([myStatistics]);

});

Expand Down
5 changes: 4 additions & 1 deletion api/triviaCSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ module.exports = function(app) {
if (date) {
jsonData = jsonData.filter((el) => el["date"] === date);
}
if (jsonData.length === 0) {
jsonData = csvJSON(data).filter((el) => el["date"] === "2022-1-1");
}

const questions = []
const questions = [];
const letters = ["a","b","c","d"];

jsonData.forEach(function(obj, ind, arr) {
Expand Down
2 changes: 1 addition & 1 deletion app/components/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Search extends Component {
primary_meta: d.occupation,
slug: d.slug
}));
console.log("results", results);
// console.log("results", results);
this.setState({results, query: "", showTrending: true});
});
}
Expand Down
97 changes: 97 additions & 0 deletions app/pages/apps/trivia/Statistics/Statistics.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React, { useEffect, useState } from "react";
import { Button, Classes, Dialog } from "@blueprintjs/core";
import classNames from "classnames";
import styles from "./Statistics.module.scss";

export default function Statistics({
allScores,
isOpenStatistics,
setIsOpenStatistics,
t,
}) {

const acceptClick = async () => {
setIsOpenStatistics(false);
};

useEffect(() => {
if (allScores.length === 0){
setIsOpenStatistics(false);
}
}, []);

return (
<Dialog
isOpen={isOpenStatistics}
id="statisticsD"
key={"statisticsD"}
isCloseButtonShown={false}
title={"Statistics"}
>
<div
key={"dialogbodyStatisticsTrivia"}
className={classNames(Classes.DIALOG_BODY, styles.consentform)}
>
{/* <div
key={"statisticsTriviaTitle"}
className={styles.description}
dangerouslySetInnerHTML={{ __html: " " }}
/> */}
{allScores.length === 0 ? <div></div> :
( <div className="boardS">

<div className="line1">
<div className="bestScoreB">
<span className="bestScore">{allScores[0].bestScore}</span>
<span className="bestScoreLabel">Best Score (%)</span>
</div>
<div className="currentStreakB">
<span className="currentStreak">{allScores[0].currentStreak}</span>
<span className="currentStreakLabel">Current Streak</span>
</div>
<div className="maxStreakB">
<span className="maxStreak">{allScores[0].maxStreak}</span>
<span className="maxStreakLabel">Max Streak</span>
</div>
</div>
<br/>
<div className="line2">

<div className="numberAttemptsB">
<span className="numberAttempts">{allScores[0].numberAttempts}</span>
<span className="numberAttemptsLabel">Attempts</span>
</div>

<div className="numberDaysB">
<span className="numberDays">{allScores[0].numberDays}</span>
<span className="numberDaysLabel">Days</span>
</div>

<div className="numberWinsB">
<span className="numberWins">{allScores[0].numberWins}</span>
<span className="numberWinsLabel">Wins</span>
</div>

</div>
</div>)}


<div
id={"consentbuttons2"}
key={"consentbuttons2"}
className={classNames(styles.options, "consentbuttons2")}
>
<Button
key={"closeStats"}
id={"closeStats"}
className={classNames(styles.button, styles.lite)}
onClick={acceptClick}
>
Close
</Button>

</div>
</div>
</Dialog>
);
}
Loading