aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/database/categories.sql12
-rw-r--r--backend/database/init.sql9
-rw-r--r--backend/handlers/map.go42
-rw-r--r--backend/models/models.go24
4 files changed, 76 insertions, 11 deletions
diff --git a/backend/database/categories.sql b/backend/database/categories.sql
index 5cadad5..6bbf9b0 100644
--- a/backend/database/categories.sql
+++ b/backend/database/categories.sql
@@ -2,4 +2,14 @@ INSERT INTO categories(id, name) VALUES
2(1, 'CM'), 2(1, 'CM'),
3(2, 'No SLA'), 3(2, 'No SLA'),
4(3, 'Inbounds SLA'), 4(3, 'Inbounds SLA'),
5(4, 'Any%'); \ No newline at end of file 5(4, 'Any%'),
6(5, 'All Courses');
7
8INSERT INTO game_categories(id, game_id, category_id) VALUES
9(1, 1, 1),
10(2, 1, 2),
11(3, 1, 3),
12(4, 1, 4),
13(5, 2, 1),
14(6, 2, 4),
15(7, 2, 5); \ No newline at end of file
diff --git a/backend/database/init.sql b/backend/database/init.sql
index c33821b..77a88f5 100644
--- a/backend/database/init.sql
+++ b/backend/database/init.sql
@@ -36,6 +36,15 @@ CREATE TABLE categories (
36 PRIMARY KEY (id) 36 PRIMARY KEY (id)
37); 37);
38 38
39CREATE TABLE game_categories (
40 id SERIAL,
41 game_id SMALLINT NOT NULL,
42 category_id SMALLINT NOT NULL,
43 PRIMARY KEY (id),
44 FOREIGN KEY (game_id) REFERENCES games(id),
45 FOREIGN KEY (category_id) REFERENCES categories(id)
46);
47
39CREATE TABLE maps ( 48CREATE TABLE maps (
40 id SERIAL, 49 id SERIAL,
41 game_id SMALLINT NOT NULL, 50 game_id SMALLINT NOT NULL,
diff --git a/backend/handlers/map.go b/backend/handlers/map.go
index bf7c821..8104243 100644
--- a/backend/handlers/map.go
+++ b/backend/handlers/map.go
@@ -339,6 +339,44 @@ func FetchGames(c *gin.Context) {
339 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 339 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
340 return 340 return
341 } 341 }
342 categoryPortalRows, err := database.DB.Query(`SELECT c.id, c.name FROM game_categories gc JOIN categories c ON gc.category_id = c.id WHERE gc.game_id = $1`, game.ID)
343 if err != nil {
344 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
345 return
346 }
347 for categoryPortalRows.Next() {
348 var categoryPortals models.CategoryPortal
349 if err := categoryPortalRows.Scan(&categoryPortals.Category.ID, &categoryPortals.Category.Name); err != nil {
350 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
351 return
352 }
353 getCategoryPortalCount := `
354 SELECT
355 SUM(mh.lowest_score_count) AS total_lowest_scores
356 FROM (
357 SELECT
358 map_id,
359 category_id,
360 MIN(score_count) AS lowest_score_count
361 FROM
362 map_history
363 GROUP BY
364 map_id,
365 category_id
366 ) mh
367 JOIN maps m ON mh.map_id = m.id
368 JOIN games g ON m.game_id = g.id
369 WHERE
370 mh.category_id = $1 and g.id = $2
371 GROUP BY
372 g.id,
373 g.name,
374 mh.category_id;
375 `
376 database.DB.QueryRow(getCategoryPortalCount, categoryPortals.Category.ID, game.ID).Scan(&categoryPortals.PortalCount)
377 // not checking for errors since there can be no record for category - just let it have 0
378 game.CategoryPortals = append(game.CategoryPortals, categoryPortals)
379 }
342 games = append(games, game) 380 games = append(games, game)
343 } 381 }
344 c.JSON(http.StatusOK, models.Response{ 382 c.JSON(http.StatusOK, models.Response{
@@ -441,7 +479,7 @@ func FetchChapterMaps(c *gin.Context) {
441 return 479 return
442 } 480 }
443 var response ChapterMapsResponse 481 var response ChapterMapsResponse
444 rows, err := database.DB.Query(`SELECT m.id, m.name, c.name, m.is_disabled, m.image FROM maps m INNER JOIN chapters c ON m.chapter_id = c.id WHERE chapter_id = $1`, chapterID) 482 rows, err := database.DB.Query(`SELECT m.id, m.name, c.name, m.is_disabled, m.image, MIN(mh.score_count) FROM maps m INNER JOIN chapters c ON m.chapter_id = c.id INNER JOIN map_history mh ON m.id = mh.map_id WHERE chapter_id = $1 GROUP BY m.id, c.name ORDER BY m.id;`, chapterID)
445 if err != nil { 483 if err != nil {
446 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 484 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
447 return 485 return
@@ -450,7 +488,7 @@ func FetchChapterMaps(c *gin.Context) {
450 var chapterName string 488 var chapterName string
451 for rows.Next() { 489 for rows.Next() {
452 var mapShort models.MapShort 490 var mapShort models.MapShort
453 if err := rows.Scan(&mapShort.ID, &mapShort.Name, &chapterName, &mapShort.IsDisabled, &mapShort.Image); err != nil { 491 if err := rows.Scan(&mapShort.ID, &mapShort.Name, &chapterName, &mapShort.IsDisabled, &mapShort.Image, &mapShort.PortalCount); err != nil {
454 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 492 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
455 return 493 return
456 } 494 }
diff --git a/backend/models/models.go b/backend/models/models.go
index b766b97..64d90d1 100644
--- a/backend/models/models.go
+++ b/backend/models/models.go
@@ -50,10 +50,12 @@ type Map struct {
50} 50}
51 51
52type MapShort struct { 52type MapShort struct {
53 ID int `json:"id"` 53 ID int `json:"id"`
54 Name string `json:"name"` 54 Name string `json:"name"`
55 Image string `json:"image"` 55 Image string `json:"image"`
56 IsDisabled bool `json:"is_disabled"` 56 IsDisabled bool `json:"is_disabled"`
57 PortalCount int `json:"portal_count"`
58 Difficulty int `json:"difficulty"`
57} 59}
58 60
59type MapSummary struct { 61type MapSummary struct {
@@ -87,10 +89,11 @@ type UserRanking struct {
87} 89}
88 90
89type Game struct { 91type Game struct {
90 ID int `json:"id"` 92 ID int `json:"id"`
91 Name string `json:"name"` 93 Name string `json:"name"`
92 Image string `json:"image"` 94 Image string `json:"image"`
93 IsCoop bool `json:"is_coop"` 95 IsCoop bool `json:"is_coop"`
96 CategoryPortals []CategoryPortal `json:"category_portals"`
94} 97}
95 98
96type Chapter struct { 99type Chapter struct {
@@ -100,6 +103,11 @@ type Chapter struct {
100 IsDisabled bool `json:"is_disabled"` 103 IsDisabled bool `json:"is_disabled"`
101} 104}
102 105
106type CategoryPortal struct {
107 Category Category `json:"category"`
108 PortalCount int `json:"portal_count"`
109}
110
103type Category struct { 111type Category struct {
104 ID int `json:"id"` 112 ID int `json:"id"`
105 Name string `json:"name"` 113 Name string `json:"name"`