Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game event buytime_ended triggers only once #370

Closed
1 task done
dannyhanke opened this issue Jul 30, 2022 · 3 comments
Closed
1 task done

Game event buytime_ended triggers only once #370

dannyhanke opened this issue Jul 30, 2022 · 3 comments

Comments

@dannyhanke
Copy link

dannyhanke commented Jul 30, 2022

Research

  • I searched but did not find an existing issue or discussion about this bug.

Description

Hi,

I am trying to listen to the buytime_ended event to calculate the correct team economy value.
It seems though that this event is triggered only once after the first round. For all further rounds it is not triggered.
I was not able to find further documentation on this event nor any other information.

Many thanks in advance.

Code to reproduce

import fs from 'fs';
import { DemoFile } from 'demofile';

const demofile = new DemoFile();
const stream = fs.createReadStream('demo.dem');

demofile.gameEvents.on('buytime_ended', () => {
  console.log('buytime ended');
});

demofile.parseStream(stream);

Affected demos

No response

@dannyhanke dannyhanke added the bug label Jul 30, 2022
@dannyhanke dannyhanke changed the title Game even buytime_ended triggers only once Game event buytime_ended triggers only once Jul 30, 2022
@saul
Copy link
Owner

saul commented Aug 3, 2022

I've cross-checked with some of the demos I have locally and I can reproduce this issue. I don't think it's an issue with the parser - some events just don't seem to be recorded correctly in demos. Instead you can add up every player's freezeTimeEndEquipmentValue at the end of each round, like so:

demoFile.gameEvents.on("round_end", e => {
  let tValue = 0;
  let ctValue = 0;

  for (const player of demoFile.players) {
    if (player.teamNumber == TeamNumber.Terrorists) {
      tValue += player.freezeTimeEndEquipmentValue;
    } else {
      ctValue += player.freezeTimeEndEquipmentValue;
    }
  }

  console.log(
    `End of round #${demoFile.gameRules.roundsPlayed} in ${demoFile.gameRules.phase}:`
  );
  console.log(`  - Terrorist: \$${tValue}`);
  console.log(`  - Counter-Terrorist: \$${ctValue}`);
});

Let me know if that doesn't work for you can I'll re-open.

@saul saul closed this as completed Aug 3, 2022
@dannyhanke
Copy link
Author

Thank you @saul for checking and confirming the problem.

Unfortunately the freezeTimeEndEquipmentValue does not work 100% of the time, because players sometimes buy after the freeze time is over. That is the reason I was going for the buytime_ended event.

I will try to calculate the correct equipment in a different way then. Thanks again for checking and great work on this library 👍

@saul saul reopened this Aug 4, 2022
@saul
Copy link
Owner

saul commented Aug 4, 2022

Good point! Try this instead:

let buyTimeTicks = -1;
demoFile.conVars.on("change", e => {
  if (e.name === "mp_buytime") {
    const secs = parseInt(e.value);
    buyTimeTicks = secs * demoFile.tickRate;
    console.log(`Buy time is ${secs} secs (${buyTimeTicks} ticks)`);
  }
});

let buyTimeEndTick = -1;
demoFile.on("tickend", e => {
  if (buyTimeEndTick < 0 || demoFile.currentTick < buyTimeEndTick) return;

  buyTimeEndTick = -1;

  // Buy time has ended
  let tValue = 0;
  let ctValue = 0;

  for (const player of demoFile.players) {
    if (player.teamNumber == TeamNumber.Terrorists) {
      tValue += player.currentEquipmentValue;
    } else if (player.teamNumber == TeamNumber.CounterTerrorists) {
      ctValue += player.currentEquipmentValue;
    }
  }

  console.log(
    `Buy time end round #${demoFile.gameRules.roundsPlayed} in ${demoFile.gameRules.phase}:`
  );
  console.log(`  - Terrorist: \$${tValue}`);
  console.log(`  - Counter-Terrorist: \$${ctValue}`);
});

demoFile.gameEvents.on("round_start", e => {
  if (buyTimeTicks >= 0) buyTimeEndTick = demoFile.currentTick + buyTimeTicks;
});

@saul saul closed this as completed Aug 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants