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.
67 lines
1.9 KiB
JavaScript
67 lines
1.9 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);
|
|
});
|
|
|
|
}
|
|
|
|
// Globals for easier clientside alerts/logs, etc.
|
|
global.socketAlert = function(socket, message, type = 'alert'){
|
|
global.io.to(socket).emit(
|
|
type, {'message': message}
|
|
);
|
|
}
|
|
|