diff --git a/active-games.html b/active-games.html new file mode 100644 index 0000000..e65f3f9 --- /dev/null +++ b/active-games.html @@ -0,0 +1,13 @@ + + + + + + Document + + + + + + + diff --git a/js/DataBase.js b/js/DataBase.js index ea8ba57..2e15e32 100644 --- a/js/DataBase.js +++ b/js/DataBase.js @@ -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; @@ -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); + } + } } diff --git a/js/GameHandeler.js b/js/GameHandeler.js new file mode 100644 index 0000000..c5df3cc --- /dev/null +++ b/js/GameHandeler.js @@ -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!"); + } + } +} diff --git a/js/main.js b/js/main.js index f48ee1e..67d341a 100644 --- a/js/main.js +++ b/js/main.js @@ -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) { @@ -28,8 +37,6 @@ class Main { } } async CreateAccount() { - this.dataBase.SignUpUser(this.email); - const { data: { user }, } = await this.dataBase.supabase.auth.getUser(); @@ -38,8 +45,6 @@ class Main { } async SignInUser() { - this.dataBase.SignInUser(this.email); - const { data: { user }, } = await this.dataBase.supabase.auth.getUser(); @@ -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), ); } }