Add server screen
This commit is contained in:
parent
89fafeafd3
commit
e2129bbb26
20 changed files with 672 additions and 174 deletions
88
backend/src/game-server.ts
Normal file
88
backend/src/game-server.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
import { PhysicalContainer } from './physics/containers/physical-container';
|
||||
import { Player } from './players/player';
|
||||
import ioserver from 'socket.io';
|
||||
import { TransportEvents, deserialize, settings, ServerInformation } from 'shared';
|
||||
import { createWorld } from './map/create-world';
|
||||
import { DeltaTimeCalculator } from './helper/delta-time-calculator';
|
||||
import { Options } from './options';
|
||||
|
||||
export class GameServer {
|
||||
private objects = new PhysicalContainer();
|
||||
private players: Array<Player> = [];
|
||||
private deltaTimes: Array<number> = [];
|
||||
private deltaTimeCalculator = new DeltaTimeCalculator();
|
||||
|
||||
private serverName: string;
|
||||
private playerLimit: number;
|
||||
constructor(io: ioserver.Server, options: Options) {
|
||||
this.serverName = options.name;
|
||||
this.playerLimit = options.playerLimit;
|
||||
|
||||
createWorld(this.objects);
|
||||
this.objects.initialize();
|
||||
|
||||
io.on('connection', (socket: SocketIO.Socket) => {
|
||||
socket.on(TransportEvents.PlayerJoining, () => {
|
||||
const player = new Player(this.objects, socket);
|
||||
this.players.push(player);
|
||||
socket.on(TransportEvents.PlayerToServer, (json: string) => {
|
||||
const command = deserialize(json);
|
||||
player.sendCommand(command);
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
player.destroy();
|
||||
this.players = this.players.filter((p) => p !== player);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('join', (room_name: string) => {
|
||||
socket.join(room_name);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public start() {
|
||||
this.handlePhysics();
|
||||
}
|
||||
|
||||
private handlePhysics() {
|
||||
const delta = this.deltaTimeCalculator.getNextDeltaTimeInMilliseconds();
|
||||
this.deltaTimes.push(delta);
|
||||
const framesBetweenDeltaTimeCalculation = 1000;
|
||||
|
||||
if (this.deltaTimes.length > framesBetweenDeltaTimeCalculation) {
|
||||
this.deltaTimes.sort((a, b) => a - b);
|
||||
console.log(
|
||||
`Median physics time: ${this.deltaTimes[
|
||||
framesBetweenDeltaTimeCalculation
|
||||
].toFixed(2)} ms`,
|
||||
);
|
||||
console.log(
|
||||
`Memory used: ${(process.memoryUsage().rss / 1024 / 1024).toFixed(2)} MB`,
|
||||
);
|
||||
this.deltaTimes = [];
|
||||
console.log(this.players.map((p) => p.latency));
|
||||
}
|
||||
|
||||
this.objects.stepObjects(delta / 1000);
|
||||
this.players.forEach((p) => p.step(delta / 1000));
|
||||
|
||||
const physicsDelta = this.deltaTimeCalculator.getDeltaTimeInMilliseconds();
|
||||
this.deltaTimes.push(physicsDelta);
|
||||
const sleepTime = settings.targetPhysicsDeltaTimeInMilliseconds - physicsDelta;
|
||||
if (sleepTime >= settings.minPhysicsSleepTime) {
|
||||
setTimeout(this.handlePhysics.bind(this), sleepTime);
|
||||
} else {
|
||||
setImmediate(this.handlePhysics.bind(this));
|
||||
}
|
||||
}
|
||||
|
||||
public get serverInfo(): ServerInformation {
|
||||
return {
|
||||
serverName: this.serverName,
|
||||
playerCount: this.players.length,
|
||||
playerLimit: this.playerLimit,
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue