Skip to content

Commit

Permalink
Merge pull request #55 from JoelBerglund05/CreateGames
Browse files Browse the repository at this point in the history
Create games
  • Loading branch information
JoelBerglund05 authored Nov 12, 2024
2 parents e5390d6 + f9a8268 commit 51f5296
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 20 deletions.
13 changes: 13 additions & 0 deletions active-games.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="createGame">Sök motståndare</button>
<button id="tmp">wdojwepdok</button>
<script src="./js/main.js" type="module"></script>
</body>
</html>
66 changes: 58 additions & 8 deletions js/DataBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ export default class DataBase {
const { data, error } = await this.supabase
.from(table)
.select()
.eq("id", idToRow);

if (error) {
console.log("Kunde inte hämta data: ", error);
return;
}

return data;
}

async SignUpUser(email, password) {

if (password[0] !== password[1]) {
alert("Lösenorden är inte samma");
return;
Expand All @@ -38,21 +37,72 @@ export default class DataBase {
}
}

async SignInUser(email) {
async SignInUser(email, password) {
const { data, error } = await this.supabase.auth.signInWithOtp({
email: email,
password: password,
email: email.value,
password: password.value,
});
if (error) {
console.log("Kunde inte logga in användaren: ", error);
console.log("swqeawd ", error);
}
console.log(this.supabase.auth.getUser());
console.log(error);
}

async LogOutUser() {
const { error } = await this.supabase.auth.signOut();
let { error } = await this.supabase.auth.signOut();
if (error) {
console.log("kunde inte logga ut: ", error);
}
}

async GetUser() {
const {
data: { user },
} = await this.supabase.auth.getUser();
return user;
}

async CreateGame() {
const initialPoints = 0;
const user = await this.GetUser();

const { data, error } = await this.supabase
.from("activeGames")
.insert([
{
player1Points: initialPoints,
player2Points: initialPoints,
playerTurn: user.id,
userId1: user.id,
},
])
.select();

if (error) {
console.log("Kunde inte spara data: ", error);
}
}

async GetAllActiveGames() {
const { data, error } = await this.supabase.from("activeGames").select();

if (error) {
console.log("Kunde inte logga ut användaren: ", error);
console.log("Kunde inte hämta data: ", error);
}
}

async UpdateSpesificActiveGame(activeGameId) {
const user = await this.GetUser();

const { data, error } = await this.supabase
.from("activeGames")
.update({ userId2: user.id })
.eq("id", activeGameId)
.select();

if (error) {
console.log("Kunde inte updatera data: ", error);
}
}
}
31 changes: 31 additions & 0 deletions js/GameHandeler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default class GameHandeler {
constructor() {}
async CreateGame(dataBase) {
const activeGames = await dataBase.GetAllActiveGames();
const user = await dataBase.GetUser();

let foundEmptyGame = false;
activeGames.forEach(async (emptyId) => {
if (
emptyId.userId2 === null &&
!foundEmptyGame &&
emptyId.userId1 !== user.id
) {
console.log(
"Stämmer detta? ",
emptyId.userId1 !== user.id,
user.id,
emptyId.userId1,
);
foundEmptyGame = true;
emptyId.userId2 = user.id;
await dataBase.UpdateSpesificActiveGame(emptyId.id);
}
});

if (!foundEmptyGame) {
dataBase.CreateGame();
console.log("yeppi!");
}
}
}
32 changes: 20 additions & 12 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import DataBase from "./DataBase.js";
import EventManager from "./EventManager.js";
import GameHandeler from "./GameHandeler.js";

class Main {
constructor() {
this.dataBase = new DataBase();
this.eventManager = new EventManager();
this.gameHandeler = new GameHandeler();
this.btnDBRequest = document.getElementById("btnDBRequest");
this.displayData = document.getElementById("dBData");
this.btnCreateAccount = document.getElementById("createAccount");
this.email = document.getElementById("email");
this.password = [
document.getElementById("password1"),
document.getElementById("password2"),
];
this.btnSignIn = document.getElementById("signIn");
this.btnCreateGame = document.getElementById("createGame");

this.tmp = document.getElementById("tmp");
}
RegisterServiceWorker() {
if ("serviceWorker" in navigator) {
Expand All @@ -28,8 +37,6 @@ class Main {
}
}
async CreateAccount() {
this.dataBase.SignUpUser(this.email);

const {
data: { user },
} = await this.dataBase.supabase.auth.getUser();
Expand All @@ -38,8 +45,6 @@ class Main {
}

async SignInUser() {
this.dataBase.SignInUser(this.email);

const {
data: { user },
} = await this.dataBase.supabase.auth.getUser();
Expand All @@ -50,22 +55,25 @@ class Main {
}
Main() {
const clickEvent = "click";
this.eventManager.EventListener(this.tmp, clickEvent, () => {
console.log(this.dataBase);
});
this.RegisterServiceWorker();

this.eventManager.EventListener(this.btnDBRequest, clickEvent, () =>
this.DisplayQuestion(1),
);

this.eventManager.EventListener(
this.btnCreateAccount,
clickEvent,
this.CreateAccount.bind(this),
this.eventManager.EventListener(this.btnCreateAccount, clickEvent, () =>
this.dataBase.SignUpUser(this.email, this.password),
);

this.eventManager.EventListener(this.btnSignIn, clickEvent, () =>
this.dataBase.SignInUser(this.email, this.password),
);

this.eventManager.EventListener(
this.btnSignIn,
clickEvent,
this.SignInUser.bind(this),
this.eventManager.EventListener(this.btnCreateGame, clickEvent, () =>
this.gameHandeler.CreateGame(this.dataBase),
);
}
}
Expand Down

0 comments on commit 51f5296

Please sign in to comment.