mirror of
https://codeberg.org/demostf/frontend.git
synced 2026-06-04 02:34:13 +02:00
show projectiles in viewer
This commit is contained in:
parent
213d2c6753
commit
22ad43a4b7
7 changed files with 215 additions and 89 deletions
|
|
@ -1,67 +1,79 @@
|
|||
import {ParsedDemo, PlayerState, WorldBoundaries, Kill, BuildingState} from "./Parser";
|
||||
import {BuildingState, Kill, ParsedDemo, PlayerState, ProjectileState, ProjectileType, WorldBoundaries} from "./Parser";
|
||||
|
||||
function getCacheBuster(): string {
|
||||
const url = document.querySelector('script[src*="viewer"]').attributes.src.value;
|
||||
return url.substring("/viewer.js".length);
|
||||
const url = document.querySelector('script[src*="viewer"]').attributes.src.value;
|
||||
return url.substring("/viewer.js".length);
|
||||
}
|
||||
|
||||
export class AsyncParser {
|
||||
buffer: ArrayBuffer;
|
||||
demo: ParsedDemo;
|
||||
world: WorldBoundaries;
|
||||
progressCallback: (progress: number) => void;
|
||||
buffer: ArrayBuffer;
|
||||
demo: ParsedDemo;
|
||||
world: WorldBoundaries;
|
||||
progressCallback: (progress: number) => void;
|
||||
|
||||
constructor(buffer: ArrayBuffer, progressCallback: (progress: number) => void) {
|
||||
this.buffer = buffer;
|
||||
this.progressCallback = progressCallback;
|
||||
}
|
||||
constructor(buffer: ArrayBuffer, progressCallback: (progress: number) => void) {
|
||||
this.buffer = buffer;
|
||||
this.progressCallback = progressCallback;
|
||||
}
|
||||
|
||||
cache(): Promise<ParsedDemo> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(`/parse-worker.js${getCacheBuster()}`);
|
||||
worker.postMessage({
|
||||
buffer: this.buffer
|
||||
}, [this.buffer]);
|
||||
worker.onmessage = (event) => {
|
||||
if (event.data.error) {
|
||||
reject(event.data.error);
|
||||
return;
|
||||
} else if (event.data.progress) {
|
||||
this.progressCallback(event.data.progress);
|
||||
return;
|
||||
} else if (event.data.demo) {
|
||||
const cachedData: ParsedDemo = event.data.demo;
|
||||
console.log(`packed data: ${(cachedData.data.length / (1024 * 1024)).toFixed(1)}MB`);
|
||||
this.world = cachedData.world;
|
||||
this.demo = new ParsedDemo(cachedData.playerCount, cachedData.buildingCount, cachedData.world, cachedData.header, cachedData.data, cachedData.kills, cachedData.playerInfo, cachedData.tickCount);
|
||||
resolve(this.demo);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
cache(): Promise<ParsedDemo> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(`/parse-worker.js${getCacheBuster()}`);
|
||||
worker.postMessage({
|
||||
buffer: this.buffer
|
||||
}, [this.buffer]);
|
||||
worker.onmessage = (event) => {
|
||||
if (event.data.error) {
|
||||
reject(event.data.error);
|
||||
return;
|
||||
} else if (event.data.progress) {
|
||||
this.progressCallback(event.data.progress);
|
||||
return;
|
||||
} else if (event.data.demo) {
|
||||
const cachedData: ParsedDemo = event.data.demo;
|
||||
console.log(`packed data: ${(cachedData.data.length / (1024 * 1024)).toFixed(1)}MB`);
|
||||
this.world = cachedData.world;
|
||||
this.demo = new ParsedDemo(cachedData.playerCount, cachedData.buildingCount, cachedData.projectileCount, cachedData.world, cachedData.header, cachedData.data, cachedData.kills, cachedData.playerInfo, cachedData.tickCount);
|
||||
resolve(this.demo);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getPlayersAtTick(tick: number): PlayerState[] {
|
||||
const players: PlayerState[] = [];
|
||||
for (let i = 0; i < this.demo.playerCount; i++) {
|
||||
players.push(this.demo.getPlayer(tick, i));
|
||||
}
|
||||
getPlayersAtTick(tick: number): PlayerState[] {
|
||||
const players: PlayerState[] = [];
|
||||
for (let i = 0; i < this.demo.playerCount; i++) {
|
||||
players.push(this.demo.getPlayer(tick, i));
|
||||
}
|
||||
|
||||
return players;
|
||||
}
|
||||
return players;
|
||||
}
|
||||
|
||||
getBuildingsAtTick(tick: number): BuildingState[] {
|
||||
const buildings: BuildingState[] = [];
|
||||
for (let i = 0; i < this.demo.buildingCount; i++) {
|
||||
const building = this.demo.getBuilding(tick, i);
|
||||
if (building.health > 0) {
|
||||
buildings.push(building);
|
||||
}
|
||||
}
|
||||
getBuildingsAtTick(tick: number): BuildingState[] {
|
||||
const buildings: BuildingState[] = [];
|
||||
for (let i = 0; i < this.demo.buildingCount; i++) {
|
||||
const building = this.demo.getBuilding(tick, i);
|
||||
if (building.health > 0) {
|
||||
buildings.push(building);
|
||||
}
|
||||
}
|
||||
|
||||
return buildings;
|
||||
}
|
||||
return buildings;
|
||||
}
|
||||
|
||||
getKills(): Kill[] {
|
||||
return this.demo.kills
|
||||
}
|
||||
getProjectilesAtTick(tick: number): ProjectileState[] {
|
||||
const projectiles: ProjectileState[] = [];
|
||||
for (let i = 0; i < this.demo.projectileCount; i++) {
|
||||
const projectile = this.demo.getProjectile(tick, i);
|
||||
if (projectile.projectileType !== ProjectileType.Unknown && projectile.position.x > this.world.boundary_min.x && projectile.position.y > this.world.boundary_min.y) {
|
||||
projectiles.push(projectile);
|
||||
}
|
||||
}
|
||||
|
||||
return projectiles;
|
||||
}
|
||||
|
||||
getKills(): Kill[] {
|
||||
return this.demo.kills
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue