From c3413e1c2c1b181127f75e87e8f4b2f9182a09da Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 17 Oct 2024 11:15:47 +0100 Subject: [PATCH] Add very basic DB access Currently access DB, and return basic SELECT Login details will be changed of course... --- public/board.js | 3 +++ public/index.html | 7 ++++++- public/main.js | 16 ++++++++++++++++ server.js | 21 +++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/public/board.js b/public/board.js index a1383b6..b397e43 100644 --- a/public/board.js +++ b/public/board.js @@ -802,6 +802,9 @@ class Board{ // Run board commands here for testing let board = new Board; +// Await DB promise for decks before loading the game +requestGetCards(); // TODO: Make actually wait for the DB response before continuing... + // Fill each players deck with their cards createDecks(); diff --git a/public/index.html b/public/index.html index 8cc583f..265c1de 100644 --- a/public/index.html +++ b/public/index.html @@ -56,11 +56,16 @@ +
+
+ +
+ - + diff --git a/public/main.js b/public/main.js index 53be0b7..54873d9 100644 --- a/public/main.js +++ b/public/main.js @@ -148,3 +148,19 @@ socket.on('responseJoinRoom', function (name) { } }); +// getCards +function requestGetCards(){ + console.log('+ requestGetCards'); + socket.emit('requestGetCards', playerName); +} +function responseGetCards(data){ + console.log(data); + if(!data.success){ + alert(data.message); + } +} +socket.on('responseGetCards', function (data) { + console.log('<< responseGetCards'); + responseGetCards(data); +}); + diff --git a/server.js b/server.js index de34cb3..9d5658f 100644 --- a/server.js +++ b/server.js @@ -1,4 +1,5 @@ const express = require('express'); +const database = require('./database'); const app = express(); const http = require('http').Server(app); const port = process.env.PORT || 3000; @@ -8,6 +9,7 @@ const io = require('socket.io')(http); app.use(express.static(__dirname + '/public')); http.listen(port, () => console.log('listening on port ' + port)); +database.connect(); io.on('connection', onConnection); @@ -36,6 +38,10 @@ function onConnection(socket){ console.log('+ User connected'); console.log(''); + socket.on('requestGetCards', function() { + requestGetCards(socket); + }); + socket.on('requestRooms', function(filter) { requestRooms(socket, filter); }); @@ -195,3 +201,18 @@ function requestJoinRoom(socket, playerName, roomId){ } +function requestGetCards(socket){ + let response = {'success':false, 'message':'Nothing happened'}; + response.success = true; + + // Await promise, once it's done get the data, if errors send err + database.getCards().then(data => { + response.message = data; + io.to(socket.id).emit('responseGetCards', response); + }) + .catch(err => { + response.message = err; + io.to(socket.id).emit('responseGetCards', err); + }) +} +