diff options
Diffstat (limited to 'backend/controllers/recordController.go')
| -rw-r--r-- | backend/controllers/recordController.go | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/backend/controllers/recordController.go b/backend/controllers/recordController.go index 3b4bdc7..bafa844 100644 --- a/backend/controllers/recordController.go +++ b/backend/controllers/recordController.go | |||
| @@ -43,11 +43,11 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 43 | return | 43 | return |
| 44 | } | 44 | } |
| 45 | // Check if map is sp or mp | 45 | // Check if map is sp or mp |
| 46 | var wrScore int | 46 | var gameID int |
| 47 | var wrTime int | ||
| 48 | var isCoop bool | 47 | var isCoop bool |
| 49 | var isDisabled bool | 48 | var isDisabled bool |
| 50 | err := database.DB.QueryRow(`SELECT wr_score, wr_time, is_coop, is_disabled FROM maps WHERE id = $1;`, mapId).Scan(&wrScore, &wrTime, &isCoop, &isDisabled) | 49 | sql := `SELECT game_id, is_disabled FROM maps WHERE id = $1;` |
| 50 | err := database.DB.QueryRow(sql, mapId).Scan(&gameID, &isDisabled) | ||
| 51 | if err != nil { | 51 | if err != nil { |
| 52 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 52 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 53 | return | 53 | return |
| @@ -56,6 +56,9 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 56 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map is not available for competitive boards.")) | 56 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map is not available for competitive boards.")) |
| 57 | return | 57 | return |
| 58 | } | 58 | } |
| 59 | if gameID == 2 { | ||
| 60 | isCoop = true | ||
| 61 | } | ||
| 59 | // Get record request | 62 | // Get record request |
| 60 | var record models.RecordRequest | 63 | var record models.RecordRequest |
| 61 | score_count, err := strconv.Atoi(c.PostForm("score_count")) | 64 | score_count, err := strconv.Atoi(c.PostForm("score_count")) |
| @@ -104,6 +107,14 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 104 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 107 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 105 | return | 108 | return |
| 106 | } | 109 | } |
| 110 | // Create database transaction for inserts | ||
| 111 | tx, err := database.DB.Begin() | ||
| 112 | if err != nil { | ||
| 113 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 114 | return | ||
| 115 | } | ||
| 116 | // Defer to a rollback in case anything fails | ||
| 117 | defer tx.Rollback() | ||
| 107 | fileID := "" | 118 | fileID := "" |
| 108 | for i, header := range files { | 119 | for i, header := range files { |
| 109 | uuid := uuid.New().String() | 120 | uuid := uuid.New().String() |
| @@ -131,7 +142,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 131 | if i == 1 { | 142 | if i == 1 { |
| 132 | partnerDemoUUID = uuid | 143 | partnerDemoUUID = uuid |
| 133 | } | 144 | } |
| 134 | _, err = database.DB.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) |
| 135 | if err != nil { | 146 | if err != nil { |
| 136 | deleteFile(srv, file.Id) | 147 | deleteFile(srv, file.Id) |
| 137 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 148 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| @@ -152,37 +163,41 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 152 | partnerID = user.(models.User).SteamID | 163 | partnerID = user.(models.User).SteamID |
| 153 | hostID = record.PartnerID | 164 | hostID = record.PartnerID |
| 154 | } | 165 | } |
| 155 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID) | 166 | _, err := tx.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID) |
| 156 | if err != nil { | 167 | if err != nil { |
| 157 | deleteFile(srv, fileID) | 168 | deleteFile(srv, fileID) |
| 158 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 169 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 159 | return | 170 | return |
| 160 | } | 171 | } |
| 161 | // If a new world record based on portal count | 172 | // If a new world record based on portal count |
| 162 | if record.ScoreCount < wrScore { | 173 | // if record.ScoreCount < wrScore { |
| 163 | _, err := database.DB.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3;`, record.ScoreCount, record.ScoreTime, mapId) | 174 | // _, err := tx.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3;`, record.ScoreCount, record.ScoreTime, mapId) |
| 164 | if err != nil { | 175 | // if err != nil { |
| 165 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 176 | // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 166 | return | 177 | // return |
| 167 | } | 178 | // } |
| 168 | } | 179 | // } |
| 169 | } else { | 180 | } else { |
| 170 | sql := `INSERT INTO records_sp(map_id,score_count,score_time,user_id,demo_id) | 181 | sql := `INSERT INTO records_sp(map_id,score_count,score_time,user_id,demo_id) |
| 171 | VALUES($1, $2, $3, $4, $5);` | 182 | VALUES($1, $2, $3, $4, $5);` |
| 172 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, user.(models.User).SteamID, hostDemoUUID) | 183 | _, err := tx.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, user.(models.User).SteamID, hostDemoUUID) |
| 173 | if err != nil { | 184 | if err != nil { |
| 174 | deleteFile(srv, fileID) | 185 | deleteFile(srv, fileID) |
| 175 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 186 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 176 | return | 187 | return |
| 177 | } | 188 | } |
| 178 | // If a new world record based on portal count | 189 | // If a new world record based on portal count |
| 179 | if record.ScoreCount < wrScore { | 190 | // if record.ScoreCount < wrScore { |
| 180 | _, err := database.DB.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3;`, record.ScoreCount, record.ScoreTime, mapId) | 191 | // _, err := tx.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3;`, record.ScoreCount, record.ScoreTime, mapId) |
| 181 | if err != nil { | 192 | // if err != nil { |
| 182 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 193 | // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 183 | return | 194 | // return |
| 184 | } | 195 | // } |
| 185 | } | 196 | // } |
| 197 | } | ||
| 198 | if err = tx.Commit(); err != nil { | ||
| 199 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 200 | return | ||
| 186 | } | 201 | } |
| 187 | c.JSON(http.StatusOK, models.Response{ | 202 | c.JSON(http.StatusOK, models.Response{ |
| 188 | Success: true, | 203 | Success: true, |
| @@ -198,10 +213,10 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 198 | // @Tags demo | 213 | // @Tags demo |
| 199 | // @Accept json | 214 | // @Accept json |
| 200 | // @Produce octet-stream | 215 | // @Produce octet-stream |
| 201 | // @Param uuid path int true "Demo UUID" | 216 | // @Param uuid query int true "Demo UUID" |
| 202 | // @Success 200 {file} binary "Demo File" | 217 | // @Success 200 {file} binary "Demo File" |
| 203 | // @Failure 400 {object} models.Response | 218 | // @Failure 400 {object} models.Response |
| 204 | // @Router /demo [get] | 219 | // @Router /demos [get] |
| 205 | func DownloadDemoWithID(c *gin.Context) { | 220 | func DownloadDemoWithID(c *gin.Context) { |
| 206 | uuid := c.Query("uuid") | 221 | uuid := c.Query("uuid") |
| 207 | var locationID string | 222 | var locationID string |