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.
cardGame/server.js

132 lines
3.8 KiB
JavaScript

const express = require('express');
const database = require('./database');
const rooms = require('./rooms');
const gameMod = require('./gameMod');
const app = express();
const http = require('http').Server(app);
const port = process.env.PORT || 3000;
const io = require('socket.io')(http);
global.io = io;
// To log the player sockets, so they can be easily referred to
// maybe jank, but can't see alternative currently
global.playerSocket = {};
global.roomData = {}; // Made global for now, as to not replicate. Maybe sub-optimal?
// 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);
function onConnection(socket){
console.log('+ User connected');
console.log('');
// Rooms (joining, creating, etc)
socket.on('requestRooms', function(filter) {
rooms.requestRooms(socket, filter);
});
socket.on('requestJoinRoom', function(playerName, roomId) {
rooms.requestJoinRoom(socket, playerName, roomId);
});
socket.on('requestCreateRoom', function(playerName) {
rooms.requestCreateRoom(socket, playerName);
});
// Game (actual things relating to the game)
// The socket should only be in one game, so socket.on should
// do this, but passing room/player anyways as it's how I've written some
// roomData bits. TODO: Look if I can do this better...
socket.on('passTurn', function(roomId, playerId) {
gameMod.passTurn(roomId, playerId);
});
socket.on('drawACard', function(roomId, playerId) {
gameMod.drawACard(roomId, playerId);
});
}
global.getPlayerSocketFromRoom = function(playerId, roomId){
return roomData[roomId].playerData[playerId].socketId;
}
// Globals for easier clientside alerts/logs, etc.
global.socketAlert = function(socket, message, type = 'alert'){
global.io.to(socket).emit(
type, {'message': message}
);
}
// passTurn response for all clients in room
// TODO: include spectators when added
global.socketResponsePassTurn = function(roomId){
let clients = global.io.sockets.adapter.rooms.get(roomId);
for (const clientId of clients) {
const clientSocket = global.io.sockets.sockets.get(clientId);
console.log('>> '+clientSocket.playerId+' passed turn');
// Send the data back. Whose turn it is, and what turn
let turnData = {
'playerTurn': global.roomData[roomId].itemData.component.playerTurn,
'turn': global.roomData[roomId].itemData.component.turn,
};
global.io.to(clientSocket.id).emit('responsePassTurn', turnData);
}
}
// Then emit the deckSize and hand size to all the player's sockets
// TODO: spectators in future, won't comment about this from now on
global.socketResponseDrawCard = function(roomId, playerId){
let clients = global.io.sockets.adapter.rooms.get(roomId);
for (const clientId of clients) {
const clientSocket = global.io.sockets.sockets.get(clientId);
console.log('>> '+roomData[roomId].playerData[playerId].playerId+' drew card');
// Send the data back
global.io.to(clientSocket.id).emit(
'responseDrawCard'
,global.roomData[roomId].itemData.component.cardCount
);
}
}
global.socketResponsePlayerDrewCard = function(roomId, playerId){
// This function may be redundant fast, as hand, board, grave, void
// anywhere that's visible to one (or both) players will need all the data
// sending, so will likely be one function that returns it all?
// Could do each thing as their own, and union the data too?
global.io.to(global.getPlayerSocketFromRoom(playerId, roomId)).emit(
'responsePlayerDrewCard'
,gameMod.getPlayerHandData(roomId, playerId)
);
}