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:
parent
32b3e41aa8
commit
60864c38dc
5 changed files with 34 additions and 21 deletions
|
|
@ -8,13 +8,13 @@ import {handleStringTable, handleStringTables, handleStringTableUpdate} from '..
|
|||
import {Building} from './Building';
|
||||
import {Death} from './Death';
|
||||
import {GameEventDefinition} from './GameEvent';
|
||||
import {PacketEntity} from './PacketEntity';
|
||||
import {EntityId, PacketEntity} from './PacketEntity';
|
||||
import {Player} from './Player';
|
||||
import {PlayerResource} from './PlayerResource';
|
||||
import {SendTable} from './SendTable';
|
||||
import {ServerClass} from './ServerClass';
|
||||
import {StringTable} from './StringTable';
|
||||
import {Team} from './Team';
|
||||
import {Team, TeamNumber} from './Team';
|
||||
import {UserInfo} from './UserInfo';
|
||||
import {Weapon} from './Weapon';
|
||||
import {World} from './World';
|
||||
|
|
@ -30,14 +30,14 @@ export class Match {
|
|||
public staticBaseLines: BitStream[];
|
||||
public eventDefinitions: Map<number, GameEventDefinition>;
|
||||
public world: World;
|
||||
public players: Map<number, Player>;
|
||||
public playerEntityMap: Map<EntityId, Player>;
|
||||
public entityClasses: {[entityId: string]: ServerClass};
|
||||
public sendTableMap: {[name: string]: SendTable};
|
||||
public baseLineCache: {[serverClass: string]: PacketEntity};
|
||||
public weaponMap: {[entityId: string]: Weapon};
|
||||
public outerMap: {[outer: number]: number};
|
||||
public teams: Team[];
|
||||
public teamMap: {[entityId: string]: Team};
|
||||
public teams: Map<TeamNumber, Team> = new Map();
|
||||
public teamEntityMap: Map<EntityId, Team>;
|
||||
public version: number;
|
||||
public buildings: {[entityId: string]: Building} = {};
|
||||
public playerResources: PlayerResource[] = [];
|
||||
|
|
@ -58,7 +58,7 @@ export class Match {
|
|||
this.serverClasses = [];
|
||||
this.staticBaseLines = [];
|
||||
this.eventDefinitions = new Map();
|
||||
this.players = new Map();
|
||||
this.playerEntityMap = new Map();
|
||||
this.world = {
|
||||
boundaryMin: {x: 0, y: 0, z: 0},
|
||||
boundaryMax: {x: 0, y: 0, z: 0},
|
||||
|
|
@ -68,8 +68,7 @@ export class Match {
|
|||
this.baseLineCache = {};
|
||||
this.weaponMap = {};
|
||||
this.outerMap = {};
|
||||
this.teams = [];
|
||||
this.teamMap = {};
|
||||
this.teamEntityMap = new Map();
|
||||
this.version = 0;
|
||||
}
|
||||
|
||||
|
|
@ -190,7 +189,7 @@ export class Match {
|
|||
}
|
||||
|
||||
public getPlayerByUserId(userId: number): Player {
|
||||
for (const player of this.players.values()) {
|
||||
for (const player of this.playerEntityMap.values()) {
|
||||
if (player.user.userId === userId) {
|
||||
return player;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ export enum PVS {
|
|||
DELETE = 4,
|
||||
}
|
||||
|
||||
export type EntityId = number;
|
||||
|
||||
export class PacketEntity {
|
||||
public serverClass: ServerClass;
|
||||
public entityIndex: number;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,12 @@
|
|||
export enum TeamNumber {
|
||||
UNASGINED = 0,
|
||||
SPECTATOR = 1,
|
||||
RED = 2,
|
||||
BLU = 3,
|
||||
}
|
||||
|
||||
export interface Team {
|
||||
teamNumber: number;
|
||||
teamNumber: TeamNumber;
|
||||
name: string;
|
||||
score: number;
|
||||
roundsWon: number;
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ function handlePlayerSpawn(packet: GameEventPacket, match: Match) {
|
|||
const values = packet.event.values as PlayerSpawnEventValues;
|
||||
const userId = values.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';
|
||||
const classId = values.class;
|
||||
if (player) {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import {LifeState, Player} from '../Data/Player';
|
|||
import {SendProp} from '../Data/SendProp';
|
||||
import {Vector} from '../Data/Vector';
|
||||
import {CWeaponMedigun, Weapon} from '../Data/Weapon';
|
||||
import {TeamNumber} from '../Data/Team';
|
||||
|
||||
export function handlePacketEntities(packet: PacketEntitiesPacket, match: Match) {
|
||||
for (const removedEntityId of packet.removedEntities) {
|
||||
|
|
@ -79,11 +80,11 @@ function handleEntity(entity: PacketEntity, match: Match) {
|
|||
* "DT_TFPlayerShared.m_flCloakMeter": 100,
|
||||
*/
|
||||
|
||||
const player: Player = (match.players.has(entity.entityIndex)) ?
|
||||
match.players.get(entity.entityIndex) as Player :
|
||||
const player: Player = (match.playerEntityMap.has(entity.entityIndex)) ?
|
||||
match.playerEntityMap.get(entity.entityIndex) as Player :
|
||||
new Player(match, match.getUserInfoForEntity(entity));
|
||||
if (!match.players.has(entity.entityIndex)) {
|
||||
match.players.set(entity.entityIndex, player);
|
||||
if (!match.playerEntityMap.has(entity.entityIndex)) {
|
||||
match.playerEntityMap.set(entity.entityIndex, player);
|
||||
}
|
||||
|
||||
for (const prop of entity.props) {
|
||||
|
|
@ -159,19 +160,23 @@ function handleEntity(entity: PacketEntity, match: Match) {
|
|||
break;
|
||||
case'CTFTeam':
|
||||
try {
|
||||
const teamId = entity.getProperty('DT_Team', 'm_iTeamNum').value as number;
|
||||
if (!match.teams[teamId]) {
|
||||
match.teams[teamId] = {
|
||||
const teamId = entity.getProperty('DT_Team', 'm_iTeamNum').value as TeamNumber;
|
||||
if (!match.teams.has(teamId)) {
|
||||
const team = {
|
||||
name: entity.getProperty('DT_Team', 'm_szTeamname').value as string,
|
||||
score: entity.getProperty('DT_Team', 'm_iScore').value as number,
|
||||
roundsWon: entity.getProperty('DT_Team', 'm_iRoundsWon').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) {
|
||||
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) {
|
||||
const propName = prop.definition.ownerTableName + '.' + prop.definition.name;
|
||||
switch (propName) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue