Create item elements in server, and send to client

develop
Nathan Steel 1 year ago
parent 2da3a75986
commit ed2ecdd2c2

@ -225,6 +225,8 @@ function requestDeck(itemData = null){
// To continue from previous item/itemCount from other funcs, something like this // To continue from previous item/itemCount from other funcs, something like this
let item = []; let item = [];
let itemCount = 0; let itemCount = 0;
let player = {};
let players = [];
// Jank check to allow current client call, and new roomMod.roomGeneration call to // Jank check to allow current client call, and new roomMod.roomGeneration call to
// both work (will replace client call with roomGeneration when closer) // both work (will replace client call with roomGeneration when closer)
@ -235,28 +237,53 @@ function requestDeck(itemData = null){
if(itemData['itemCount'] !== undefined){ if(itemData['itemCount'] !== undefined){
itemCount = itemData['itemCount']; itemCount = itemData['itemCount'];
} }
if(itemData['player'] !== undefined){
player = itemData['player'];
}
if(itemData['players'] !== undefined){
players = itemData['players'];
}
}else{
itemData = {};
} }
// TODO: Deck data/deckIn should be in deckGen, other stuff probably in a cardGen
// function
let deckData = {}; // New but may be useful let deckData = {}; // New but may be useful
let deckIn = {}; // Which deck the item is in? Kinda the player/boardelement thing? let deckIn = {}; // Which deck the item is in? Kinda the player/boardelement thing?
let cardData = {}; let cardData = {};
let boardElement = {}; let boardElement = {};
// New(in here)
let cardStatus = {}; // Tapped, etc
// player
let listPosition = {};
let cardFace = {};
// REALLY NEW
let cardColours = {}; // Array of req.
let cardAttack = {}; // Base, Current
let cardSprite = {}; // Maybe from DB?
// TODO: Set the player. For now will do this in front-end as testing currently // TODO: Set the player. For now will do this in front-end as testing currently
let forPlayer = 0; // TODO: Change to actually have each player select a deck
// Loop and create the deck first // Loop and create the deck first
//console.log(decks);
decks.forEach((deck) => { decks.forEach((deck) => {
item.push(itemCount); // Add new item to add stuff for item.push(itemCount); // Add new item to add stuff for
deckData[itemCount] = {'deckId':deck.deckId, 'playerId':deck.playerId, 'deckName':deck.deckName}; deckData[itemCount] = {'deckId':deck.deckId, 'playerId':deck.playerId, 'deckName':deck.deckName, 'maxLength': 0}; // Max length for listPositioning later in cardBuild
boardElement[itemCount] = 'realDeck'; boardElement[itemCount] = 'realDeck';
itemCount++; cardFace[itemCount] = 0; // Face down for deck
player[itemCount] = forPlayer;
itemCount++; // Needed/good
forPlayer++; // Jank/bad
}) })
console.log(deckData); //console.log(deckData);
console.log(deckList); //console.log(deckList);
// Loop each item in the deckList // Loop each item in the deckList
// Loop inside it X times where X is cardCount // Loop inside it X times where X is cardCount
// Add the builtCard with same cardId as deckList item X times // Add the builtCard with same cardId as deckList item X times
// and load that deck for them. This just sets first deck to player0 to players.length
deckList.forEach((deckListItem) => { deckList.forEach((deckListItem) => {
let deckItem = null; let deckItem = null;
@ -266,9 +293,12 @@ function requestDeck(itemData = null){
// Needs to check deck AND player id, as that's the primary key (together) // Needs to check deck AND player id, as that's the primary key (together)
if(deckData[key].deckId == deckListItem.deckId && deckData[key].playerId == deckListItem.playerId){ if(deckData[key].deckId == deckListItem.deckId && deckData[key].playerId == deckListItem.playerId){
deckItem = key; // Key is the `item` key deckItem = key; // Key is the `item` key
// Now add cards to the player that this deck belongs to
forPlayer = player[key];
} }
}; };
// For each new card, loop to the cardCount (how many cards in deck) // For each new card, loop to the cardCount (how many cards in deck)
// and add to the deck // and add to the deck
for(let i = 0; i < deckListItem.cardCount; i++){ for(let i = 0; i < deckListItem.cardCount; i++){
@ -280,25 +310,49 @@ function requestDeck(itemData = null){
// Associate the card with the deck // Associate the card with the deck
// TODO: Change deckIn to something more sensical // TODO: Change deckIn to something more sensical
deckIn[itemCount] = deckItem; deckIn[itemCount] = deckItem;
// Attempt to set the listPosition of each card in related deck
// Increase the length of the deck in deckItem
listPosition[itemCount] = deckData[deckItem].maxLength; // TODO: better
deckData[deckItem].maxLength++;
// Adding to get everything sorted in one!
cardStatus[itemCount] = null;
cardFace[itemCount] = 0; // Face down by default (in deck)
cardColours[itemCount] = []; // TODO;
// From cardData set the base attack, and current (same when deckbuild)
let atk = cardData[itemCount].atk;
cardAttack[itemCount] = [atk, atk]; // Base, Current
player[itemCount] = forPlayer; // Jank TODO: actually set to correct player
itemCount++; // Increment item to not overwrite itemCount++; // Increment item to not overwrite
} }
}); });
// ADD all new elements, and updated data into itemData
itemData.item = item;
itemData.itemCount = itemCount;
itemData.player = player;
itemData.players = players;
itemData.deckData = deckData;
itemData.deckIn = deckIn;
itemData.cardData = cardData;
itemData.boardElement = boardElement;
itemData.cardStatus = cardStatus;
itemData.listPosition = listPosition;
itemData.cardFace = cardFace;
itemData.cardColours = cardColours;
//itemData.cardSprite = cardSprite;
// item, itemCount, deckData, cardData, boardElement // item, itemCount, deckData, cardData, boardElement
//console.log(cardData); //console.log(cardData);
// Returning everything to be looped in the front-end // Returning everything to be looped in the front-end
// This won't be looped as it will at final, instead just for deck generation // This won't be looped as it will at final, instead just for deck generation
// Returned as object over array, as easier to disect when gets to front-end // Returned as object over array, as easier to disect when gets to front-end
let dataReturn = { let dataReturn = itemData;
item: item,
itemCount: itemCount,
deckData: deckData,
deckIn: deckIn,
cardData: cardData,
boardElement: boardElement,
};
return resolve(dataReturn); return resolve(dataReturn);
//return resolve(cardData); //return resolve(cardData);

