From 1a14309e212a697bae8acd4ddb17723b0f6670a6 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Mon, 3 Jul 2023 08:40:43 +0000 Subject: feat: parser for getting portal and tick count (#42) Former-commit-id: 1619ece868b7009a661dcc3b622746cc09981042 --- backend/controllers/recordController.go | 2 +- backend/parser/parser.go | 41 ++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/backend/controllers/recordController.go b/backend/controllers/recordController.go index c865bfb..183ab27 100644 --- a/backend/controllers/recordController.go +++ b/backend/controllers/recordController.go @@ -106,7 +106,7 @@ func CreateRecordWithDemo(c *gin.Context) { c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) return } - hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo(record.HostDemo) + hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo("parser/demos/" + header.Filename) if err != nil { c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) return diff --git a/backend/parser/parser.go b/backend/parser/parser.go index 6f9a24f..040b94a 100644 --- a/backend/parser/parser.go +++ b/backend/parser/parser.go @@ -1,7 +1,42 @@ package parser -import "mime/multipart" +import ( + "bufio" + "fmt" + "os/exec" + "strconv" + "strings" +) -func ProcessDemo(demo *multipart.FileHeader) (scoreCount int, scoreTime int, err error) { - return 0, 0, nil +func ProcessDemo(demoPath string) (int, int, error) { + cmd := exec.Command("bash", "-c", fmt.Sprintf(`echo "FEXBash" && ./parser %s`, demoPath)) + stdout, err := cmd.StdoutPipe() + if err != nil { + return 0, 0, err + } + cmd.Start() + scanner := bufio.NewScanner(stdout) + var cmTicks, portalCount int + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, "CM ticks") { + cmTicksStr := strings.TrimSpace(strings.Split(line, ":")[1]) + cmTicks, err = strconv.Atoi(cmTicksStr) + if err != nil { + return 0, 0, err + } + } + if strings.Contains(line, "Portal count") { + portalCountStr := strings.TrimSpace(strings.Split(line, ":")[1]) + portalCount, err = strconv.Atoi(portalCountStr) + if err != nil { + return 0, 0, err + } + } + } + err = cmd.Wait() + if err != nil { + return 0, 0, err + } + return cmTicks, portalCount, nil } -- cgit v1.2.3