diff options
Diffstat (limited to '')
| -rw-r--r-- | backend/controllers/homeController.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/backend/controllers/homeController.go b/backend/controllers/homeController.go index 635038b..42dfaa7 100644 --- a/backend/controllers/homeController.go +++ b/backend/controllers/homeController.go | |||
| @@ -75,3 +75,93 @@ func Login(c *gin.Context) { | |||
| 75 | return | 75 | return |
| 76 | } | 76 | } |
| 77 | } | 77 | } |
| 78 | |||
| 79 | func Rankings(c *gin.Context) { | ||
| 80 | rows, err := database.DB.Query(`SELECT steam_id, username FROM users;`) | ||
| 81 | if err != nil { | ||
| 82 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 83 | return | ||
| 84 | } | ||
| 85 | var spRankings []models.UserRanking | ||
| 86 | var mpRankings []models.UserRanking | ||
| 87 | for rows.Next() { | ||
| 88 | var userID, username string | ||
| 89 | err := rows.Scan(&userID, &username) | ||
| 90 | if err != nil { | ||
| 91 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 92 | return | ||
| 93 | } | ||
| 94 | // Getting all sp records for each user | ||
| 95 | var uniqueSingleUserRecords, totalSingleMaps int | ||
| 96 | sql := `SELECT COUNT(DISTINCT map_id), (SELECT COUNT(map_name) FROM maps | ||
| 97 | WHERE is_coop = FALSE AND is_disabled = false) FROM records_sp WHERE user_id = $1;` | ||
| 98 | err = database.DB.QueryRow(sql, userID).Scan(&uniqueSingleUserRecords, &totalSingleMaps) | ||
| 99 | if err != nil { | ||
| 100 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 101 | return | ||
| 102 | } | ||
| 103 | // Has all singleplayer records | ||
| 104 | if uniqueSingleUserRecords == totalSingleMaps { | ||
| 105 | var ranking models.UserRanking | ||
| 106 | ranking.UserID = userID | ||
| 107 | ranking.Username = username | ||
| 108 | sql := `SELECT DISTINCT map_id, score_count FROM records_sp WHERE user_id = $1 ORDER BY map_id, score_count;` | ||
| 109 | rows, err := database.DB.Query(sql, userID) | ||
| 110 | if err != nil { | ||
| 111 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 112 | return | ||
| 113 | } | ||
| 114 | totalScore := 0 | ||
| 115 | var maps []int | ||
| 116 | for rows.Next() { | ||
| 117 | var mapID, scoreCount int | ||
| 118 | rows.Scan(&mapID, &scoreCount) | ||
| 119 | if len(maps) != 0 && maps[len(maps)-1] == mapID { | ||
| 120 | continue | ||
| 121 | } | ||
| 122 | totalScore += scoreCount | ||
| 123 | maps = append(maps, mapID) | ||
| 124 | } | ||
| 125 | ranking.TotalScore = totalScore | ||
| 126 | spRankings = append(spRankings, ranking) | ||
| 127 | } | ||
| 128 | // Getting all mp records for each user | ||
| 129 | var uniqueMultiUserRecords, totalMultiMaps int | ||
| 130 | sql = `SELECT COUNT(DISTINCT map_id), (SELECT COUNT(map_name) FROM maps | ||
| 131 | WHERE is_coop = TRUE AND is_disabled = false) FROM records_mp WHERE host_id = $1 OR partner_id = $2;` | ||
| 132 | err = database.DB.QueryRow(sql, userID, userID).Scan(&uniqueMultiUserRecords, &totalMultiMaps) | ||
| 133 | if err != nil { | ||
| 134 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 135 | return | ||
| 136 | } | ||
| 137 | // Has all singleplayer records | ||
| 138 | if uniqueMultiUserRecords == totalMultiMaps { | ||
| 139 | var ranking models.UserRanking | ||
| 140 | ranking.UserID = userID | ||
| 141 | ranking.Username = username | ||
| 142 | sql := `SELECT DISTINCT map_id, score_count FROM records_mp WHERE host_id = $1 OR partner_id = $2 ORDER BY map_id, score_count;` | ||
| 143 | rows, err := database.DB.Query(sql, userID, userID) | ||
| 144 | if err != nil { | ||
| 145 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 146 | return | ||
| 147 | } | ||
| 148 | totalScore := 0 | ||
| 149 | var maps []int | ||
| 150 | for rows.Next() { | ||
| 151 | var mapID, scoreCount int | ||
| 152 | rows.Scan(&mapID, &scoreCount) | ||
| 153 | if len(maps) != 0 && maps[len(maps)-1] == mapID { | ||
| 154 | continue | ||
| 155 | } | ||
| 156 | totalScore += scoreCount | ||
| 157 | maps = append(maps, mapID) | ||
| 158 | } | ||
| 159 | ranking.TotalScore = totalScore | ||
| 160 | mpRankings = append(mpRankings, ranking) | ||
| 161 | } | ||
| 162 | } | ||
| 163 | c.JSON(http.StatusOK, models.RankingsResponse{ | ||
| 164 | RankingsSP: spRankings, | ||
| 165 | RankingsMP: mpRankings, | ||
| 166 | }) | ||
| 167 | } | ||