From 0d67bac5c574f4afd626d4482ae5eaacfbf0499f Mon Sep 17 00:00:00 2001 From: James Raspass Date: Wed, 1 Jul 2020 04:08:10 +0100 Subject: [PATCH] Move trophies out of the routes namespace --- golfer/golfer.go | 9 ++++- routes/golfer.go | 19 ++++++----- routes/types.go | 69 -------------------------------------- routes/user.go | 6 ++-- trophy/trophy.go | 75 ++++++++++++++++++++++++++++++++++++++++++ views/golfer-info.html | 6 ++-- 6 files changed, 100 insertions(+), 84 deletions(-) create mode 100644 trophy/trophy.go diff --git a/golfer/golfer.go b/golfer/golfer.go index ae2f6a44c..1aeb88392 100644 --- a/golfer/golfer.go +++ b/golfer/golfer.go @@ -4,6 +4,8 @@ import ( "database/sql" "errors" "time" + + "github.com/code-golf/code-golf/trophy" ) type Golfer struct { @@ -26,12 +28,17 @@ type GolferInfo struct { // Count of holes/langs/trophies done Holes, Langs, Trophies int + // Count of holes/langs/trophies available + HolesTotal, LangsTotal, TrophiesTotal int + // Start date TeedOff time.Time } func GetInfo(db *sql.DB, name string) *GolferInfo { - var info GolferInfo + info := GolferInfo{ + TrophiesTotal: len(trophy.List), + } if err := db.QueryRow( `WITH ranked AS ( diff --git a/routes/golfer.go b/routes/golfer.go index 6567d552a..88441e360 100644 --- a/routes/golfer.go +++ b/routes/golfer.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/code-golf/code-golf/golfer" + "github.com/code-golf/code-golf/trophy" ) // Golfer serves GET /golfers/{golfer} @@ -14,14 +15,14 @@ func Golfer(w http.ResponseWriter, r *http.Request) { type EarnedTrophy struct { Count, Percent int Earned sql.NullTime - Trophy Trophy + Trophy trophy.Trophy } data := struct { Max int Trophies []EarnedTrophy }{ - Trophies: make([]EarnedTrophy, 0, len(trophies)), + Trophies: make([]EarnedTrophy, 0, len(trophy.List)), } tx, err := db(r).BeginTx( @@ -56,20 +57,20 @@ func Golfer(w http.ResponseWriter, r *http.Request) { } for rows.Next() { - var trophy EarnedTrophy + var earned EarnedTrophy if err := rows.Scan( - &trophy.Trophy.ID, - &trophy.Count, - &trophy.Earned, + &earned.Trophy.ID, + &earned.Count, + &earned.Earned, ); err != nil { panic(err) } - trophy.Percent = trophy.Count * 100 / data.Max - trophy.Trophy = trophiesByID[trophy.Trophy.ID] + earned.Percent = earned.Count * 100 / data.Max + earned.Trophy = trophy.ByID[earned.Trophy.ID] - data.Trophies = append(data.Trophies, trophy) + data.Trophies = append(data.Trophies, earned) } if err := rows.Err(); err != nil { diff --git a/routes/types.go b/routes/types.go index 919dc7be0..00a7a1b95 100644 --- a/routes/types.go +++ b/routes/types.go @@ -18,11 +18,6 @@ type Hole struct { Preamble template.HTML } -type Trophy struct { - Emoji, ID, Name string - Description template.HTML -} - var langs = []Lang{ {"bash", "Bash"}, {"brainfuck", "Brainfuck"}, @@ -49,70 +44,10 @@ var langs = []Lang{ {"swift", "Swift"}, } -var trophies = []Trophy{ - { - "β˜•", "caffeinated", "Caffeinated", - "Solve the same hole in both Java and JavaScript.", - }, - { - "🐘", "elephpant-in-the-room", "ElePHPant in the Room", - "Solve any hole in PHP.", - }, - { - "πŸŽ‚", "happy-birthday-code-golf", "Happy Birthday, Code Golf", - "Solve any hole in any language on 2 Oct.", - }, - { - "πŸ‘‹", "hello-world", "Hello, World!", - "Solve any hole in any language.", - }, - { - "🧠", "inception", "Inception", - "Solve Brainfuck in Brainfuck.", - }, - { - "πŸ’Ό", "interview-ready", "Interview Ready", - "Solve Fizz Buzz in any language.", - }, - { - "πŸ‰", "its-over-9000", "It’s Over 9000!", - "Earn over 9,000 points.", - }, - { - "⭐", "my-god-its-full-of-stars", "My God, It’s Full of Stars", - "Star the Code Golf repository.", - }, - { - "🐍", "ouroboros", "Ouroboros", - "Solve Quine in Python.", - }, - { - "πŸ’Ύ", "patches-welcome", "Patches Welcome", - "Contribute a merged PR to the Code Golf repository.", - }, - { - "πŸ”£", "polyglot", "Polyglot", - "Solve at least one hole in every language.", - }, - { - "πŸ¦₯", "slowcoach", "Slowcoach", - "Fail an attempt by exceeding the time limit.", - }, - { - "πŸͺ", "tim-toady", "Tim Toady", - "Solve the same hole in both Perl and Raku.", - }, - { - "🍺", "the-watering-hole", "The Watering Hole", - "Solve your nineteenth hole.", - }, -} - var holes []Hole var langByID = map[string]Lang{} var holeByID = map[string]Hole{} -var trophiesByID = map[string]Trophy{} func init() { var holesTOML map[string]Hole @@ -171,8 +106,4 @@ func init() { holeByID[hole.ID] = holes[i] } - - for _, trophy := range trophies { - trophiesByID[trophy.ID] = trophy - } } diff --git a/routes/user.go b/routes/user.go index c5009fc4e..226f5470c 100644 --- a/routes/user.go +++ b/routes/user.go @@ -5,6 +5,8 @@ import ( "errors" "net/http" "time" + + "github.com/code-golf/code-golf/trophy" ) // User serves GET /users/{user} @@ -15,14 +17,14 @@ func User(w http.ResponseWriter, r *http.Request) { Login string Points int Ranks map[string]map[string]int - Trophies []Trophy + Trophies []trophy.Trophy TrophiesEarned map[string]*time.Time }{ Holes: holes, Langs: langs, Login: param(r, "user"), Ranks: map[string]map[string]int{}, - Trophies: trophies, + Trophies: trophy.List, TrophiesEarned: map[string]*time.Time{}, } diff --git a/trophy/trophy.go b/trophy/trophy.go new file mode 100644 index 000000000..54d59a8d5 --- /dev/null +++ b/trophy/trophy.go @@ -0,0 +1,75 @@ +package trophy + +import "html/template" + +type Trophy struct { + Emoji, ID, Name string + Description template.HTML +} + +var ByID = map[string]Trophy{} + +func init() { + for _, trophy := range List { + ByID[trophy.ID] = trophy + } +} + +var List = []Trophy{ + { + "β˜•", "caffeinated", "Caffeinated", + "Solve the same hole in both Java and JavaScript.", + }, + { + "🐘", "elephpant-in-the-room", "ElePHPant in the Room", + "Solve any hole in PHP.", + }, + { + "πŸŽ‚", "happy-birthday-code-golf", "Happy Birthday, Code Golf", + "Solve any hole in any language on 2 Oct.", + }, + { + "πŸ‘‹", "hello-world", "Hello, World!", + "Solve any hole in any language.", + }, + { + "🧠", "inception", "Inception", + "Solve Brainfuck in Brainfuck.", + }, + { + "πŸ’Ό", "interview-ready", "Interview Ready", + "Solve Fizz Buzz in any language.", + }, + { + "πŸ‰", "its-over-9000", "It’s Over 9000!", + "Earn over 9,000 points.", + }, + { + "⭐", "my-god-its-full-of-stars", "My God, It’s Full of Stars", + "Star the Code Golf repository.", + }, + { + "🐍", "ouroboros", "Ouroboros", + "Solve Quine in Python.", + }, + { + "πŸ’Ύ", "patches-welcome", "Patches Welcome", + "Contribute a merged PR to the Code Golf repository.", + }, + { + "πŸ”£", "polyglot", "Polyglot", + "Solve at least one hole in every language.", + }, + { + "πŸ¦₯", "slowcoach", "Slowcoach", + "Fail an attempt by exceeding the time limit.", + }, + { + "πŸͺ", "tim-toady", "Tim Toady", + "Solve the same hole in both Perl and Raku.", + }, + { + "🍺", "the-watering-hole", "The Watering Hole", + "Solve your nineteenth hole.", + }, +} diff --git a/views/golfer-info.html b/views/golfer-info.html index e18b47567..9e01bff56 100644 --- a/views/golfer-info.html +++ b/views/golfer-info.html @@ -72,11 +72,11 @@

{{ .Name }}

πŸ’°

Sponsor

{{ end }} -

β›³

{{ .Holes }} / 42 Holes

+

β›³

{{ .Holes }} / {{ .HolesTotal }} Holes

-

πŸ”£

{{ .Langs }} / 23 Languages

+

πŸ”£

{{ .Langs }} / {{ .LangsTotal }} Languages

-

πŸ†

{{ .Trophies }} / 14 Trophies

+

πŸ†

{{ .Trophies }} / {{ .TrophiesTotal }} Trophies

πŸ“…

Teed off {{ time .TeedOff }}