From 92447e02e5fc3977c9cfbca7a8de4132cbb4f13b Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Wed, 16 Oct 2024 21:13:54 +0300 Subject: refactor: upload run logic improvement --- backend/parser/parser.go | 196 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 180 insertions(+), 16 deletions(-) (limited to 'backend/parser/parser.go') diff --git a/backend/parser/parser.go b/backend/parser/parser.go index 1a80d4a..4605600 100644 --- a/backend/parser/parser.go +++ b/backend/parser/parser.go @@ -4,30 +4,52 @@ import ( "errors" "math" "os" + "regexp" + "strconv" + "strings" "github.com/pektezol/bitreader" ) +type Result struct { + MapID int + ServerNumber int + PortalCount int + TickCount int + HostSteamID string + PartnerSteamID string + IsHost bool +} + // Don't try to understand it, feel it. -func ProcessDemo(filePath string) (portalCount int, tickCount int, hostSteamID string, partnerSteamID string, err error) { +func ProcessDemo(filePath string) (Result, error) { + var result Result file, err := os.Open(filePath) if err != nil { - return 0, 0, "", "", err + return Result{}, err } reader := bitreader.NewReader(file, true) demoFileStamp := reader.TryReadString() demoProtocol := reader.TryReadSInt32() networkProtocol := reader.TryReadSInt32() - reader.SkipBytes(1056) + serverName := reader.TryReadStringLength(260) + // clientName := reader.TryReadStringLength(260) + reader.SkipBytes(260) + mapName := reader.TryReadStringLength(260) + reader.SkipBytes(276) if demoFileStamp != "HL2DEMO" { - return 0, 0, "", "", errors.New("invalid demo file stamp") + return Result{}, errors.New("invalid demo file stamp") } if demoProtocol != 4 { - return 0, 0, "", "", errors.New("this parser only supports demos from new engine") + return Result{}, errors.New("this parser only supports demos from new engine") } if networkProtocol != 2001 { - return 0, 0, "", "", errors.New("this parser only supports demos from portal 2") + return Result{}, errors.New("this parser only supports demos from portal 2") + } + if mapDict[mapName] == 0 { + return Result{}, errors.New("demo recorded on an invalid map") } + result.MapID = mapDict[mapName] for { packetType := reader.TryReadUInt8() reader.SkipBits(40) @@ -119,7 +141,17 @@ func ProcessDemo(filePath string) (portalCount int, tickCount int, hostSteamID s packetReader.SkipBytes(2) packetReader.SkipBits(uint64(packetReader.TryReadUInt16())) case 16: - packetReader.TryReadString() + print := packetReader.TryReadString() + re := regexp.MustCompile(`Server Number: (\d+)`) + match := re.FindStringSubmatch(print) + if len(match) >= 1 { + serverNumber := match[1] + n, err := strconv.Atoi(serverNumber) + if err != nil { + return Result{}, err + } + result.ServerNumber = n + } case 17: var length uint16 if packetReader.TryReadBool() { @@ -180,8 +212,8 @@ func ProcessDemo(filePath string) (portalCount int, tickCount int, hostSteamID s NumPortals: userMessageReader.TryReadSInt32(), TimeTaken: userMessageReader.TryReadSInt32(), } - portalCount = int(scoreboardTempUpdate.NumPortals) - tickCount = int(math.Round(float64((float32(scoreboardTempUpdate.TimeTaken) / 100.0) / float32(1.0/60.0)))) + result.PortalCount = int(scoreboardTempUpdate.NumPortals) + result.TickCount = int(math.Round(float64((float32(scoreboardTempUpdate.TimeTaken) / 100.0) / float32(1.0/60.0)))) } case 24: packetReader.SkipBits(20) @@ -216,8 +248,8 @@ func ProcessDemo(filePath string) (portalCount int, tickCount int, hostSteamID s packetReader.SkipBytes(packetReader.TryReadBits(32)) case 33: packetReader.SkipBits(packetReader.TryReadBits(32)) - default: - return 0, 0, "", "", errors.New("unknown msg type") + // default: + // return Result{}, errors.New(fmt.Sprintf("unknown msg type %d", messageType)) } } case 3, 7: @@ -252,7 +284,7 @@ func ProcessDemo(filePath string) (portalCount int, tickCount int, hostSteamID s if stringTableReader.TryReadBool() { byteLen, err := stringTableReader.ReadBits(16) if err != nil { - return 0, 0, "", "", errors.New("error on reading entry length") + return Result{}, errors.New("error on reading entry length") } stringTableEntryReader := bitreader.NewReaderFromBytes(stringTableReader.TryReadBytesToSlice(byteLen), true) if tableName == "userinfo" { @@ -285,10 +317,14 @@ func ProcessDemo(filePath string) (portalCount int, tickCount int, hostSteamID s userInfo.FilesDownloaded = stringTableEntryReader.TryReadUInt8() stringTableEntryReader.SkipBytes(3) if guidCount == 0 { - hostSteamID = userInfo.GUID + result.HostSteamID = userInfo.GUID + if strings.Contains(serverName, "localhost") { + result.IsHost = true + } } else if guidCount == 1 { - partnerSteamID = userInfo.GUID + result.PartnerSteamID = userInfo.GUID } + guidCount++ } } } @@ -303,11 +339,139 @@ func ProcessDemo(filePath string) (portalCount int, tickCount int, hostSteamID s } } default: - return 0, 0, "", "", errors.New("invalid packet type") + return Result{}, errors.New("invalid packet type") } if packetType == 7 { break } } - return portalCount, tickCount, hostSteamID, partnerSteamID, nil + return result, nil +} + +var mapDict = map[string]int{ + "sp_a1_intro1": 1, + "sp_a1_intro2": 2, + "sp_a1_intro3": 3, + "sp_a1_intro4": 4, + "sp_a1_intro5": 5, + "sp_a1_intro6": 6, + "sp_a1_intro7": 7, + "sp_a1_wakeup": 8, + "sp_a2_intro": 9, + + "sp_a2_laser_intro": 10, + "sp_a2_laser_stairs": 11, + "sp_a2_dual_lasers": 12, + "sp_a2_laser_over_goo": 13, + "sp_a2_catapult_intro": 14, + "sp_a2_trust_fling": 15, + "sp_a2_pit_flings": 16, + "sp_a2_fizzler_intro": 17, + + "sp_a2_sphere_peek": 18, + "sp_a2_ricochet": 19, + "sp_a2_bridge_intro": 20, + "sp_a2_bridge_the_gap": 21, + "sp_a2_turret_intro": 22, + "sp_a2_laser_relays": 23, + "sp_a2_turret_blocker": 24, + "sp_a2_laser_vs_turret": 25, + "sp_a2_pull_the_rug": 26, + + "sp_a2_column_blocker": 27, + "sp_a2_laser_chaining": 28, + "sp_a2_triple_laser": 29, + "sp_a2_bts1": 30, + "sp_a2_bts2": 31, + + "sp_a2_bts3": 32, + "sp_a2_bts4": 33, + "sp_a2_bts5": 34, + "sp_a2_core": 35, + + "sp_a3_01": 36, + "sp_a3_03": 37, + "sp_a3_jump_intro": 38, + "sp_a3_bomb_flings": 39, + "sp_a3_crazy_box": 40, + "sp_a3_transition01": 41, + + "sp_a3_speed_ramp": 42, + "sp_a3_speed_flings": 43, + "sp_a3_portal_intro": 44, + "sp_a3_end": 45, + + "sp_a4_intro": 46, + "sp_a4_tb_intro": 47, + "sp_a4_tb_trust_drop": 48, + "sp_a4_tb_wall_button": 49, + "sp_a4_tb_polarity": 50, + "sp_a4_tb_catch": 51, + "sp_a4_stop_the_box": 52, + "sp_a4_laser_catapult": 53, + "sp_a4_laser_platform": 54, + "sp_a4_speed_catch": 55, + "sp_a4_jump_polarity": 56, + + "sp_a4_finale1": 57, + "sp_a4_finale2": 58, + "sp_a4_finale3": 59, + "sp_a4_finale4": 60, + + "mp_coop_start": 61, + "mp_coop_lobby_2": 62, + + "mp_coop_doors": 63, + "mp_coop_race_2": 64, + "mp_coop_laser_2": 65, + "mp_coop_rat_maze": 66, + "mp_coop_laser_crusher": 67, + "mp_coop_teambts": 68, + + "mp_coop_fling_3": 69, + "mp_coop_infinifling_train": 70, + "mp_coop_come_along": 71, + "mp_coop_fling_1": 72, + "mp_coop_catapult_1": 73, + "mp_coop_multifling_1": 74, + "mp_coop_fling_crushers": 75, + "mp_coop_fan": 76, + + "mp_coop_wall_intro": 77, + "mp_coop_wall_2": 78, + "mp_coop_catapult_wall_intro": 79, + "mp_coop_wall_block": 80, + "mp_coop_catapult_2": 81, + "mp_coop_turret_walls": 82, + "mp_coop_turret_ball": 83, + "mp_coop_wall_5": 84, + + "mp_coop_tbeam_redirect": 85, + "mp_coop_tbeam_drill": 86, + "mp_coop_tbeam_catch_grind_1": 87, + "mp_coop_tbeam_laser_1": 88, + "mp_coop_tbeam_polarity": 89, + "mp_coop_tbeam_polarity2": 90, + "mp_coop_tbeam_polarity3": 91, + "mp_coop_tbeam_maze": 92, + "mp_coop_tbeam_end": 93, + + "mp_coop_paint_come_along": 94, + "mp_coop_paint_redirect": 95, + "mp_coop_paint_bridge": 96, + "mp_coop_paint_walljumps": 97, + "mp_coop_paint_speed_flings": 98, + "mp_coop_paint_red_racer": 99, + "mp_coop_paint_speed_catch": 100, + "mp_coop_paint_longjump_intro": 101, + + "mp_coop_seperation_1": 102, + "mp_coop_tripleaxis": 103, + "mp_coop_catapult_catch": 104, + "mp_coop_2paints_1bridge": 105, + "mp_coop_paint_conversion": 106, + "mp_coop_bridge_catch": 107, + "mp_coop_laser_tbeam": 108, + "mp_coop_paint_rat_maze": 109, + "mp_coop_paint_crazy_box": 110, } -- cgit v1.2.3