Webpage for Games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.3 KiB

import {Room} from "./room"
import {Client} from "./client"
import {log} from "./logger"
import fs = require("fs");
import SocketIO = require("socket.io");
export class ConnectionManager {
static Instance: ConnectionManager;
io: SocketIO.Server;
rooms: Room[];
constructor(io: SocketIO.Server) {
ConnectionManager.Instance = this;
this.io = io;
this.rooms = [];
let drawSettings = {
project: {
name: 'global-draw',
playerCounts: null
},
always: true,
spectators: true
};
let drawRoom = this.createRoom(drawSettings, '');
drawRoom.id = 'global-draw-room';
drawRoom.startGame();
this.rooms.push(drawRoom);
}
static RoomListByGame(game: string): Room[] {
return this.Instance.rooms.filter(l => l.gameName === game)
}
static ClientListByClientId(clientId: string): Client[] {
let room = Room.getByClientId(clientId, this.Instance.rooms);
return room.clients
}
newSocket(socket: SocketIO.Socket): void {
let client = new Client(socket, this);
log('connection', client)
}
roomListUpdate(): void {
this.io.sockets.emit('room-list', serializeObject(this.rooms))
}
createRoom(settings: Settings.Global | any, name: string): Room {
let roomId = Room.generateCode(10);
// @ts-ignore
let room = new Room(name, roomId, settings, this.io);
this.rooms.push(room);
this.roomListUpdate();
return room
}
deleteRoom(room: Room): void {
this.rooms.splice(this.rooms.indexOf(room), 1);
this.roomListUpdate();
log('lobby-deleted', null, room)
}
//Starts the game of a room with given id
startGame(client: Client, _roomId: string): void {
let lobby = Room.getByClientId(client.id, this.rooms);
if (!lobby) return;
if (!lobby.hasStarted) {
lobby.startGame();
log('game-started', client, lobby)
}
this.io.sockets.emit('room-list', serializeObject(this.rooms))
}
//Stops the game of a lobby with given id
stopGame(client: Client, lobbyId: string): void {
let lobby = Room.getByRoomId(lobbyId, this.rooms);
if (!lobby) return;
lobby.stopGame(client);
log('game-stopped', client, lobby)
}
//Saves user feedback to a file
saveFeedbackToFile(client: Client, content: string): void {
let date = new Date(Date.now()).toString();
let path = "feedback/" + client.game + '.txt';
let saveToFile = (content: string) => {
fs.writeFile(path, content, (err: any) => {
if (err)
log('save-error', client, null, err.message);
else
log('feedback', client, null, path)
});
};
if (fs.existsSync(path)) {
fs.readFile(path, 'utf8', (err, data) => {
if (err)
log('load-error', client, null, err.message);
else {
log('load-success', client, null);
let newContent = data + '\n\n\n\n' + date + '\n\n' + content;
saveToFile(newContent)
}
})
} else {
saveToFile(date + '\n' + content)
}
}
//Removes a disconnected client from all references
disconnected(client: Client): void {
let room = Room.getByClientId(client.id, this.rooms);
if (room)
client.leaveRoom(room.id);
log('disconnection', client)
}
}
export function serializeObject(object: any): any {
function serialize(obj: any) {
if (!obj)
return obj;
if (obj.serialized)
return obj.serialized;
else if (obj instanceof Array) {
let content = [];
obj.forEach(o => {
content.push(serialize(o))
});
return content
}
return obj
}
return serialize(object)
}