diff options
| author | Nidboj132 <lol2s@vp.plm> | 2023-09-05 18:23:11 +0200 |
|---|---|---|
| committer | Nidboj132 <lol2s@vp.plm> | 2023-09-05 18:23:11 +0200 |
| commit | 3869cb67351ccf3bc45b076f31afdc7133292c39 (patch) | |
| tree | dc03341e147dde0964bf6be84b14e13424c647b7 /backend/handlers/record.go | |
| parent | added graph and fixed some css (diff) | |
| parent | fix: create map summary, why the fuck does this have to be a pointer integer?? (diff) | |
| download | lphub-3869cb67351ccf3bc45b076f31afdc7133292c39.tar.gz lphub-3869cb67351ccf3bc45b076f31afdc7133292c39.tar.bz2 lphub-3869cb67351ccf3bc45b076f31afdc7133292c39.zip | |
Merge branch 'main' of https://github.com/pektezol/LeastPortalsHub
Former-commit-id: 221385f463b7f5b0fc43a093b2c7c46e68d46d68
Diffstat (limited to '')
| -rw-r--r-- | backend/handlers/record.go (renamed from backend/controllers/recordController.go) | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/backend/controllers/recordController.go b/backend/handlers/record.go index 951be41..3d29eb8 100644 --- a/backend/controllers/recordController.go +++ b/backend/handlers/record.go | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| @@ -19,6 +19,18 @@ import ( | |||
| 19 | "google.golang.org/api/drive/v3" | 19 | "google.golang.org/api/drive/v3" |
| 20 | ) | 20 | ) |
| 21 | 21 | ||
| 22 | type RecordRequest struct { | ||
| 23 | HostDemo *multipart.FileHeader `json:"host_demo" form:"host_demo" binding:"required" swaggerignore:"true"` | ||
| 24 | PartnerDemo *multipart.FileHeader `json:"partner_demo" form:"partner_demo" swaggerignore:"true"` | ||
| 25 | IsPartnerOrange bool `json:"is_partner_orange" form:"is_partner_orange"` | ||
| 26 | PartnerID string `json:"partner_id" form:"partner_id"` | ||
| 27 | } | ||
| 28 | |||
| 29 | type RecordResponse struct { | ||
| 30 | ScoreCount int `json:"score_count"` | ||
| 31 | ScoreTime int `json:"score_time"` | ||
| 32 | } | ||
| 33 | |||
| 22 | // POST Record | 34 | // POST Record |
| 23 | // | 35 | // |
| 24 | // @Description Post record with demo of a specific map. | 36 | // @Description Post record with demo of a specific map. |
| @@ -31,7 +43,7 @@ import ( | |||
| 31 | // @Param partner_demo formData file false "Partner Demo" | 43 | // @Param partner_demo formData file false "Partner Demo" |
| 32 | // @Param is_partner_orange formData boolean false "Is Partner Orange" | 44 | // @Param is_partner_orange formData boolean false "Is Partner Orange" |
| 33 | // @Param partner_id formData string false "Partner ID" | 45 | // @Param partner_id formData string false "Partner ID" |
| 34 | // @Success 200 {object} models.Response{data=models.RecordResponse} | 46 | // @Success 200 {object} models.Response{data=RecordResponse} |
| 35 | // @Failure 400 {object} models.Response | 47 | // @Failure 400 {object} models.Response |
| 36 | // @Failure 401 {object} models.Response | 48 | // @Failure 401 {object} models.Response |
| 37 | // @Router /maps/{id}/record [post] | 49 | // @Router /maps/{id}/record [post] |
| @@ -54,6 +66,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 54 | return | 66 | return |
| 55 | } | 67 | } |
| 56 | if isDisabled { | 68 | if isDisabled { |
| 69 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) | ||
| 57 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map is not available for competitive boards.")) | 70 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map is not available for competitive boards.")) |
| 58 | return | 71 | return |
| 59 | } | 72 | } |
| @@ -61,12 +74,14 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 61 | isCoop = true | 74 | isCoop = true |
| 62 | } | 75 | } |
| 63 | // Get record request | 76 | // Get record request |
| 64 | var record models.RecordRequest | 77 | var record RecordRequest |
| 65 | if err := c.ShouldBind(&record); err != nil { | 78 | if err := c.ShouldBind(&record); err != nil { |
| 79 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) | ||
| 66 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 80 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 67 | return | 81 | return |
| 68 | } | 82 | } |
| 69 | if isCoop && (record.PartnerDemo == nil || record.PartnerID == "") { | 83 | if isCoop && (record.PartnerDemo == nil || record.PartnerID == "") { |
| 84 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) | ||
| 70 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid entry for coop record submission.")) | 85 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid entry for coop record submission.")) |
| 71 | return | 86 | return |
| 72 | } | 87 | } |
| @@ -96,23 +111,27 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 96 | // Upload & insert into demos | 111 | // Upload & insert into demos |
| 97 | err = c.SaveUploadedFile(header, "backend/parser/"+uuid+".dem") | 112 | err = c.SaveUploadedFile(header, "backend/parser/"+uuid+".dem") |
| 98 | if err != nil { | 113 | if err != nil { |
| 114 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailSaveDemo) | ||
| 99 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 115 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 100 | return | 116 | return |
| 101 | } | 117 | } |
| 102 | defer os.Remove("backend/parser/" + uuid + ".dem") | 118 | defer os.Remove("backend/parser/" + uuid + ".dem") |
| 103 | f, err := os.Open("backend/parser/" + uuid + ".dem") | 119 | f, err := os.Open("backend/parser/" + uuid + ".dem") |
| 104 | if err != nil { | 120 | if err != nil { |
| 121 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailOpenDemo) | ||
| 105 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 122 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 106 | return | 123 | return |
| 107 | } | 124 | } |
| 108 | defer f.Close() | 125 | defer f.Close() |
| 109 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | 126 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) |
| 110 | if err != nil { | 127 | if err != nil { |
| 128 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailCreateDemo) | ||
| 111 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 129 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 112 | return | 130 | return |
| 113 | } | 131 | } |
| 114 | hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo("backend/parser/" + uuid + ".dem") | 132 | hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo("backend/parser/" + uuid + ".dem") |
| 115 | if err != nil { | 133 | if err != nil { |
| 134 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailProcessDemo) | ||
| 116 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 135 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 117 | return | 136 | return |
| 118 | } | 137 | } |
| @@ -126,6 +145,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 126 | _, err = tx.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id) | 145 | _, err = tx.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id) |
| 127 | if err != nil { | 146 | if err != nil { |
| 128 | deleteFile(srv, file.Id) | 147 | deleteFile(srv, file.Id) |
| 148 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertDemo) | ||
| 129 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 149 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 130 | return | 150 | return |
| 131 | } | 151 | } |
| @@ -147,6 +167,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 147 | if err != nil { | 167 | if err != nil { |
| 148 | deleteFile(srv, hostDemoFileID) | 168 | deleteFile(srv, hostDemoFileID) |
| 149 | deleteFile(srv, partnerDemoFileID) | 169 | deleteFile(srv, partnerDemoFileID) |
| 170 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertRecord) | ||
| 150 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 171 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 151 | return | 172 | return |
| 152 | } | 173 | } |
| @@ -164,6 +185,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 164 | _, err := tx.Exec(sql, mapId, hostDemoScoreCount, hostDemoScoreTime, user.(models.User).SteamID, hostDemoUUID) | 185 | _, err := tx.Exec(sql, mapId, hostDemoScoreCount, hostDemoScoreTime, user.(models.User).SteamID, hostDemoUUID) |
| 165 | if err != nil { | 186 | if err != nil { |
| 166 | deleteFile(srv, hostDemoFileID) | 187 | deleteFile(srv, hostDemoFileID) |
| 188 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertRecord) | ||
| 167 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 189 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 168 | return | 190 | return |
| 169 | } | 191 | } |
| @@ -180,10 +202,11 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 180 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | 202 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) |
| 181 | return | 203 | return |
| 182 | } | 204 | } |
| 205 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordSuccess) | ||
| 183 | c.JSON(http.StatusOK, models.Response{ | 206 | c.JSON(http.StatusOK, models.Response{ |
| 184 | Success: true, | 207 | Success: true, |
| 185 | Message: "Successfully created record.", | 208 | Message: "Successfully created record.", |
| 186 | Data: models.RecordResponse{ScoreCount: hostDemoScoreCount, ScoreTime: hostDemoScoreTime}, | 209 | Data: RecordResponse{ScoreCount: hostDemoScoreCount, ScoreTime: hostDemoScoreTime}, |
| 187 | }) | 210 | }) |
| 188 | } | 211 | } |
| 189 | 212 | ||
| @@ -216,6 +239,10 @@ func DownloadDemoWithID(c *gin.Context) { | |||
| 216 | url := "https://drive.google.com/uc?export=download&id=" + locationID | 239 | url := "https://drive.google.com/uc?export=download&id=" + locationID |
| 217 | fileName := uuid + ".dem" | 240 | fileName := uuid + ".dem" |
| 218 | output, err := os.Create(fileName) | 241 | output, err := os.Create(fileName) |
| 242 | if err != nil { | ||
| 243 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 244 | return | ||
| 245 | } | ||
| 219 | defer os.Remove(fileName) | 246 | defer os.Remove(fileName) |
| 220 | defer output.Close() | 247 | defer output.Close() |
| 221 | response, err := http.Get(url) | 248 | response, err := http.Get(url) |
| @@ -253,6 +280,7 @@ func serviceAccount() *http.Client { | |||
| 253 | return client | 280 | return client |
| 254 | } | 281 | } |
| 255 | 282 | ||
| 283 | // Create Gdrive file | ||
| 256 | func createFile(service *drive.Service, name string, mimeType string, content io.Reader, parentId string) (*drive.File, error) { | 284 | func createFile(service *drive.Service, name string, mimeType string, content io.Reader, parentId string) (*drive.File, error) { |
| 257 | f := &drive.File{ | 285 | f := &drive.File{ |
| 258 | MimeType: mimeType, | 286 | MimeType: mimeType, |
| @@ -269,6 +297,7 @@ func createFile(service *drive.Service, name string, mimeType string, content io | |||
| 269 | return file, nil | 297 | return file, nil |
| 270 | } | 298 | } |
| 271 | 299 | ||
| 300 | // Delete Gdrive file | ||
| 272 | func deleteFile(service *drive.Service, fileId string) { | 301 | func deleteFile(service *drive.Service, fileId string) { |
| 273 | service.Files.Delete(fileId) | 302 | service.Files.Delete(fileId) |
| 274 | } | 303 | } |