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

feat: mimic some source 1 game events #403

Merged
merged 6 commits into from
Aug 22, 2023
Merged

Conversation

akiver
Copy link
Collaborator

@akiver akiver commented Aug 16, 2023

This PR mimic the following Source 1 game events with CS2 demos based on prop updates:

  • WeaponFire
  • RoundStart
  • RoundEnd
  • RoundEndOfficially
  • BombPickup
  • BombDropped
  • BombPlantBegin
  • BombPlantAborted
  • BombPlanted
  • BombDefuse
  • BombExplode
  • BombDefuseStart
  • BombDefuseAborted
  • FlashExplode

Test code:

package main

import (
	"errors"
	"fmt"
	"os"
	"time"

	demoinfocs "github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs"
	"github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs/events"
)

func main() {
	var err error
	f, err := os.Open("./faze-vs-legends-m2-nuke.dem")
	checkError(err)

	defer f.Close()

	p := demoinfocs.NewParser(f)
	defer p.Close()

	_, err = p.ParseHeader()
	checkError(err)

	p.RegisterEventHandler(func(e events.RoundStart) {
		fmt.Println(p.GameState().IngameTick(), "round_start", p.GameState().TotalRoundsPlayed()+1)
	})

	p.RegisterEventHandler(func(e events.RoundEnd) {
		fmt.Println(p.GameState().IngameTick(), "round_end")
	})

	p.RegisterEventHandler(func(e events.RoundEndOfficial) {
		fmt.Println(p.GameState().IngameTick(), "round_end_officially")
	})

	p.RegisterEventHandler(func(e events.FlashExplode) {
		fmt.Println(p.GameState().IngameTick(), "flash_explode", e.Thrower.Name)
	})

	p.RegisterEventHandler(func(e events.PlayerFlashed) {
		fmt.Println(p.GameState().IngameTick(), "player_flashed", e.Attacker.Name, e.Player.Name, e.FlashDuration())
	})

	p.RegisterEventHandler(func(e events.WeaponFire) {
		fmt.Println(p.GameState().IngameTick(), "weapon_fire", e.Shooter.Name, e.Weapon)
	})

	p.RegisterEventHandler(func(e events.BombPickup) {
		fmt.Println(p.GameState().IngameTick(), "bomb_pickup", e.Player.Name)
	})

	p.RegisterEventHandler(func(e events.BombDropped) {
		fmt.Println(p.GameState().IngameTick(), "bomb_dropped", e.Player.Name)
	})

	p.RegisterEventHandler(func(e events.BombPlantBegin) {
		fmt.Println(p.GameState().IngameTick(), "bomb_plant_begin", e.Player.Name, string(e.Site))
	})

	p.RegisterEventHandler(func(e events.BombPlantAborted) {
		fmt.Println(p.GameState().IngameTick(), "bomb_plant_aborted", e.Player.Name)
	})

	p.RegisterEventHandler(func(e events.BombPlanted) {
		fmt.Println(p.GameState().IngameTick(), "bomb_planted", e.Player.Name, string(e.Site))
	})

	p.RegisterEventHandler(func(e events.BombExplode) {
		fmt.Println(p.GameState().IngameTick(), "bomb_exploded", e.Player.Name, string(e.Site))
	})

	p.RegisterEventHandler(func(e events.BombDefused) {
		fmt.Println(p.GameState().IngameTick(), "bomb_defused", e.Player.Name, string(e.Site))
	})

	p.RegisterEventHandler(func(e events.BombDefuseStart) {
		fmt.Println(p.GameState().IngameTick(), "bomb_defuse_start", e.Player.Name)
	})

	p.RegisterEventHandler(func(e events.BombDefuseAborted) {
		fmt.Println(p.GameState().IngameTick(), "bomb_defuse_aborted", e.Player.Name)
	})

	now := time.Now()
	err = p.ParseToEnd()
	if errors.Is(err, demoinfocs.ErrUnexpectedEndOfDemo) {
		fmt.Println("Parsing done with unexpected EOF")
	} else {
		checkError(err)
	}
	fmt.Println(time.Since(now))
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

@akiver akiver changed the title feat: mimic some source 1game events feat: mimic some source 1 game events Aug 16, 2023
@BestAwperEver
Copy link
Contributor

BestAwperEver commented Aug 17, 2023

Hey, great job here! If I may have a suggestion, we should probably add something like MimicSource1Events (and maybe set to true in the DefaultParserConfig) to the parser config to make it explicit that we do not actually have these events in the Source 2 demos.

@markus-wa
Copy link
Owner

amazing work - great job!

@akiver
Copy link
Collaborator Author

akiver commented Aug 17, 2023

Hey, great job here! If I may have a suggestion, we should probably add something like MimicSource1Events (and maybe set to true in the DefaultParserConfig) to the parser config to make it explicit that we do not actually have these events in the Source 2 demos.

Added!

@markus-wa
Copy link
Owner

@akiver can you change MimicSource1Events to DisableMimicSource1Events?

That way it will be enabled by default for users using custom configs, not just those using the DefaultParserConfig.

@akiver
Copy link
Collaborator Author

akiver commented Aug 21, 2023

@akiver can you change MimicSource1Events to DisableMimicSource1Events?

That way it will be enabled by default for users using custom configs, not just those using the DefaultParserConfig.

Changed!

@markus-wa markus-wa merged commit 999291c into markus-wa:s2 Aug 22, 2023
1 check failed
@akiver akiver deleted the s2-events branch December 4, 2023 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants