diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-10-18 22:51:20 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-10-18 22:51:20 +0300 |
| commit | 2728972d3428d82b73c49be8b600e0334c42775b (patch) | |
| tree | 8764101e5c168da49f7b68d88f5dd9573b907ede /backend | |
| parent | backend: fix prop crushers map name (diff) | |
| download | lphub-2728972d3428d82b73c49be8b600e0334c42775b.tar.gz lphub-2728972d3428d82b73c49be8b600e0334c42775b.tar.bz2 lphub-2728972d3428d82b73c49be8b600e0334c42775b.zip | |
backend: optimize create record
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/handlers/record.go | 30 | ||||
| -rw-r--r-- | backend/parser/parser.go | 12 |
2 files changed, 18 insertions, 24 deletions
diff --git a/backend/handlers/record.go b/backend/handlers/record.go index 2338097..ed9250d 100644 --- a/backend/handlers/record.go +++ b/backend/handlers/record.go | |||
| @@ -115,36 +115,28 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 115 | for i, header := range demoFiles { | 115 | for i, header := range demoFiles { |
| 116 | uuid := uuid.New().String() | 116 | uuid := uuid.New().String() |
| 117 | // Upload & insert into demos | 117 | // Upload & insert into demos |
| 118 | err = c.SaveUploadedFile(header, "backend/parser/"+uuid+".dem") | 118 | err = c.SaveUploadedFile(header, "parser/"+uuid+".dem") |
| 119 | if err != nil { | 119 | if err != nil { |
| 120 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordSaveDemoFail, err.Error()) | 120 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordSaveDemoFail, err.Error()) |
| 121 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 121 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 122 | return | 122 | return |
| 123 | } | 123 | } |
| 124 | defer os.Remove("backend/parser/" + uuid + ".dem") | 124 | defer os.Remove("parser/" + uuid + ".dem") |
| 125 | f, err := os.Open("backend/parser/" + uuid + ".dem") | 125 | f, err := os.Open("parser/" + uuid + ".dem") |
| 126 | if err != nil { | 126 | if err != nil { |
| 127 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordOpenDemoFail, err.Error()) | 127 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordOpenDemoFail, err.Error()) |
| 128 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 128 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 129 | return | 129 | return |
| 130 | } | 130 | } |
| 131 | defer f.Close() | 131 | defer f.Close() |
| 132 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | 132 | parserResult, err := parser.ProcessDemo("parser/" + uuid + ".dem") |
| 133 | if err != nil { | ||
| 134 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordCreateDemoFail, err.Error()) | ||
| 135 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 136 | return | ||
| 137 | } | ||
| 138 | parserResult, err := parser.ProcessDemo("backend/parser/" + uuid + ".dem") | ||
| 139 | if err != nil { | 133 | if err != nil { |
| 140 | deleteFile(srv, file.Id) | ||
| 141 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordProcessDemoFail, err.Error()) | 134 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordProcessDemoFail, err.Error()) |
| 142 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 135 | c.JSON(http.StatusOK, models.ErrorResponse("Error while processing demo: "+err.Error())) |
| 143 | return | 136 | return |
| 144 | } | 137 | } |
| 145 | if mapID != parserResult.MapID { | 138 | if mapID != parserResult.MapID { |
| 146 | deleteFile(srv, file.Id) | 139 | c.JSON(http.StatusOK, models.ErrorResponse("Demo map does not match selected map id.")) |
| 147 | c.JSON(http.StatusOK, models.ErrorResponse("demo map does not match uploaded map id")) | ||
| 148 | return | 140 | return |
| 149 | } | 141 | } |
| 150 | hostDemoScoreCount = parserResult.PortalCount | 142 | hostDemoScoreCount = parserResult.PortalCount |
| @@ -152,7 +144,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 152 | hostSteamID = parserResult.HostSteamID | 144 | hostSteamID = parserResult.HostSteamID |
| 153 | partnerSteamID = parserResult.PartnerSteamID | 145 | partnerSteamID = parserResult.PartnerSteamID |
| 154 | if hostDemoScoreCount == 0 && hostDemoScoreTime == 0 { | 146 | if hostDemoScoreCount == 0 && hostDemoScoreTime == 0 { |
| 155 | deleteFile(srv, file.Id) | ||
| 156 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordProcessDemoFail, err.Error()) | 147 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordProcessDemoFail, err.Error()) |
| 157 | c.JSON(http.StatusOK, models.ErrorResponse("Processing demo went wrong. Please contact a web admin and provide the demo in question.")) | 148 | c.JSON(http.StatusOK, models.ErrorResponse("Processing demo went wrong. Please contact a web admin and provide the demo in question.")) |
| 158 | return | 149 | return |
| @@ -160,22 +151,25 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 160 | if !isCoop { | 151 | if !isCoop { |
| 161 | convertedSteamID := strconv.FormatInt(convertSteamID64(hostSteamID), 10) | 152 | convertedSteamID := strconv.FormatInt(convertSteamID64(hostSteamID), 10) |
| 162 | if convertedSteamID != user.(models.User).SteamID { | 153 | if convertedSteamID != user.(models.User).SteamID { |
| 163 | deleteFile(srv, file.Id) | ||
| 164 | c.JSON(http.StatusOK, models.ErrorResponse(fmt.Sprintf("Host SteamID from demo and request does not match! Check your submission and try again.\nDemo Host SteamID: %s\nRequest Host SteamID: %s", convertedSteamID, user.(models.User).SteamID))) | 154 | c.JSON(http.StatusOK, models.ErrorResponse(fmt.Sprintf("Host SteamID from demo and request does not match! Check your submission and try again.\nDemo Host SteamID: %s\nRequest Host SteamID: %s", convertedSteamID, user.(models.User).SteamID))) |
| 165 | return | 155 | return |
| 166 | } | 156 | } |
| 167 | } else { | 157 | } else { |
| 168 | if parserResult.IsHost && i != 0 { | 158 | if parserResult.IsHost && i != 0 { |
| 169 | deleteFile(srv, file.Id) | ||
| 170 | c.JSON(http.StatusOK, models.ErrorResponse("Given partner demo is a host demo.")) | 159 | c.JSON(http.StatusOK, models.ErrorResponse("Given partner demo is a host demo.")) |
| 171 | return | 160 | return |
| 172 | } | 161 | } |
| 173 | if !parserResult.IsHost && i == 0 { | 162 | if !parserResult.IsHost && i == 0 { |
| 174 | deleteFile(srv, file.Id) | ||
| 175 | c.JSON(http.StatusOK, models.ErrorResponse("Given host demo is a partner demo.")) | 163 | c.JSON(http.StatusOK, models.ErrorResponse("Given host demo is a partner demo.")) |
| 176 | return | 164 | return |
| 177 | } | 165 | } |
| 178 | } | 166 | } |
| 167 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | ||
| 168 | if err != nil { | ||
| 169 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordCreateDemoFail, err.Error()) | ||
| 170 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 171 | return | ||
| 172 | } | ||
| 179 | if i == 0 { | 173 | if i == 0 { |
| 180 | hostDemoFileID = file.Id | 174 | hostDemoFileID = file.Id |
| 181 | hostDemoUUID = uuid | 175 | hostDemoUUID = uuid |
diff --git a/backend/parser/parser.go b/backend/parser/parser.go index bf25caa..aa19592 100644 --- a/backend/parser/parser.go +++ b/backend/parser/parser.go | |||
| @@ -38,16 +38,16 @@ func ProcessDemo(filePath string) (Result, error) { | |||
| 38 | mapName := reader.TryReadStringLength(260) | 38 | mapName := reader.TryReadStringLength(260) |
| 39 | reader.SkipBytes(276) | 39 | reader.SkipBytes(276) |
| 40 | if demoFileStamp != "HL2DEMO" { | 40 | if demoFileStamp != "HL2DEMO" { |
| 41 | return Result{}, errors.New("invalid demo file stamp") | 41 | return Result{}, errors.New("Invalid demo file stamp.") |
| 42 | } | 42 | } |
| 43 | if demoProtocol != 4 { | 43 | if demoProtocol != 4 { |
| 44 | return Result{}, errors.New("this parser only supports demos from new engine") | 44 | return Result{}, errors.New("Invalid demo protocol.") |
| 45 | } | 45 | } |
| 46 | if networkProtocol != 2001 { | 46 | if networkProtocol != 2001 { |
| 47 | return Result{}, errors.New("this parser only supports demos from portal 2") | 47 | return Result{}, errors.New("Invalid network protocol.") |
| 48 | } | 48 | } |
| 49 | if mapDict[mapName] == 0 { | 49 | if mapDict[mapName] == 0 { |
| 50 | return Result{}, errors.New("demo recorded on an invalid map") | 50 | return Result{}, errors.New("Invalid map.") |
| 51 | } | 51 | } |
| 52 | result.MapID = mapDict[mapName] | 52 | result.MapID = mapDict[mapName] |
| 53 | for { | 53 | for { |
| @@ -419,7 +419,7 @@ var mapDict = map[string]int{ | |||
| 419 | "sp_a4_finale4": 60, | 419 | "sp_a4_finale4": 60, |
| 420 | 420 | ||
| 421 | "mp_coop_start": 61, | 421 | "mp_coop_start": 61, |
| 422 | "mp_coop_lobby_2": 62, | 422 | "mp_coop_lobby_3": 62, |
| 423 | 423 | ||
| 424 | "mp_coop_doors": 63, | 424 | "mp_coop_doors": 63, |
| 425 | "mp_coop_race_2": 64, | 425 | "mp_coop_race_2": 64, |
| @@ -465,7 +465,7 @@ var mapDict = map[string]int{ | |||
| 465 | "mp_coop_paint_speed_catch": 100, | 465 | "mp_coop_paint_speed_catch": 100, |
| 466 | "mp_coop_paint_longjump_intro": 101, | 466 | "mp_coop_paint_longjump_intro": 101, |
| 467 | 467 | ||
| 468 | "mp_coop_seperation_1": 102, | 468 | "mp_coop_separation_1": 102, |
| 469 | "mp_coop_tripleaxis": 103, | 469 | "mp_coop_tripleaxis": 103, |
| 470 | "mp_coop_catapult_catch": 104, | 470 | "mp_coop_catapult_catch": 104, |
| 471 | "mp_coop_2paints_1bridge": 105, | 471 | "mp_coop_2paints_1bridge": 105, |