Add very basic DB access

Currently access DB, and return basic SELECT
Login details will be changed of course...
develop
Nathan Steel 1 year ago
parent 261d737601
commit c3413e1c2c

@ -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();

@ -56,11 +56,16 @@
</select>
</div>
<hr>
<div>
<button onclick="requestGetCards()">Get Cards (DB)</button>
</div>
<script src="/socket.io/socket.io.js"></script>
<script src="/cards.js"></script><!-- Temp until DB -->
<script src="/shapes.js"></script>
<script src="/board.js"></script>
<script src="/main.js"></script>
<script src="/board.js"></script>
</body>
</html>

@ -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);
});

@ -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);
})
}

Loading…
Cancel
Save