Skip to content

Commit

Permalink
some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBerglund05 committed Dec 11, 2024
1 parent 786aa77 commit ac6485c
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 54 deletions.
23 changes: 22 additions & 1 deletion css/main.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion css/main.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion css/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ h6 {
background-color: $white;
}

.white-text {
color: $white;
}

.header {
color: $black;
display: flex;
Expand All @@ -49,6 +53,7 @@ h6 {
border: 0;
border-radius: 5px;
padding: 1em 3em 1em 3em;
text-decoration: none;
}
.marigin-big {
margin-top: 50svh;
Expand All @@ -72,18 +77,35 @@ h6 {

.grid-1-col {
display: grid;
grid-template-columns: 10em;
justify-content: center;
}

.center-text {
text-align: center;
}

.marigin-small {
margin-top: 10em;
}

.points-card {
padding: 2em;
border-radius: 1em;
}

.points {
font: 2em bold;

}

button:hover {
cursor: pointer;
}

.background-primary {
background-color: $primary-color;
}

@media (width < 45em) {
.question-grid {
margin-left: 1em;
Expand Down
28 changes: 19 additions & 9 deletions game.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,38 @@
<h1 id="question" class="header-h1 marign-none"></h1>
</header>
<section class="marigin-big">
<form action="submit" class="question-grid">
<button id="btn-1" class="answer-btn"></button>
<button id="btn-2" class="answer-btn"></button>
<button id="btn-3" class="answer-btn"></button>
<button id="btn-4" class="answer-btn"></button>
</form>
<div class="question-grid">
<button type="button" id="btn-1" class="answer-btn"></button>
<button type="button" id="btn-2" class="answer-btn"></button>
<button type="button" id="btn-3" class="answer-btn"></button>
<button type="button" id="btn-4" class="answer-btn"></button>
</div>
</section>
</article>
</template>
<template id="points">
<article class="question-vh">
<header class="header">
<h1 class="header-h1 marign-none">Poäng!</h1>
<h1 id="points-header" class="header-h1 marign-none">Dit resultat</h1>
</header>
<section class="grid-1-col marigin-medium">
<p class="center-text">Du fick:</p>
<div class="background-primary points-card">
<h2 class="points-card-header center-text white-text">Dina poäng är:</h2>
<p id="set-points" class="center-text points white-text"></p>
</div>
</section>
<footer class="marigin-small">
<nav aria-label="navigation" class="grid-1-col">
<a href="./index.html" class="answer-btn">Gå till baka</a>
</nav>
</footer>
</article>
</template>
</head>
<body class="white-bg marign-none">
<div id="container"></div>
<main id="container">

</main>
<script type="module" src="./js/main.js"></script>
</body>
</html>
11 changes: 6 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="manifest" href="manifest.json" />
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/bootstraps/bootstrap.min.css" />
<title>Document</title>
</head>
<body class="d-flex justify-content-center">
<p>hej</p>
<button type="submit" id="btnDBRequest">press here</button>
<p id="dBData"></p>

<body class="white-bg">
<h1 class="center-text">Kunskapskampen!</h1>
<nav class="grid-1-col marigin-big">
<a href="./game.html" class="answer-btn" id="btnDBRequest">Spela</a>
</nav>
<script type="module" src="./js/main.js"></script>
</body>
</html>
3 changes: 1 addition & 2 deletions js/DataBase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default class DataBase {
async GetQuestion(category) {
fetch("https://eel-simple-highly.ngrok-free.app/api/question", {
await fetch("https://eel-simple-highly.ngrok-free.app/api/question", {
method: "POST",
headers: {
accept: "application/json",
Expand All @@ -13,7 +13,6 @@ export default class DataBase {
.then((response) => response.json())
.then((json) => {
sessionStorage.setItem("question", JSON.stringify(json));
return JSON.stringify(json);
});
}
}
5 changes: 4 additions & 1 deletion js/EventManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export default class EventManager {
EventListener(element, eventType, method) {
if (element) {
element.addEventListener(eventType, method);
element.addEventListener(eventType, (event) => {
event.preventDefault();
method(event.target.textContent);
});
}
}
}
132 changes: 104 additions & 28 deletions js/GameHandeler.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,123 @@
export default class GameHandeler {
constructor() {}
async CreateGame(dataBase) {
const categori = ["geografi"];
const amountsOfCategories = categori.length;
const minCeiled = Math.ceil(0);
const maxFloored = Math.floor(amountsOfCategories);
const randomQuestion = Math.floor(
Math.random() * (maxFloored - minCeiled) + minCeiled,
);
let questionsAnswerd = 0;
let placedAnswers = [0, 1, 2, 3];
let answer;
constructor() {
this.gameContainer = document.getElementById("container");
this.questionsAnswerd = 0;
this.points = 0;
}

ShuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

async CreateGameScreen() {
let answersLeftToPlace = [0, 1, 2, 3];
this.ShuffleArray(answersLeftToPlace);

const data = await dataBase.GetQuestion(categori[randomQuestion]);
console.log(data);
const json = JSON.parse(sessionStorage.getItem("question"));

this.questionsAnswerd = parseInt(
sessionStorage.getItem("questionsAnswerd") || 0,
);

const template = document.getElementById("answer-form");
console.log("Before rendering - questionsAnswerd:", this.questionsAnswerd);
console.log("Total questions in JSON:", json.questions.length);

// Clone it (this is where the magic happens!)
const questionHtml = template.content.cloneNode(true).firstElementChild;
// Get the existing template content or create new if not exists
let questionHtml = this.gameContainer.querySelector(".question-vh");
if (!questionHtml) {
const template = document.getElementById("answer-form");
questionHtml = template.content.cloneNode(true).firstElementChild;
this.gameContainer.appendChild(questionHtml);
}

// Update the content
// Update the content directly in the existing elements
const question = questionHtml.querySelector("#question");
const answers = [
const answersBtn = [
questionHtml.querySelector("#btn-1"),
questionHtml.querySelector("#btn-2"),
questionHtml.querySelector("#btn-3"),
questionHtml.querySelector("#btn-4"),
];

question.textContent = json[questionsAnswerd].question;
const answer = [
json.questions[this.questionsAnswerd].answer1,
json.questions[this.questionsAnswerd].answer2,
json.questions[this.questionsAnswerd].answer3,
json.questions[this.questionsAnswerd].answer4,
];

// Update question text
question.textContent = json.questions[this.questionsAnswerd].question;

// Update answer buttons
for (let i = 0; i < 4; i++) {
answer = Math.floor(
Math.random() * (placedAnswers.length - minCeiled) + minCeiled,
);
placedAnswers.splice(answer, 1);
answers[i].textContent = json[questionsAnswerd]["answer" + (i + 1)];
answersBtn[i].textContent = answer[answersLeftToPlace[i]];
}

console.log(questionHtml, json);
// Log to verify
console.log(
"Updated question:",
json.questions[this.questionsAnswerd].question,
"questions answerd: ", this.questionsAnswerd,
);
}

CreatePointsScreen() {
const gameScreenParentNode = document.getElementById("container");
const gameScreenNode = gameScreenParentNode.querySelector(".question-vh");
const deletedChildNode = gameScreenParentNode.removeChild(gameScreenNode);

return questionHtml;
const template = document.getElementById("points");

// Clone it (this is where the magic happens!)
const pointsHtml = template.content.cloneNode(true).firstElementChild;

// Update the content
const heading = pointsHtml.querySelector("#points-header");
const pointsSetter = pointsHtml.querySelector("#set-points");

pointsSetter.textContent = sessionStorage.getItem("points");

console.log(pointsHtml);

sessionStorage.setItem("questionsAnswerd", 0);
sessionStorage.setItem("points", 0);

this.gameContainer.appendChild(pointsHtml);
}

ButtonAnswer(answer) {
const json = JSON.parse(sessionStorage.getItem("question"));
this.questionsAnswerd = parseInt(
sessionStorage.getItem("questionsAnswerd") || 0,
);

console.log("Current question index:", this.questionsAnswerd);
console.log("Total questions:", json.questions.length);
console.log("Submitted answer:", answer);
console.log("Correct answer:", json.questions[this.questionsAnswerd].answer1);

console.log(json, "oihqawidj", answer);

if (json.questions[this.questionsAnswerd].answer1 == answer) {
this.points = sessionStorage.getItem("points");
this.points++;
sessionStorage.setItem("points", this.points);
}

// Increment questionsAnswerd before checking
this.questionsAnswerd++;
sessionStorage.setItem("questionsAnswerd", this.questionsAnswerd);

// Check if we've reached the total number of questions
if (this.questionsAnswerd < json.questions.length) {
this.CreateGameScreen();
} else {
this.CreatePointsScreen();
}
}
}
Loading

0 comments on commit ac6485c

Please sign in to comment.