aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-07-03 08:40:43 +0000
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-07-03 08:40:43 +0000
commit1a14309e212a697bae8acd4ddb17723b0f6670a6 (patch)
treeea6a255e6170c31b2928f164e4b3329caec091b0
parentfeat: add moderator status into jwt token (diff)
downloadlphub-1a14309e212a697bae8acd4ddb17723b0f6670a6.tar.gz
lphub-1a14309e212a697bae8acd4ddb17723b0f6670a6.tar.bz2
lphub-1a14309e212a697bae8acd4ddb17723b0f6670a6.zip
feat: parser for getting portal and tick count (#42)
Former-commit-id: 1619ece868b7009a661dcc3b622746cc09981042
-rw-r--r--backend/controllers/recordController.go2
-rw-r--r--backend/parser/parser.go41
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) {
106 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 106 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
107 return 107 return
108 } 108 }
109 hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo(record.HostDemo) 109 hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo("parser/demos/" + header.Filename)
110 if err != nil { 110 if err != nil {
111 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 111 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
112 return 112 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 @@
1package parser 1package parser
2 2
3import "mime/multipart" 3import (
4 "bufio"
5 "fmt"
6 "os/exec"
7 "strconv"
8 "strings"
9)
4 10
5func ProcessDemo(demo *multipart.FileHeader) (scoreCount int, scoreTime int, err error) { 11func ProcessDemo(demoPath string) (int, int, error) {
6 return 0, 0, nil 12 cmd := exec.Command("bash", "-c", fmt.Sprintf(`echo "FEXBash" && ./parser %s`, demoPath))
13 stdout, err := cmd.StdoutPipe()
14 if err != nil {
15 return 0, 0, err
16 }
17 cmd.Start()
18 scanner := bufio.NewScanner(stdout)
19 var cmTicks, portalCount int
20 for scanner.Scan() {
21 line := scanner.Text()
22 if strings.Contains(line, "CM ticks") {
23 cmTicksStr := strings.TrimSpace(strings.Split(line, ":")[1])
24 cmTicks, err = strconv.Atoi(cmTicksStr)
25 if err != nil {
26 return 0, 0, err
27 }
28 }
29 if strings.Contains(line, "Portal count") {
30 portalCountStr := strings.TrimSpace(strings.Split(line, ":")[1])
31 portalCount, err = strconv.Atoi(portalCountStr)
32 if err != nil {
33 return 0, 0, err
34 }
35 }
36 }
37 err = cmd.Wait()
38 if err != nil {
39 return 0, 0, err
40 }
41 return cmTicks, portalCount, nil
7} 42}