diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-08-30 19:14:36 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-08-30 19:14:36 +0300 |
| commit | c40fe29289dbe2a8ea8847d79183fa256268075c (patch) | |
| tree | 631c6e7add9446e51edcd0f82c0fb6e4f18ac384 | |
| parent | fix: change disabled maps (diff) | |
| download | lphub-c40fe29289dbe2a8ea8847d79183fa256268075c.tar.gz lphub-c40fe29289dbe2a8ea8847d79183fa256268075c.tar.bz2 lphub-c40fe29289dbe2a8ea8847d79183fa256268075c.zip | |
feat: finalized profile scores in one array (#51)
Former-commit-id: 2a8e03ef339fb40d10b4770b004e35809ba05d7a
| -rw-r--r-- | backend/handlers/user.go | 158 |
1 files changed, 80 insertions, 78 deletions
diff --git a/backend/handlers/user.go b/backend/handlers/user.go index 51eadb4..affebb0 100644 --- a/backend/handlers/user.go +++ b/backend/handlers/user.go | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | package handlers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "log" | ||
| 4 | "net/http" | 5 | "net/http" |
| 5 | "os" | 6 | "os" |
| 6 | "regexp" | 7 | "regexp" |
| @@ -12,15 +13,15 @@ import ( | |||
| 12 | ) | 13 | ) |
| 13 | 14 | ||
| 14 | type ProfileResponse struct { | 15 | type ProfileResponse struct { |
| 15 | Profile bool `json:"profile"` | 16 | Profile bool `json:"profile"` |
| 16 | SteamID string `json:"steam_id"` | 17 | SteamID string `json:"steam_id"` |
| 17 | UserName string `json:"user_name"` | 18 | UserName string `json:"user_name"` |
| 18 | AvatarLink string `json:"avatar_link"` | 19 | AvatarLink string `json:"avatar_link"` |
| 19 | CountryCode string `json:"country_code"` | 20 | CountryCode string `json:"country_code"` |
| 20 | Titles []models.Title `json:"titles"` | 21 | Titles []models.Title `json:"titles"` |
| 21 | Links models.Links `json:"links"` | 22 | Links models.Links `json:"links"` |
| 22 | Rankings ProfileRankings `json:"rankings"` | 23 | Rankings ProfileRankings `json:"rankings"` |
| 23 | Records ProfileRecords `json:"records"` | 24 | Records []ProfileRecords `json:"records"` |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 26 | type ProfileRankings struct { | 27 | type ProfileRankings struct { |
| @@ -34,16 +35,13 @@ type ProfileRankingsDetails struct { | |||
| 34 | CompletionCount int `json:"completion_count"` | 35 | CompletionCount int `json:"completion_count"` |
| 35 | CompletionTotal int `json:"completion_total"` | 36 | CompletionTotal int `json:"completion_total"` |
| 36 | } | 37 | } |
| 37 | |||
| 38 | type ProfileRecords struct { | 38 | type ProfileRecords struct { |
| 39 | P2Singleplayer []ProfileRecordsDetails `json:"portal2_singleplayer"` | 39 | GameID int `json:"game_id"` |
| 40 | P2Cooperative []ProfileRecordsDetails `json:"portal2_cooperative"` | 40 | CategoryID int `json:"category_id"` |
| 41 | } | 41 | MapID int `json:"map_id"` |
| 42 | 42 | MapName string `json:"map_name"` | |
| 43 | type ProfileRecordsDetails struct { | 43 | MapWRCount int `json:"map_wr_count"` |
| 44 | MapID int `json:"map_id"` | 44 | Scores []ProfileScores `json:"scores"` |
| 45 | MapName string `json:"map_name"` | ||
| 46 | Scores []ProfileScores `json:"scores"` | ||
| 47 | } | 45 | } |
| 48 | 46 | ||
| 49 | type ProfileScores struct { | 47 | type ProfileScores struct { |
| @@ -85,42 +83,42 @@ func Profile(c *gin.Context) { | |||
| 85 | return | 83 | return |
| 86 | } | 84 | } |
| 87 | // TODO: Get rankings (all maps done in one game) | 85 | // TODO: Get rankings (all maps done in one game) |
| 88 | records := ProfileRecords{ | 86 | records := []ProfileRecords{} |
| 89 | P2Singleplayer: []ProfileRecordsDetails{}, | ||
| 90 | P2Cooperative: []ProfileRecordsDetails{}, | ||
| 91 | } | ||
| 92 | // Get singleplayer records | 87 | // Get singleplayer records |
| 93 | sql = `SELECT m.game_id, sp.map_id, m."name", sp.score_count, sp.score_time, sp.demo_id, sp.record_date | 88 | sql = `SELECT m.game_id, m.chapter_id, sp.map_id, m."name", (SELECT mr.score_count FROM map_routes mr WHERE mr.map_id = sp.map_id ORDER BY mr.score_count ASC LIMIT 1) AS wr_count, sp.score_count, sp.score_time, sp.demo_id, sp.record_date |
| 94 | FROM records_sp sp INNER JOIN maps m ON sp.map_id = m.id WHERE sp.user_id = $1 ORDER BY sp.map_id, sp.score_count, sp.score_time;` | 89 | FROM records_sp sp INNER JOIN maps m ON sp.map_id = m.id WHERE sp.user_id = $1 ORDER BY sp.map_id, sp.score_count, sp.score_time;` |
| 95 | rows, err := database.DB.Query(sql, user.(models.User).SteamID) | 90 | rows, err := database.DB.Query(sql, user.(models.User).SteamID) |
| 96 | if err != nil { | 91 | if err != nil { |
| 97 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 92 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 98 | return | 93 | return |
| 99 | } | 94 | } |
| 95 | log.Println("rows:", rows) | ||
| 100 | for rows.Next() { | 96 | for rows.Next() { |
| 97 | var gameID int | ||
| 98 | var categoryID int | ||
| 101 | var mapID int | 99 | var mapID int |
| 102 | var mapName string | 100 | var mapName string |
| 103 | var gameID int | 101 | var mapWR int |
| 104 | score := ProfileScores{} | 102 | score := ProfileScores{} |
| 105 | rows.Scan(&gameID, &mapID, &mapName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) | 103 | rows.Scan(&gameID, &categoryID, &mapID, &mapName, &mapWR, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) |
| 106 | if gameID != 1 { | ||
| 107 | continue | ||
| 108 | } | ||
| 109 | // More than one record in one map | 104 | // More than one record in one map |
| 110 | if len(records.P2Singleplayer) != 0 && mapID == records.P2Singleplayer[len(records.P2Singleplayer)-1].MapID { | 105 | if len(records) != 0 && mapID == records[len(records)-1].MapID { |
| 111 | records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores = append(records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores, score) | 106 | records[len(records)-1].Scores = append(records[len(records)-1].Scores, score) |
| 112 | continue | 107 | continue |
| 113 | } | 108 | } |
| 114 | // New map | 109 | // New map |
| 115 | records.P2Singleplayer = append(records.P2Singleplayer, ProfileRecordsDetails{ | 110 | records = append(records, ProfileRecords{ |
| 116 | MapID: mapID, | 111 | GameID: gameID, |
| 117 | MapName: mapName, | 112 | CategoryID: categoryID, |
| 118 | Scores: []ProfileScores{}, | 113 | MapID: mapID, |
| 114 | MapName: mapName, | ||
| 115 | MapWRCount: mapWR, | ||
| 116 | Scores: []ProfileScores{}, | ||
| 119 | }) | 117 | }) |
| 120 | records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores = append(records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores, score) | 118 | records[len(records)-1].Scores = append(records[len(records)-1].Scores, score) |
| 121 | } | 119 | } |
| 122 | // Get multiplayer records | 120 | // Get multiplayer records |
| 123 | sql = `SELECT m.game_id, mp.map_id, m."name", mp.score_count, mp.score_time, CASE WHEN host_id = $1 THEN mp.host_demo_id WHEN partner_id = $1 THEN mp.partner_demo_id END demo_id, mp.record_date | 121 | sql = `SELECT m.game_id, m.chapter_id, mp.map_id, m."name", (SELECT mr.score_count FROM map_routes mr WHERE mr.map_id = mp.map_id ORDER BY mr.score_count ASC LIMIT 1) AS wr_count, mp.score_count, mp.score_time, CASE WHEN host_id = $1 THEN mp.host_demo_id WHEN partner_id = $1 THEN mp.partner_demo_id END demo_id, mp.record_date |
| 124 | FROM records_mp mp INNER JOIN maps m ON mp.map_id = m.id WHERE mp.host_id = $1 OR mp.partner_id = $1 ORDER BY mp.map_id, mp.score_count, mp.score_time;` | 122 | FROM records_mp mp INNER JOIN maps m ON mp.map_id = m.id WHERE mp.host_id = $1 OR mp.partner_id = $1 ORDER BY mp.map_id, mp.score_count, mp.score_time;` |
| 125 | rows, err = database.DB.Query(sql, user.(models.User).SteamID) | 123 | rows, err = database.DB.Query(sql, user.(models.User).SteamID) |
| 126 | if err != nil { | 124 | if err != nil { |
| @@ -128,26 +126,28 @@ func Profile(c *gin.Context) { | |||
| 128 | return | 126 | return |
| 129 | } | 127 | } |
| 130 | for rows.Next() { | 128 | for rows.Next() { |
| 129 | var gameID int | ||
| 130 | var categoryID int | ||
| 131 | var mapID int | 131 | var mapID int |
| 132 | var mapName string | 132 | var mapName string |
| 133 | var gameID int | 133 | var mapWR int |
| 134 | score := ProfileScores{} | 134 | score := ProfileScores{} |
| 135 | rows.Scan(&gameID, &mapID, &mapName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) | 135 | rows.Scan(&gameID, &categoryID, &mapID, &mapName, &mapWR, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) |
| 136 | if gameID != 1 { | ||
| 137 | continue | ||
| 138 | } | ||
| 139 | // More than one record in one map | 136 | // More than one record in one map |
| 140 | if len(records.P2Cooperative) != 0 && mapID == records.P2Cooperative[len(records.P2Cooperative)-1].MapID { | 137 | if len(records) != 0 && mapID == records[len(records)-1].MapID { |
| 141 | records.P2Cooperative[len(records.P2Cooperative)-1].Scores = append(records.P2Cooperative[len(records.P2Cooperative)-1].Scores, score) | 138 | records[len(records)-1].Scores = append(records[len(records)-1].Scores, score) |
| 142 | continue | 139 | continue |
| 143 | } | 140 | } |
| 144 | // New map | 141 | // New map |
| 145 | records.P2Cooperative = append(records.P2Cooperative, ProfileRecordsDetails{ | 142 | records = append(records, ProfileRecords{ |
| 146 | MapID: mapID, | 143 | GameID: gameID, |
| 147 | MapName: mapName, | 144 | CategoryID: categoryID, |
| 148 | Scores: []ProfileScores{}, | 145 | MapID: mapID, |
| 146 | MapName: mapName, | ||
| 147 | MapWRCount: mapWR, | ||
| 148 | Scores: []ProfileScores{}, | ||
| 149 | }) | 149 | }) |
| 150 | records.P2Cooperative[len(records.P2Cooperative)-1].Scores = append(records.P2Cooperative[len(records.P2Cooperative)-1].Scores, score) | 150 | records[len(records)-1].Scores = append(records[len(records)-1].Scores, score) |
| 151 | } | 151 | } |
| 152 | c.JSON(http.StatusOK, models.Response{ | 152 | c.JSON(http.StatusOK, models.Response{ |
| 153 | Success: true, | 153 | Success: true, |
| @@ -216,42 +216,42 @@ func FetchUser(c *gin.Context) { | |||
| 216 | user.Titles = append(user.Titles, title) | 216 | user.Titles = append(user.Titles, title) |
| 217 | } | 217 | } |
| 218 | // TODO: Get rankings (all maps done in one game) | 218 | // TODO: Get rankings (all maps done in one game) |
| 219 | records := ProfileRecords{ | 219 | records := []ProfileRecords{} |
| 220 | P2Singleplayer: []ProfileRecordsDetails{}, | ||
| 221 | P2Cooperative: []ProfileRecordsDetails{}, | ||
| 222 | } | ||
| 223 | // Get singleplayer records | 220 | // Get singleplayer records |
| 224 | sql = `SELECT m.game_id, sp.map_id, m."name", sp.score_count, sp.score_time, sp.demo_id, sp.record_date | 221 | sql = `SELECT m.game_id, m.chapter_id, sp.map_id, m."name", (SELECT mr.score_count FROM map_routes mr WHERE mr.map_id = sp.map_id ORDER BY mr.score_count ASC LIMIT 1) AS wr_count, sp.score_count, sp.score_time, sp.demo_id, sp.record_date |
| 225 | FROM records_sp sp INNER JOIN maps m ON sp.map_id = m.id WHERE sp.user_id = $1 ORDER BY sp.map_id, sp.score_count, sp.score_time;` | 222 | FROM records_sp sp INNER JOIN maps m ON sp.map_id = m.id WHERE sp.user_id = $1 ORDER BY sp.map_id, sp.score_count, sp.score_time;` |
| 226 | rows, err = database.DB.Query(sql, user.SteamID) | 223 | rows, err = database.DB.Query(sql, user.SteamID) |
| 227 | if err != nil { | 224 | if err != nil { |
| 228 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 225 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 229 | return | 226 | return |
| 230 | } | 227 | } |
| 228 | log.Println("rows:", rows) | ||
| 231 | for rows.Next() { | 229 | for rows.Next() { |
| 230 | var gameID int | ||
| 231 | var categoryID int | ||
| 232 | var mapID int | 232 | var mapID int |
| 233 | var mapName string | 233 | var mapName string |
| 234 | var gameID int | 234 | var mapWR int |
| 235 | score := ProfileScores{} | 235 | score := ProfileScores{} |
| 236 | rows.Scan(&gameID, &mapID, &mapName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) | 236 | rows.Scan(&gameID, &categoryID, &mapID, &mapName, &mapWR, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) |
| 237 | if gameID != 1 { | ||
| 238 | continue | ||
| 239 | } | ||
| 240 | // More than one record in one map | 237 | // More than one record in one map |
| 241 | if len(records.P2Singleplayer) != 0 && mapID == records.P2Singleplayer[len(records.P2Singleplayer)-1].MapID { | 238 | if len(records) != 0 && mapID == records[len(records)-1].MapID { |
| 242 | records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores = append(records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores, score) | 239 | records[len(records)-1].Scores = append(records[len(records)-1].Scores, score) |
| 243 | continue | 240 | continue |
| 244 | } | 241 | } |
| 245 | // New map | 242 | // New map |
| 246 | records.P2Singleplayer = append(records.P2Singleplayer, ProfileRecordsDetails{ | 243 | records = append(records, ProfileRecords{ |
| 247 | MapID: mapID, | 244 | GameID: gameID, |
| 248 | MapName: mapName, | 245 | CategoryID: categoryID, |
| 249 | Scores: []ProfileScores{}, | 246 | MapID: mapID, |
| 247 | MapName: mapName, | ||
| 248 | MapWRCount: mapWR, | ||
| 249 | Scores: []ProfileScores{}, | ||
| 250 | }) | 250 | }) |
| 251 | records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores = append(records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores, score) | 251 | records[len(records)-1].Scores = append(records[len(records)-1].Scores, score) |
| 252 | } | 252 | } |
| 253 | // Get multiplayer records | 253 | // Get multiplayer records |
| 254 | sql = `SELECT m.game_id, mp.map_id, m."name", mp.score_count, mp.score_time, CASE WHEN host_id = $1 THEN mp.host_demo_id WHEN partner_id = $1 THEN mp.partner_demo_id END demo_id, mp.record_date | 254 | sql = `SELECT m.game_id, m.chapter_id, mp.map_id, m."name", (SELECT mr.score_count FROM map_routes mr WHERE mr.map_id = mp.map_id ORDER BY mr.score_count ASC LIMIT 1) AS wr_count, mp.score_count, mp.score_time, CASE WHEN host_id = $1 THEN mp.host_demo_id WHEN partner_id = $1 THEN mp.partner_demo_id END demo_id, mp.record_date |
| 255 | FROM records_mp mp INNER JOIN maps m ON mp.map_id = m.id WHERE mp.host_id = $1 OR mp.partner_id = $1 ORDER BY mp.map_id, mp.score_count, mp.score_time;` | 255 | FROM records_mp mp INNER JOIN maps m ON mp.map_id = m.id WHERE mp.host_id = $1 OR mp.partner_id = $1 ORDER BY mp.map_id, mp.score_count, mp.score_time;` |
| 256 | rows, err = database.DB.Query(sql, user.SteamID) | 256 | rows, err = database.DB.Query(sql, user.SteamID) |
| 257 | if err != nil { | 257 | if err != nil { |
| @@ -259,26 +259,28 @@ func FetchUser(c *gin.Context) { | |||
| 259 | return | 259 | return |
| 260 | } | 260 | } |
| 261 | for rows.Next() { | 261 | for rows.Next() { |
| 262 | var gameID int | ||
| 263 | var categoryID int | ||
| 262 | var mapID int | 264 | var mapID int |
| 263 | var mapName string | 265 | var mapName string |
| 264 | var gameID int | 266 | var mapWR int |
| 265 | score := ProfileScores{} | 267 | score := ProfileScores{} |
| 266 | rows.Scan(&gameID, &mapID, &mapName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) | 268 | rows.Scan(&gameID, &categoryID, &mapID, &mapName, &mapWR, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) |
| 267 | if gameID != 1 { | ||
| 268 | continue | ||
| 269 | } | ||
| 270 | // More than one record in one map | 269 | // More than one record in one map |
| 271 | if len(records.P2Cooperative) != 0 && mapID == records.P2Cooperative[len(records.P2Cooperative)-1].MapID { | 270 | if len(records) != 0 && mapID == records[len(records)-1].MapID { |
| 272 | records.P2Cooperative[len(records.P2Cooperative)-1].Scores = append(records.P2Cooperative[len(records.P2Cooperative)-1].Scores, score) | 271 | records[len(records)-1].Scores = append(records[len(records)-1].Scores, score) |
| 273 | continue | 272 | continue |
| 274 | } | 273 | } |
| 275 | // New map | 274 | // New map |
| 276 | records.P2Cooperative = append(records.P2Cooperative, ProfileRecordsDetails{ | 275 | records = append(records, ProfileRecords{ |
| 277 | MapID: mapID, | 276 | GameID: gameID, |
| 278 | MapName: mapName, | 277 | CategoryID: categoryID, |
| 279 | Scores: []ProfileScores{}, | 278 | MapID: mapID, |
| 279 | MapName: mapName, | ||
| 280 | MapWRCount: mapWR, | ||
| 281 | Scores: []ProfileScores{}, | ||
| 280 | }) | 282 | }) |
| 281 | records.P2Cooperative[len(records.P2Cooperative)-1].Scores = append(records.P2Cooperative[len(records.P2Cooperative)-1].Scores, score) | 283 | records[len(records)-1].Scores = append(records[len(records)-1].Scores, score) |
| 282 | } | 284 | } |
| 283 | c.JSON(http.StatusOK, models.Response{ | 285 | c.JSON(http.StatusOK, models.Response{ |
| 284 | Success: true, | 286 | Success: true, |