You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.9 KiB
JavaScript
111 lines
2.9 KiB
JavaScript
// Build a room, fill will players, etc.
|
|
const cardGen = require('./cardGen');
|
|
const components = require('./components');
|
|
|
|
// Room should, setPlayers, add them to correct team (TODO), build their decks, and first shuffle
|
|
|
|
function startItemCount(){
|
|
|
|
let item = [];
|
|
let itemCount = 0;
|
|
|
|
returns = {'item': item, 'itemCount': itemCount};
|
|
return(returns);
|
|
}
|
|
|
|
function setPlayers(playerData, itemData){
|
|
|
|
itemData.player = {}; //let player = {}; // Player item belongs to
|
|
itemData.players = []; // List of the players (an associated playerItem?)
|
|
|
|
let playerNo = 0 + itemData['itemCount']; // Start loop from current itemCount
|
|
playerCount = playerData.length + itemData['itemCount']; // End at playerCount diff from itemCount
|
|
let i = 0;
|
|
for(playerNo; playerNo < playerCount; playerNo++){
|
|
|
|
itemData['players'].push([playerNo, playerData[i]]); // Add player no to array so can be looped
|
|
i++;
|
|
|
|
}
|
|
|
|
return itemData;
|
|
}
|
|
|
|
// For future, when 2v2s, and 5v1 Raids, etc.
|
|
function setTeams(){
|
|
|
|
}
|
|
|
|
function roomGeneration(roomId){
|
|
return new Promise((resolve, reject) => {
|
|
(async () => {
|
|
|
|
// Player's sockets
|
|
console.log('--- Room for generation ---');
|
|
console.log(io.sockets.adapter.rooms.get(roomId));
|
|
|
|
let itemData = startItemCount();
|
|
|
|
// Player data with Sockets
|
|
let playerData = [];
|
|
let playerOrder = {}; // Need a better name
|
|
let i = 1;
|
|
let clients = global.io.sockets.adapter.rooms.get(roomId);
|
|
for (const clientId of clients) {
|
|
const clientSocket = global.io.sockets.sockets.get(clientId);
|
|
|
|
// Which order the player is? Just using the array
|
|
playerOrder[clientSocket.playerId] = playerData.length;
|
|
|
|
playerData.push({
|
|
'playerDataId': playerData.length
|
|
,'playerId': clientSocket.playerId
|
|
,'deck':{'playerId':i,'deckId':1}
|
|
,'socketId': clientSocket.id // TODO: ONLY FOR SERVERSIDE!!!
|
|
});
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
// Add players for the room (seperate to playerData, currently what's used)
|
|
// ??
|
|
itemData = setPlayers(playerData, itemData);
|
|
|
|
// TODO: Get their selected decks
|
|
|
|
// Add all the empty components to the room itemData
|
|
itemData.component = components.component;
|
|
|
|
// Generate the decks, and card within the deck cardLists
|
|
[itemData] = await Promise.all([ cardGen.requestDeck(itemData) ]);
|
|
|
|
// Some room stuff, likely change this
|
|
itemData.roomId = roomId;
|
|
itemData.turn = 0; // The turn count of the match
|
|
itemData.playersTurn = 0; // This means it's playerData[0] turn
|
|
|
|
// Room has just been created, so add the itemData and playerData to the room
|
|
// on server-side so it's easily accessible
|
|
roomData[roomId].itemData = itemData;
|
|
roomData[roomId].playerData = playerData;
|
|
roomData[roomId].playerOrder = playerOrder;
|
|
|
|
// Return the all the itemData to the client(s)
|
|
// TODO: This will need to give different data for each, or at least
|
|
// to differ the boardside
|
|
return resolve(itemData);
|
|
})()
|
|
});
|
|
}
|
|
|
|
// TODO: disconnect, reconnect, resume
|
|
|
|
module.exports = {
|
|
startItemCount
|
|
,setPlayers
|
|
,roomGeneration
|
|
};
|
|
|