diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-25 22:43:55 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-25 22:43:55 +0300 |
| commit | 7f420a2c34667ed7addab356b17e4be2612ecc0b (patch) | |
| tree | 75196ccaa66d3488923634580f20a52973787704 /backend/handlers/home.go | |
| parent | docs: add delete record, update discussion and main (#56) (diff) | |
| download | lphub-7f420a2c34667ed7addab356b17e4be2612ecc0b.tar.gz lphub-7f420a2c34667ed7addab356b17e4be2612ecc0b.tar.bz2 lphub-7f420a2c34667ed7addab356b17e4be2612ecc0b.zip | |
feat: completed profile rankings (#51)
Former-commit-id: a2ef452a32803181f6937f08840e5f0e55ed6403
Diffstat (limited to 'backend/handlers/home.go')
| -rw-r--r-- | backend/handlers/home.go | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/backend/handlers/home.go b/backend/handlers/home.go index 5863218..16ac993 100644 --- a/backend/handlers/home.go +++ b/backend/handlers/home.go | |||
| @@ -43,8 +43,8 @@ func Rankings(c *gin.Context) { | |||
| 43 | Multiplayer: []models.UserRanking{}, | 43 | Multiplayer: []models.UserRanking{}, |
| 44 | } | 44 | } |
| 45 | // Singleplayer rankings | 45 | // Singleplayer rankings |
| 46 | sql := `SELECT u.steam_id, u.user_name, COUNT(DISTINCT map_id), | 46 | sql := `SELECT u.steam_id, u.user_name, u.avatar_link, COUNT(DISTINCT map_id), |
| 47 | (SELECT COUNT(maps.name) FROM maps INNER JOIN games g ON maps.game_id = g.id WHERE g.is_coop = FALSE AND is_disabled = false), | 47 | (SELECT COUNT(maps.name) FROM maps INNER JOIN games g ON maps.game_id = g.id WHERE g."name" = 'Portal 2 - Singleplayer' AND is_disabled = false), |
| 48 | (SELECT SUM(min_score_count) AS total_min_score_count FROM ( | 48 | (SELECT SUM(min_score_count) AS total_min_score_count FROM ( |
| 49 | SELECT | 49 | SELECT |
| 50 | user_id, | 50 | user_id, |
| @@ -63,7 +63,7 @@ func Rankings(c *gin.Context) { | |||
| 63 | ranking := models.UserRanking{} | 63 | ranking := models.UserRanking{} |
| 64 | var currentCount int | 64 | var currentCount int |
| 65 | var totalCount int | 65 | var totalCount int |
| 66 | err = rows.Scan(&ranking.User.SteamID, &ranking.User.UserName, ¤tCount, &totalCount, &ranking.TotalScore) | 66 | err = rows.Scan(&ranking.User.SteamID, &ranking.User.UserName, &ranking.User.AvatarLink, ¤tCount, &totalCount, &ranking.TotalScore) |
| 67 | if err != nil { | 67 | if err != nil { |
| 68 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 68 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 69 | return | 69 | return |
| @@ -74,8 +74,8 @@ func Rankings(c *gin.Context) { | |||
| 74 | response.Singleplayer = append(response.Singleplayer, ranking) | 74 | response.Singleplayer = append(response.Singleplayer, ranking) |
| 75 | } | 75 | } |
| 76 | // Multiplayer rankings | 76 | // Multiplayer rankings |
| 77 | sql = `SELECT u.steam_id, u.user_name, COUNT(DISTINCT map_id), | 77 | sql = `SELECT u.steam_id, u.user_name, u.avatar_link, COUNT(DISTINCT map_id), |
| 78 | (SELECT COUNT(maps.name) FROM maps INNER JOIN games g ON maps.game_id = g.id WHERE g.is_coop = FALSE AND is_disabled = false), | 78 | (SELECT COUNT(maps.name) FROM maps INNER JOIN games g ON maps.game_id = g.id WHERE g."name" = 'Portal 2 - Cooperative' AND is_disabled = false), |
| 79 | (SELECT SUM(min_score_count) AS total_min_score_count FROM ( | 79 | (SELECT SUM(min_score_count) AS total_min_score_count FROM ( |
| 80 | SELECT | 80 | SELECT |
| 81 | host_id, | 81 | host_id, |
| @@ -95,7 +95,7 @@ func Rankings(c *gin.Context) { | |||
| 95 | ranking := models.UserRanking{} | 95 | ranking := models.UserRanking{} |
| 96 | var currentCount int | 96 | var currentCount int |
| 97 | var totalCount int | 97 | var totalCount int |
| 98 | err = rows.Scan(&ranking.User.SteamID, &ranking.User.UserName, ¤tCount, &totalCount, &ranking.TotalScore) | 98 | err = rows.Scan(&ranking.User.SteamID, &ranking.User.UserName, &ranking.User.AvatarLink, ¤tCount, &totalCount, &ranking.TotalScore) |
| 99 | if err != nil { | 99 | if err != nil { |
| 100 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 100 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 101 | return | 101 | return |
| @@ -121,12 +121,48 @@ func Rankings(c *gin.Context) { | |||
| 121 | sort.Slice(response.Singleplayer, func(i, j int) bool { | 121 | sort.Slice(response.Singleplayer, func(i, j int) bool { |
| 122 | return response.Singleplayer[i].TotalScore < response.Singleplayer[j].TotalScore | 122 | return response.Singleplayer[i].TotalScore < response.Singleplayer[j].TotalScore |
| 123 | }) | 123 | }) |
| 124 | placement := 1 | ||
| 125 | ties := 0 | ||
| 126 | for index := 0; index < len(response.Singleplayer); index++ { | ||
| 127 | if index != 0 && response.Singleplayer[index-1].TotalScore == response.Singleplayer[index].TotalScore { | ||
| 128 | ties++ | ||
| 129 | response.Singleplayer[index].Placement = placement - ties | ||
| 130 | } else { | ||
| 131 | ties = 0 | ||
| 132 | response.Singleplayer[index].Placement = placement | ||
| 133 | } | ||
| 134 | placement++ | ||
| 135 | } | ||
| 124 | sort.Slice(response.Multiplayer, func(i, j int) bool { | 136 | sort.Slice(response.Multiplayer, func(i, j int) bool { |
| 125 | return response.Multiplayer[i].TotalScore < response.Multiplayer[j].TotalScore | 137 | return response.Multiplayer[i].TotalScore < response.Multiplayer[j].TotalScore |
| 126 | }) | 138 | }) |
| 139 | placement = 1 | ||
| 140 | ties = 0 | ||
| 141 | for index := 0; index < len(response.Multiplayer); index++ { | ||
| 142 | if index != 0 && response.Multiplayer[index-1].TotalScore == response.Multiplayer[index].TotalScore { | ||
| 143 | ties++ | ||
| 144 | response.Multiplayer[index].Placement = placement - ties | ||
| 145 | } else { | ||
| 146 | ties = 0 | ||
| 147 | response.Multiplayer[index].Placement = placement | ||
| 148 | } | ||
| 149 | placement++ | ||
| 150 | } | ||
| 127 | sort.Slice(response.Overall, func(i, j int) bool { | 151 | sort.Slice(response.Overall, func(i, j int) bool { |
| 128 | return response.Overall[i].TotalScore < response.Overall[j].TotalScore | 152 | return response.Overall[i].TotalScore < response.Overall[j].TotalScore |
| 129 | }) | 153 | }) |
| 154 | placement = 1 | ||
| 155 | ties = 0 | ||
| 156 | for index := 0; index < len(response.Overall); index++ { | ||
| 157 | if index != 0 && response.Overall[index-1].TotalScore == response.Overall[index].TotalScore { | ||
| 158 | ties++ | ||
| 159 | response.Overall[index].Placement = placement - ties | ||
| 160 | } else { | ||
| 161 | ties = 0 | ||
| 162 | response.Overall[index].Placement = placement | ||
| 163 | } | ||
| 164 | placement++ | ||
| 165 | } | ||
| 130 | c.JSON(http.StatusOK, models.Response{ | 166 | c.JSON(http.StatusOK, models.Response{ |
| 131 | Success: true, | 167 | Success: true, |
| 132 | Message: "Successfully retrieved rankings.", | 168 | Message: "Successfully retrieved rankings.", |