diff options
Diffstat (limited to 'backend/handlers')
| -rw-r--r-- | backend/handlers/map.go | 97 |
1 files changed, 91 insertions, 6 deletions
diff --git a/backend/handlers/map.go b/backend/handlers/map.go index 3166d1d..2f38b32 100644 --- a/backend/handlers/map.go +++ b/backend/handlers/map.go | |||
| @@ -32,8 +32,8 @@ type ChapterMapsResponse struct { | |||
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | type GameMapsResponse struct { | 34 | type GameMapsResponse struct { |
| 35 | Game models.Game `json:"game"` | 35 | Game models.Game `json:"game"` |
| 36 | Maps []models.MapShort `json:"maps"` | 36 | Maps []models.MapSelect `json:"maps"` |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | type RecordSingleplayer struct { | 39 | type RecordSingleplayer struct { |
| @@ -442,18 +442,103 @@ func FetchMaps(c *gin.Context) { | |||
| 442 | return | 442 | return |
| 443 | } | 443 | } |
| 444 | var response GameMapsResponse | 444 | var response GameMapsResponse |
| 445 | rows, err := database.DB.Query(`SELECT g.id, g.name, g.is_coop, m.id, m."name", m.is_disabled, m.image FROM games g INNER JOIN maps m ON g.id = m.game_id WHERE g.id = $1 ORDER BY m.id `, gameID) | 445 | err = database.DB.QueryRow(`SELECT g.id, g.name, g.is_coop, g.image FROM games g WHERE g.id = $1;`, gameID).Scan(&response.Game.ID, &response.Game.Name, &response.Game.IsCoop, &response.Game.Image) |
| 446 | if err != nil { | 446 | if err != nil { |
| 447 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 447 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 448 | return | 448 | return |
| 449 | } | 449 | } |
| 450 | 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`, gameID) | ||
| 451 | if err != nil { | ||
| 452 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 453 | return | ||
| 454 | } | ||
| 455 | for categoryPortalRows.Next() { | ||
| 456 | var categoryPortals models.CategoryPortal | ||
| 457 | if err := categoryPortalRows.Scan(&categoryPortals.Category.ID, &categoryPortals.Category.Name); err != nil { | ||
| 458 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 459 | return | ||
| 460 | } | ||
| 461 | getCategoryPortalCount := ` | ||
| 462 | SELECT | ||
| 463 | SUM(mh.lowest_score_count) AS total_lowest_scores | ||
| 464 | FROM ( | ||
| 465 | SELECT | ||
| 466 | map_id, | ||
| 467 | category_id, | ||
| 468 | MIN(score_count) AS lowest_score_count | ||
| 469 | FROM | ||
| 470 | map_history | ||
| 471 | GROUP BY | ||
| 472 | map_id, | ||
| 473 | category_id | ||
| 474 | ) mh | ||
| 475 | JOIN maps m ON mh.map_id = m.id | ||
| 476 | JOIN games g ON m.game_id = g.id | ||
| 477 | WHERE | ||
| 478 | mh.category_id = $1 and g.id = $2 | ||
| 479 | GROUP BY | ||
| 480 | g.id, | ||
| 481 | g.name, | ||
| 482 | mh.category_id; | ||
| 483 | ` | ||
| 484 | database.DB.QueryRow(getCategoryPortalCount, categoryPortals.Category.ID, gameID).Scan(&categoryPortals.PortalCount) | ||
| 485 | // not checking for errors since there can be no record for category - just let it have 0 | ||
| 486 | response.Game.CategoryPortals = append(response.Game.CategoryPortals, categoryPortals) | ||
| 487 | } | ||
| 488 | |||
| 489 | rows, err := database.DB.Query(` | ||
| 490 | SELECT | ||
| 491 | m.id, | ||
| 492 | m.name, | ||
| 493 | m.is_disabled, | ||
| 494 | m.image, | ||
| 495 | cat.id, | ||
| 496 | cat.name, | ||
| 497 | mh.min_score_count AS score_count | ||
| 498 | FROM | ||
| 499 | maps m | ||
| 500 | INNER JOIN | ||
| 501 | chapters c ON m.chapter_id = c.id | ||
| 502 | INNER JOIN | ||
| 503 | game_categories gc ON gc.game_id = c.game_id | ||
| 504 | INNER JOIN | ||
| 505 | categories cat ON cat.id = gc.category_id | ||
| 506 | INNER JOIN | ||
| 507 | ( | ||
| 508 | SELECT | ||
| 509 | map_id, | ||
| 510 | category_id, | ||
| 511 | MIN(score_count) AS min_score_count | ||
| 512 | FROM | ||
| 513 | map_history | ||
| 514 | GROUP BY | ||
| 515 | map_id, | ||
| 516 | category_id | ||
| 517 | ) mh ON m.id = mh.map_id AND gc.category_id = mh.category_id | ||
| 518 | WHERE | ||
| 519 | m.game_id = $1 | ||
| 520 | ORDER BY | ||
| 521 | m.id, gc.category_id, mh.min_score_count ASC; | ||
| 522 | `, gameID) | ||
| 523 | if err != nil { | ||
| 524 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 525 | return | ||
| 526 | } | ||
| 527 | var lastMapID int | ||
| 450 | for rows.Next() { | 528 | for rows.Next() { |
| 451 | var mapShort models.MapShort | 529 | var mapShort models.MapSelect |
| 452 | if err := rows.Scan(&response.Game.ID, &response.Game.Name, &response.Game.IsCoop, &mapShort.ID, &mapShort.Name, &mapShort.IsDisabled, &mapShort.Image); err != nil { | 530 | var categoryPortal models.CategoryPortal |
| 531 | if err := rows.Scan(&mapShort.ID, &mapShort.Name, &mapShort.IsDisabled, &mapShort.Image, &categoryPortal.Category.ID, &categoryPortal.Category.Name, &categoryPortal.PortalCount); err != nil { | ||
| 453 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 532 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 454 | return | 533 | return |
| 455 | } | 534 | } |
| 456 | response.Maps = append(response.Maps, mapShort) | 535 | if mapShort.ID == lastMapID { |
| 536 | response.Maps[len(response.Maps)-1].CategoryPortals = append(response.Maps[len(response.Maps)-1].CategoryPortals, categoryPortal) | ||
| 537 | } else { | ||
| 538 | mapShort.CategoryPortals = append(mapShort.CategoryPortals, categoryPortal) | ||
| 539 | response.Maps = append(response.Maps, mapShort) | ||
| 540 | lastMapID = mapShort.ID | ||
| 541 | } | ||
| 457 | } | 542 | } |
| 458 | c.JSON(http.StatusOK, models.Response{ | 543 | c.JSON(http.StatusOK, models.Response{ |
| 459 | Success: true, | 544 | Success: true, |