1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-04 00:54:14 +02:00

use map for teams

This commit is contained in:
Robin Appelman 2017-09-02 15:38:08 +02:00
commit 60864c38dc
5 changed files with 34 additions and 21 deletions

View file

@ -8,13 +8,13 @@ import {handleStringTable, handleStringTables, handleStringTableUpdate} from '..
import {Building} from './Building'; import {Building} from './Building';
import {Death} from './Death'; import {Death} from './Death';
import {GameEventDefinition} from './GameEvent'; import {GameEventDefinition} from './GameEvent';
import {PacketEntity} from './PacketEntity'; import {EntityId, PacketEntity} from './PacketEntity';
import {Player} from './Player'; import {Player} from './Player';
import {PlayerResource} from './PlayerResource'; import {PlayerResource} from './PlayerResource';
import {SendTable} from './SendTable'; import {SendTable} from './SendTable';
import {ServerClass} from './ServerClass'; import {ServerClass} from './ServerClass';
import {StringTable} from './StringTable'; import {StringTable} from './StringTable';
import {Team} from './Team'; import {Team, TeamNumber} from './Team';
import {UserInfo} from './UserInfo'; import {UserInfo} from './UserInfo';
import {Weapon} from './Weapon'; import {Weapon} from './Weapon';
import {World} from './World'; import {World} from './World';
@ -30,14 +30,14 @@ export class Match {
public staticBaseLines: BitStream[]; public staticBaseLines: BitStream[];
public eventDefinitions: Map<number, GameEventDefinition>; public eventDefinitions: Map<number, GameEventDefinition>;
public world: World; public world: World;
public players: Map<number, Player>; public playerEntityMap: Map<EntityId, Player>;
public entityClasses: {[entityId: string]: ServerClass}; public entityClasses: {[entityId: string]: ServerClass};
public sendTableMap: {[name: string]: SendTable}; public sendTableMap: {[name: string]: SendTable};
public baseLineCache: {[serverClass: string]: PacketEntity}; public baseLineCache: {[serverClass: string]: PacketEntity};
public weaponMap: {[entityId: string]: Weapon}; public weaponMap: {[entityId: string]: Weapon};
public outerMap: {[outer: number]: number}; public outerMap: {[outer: number]: number};
public teams: Team[]; public teams: Map<TeamNumber, Team> = new Map();
public teamMap: {[entityId: string]: Team}; public teamEntityMap: Map<EntityId, Team>;
public version: number; public version: number;
public buildings: {[entityId: string]: Building} = {}; public buildings: {[entityId: string]: Building} = {};
public playerResources: PlayerResource[] = []; public playerResources: PlayerResource[] = [];
@ -58,7 +58,7 @@ export class Match {
this.serverClasses = []; this.serverClasses = [];
this.staticBaseLines = []; this.staticBaseLines = [];
this.eventDefinitions = new Map(); this.eventDefinitions = new Map();
this.players = new Map(); this.playerEntityMap = new Map();
this.world = { this.world = {
boundaryMin: {x: 0, y: 0, z: 0}, boundaryMin: {x: 0, y: 0, z: 0},
boundaryMax: {x: 0, y: 0, z: 0}, boundaryMax: {x: 0, y: 0, z: 0},
@ -68,8 +68,7 @@ export class Match {
this.baseLineCache = {}; this.baseLineCache = {};
this.weaponMap = {}; this.weaponMap = {};
this.outerMap = {}; this.outerMap = {};
this.teams = []; this.teamEntityMap = new Map();
this.teamMap = {};
this.version = 0; this.version = 0;
} }
@ -190,7 +189,7 @@ export class Match {
} }
public getPlayerByUserId(userId: number): Player { public getPlayerByUserId(userId: number): Player {
for (const player of this.players.values()) { for (const player of this.playerEntityMap.values()) {
if (player.user.userId === userId) { if (player.user.userId === userId) {
return player; return player;
} }

View file

@ -9,6 +9,8 @@ export enum PVS {
DELETE = 4, DELETE = 4,
} }
export type EntityId = number;
export class PacketEntity { export class PacketEntity {
public serverClass: ServerClass; public serverClass: ServerClass;
public entityIndex: number; public entityIndex: number;

View file

@ -1,5 +1,12 @@
export enum TeamNumber {
UNASGINED = 0,
SPECTATOR = 1,
RED = 2,
BLU = 3,
}
export interface Team { export interface Team {
teamNumber: number; teamNumber: TeamNumber;
name: string; name: string;
score: number; score: number;
roundsWon: number; roundsWon: number;

View file

@ -59,7 +59,7 @@ function handlePlayerSpawn(packet: GameEventPacket, match: Match) {
const values = packet.event.values as PlayerSpawnEventValues; const values = packet.event.values as PlayerSpawnEventValues;
const userId = values.userid; const userId = values.userid;
const userState = match.getUserInfo(userId); const userState = match.getUserInfo(userId);
const player = match.players.get(userState.entityId); const player = match.playerEntityMap.get(userState.entityId);
userState.team = values.team === 2 ? 'red' : 'blue'; userState.team = values.team === 2 ? 'red' : 'blue';
const classId = values.class; const classId = values.class;
if (player) { if (player) {

View file

@ -6,6 +6,7 @@ import {LifeState, Player} from '../Data/Player';
import {SendProp} from '../Data/SendProp'; import {SendProp} from '../Data/SendProp';
import {Vector} from '../Data/Vector'; import {Vector} from '../Data/Vector';
import {CWeaponMedigun, Weapon} from '../Data/Weapon'; import {CWeaponMedigun, Weapon} from '../Data/Weapon';
import {TeamNumber} from '../Data/Team';
export function handlePacketEntities(packet: PacketEntitiesPacket, match: Match) { export function handlePacketEntities(packet: PacketEntitiesPacket, match: Match) {
for (const removedEntityId of packet.removedEntities) { for (const removedEntityId of packet.removedEntities) {
@ -79,11 +80,11 @@ function handleEntity(entity: PacketEntity, match: Match) {
* "DT_TFPlayerShared.m_flCloakMeter": 100, * "DT_TFPlayerShared.m_flCloakMeter": 100,
*/ */
const player: Player = (match.players.has(entity.entityIndex)) ? const player: Player = (match.playerEntityMap.has(entity.entityIndex)) ?
match.players.get(entity.entityIndex) as Player : match.playerEntityMap.get(entity.entityIndex) as Player :
new Player(match, match.getUserInfoForEntity(entity)); new Player(match, match.getUserInfoForEntity(entity));
if (!match.players.has(entity.entityIndex)) { if (!match.playerEntityMap.has(entity.entityIndex)) {
match.players.set(entity.entityIndex, player); match.playerEntityMap.set(entity.entityIndex, player);
} }
for (const prop of entity.props) { for (const prop of entity.props) {
@ -159,19 +160,23 @@ function handleEntity(entity: PacketEntity, match: Match) {
break; break;
case'CTFTeam': case'CTFTeam':
try { try {
const teamId = entity.getProperty('DT_Team', 'm_iTeamNum').value as number; const teamId = entity.getProperty('DT_Team', 'm_iTeamNum').value as TeamNumber;
if (!match.teams[teamId]) { if (!match.teams.has(teamId)) {
match.teams[teamId] = { const team = {
name: entity.getProperty('DT_Team', 'm_szTeamname').value as string, name: entity.getProperty('DT_Team', 'm_szTeamname').value as string,
score: entity.getProperty('DT_Team', 'm_iScore').value as number, score: entity.getProperty('DT_Team', 'm_iScore').value as number,
roundsWon: entity.getProperty('DT_Team', 'm_iRoundsWon').value as number, roundsWon: entity.getProperty('DT_Team', 'm_iRoundsWon').value as number,
players: entity.getProperty('DT_Team', '"player_array"').value as number[], players: entity.getProperty('DT_Team', '"player_array"').value as number[],
teamNumber: teamId as number, teamNumber: teamId,
}; };
match.teamMap[entity.entityIndex] = match.teams[teamId]; match.teams.set(teamId, team);
match.teamEntityMap.set(entity.entityIndex, team);
} }
} catch (e) { } catch (e) {
const team = match.teamMap[entity.entityIndex]; const team = match.teamEntityMap.get(entity.entityIndex);
if (!team) {
throw new Error(`No team with entity id: ${entity.entityIndex}`);
}
for (const prop of entity.props) { for (const prop of entity.props) {
const propName = prop.definition.ownerTableName + '.' + prop.definition.name; const propName = prop.definition.ownerTableName + '.' + prop.definition.name;
switch (propName) { switch (propName) {