Optimize performance

This commit is contained in:
schmelczerandras 2020-10-26 14:18:36 +01:00
commit b8ef90c100
2 changed files with 319 additions and 206 deletions

View file

@ -1,10 +1,4 @@
import {
CharacterTeam,
PlayerInformation,
Random,
TransportEvents,
settings,
} from 'shared';
import { CharacterTeam, PlayerInformation, Random, settings, Command } from 'shared';
import { PhysicalContainer } from '../physics/containers/physical-container';
import { NPC } from './npc';
import { Player } from './player';
@ -17,12 +11,16 @@ export class PlayerContainer {
constructor(
private readonly objects: PhysicalContainer,
private readonly playerMaxCount: number,
private readonly npcMaxCount: number,
) {
this.createNPCs();
}
public createNPCs() {
const newNpcCount = this.playerMaxCount - this._players.length - this._npcs.length;
const newNpcCount = Math.min(
this.playerMaxCount - this._players.length - this._npcs.length,
this.npcMaxCount - this._npcs.length,
);
for (let i = 0; i < newNpcCount; i++) {
const name = `BOT ${Random.choose(settings.npcNames)}`;
this._npcs.push(
@ -62,8 +60,12 @@ export class PlayerContainer {
this.players.forEach((p) => p.step(deltaTimeInSeconds));
}
public sendOnSocket(message: any) {
this._players.forEach((p) => p.socket.emit(TransportEvents.ServerToPlayer, message));
public queueCommandForEachClient(command: Command) {
this._players.forEach((p) => p.queueCommandSend(command));
}
public sendQueuedCommands() {
this._players.forEach((p) => p.sendQueuedCommandsToClient());
}
private getTeamOfNextPlayer(isNpc = false): CharacterTeam {

View file

@ -55,7 +55,7 @@ export class Player extends PlayerBase {
playerContainer: PlayerContainer,
objectContainer: PhysicalContainer,
team: CharacterTeam,
public readonly socket: SocketIO.Socket,
private readonly socket: SocketIO.Socket,
) {
super(playerInfo, playerContainer, objectContainer, team);
@ -86,7 +86,7 @@ export class Player extends PlayerBase {
super.createCharacter();
this.objectsPreviouslyInViewArea.push(this.character!);
this.sendToPlayer(new CreatePlayerCommand(this.character!));
this.queueCommandSend(new CreatePlayerCommand(this.character!));
}
private timeUntilRespawn = 0;
@ -98,21 +98,18 @@ export class Player extends PlayerBase {
this.sumDeaths++;
this.sumKills = this.character.killCount;
this.socket.emit(
TransportEvents.ServerToPlayer,
serialize(new PlayerDiedCommand(settings.playerDiedTimeout)),
);
this.queueCommandSend(new PlayerDiedCommand(settings.playerDiedTimeout));
this.character = null;
this.timeUntilRespawn = settings.playerDiedTimeout;
}
} else {
this.sendToPlayer(
this.queueCommandSend(
new ServerAnnouncement(`Reviving in ${Math.round(this.timeUntilRespawn)}…`),
);
if ((this.timeUntilRespawn -= deltaTimeInSeconds) < 0) {
this.createCharacter();
this.center = this.character!.center;
this.sendToPlayer(new ServerAnnouncement(''));
this.queueCommandSend(new ServerAnnouncement(''));
}
}
@ -136,14 +133,16 @@ export class Player extends PlayerBase {
this.objectsPreviouslyInViewArea = objectsInViewArea;
if (noLongerIntersecting.length > 0) {
this.sendToPlayer(new DeleteObjectsCommand(noLongerIntersecting.map((g) => g.id)));
this.queueCommandSend(
new DeleteObjectsCommand(noLongerIntersecting.map((g) => g.id)),
);
}
if (newlyIntersecting.length > 0) {
this.sendToPlayer(new CreateObjectsCommand(newlyIntersecting));
this.queueCommandSend(new CreateObjectsCommand(newlyIntersecting));
}
this.sendToPlayer(
this.queueCommandSend(
new RemoteCallsForObjects(
this.objectsPreviouslyInViewArea.map(
(g) => new RemoteCallsForObject(g.id, g.getRemoteCalls()),
@ -151,7 +150,7 @@ export class Player extends PlayerBase {
),
);
this.sendToPlayer(new UpdateOtherPlayerDirections(this.getOtherPlayers()));
this.queueCommandSend(new UpdateOtherPlayerDirections(this.getOtherPlayers()));
}
private getOtherPlayers(): Array<OtherPlayerDirection> {
@ -185,8 +184,14 @@ export class Player extends PlayerBase {
);
}
private sendToPlayer(command: Command) {
this.socket.emit(TransportEvents.ServerToPlayer, serialize(command));
private commandsToBeSent: Array<Command> = [];
public queueCommandSend(command: Command) {
this.commandsToBeSent.push(command);
}
public sendQueuedCommandsToClient() {
this.socket.emit(TransportEvents.ServerToPlayer, serialize(this.commandsToBeSent));
this.commandsToBeSent = [];
}
public destroy() {