const express = require('express'); const database = require('./database'); const app = express(); const http = require('http').Server(app); const port = process.env.PORT || 3000; const io = require('socket.io')(http); // util is what nodejs uses for console.log, but has a depth of 2 set // so console.logs show [Array]/[Object] instead of useful info. // This can be overridden console.log(util.inspect(LOGDATA, true, 4, true)) // 4 being new depth, true (last one) is to show colours const util = require('util') app.use(express.static(__dirname + '/public')); http.listen(port, () => console.log('listening on port ' + port)); database.connect(); io.on('connection', onConnection); // Variables let numRooms = 0; let numRoomsToPreGen = 1; const maxRooms = 3; const maxPlayersPerRoom = 2; const maxSpectatorsPerRoom = 0; // All the room //let data = []; // Normal array let data = {}; // Object array (this one for returning to player, and JSON stringify while keeping named ids) let roomData = {}; for (let roomId = 1; roomId <= numRoomsToPreGen; roomId++) { // Never have more rooms than max rooms!!! if(numRooms > maxRooms){ break; } createRoom(roomId); } // For testing DB/makeshift ORM/building my objects // Maybe if this works, put it into a deck and card module // Doing similar to I have to other stuff, that will need to be // migrated serverside anyways. let item = []; let deckItem = {}; let deckList = {}; let cardItem = {}; // cardClass, cardColourRequirement // may want to be seperate too (as well as in cardItem), so that // during match they can be altered by effects while keeping the OG card // for inspecting (and compare against new stats,reqs,etc.) // same with attack, cost, etc. things that will can be visually shown as // changed in game // Just grabbing everything from DB for now, as it's quite small at current // then will rejig when needed. // Should all cards, effects, classes etc. be loaded in on server start // then just load decks and decklists when needed? function getDecks(deckId, playerId){ // Await promise, once it's done get the data, if errors send err database.dbGetDecks().then(data => { data.forEach((deck) => { let itemId = item.length; item.push(itemId); // Add the next available item // Add the deck info to deckItem deckItem[itemId] = { 'deckId': deck.deckId, 'playerId': deck.playerId, 'deckName': deck.deckName, }; }); //console.log(item); //console.log(deckItem); }) .catch(err => { throw err; }) } getDecks(); function getDeckList(){ database.dbGetDeckList().then(data => { data.forEach((listItem) => { let itemId = item.length; item.push(itemId); // Add the next available item // Add the deck info to deckItem deckList[itemId] = { 'cardId': listItem.cardId, 'cardCount': listItem.cardCount, }; }); //console.log(item); //console.log(deckList); }) .catch(err => { throw err; }) } getDeckList(); function getCards(){ console.log database.dbGetCards().then(data => { data.forEach((card) => { let itemId = item.length; item.push(itemId); // Add the next available item // Add the deck info to deckItem cardItem[itemId] = { 'cardId': card.id, 'cardName': card.cardName, 'cardCost': card.cardCost, 'cardType': card.cardType, 'cardAttack': card.cardAttack, 'cardRarity': card.cardRarity, 'cardClass': [], 'cardColourRequirement': [], }; }); //console.log(item); //console.log(cardItem); }) .catch(err => { throw err; }) } getCards(); function getCardClasses(){ database.dbGetCardClasses().then(data => { data.forEach((cardClass) => { // Loop existing cardItems, compare cardId // If there's a match add cardClass to the card? for(let i = 0; i < item.length; i++){ // If the item has a cardItem if(i in cardItem){ // And the cardId matches the linked table if(cardItem[i].cardId == cardClass.cardId){ cardItem[i].cardClass.push(cardClass.classId); } } } }); //console.log(item); //console.log(cardItem); }) .catch(err => { throw err; }) } getCardClasses(); function getCardColourRequirement(){ database.dbGetCardColourRequirement().then(data => { data.forEach((cardColourReq) => { // Loop existing cardItems, compare cardId // If there's a match add cardClass to the card? for(let i = 0; i < item.length; i++){ // If the item has a cardItem (needed to add reqs.) if(i in cardItem){ // And the cardId matches the linked table if(cardItem[i].cardId == cardColourReq.cardId){ cardItem[i].cardColourRequirement.push([cardColourReq.colourId, cardColourReq.cost]); } } } }); //console.log(item); console.log(util.inspect(cardItem, true, 4, true)); //console.log(cardItem); }) .catch(err => { throw err; }) } getCardColourRequirement(); // Then effects which will have effects with parent triggers, and unit type checks // colour checks, all sorts. So will be more difficult. Basic (flight, etc) // shouldn't be too bad // something like effect_basic, card_effect, effect_trigger, effect_requirement, effect_option // effect_stage, effect_advanced, with advanced being an id with x triggers, triggers have // x req, advanced as with x options, x stages to be able to fine-tune fancy stuff // combining all other effects, units, cards, colours, etc. Will be a lot of though, // but better than hard coding anything more than basic effects and effect check logic // TODO: effect (as above) // End For testing function onConnection(socket){ console.log('+ User connected'); console.log(''); socket.on('requestGetCards', function(deckId, playerId) { requestGetCards(socket, deckId, playerId); }); socket.on('requestRooms', function(filter) { requestRooms(socket, filter); }); socket.on('requestJoinRoom', function(playerName, roomId) { requestJoinRoom(socket, playerName, roomId); }); socket.on('requestCreateRoom', function(playerName) { requestCreateRoom(socket, playerName); }); } function requestRooms(socket, filter){ console.log('+ requestRooms recieved'); console.log('- filter: '+filter); let response = getRooms(filter, dump = true); io.to(socket.id).emit('returnRooms', response); console.log(''); } function getRooms(filter = 'all', dump = false){ console.log('+ getRooms'); let response = { random: 'randomStuff', roomData: roomData, }; if(dump){ console.log(response); console.log(''); } return response; } function requestCreateRoom(socket, playerName){ console.log('+ createRoom recieved'); console.log('- requested by: '+playerName); response = createRoom(roomId = false, dump = true); io.to(socket.id).emit('returnCreateRoom', response); if(response.success){ let response = getRooms(filter = 'all', dump = true); io.to(socket.id).emit('returnRooms', response); } console.log(''); } function createRoom(roomId = false, dump = true){ let roomName = false; if(roomId == false){ roomId = numRooms + 1; } console.log(roomId); let response = { success: false, message: 'No idea bossman' }; // Max room limit reached console.log(numRooms); console.log(maxRooms); if(numRooms >= maxRooms){ console.log('- Room limit reached'); response = { success: false, message: 'No space '+numRooms+' out of '+maxRooms+' created.' }; // Create room }else{ console.log('- Creating room') let room = {}; room['id'] = roomId; room['name'] = 'Room:'+room['id']; roomName = room['name']; room['password'] = ''; room['timeout'] = {}; room['timeout']['s'] = 10; room['people'] = 0; //room['deck'] = []; //room['turn'] = 0; // Removed players for now, players may be seperate // and back-end only with an assoc. to current room //let players = {}; //for (let j = 0; j < maxPlayersPerRoom; j++) { //let p = {}; //p['id'] = 0; //p['name'] = ""; //p['hand'] = {}; //players[j] = p; //} //room['players'] = players; roomData[roomId] = room; numRooms = numRooms + 1; response = { success: true, message: 'Room Created: '+roomName, }; } if(dump){ console.log(response); console.log(''); } return response; } // TODO: break into requestJoinRoom, and JoinRoom? // Maybe not needed here? As won't be done via backend? function requestJoinRoom(socket, playerName, roomId){ console.log('+ requestJoinRoom recieved'); socket.playerName = playerName; for (let i = 1; i <= numRooms; i++) { let name = 'Room_' + i; let people = roomData[i]['people']; console.log('- people: '+people); console.log('- maxPlayersPerRoom: '+maxPlayersPerRoom); if (true || people < maxPlayersPerRoom) { people = roomData[i]['people'] += 1; socket.join(name); console.log('>> User ' + socket.playerName + ' connected on ' + name + ' (' + (people) + '/' + maxPlayersPerRoom + ')'); io.to(socket.id).emit('responseJoinRoom', name); if (people >= maxPlayersPerRoom) { console.log('- starting game'); //startGame(name); } return; } } io.to(socket.id).emit('responseRoom', 'error'); console.log('>> Rooms exceeded'); } // Change to Decks, or 'getPlayerDecks' for the game // Could also be a specific deck if player clicks into one? Although maybe when entering // decks they get all their decks (maybe just id actually) then load from there? function requestGetCards(socket, deckId, playerId){ console.log(deckId); console.log(playerId); let response = {'success':false, 'message':'Nothing happened'}; response.success = true; // Await promise, once it's done get the data, if errors send err database.getCardsFromDeck(deckId, playerId).then(data => { console.log(data); response.message = data; io.to(socket.id).emit('responseGetCards', response); }) .catch(err => { response.message = err; io.to(socket.id).emit('responseGetCards', err); }) }