aboutsummaryrefslogtreecommitdiff
path: root/backend/handlers/record.go
blob: 697cd4025da75906d9c8a85bcc6afcf6e05fcbe2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package handlers

import (
	"context"
	"encoding/base64"
	"io"
	"log"
	"mime/multipart"
	"net/http"
	"os"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"github.com/pektezol/leastportalshub/backend/database"
	"github.com/pektezol/leastportalshub/backend/models"
	"github.com/pektezol/leastportalshub/backend/parser"
	"golang.org/x/oauth2/google"
	"golang.org/x/oauth2/jwt"
	"google.golang.org/api/drive/v3"
)

type RecordRequest struct {
	HostDemo        *multipart.FileHeader `json:"host_demo" form:"host_demo" binding:"required" swaggerignore:"true"`
	PartnerDemo     *multipart.FileHeader `json:"partner_demo" form:"partner_demo" swaggerignore:"true"`
	IsPartnerOrange bool                  `json:"is_partner_orange" form:"is_partner_orange"`
	PartnerID       string                `json:"partner_id" form:"partner_id"`
}

type RecordResponse struct {
	ScoreCount int `json:"score_count"`
	ScoreTime  int `json:"score_time"`
}

// POST Record
//
//	@Description	Post record with demo of a specific map.
//	@Tags			maps
//	@Accept			mpfd
//	@Produce		json
//	@Param			mapid				path		int		true	"Map ID"
//	@Param			Authorization		header		string	true	"JWT Token"
//	@Param			host_demo			formData	file	true	"Host Demo"
//	@Param			partner_demo		formData	file	false	"Partner Demo"
//	@Param			is_partner_orange	formData	boolean	false	"Is Partner Orange"
//	@Param			partner_id			formData	string	false	"Partner ID"
//	@Success		200					{object}	models.Response{data=RecordResponse}
//	@Router			/maps/{mapid}/record [post]
func CreateRecordWithDemo(c *gin.Context) {
	mapId := c.Param("mapid")
	// Check if user exists
	user, exists := c.Get("user")
	if !exists {
		c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
		return
	}
	// Check if map is sp or mp
	var gameName string
	var isCoop bool
	var isDisabled bool
	sql := `SELECT g.name, m.is_disabled FROM maps m INNER JOIN games g ON m.game_id=g.id WHERE m.id = $1`
	err := database.DB.QueryRow(sql, mapId).Scan(&gameName, &isDisabled)
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	if isDisabled {
		c.JSON(http.StatusOK, models.ErrorResponse("Map is not available for competitive boards."))
		return
	}
	if gameName == "Portal 2 - Cooperative" {
		isCoop = true
	}
	// Get record request
	var record RecordRequest
	if err := c.ShouldBind(&record); err != nil {
		CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInvalidRequestFail, "BIND: "+err.Error())
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	if isCoop && (record.PartnerDemo == nil || record.PartnerID == "") {
		CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInvalidRequestFail)
		c.JSON(http.StatusOK, models.ErrorResponse("Invalid entry for coop record submission."))
		return
	}
	// Demo files
	demoFiles := []*multipart.FileHeader{record.HostDemo}
	if isCoop {
		demoFiles = append(demoFiles, record.PartnerDemo)
	}
	var hostDemoUUID, hostDemoFileID, partnerDemoUUID, partnerDemoFileID string
	var hostDemoScoreCount, hostDemoScoreTime int
	client := serviceAccount()
	srv, err := drive.New(client)
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	// Create database transaction for inserts
	tx, err := database.DB.Begin()
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	// Defer to a rollback in case anything fails
	defer tx.Rollback()
	for i, header := range demoFiles {
		uuid := uuid.New().String()
		// Upload & insert into demos
		err = c.SaveUploadedFile(header, "backend/parser/"+uuid+".dem")
		if err != nil {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordSaveDemoFail, err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
		defer os.Remove("backend/parser/" + uuid + ".dem")
		f, err := os.Open("backend/parser/" + uuid + ".dem")
		if err != nil {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordOpenDemoFail, err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
		defer f.Close()
		file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID"))
		if err != nil {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordCreateDemoFail, err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
		hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo("backend/parser/" + uuid + ".dem")
		if err != nil {
			deleteFile(srv, file.Id)
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordProcessDemoFail, err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
		if i == 0 {
			hostDemoFileID = file.Id
			hostDemoUUID = uuid
		} else if i == 1 {
			partnerDemoFileID = file.Id
			partnerDemoUUID = uuid
		}
		_, err = tx.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id)
		if err != nil {
			deleteFile(srv, file.Id)
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInsertDemoFail, err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
	}
	// Insert into records
	if isCoop {
		sql := `INSERT INTO records_mp(map_id,score_count,score_time,host_id,partner_id,host_demo_id,partner_demo_id) 
		VALUES($1, $2, $3, $4, $5, $6, $7)`
		var hostID string
		var partnerID string
		if record.IsPartnerOrange {
			hostID = user.(models.User).SteamID
			partnerID = record.PartnerID
		} else {
			partnerID = user.(models.User).SteamID
			hostID = record.PartnerID
		}
		_, err := tx.Exec(sql, mapId, hostDemoScoreCount, hostDemoScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID)
		if err != nil {
			deleteFile(srv, hostDemoFileID)
			deleteFile(srv, partnerDemoFileID)
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInsertRecordFail, err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
	} else {
		sql := `INSERT INTO records_sp(map_id,score_count,score_time,user_id,demo_id) 
		VALUES($1, $2, $3, $4, $5)`
		_, err := tx.Exec(sql, mapId, hostDemoScoreCount, hostDemoScoreTime, user.(models.User).SteamID, hostDemoUUID)
		if err != nil {
			deleteFile(srv, hostDemoFileID)
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInsertRecordFail, err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
	}
	if err = tx.Commit(); err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordSuccess)
	c.JSON(http.StatusOK, models.Response{
		Success: true,
		Message: "Successfully created record.",
		Data:    RecordResponse{ScoreCount: hostDemoScoreCount, ScoreTime: hostDemoScoreTime},
	})
}

// DELETE Record
//
//	@Description	Delete record with specified map and record id.
//	@Tags			maps
//	@Produce		json
//	@Param			mapid			path		int		true	"Map ID"
//	@Param			recordid		path		int		true	"Record ID"
//	@Param			Authorization	header		string	true	"JWT Token"
//	@Success		200				{object}	models.Response
//	@Router			/maps/{mapid}/record/{recordid} [delete]
func DeleteRecord(c *gin.Context) {
	mapID, err := strconv.Atoi(c.Param("mapid"))
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	recordID, err := strconv.Atoi(c.Param("recordid"))
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	user, exists := c.Get("user")
	if !exists {
		c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
		return
	}
	// Validate map
	var validateMapID int
	var isCoop bool
	sql := `SELECT m.id, g.is_coop FROM maps m INNER JOIN games g ON m.game_id = g.id
	INNER JOIN chapters c ON m.chapter_id = c.id WHERE m.id = $1`
	err = database.DB.QueryRow(sql, mapID).Scan(&validateMapID, &isCoop)
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	if mapID != validateMapID {
		c.JSON(http.StatusOK, models.ErrorResponse("Selected map does not exist."))
		return
	}
	if isCoop {
		// Validate if cooperative record does exist
		var validateRecordID int
		sql = `SELECT mp.id FROM records_mp mp WHERE mp.id = $1 AND mp.map_id = $2 AND (mp.host_id = $3 OR mp.partner_id = $3) AND is_deleted = false`
		err = database.DB.QueryRow(sql, recordID, mapID, user.(models.User).SteamID).Scan(&validateRecordID)
		if err != nil {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "SELECT#records_mp: "+err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
		if recordID != validateRecordID {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "recordID != validateRecordID")
			c.JSON(http.StatusOK, models.ErrorResponse("Selected record does not exist."))
			return
		}
		// Remove record
		sql = `UPDATE records_mp SET is_deleted = true WHERE id = $1`
		_, err = database.DB.Exec(sql, recordID)
		if err != nil {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "UPDATE#records_mp: "+err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
	} else {
		// Validate if singleplayer record does exist
		var validateRecordID int
		sql = `SELECT sp.id FROM records_sp sp WHERE sp.id = $1 AND sp.map_id = $2 AND sp.user_id = $3 AND is_deleted = false`
		err = database.DB.QueryRow(sql, recordID, mapID, user.(models.User).SteamID).Scan(&validateRecordID)
		if err != nil {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "SELECT#records_sp: "+err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
		if recordID != validateRecordID {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "recordID != validateRecordID")
			c.JSON(http.StatusOK, models.ErrorResponse("Selected record does not exist."))
			return
		}
		// Remove record
		sql = `UPDATE records_sp SET is_deleted = true WHERE id = $1`
		_, err = database.DB.Exec(sql, recordID)
		if err != nil {
			CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "UPDATE#records_sp: "+err.Error())
			c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
			return
		}
	}
	CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordSuccess)
	c.JSON(http.StatusOK, models.Response{
		Success: true,
		Message: "Successfully deleted record.",
		Data:    nil,
	})
}

// GET Demo
//
//	@Description	Get demo with specified demo uuid.
//	@Tags			demo
//	@Accept			json
//	@Produce		octet-stream
//	@Param			uuid	query	string	true	"Demo UUID"
//	@Success		200		{file}	binary	"Demo File"
//	@Router			/demos [get]
func DownloadDemoWithID(c *gin.Context) {
	uuid := c.Query("uuid")
	var locationID string
	if uuid == "" {
		c.JSON(http.StatusOK, models.ErrorResponse("Invalid id given."))
		return
	}
	err := database.DB.QueryRow(`SELECT location_id FROM demos WHERE id = $1`, uuid).Scan(&locationID)
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	if locationID == "" {
		c.JSON(http.StatusOK, models.ErrorResponse("Invalid id given."))
		return
	}
	url := "https://drive.google.com/uc?export=download&id=" + locationID
	fileName := uuid + ".dem"
	output, err := os.Create(fileName)
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	defer os.Remove(fileName)
	defer output.Close()
	response, err := http.Get(url)
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	defer response.Body.Close()
	_, err = io.Copy(output, response.Body)
	if err != nil {
		c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
		return
	}
	// Downloaded file
	c.Header("Content-Description", "File Transfer")
	c.Header("Content-Transfer-Encoding", "binary")
	c.Header("Content-Disposition", "attachment; filename="+fileName)
	c.Header("Content-Type", "application/octet-stream")
	c.File(fileName)
	// c.FileAttachment()
}

// Use Service account
func serviceAccount() *http.Client {
	privateKey, _ := base64.StdEncoding.DecodeString(os.Getenv("GOOGLE_PRIVATE_KEY_BASE64"))
	config := &jwt.Config{
		Email:      os.Getenv("GOOGLE_CLIENT_EMAIL"),
		PrivateKey: []byte(privateKey),
		Scopes: []string{
			drive.DriveScope,
		},
		TokenURL: google.JWTTokenURL,
	}
	client := config.Client(context.Background())
	return client
}

// Create Gdrive file
func createFile(service *drive.Service, name string, mimeType string, content io.Reader, parentId string) (*drive.File, error) {
	f := &drive.File{
		MimeType: mimeType,
		Name:     name,
		Parents:  []string{parentId},
	}
	file, err := service.Files.Create(f).Media(content).Do()

	if err != nil {
		log.Println("Could not create file: " + err.Error())
		return nil, err
	}

	return file, nil
}

// Delete Gdrive file
func deleteFile(service *drive.Service, fileId string) {
	service.Files.Delete(fileId)
}