@ -825,22 +825,50 @@ function loadBoard(data) {
cardData = data.cardData; cardData = data.cardData;
// position; // Not to do, as calculated per client (and will be per screen-size) // position; // Not to do, as calculated per client (and will be per screen-size)
// size; // Like position, calculated per client. // size; // Like position, calculated per client.
//cardStatus; // TODO: ? Statuses on load?, TODO: OR if player DCs and needs to reload! cardStatus = data.cardStatus;
player = {}; // TODO: Set each item to correct player player = data.player; // Set each item to correct player
listPosition = {}; // TODO: listPosition = data.listPosition;
cardFace = {}; // TODO: Like status, for DCs and reloads cardFace = data.cardFace; // Like status, for DCs and reloads
cardSprite = {}; // TODO: ? Maybe, or this could be done clientside based cardSprite = {}; // TODO: ? Maybe, or this could be done clientside based
// ^ on card id? // ^ on card id?
cardFace = {}; // TODO: For DB/Reconnect more-so
let deckIn = data.deckIn; // NEW, may be used, for now player substitute let deckIn = data.deckIn; // NEW, may be used, for now player substitute
let deckData = data.deckData; let deckData = data.deckData;
let cardAttack = data.cardAttack; // TODO: add to the logic
let cardColours = data.cardColours; // TODO: add to the logic
// TODO: JANK IN, CHANGE CODE TO USE NEW ARRAY!!
// Temp jank, set colour to first colour req.
for(let i = 0; i < itemCount; i++){
if(cardData[i] !== undefined){ // i may not have carddata, as realDeck
cardData[i].colour = 0;//cardData[itemCount].colour[0];
// Set the artwork (this would be done front-end I think)
cardSprite[i] = [0,0];
// Temp sprite set based on colour TODO: Change to set correct sprite from DB
switch (cardData[i].colour){
case 0: // White
cardSprite[i] = [0,0];
break;
case 1: // Blue
cardSprite[i] = [0,1];
break;
case 2: // Red
cardSprite[i] = [1,0];
break;
case 3: // Green
cardSprite[i] = [1,1];
break;
default:
break;
}
}
}
// Stuff not currently in the getDecks TODO: To be stripped down and // Stuff not currently in the getDecks TODO: To be stripped down and
// rewritten/made into generateItems or something, for each match // rewritten/made into generateItems or something, for each match
// Decks can be as-is for getting deckLists for deckselection, but // Decks can be as-is for getting deckLists for deckselection, but
// TODO: for matches need something to generate and return every item, and attribute // TODO: for matches need something to generate and return every item, and attribute
if(false){
// Temp solution // Temp solution
// Loop all items, and set their related missing attributes // Loop all items, and set their related missing attributes
// TODO: some more jank to get it 'playable' with DB entries // TODO: some more jank to get it 'playable' with DB entries
@ -901,6 +929,8 @@ function loadBoard(data) {
} }
} }
} // END FALSE/TEMP DISABLE
shuffleDeck(0); // Shuffle player 0 deck shuffleDeck(0); // Shuffle player 0 deck
shuffleDeck(1); // Shuffle player 1 deck shuffleDeck(1); // Shuffle player 1 deck

@ -61,6 +61,7 @@
<hr> <hr>
<div> <div>
<button onclick="requestStartGame()">(Re)start game</button> <button onclick="requestStartGame()">(Re)start game</button>
<button onclick="requestRoomGeneration()">RequestRoomGeneration</button>
</div> </div>
<script src="/socket.io/socket.io.js"></script> <script src="/socket.io/socket.io.js"></script>

@ -186,3 +186,25 @@ socket.on('responseStartGame', function (data) {
loadBoard(data.message); loadBoard(data.message);
}); });
// More experimenty
function requestRoomGeneration(){
console.log('+ requestRoomGeneration');
socket.emit('requestRoomGeneration');
}
function responseRoomGeneration(data){
if(!data.success){
alert(data.message);
return false;
}
return data;
}
socket.on('responseRoomGeneration', function (data) {
console.log('<< responseRoomGeneration');
responseStartGame(data);
if(data.success !== true){
alert('Err with responseRoomGeneration. '+data.message);
}
// Pass only the data to loadBoard
loadBoard(data.message);
});

@ -15,17 +15,20 @@ function setPlayers(playerCount = 2, itemData){
// Add new item attribute for 'players' // Add new item attribute for 'players'
// TODO: Maybe check if exists, and add to in that case (for replacing people?) // TODO: Maybe check if exists, and add to in that case (for replacing people?)
// Doubt that would ever make it into the game, but could still be worth // Doubt that would ever make it into the game, but could still be worth
itemData.player = {}; //let player = {}; itemData.player = {}; //let player = {}; // Player item belongs to
itemData.players = []; // List of the players (an associated playerItem?)
//itemData.
// Can be done with just referring to itemData[x], but less readable // Can be done with just referring to itemData[x], but less readable
// Will likely redefine vars each new function. For now will keep this as-is // Will likely redefine vars each new function. For now will keep this as-is
let playerNo = 0 + itemData['itemCount']; // Start loop from current itemCount let playerNo = 0 + itemData['itemCount']; // Start loop from current itemCount
playerCount = playerCount + itemData['itemCount']; // End at playerCount diff from itemCount playerCount = playerCount + itemData['itemCount']; // End at playerCount diff from itemCount
for(playerNo; playerNo <= playerCount; playerNo++){ for(playerNo; playerNo < playerCount; playerNo++){
itemData['item'].push(playerNo); itemData['item'].push(playerNo);
itemData['player'][itemData['itemCount']] = playerNo; // The player belongs to itself itemData['player'][itemData['itemCount']] = playerNo; // The player belongs to itself
itemData['itemCount']++; itemData['itemCount']++;
itemData['players'].push(playerNo); // Add player no to array so can be looped
} }
// Return related item and item attributes // Return related item and item attributes
//returns = {'item': item, 'itemCount': itemCount, 'player': player}; //returns = {'item': item, 'itemCount': itemCount, 'player': player};
@ -49,11 +52,11 @@ function roomGeneration(playerCount, teamCount = null, playerTeams = null){
// TODO: Get their selected decks (will need to pass somewhere) // TODO: Get their selected decks (will need to pass somewhere)
// Generate the decks, and card within the deck cardLists // Generate the decks, and card within the deck cardLists
const [deckData] = await Promise.all([ cardGen.requestDeck(itemData) ]); [itemData] = await Promise.all([ cardGen.requestDeck(itemData) ]);
console.log('deckData'); //console.log('deckData');
console.log(deckData); //console.log(deckData);
return resolve(deckData); return resolve(itemData);
})() })()
}); });
} }

@ -58,6 +58,19 @@ function requestStartGame(socket){
io.to(socket.id).emit('responseStartGame', err); io.to(socket.id).emit('responseStartGame', err);
}); });
} }
function requestRoomGeneration(socket){
response = {success: false, message: 'Failed requestRoomGeneration() server.js'};
roomMod.roomGeneration().then(data => {
response.success = true;
response.message = data;
io.to(socket.id).emit('responseRoomGeneration', response);
})
.catch(err => {
response.message = err;
io.to(socket.id).emit('responseRoomGeneration', err);
});
}
function onConnection(socket){ function onConnection(socket){
@ -70,6 +83,9 @@ function onConnection(socket){
socket.on('requestStartGame', function() { socket.on('requestStartGame', function() {
requestStartGame(socket); requestStartGame(socket);
}); });
socket.on('requestRoomGeneration', function() {
requestRoomGeneration(socket);
});
socket.on('requestRooms', function(filter) { socket.on('requestRooms', function(filter) {
requestRooms(socket, filter); requestRooms(socket, filter);

Loading…
Cancel
Save