Skip to content

Commit

Permalink
Move trophies out of the routes namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
JRaspass committed Jul 1, 2020
1 parent fba3b11 commit 0d67bac
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 84 deletions.
9 changes: 8 additions & 1 deletion golfer/golfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"database/sql"
"errors"
"time"

"github.com/code-golf/code-golf/trophy"
)

type Golfer struct {
Expand All @@ -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 (
Expand Down
19 changes: 10 additions & 9 deletions routes/golfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
69 changes: 0 additions & 69 deletions routes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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 <a href=//github.com/code-golf/code-golf/commit/4b44>2 Oct</a>.",
},
{
"👋", "hello-world", "Hello, World!",
"Solve any hole in any language.",
},
{
"🧠", "inception", "Inception",
"Solve <a href=/brainfuck#brainfuck>Brainfuck in Brainfuck</a>.",
},
{
"💼", "interview-ready", "Interview Ready",
"Solve <a href=/fizz-buzz>Fizz Buzz</a> 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 <a href=//github.com/code-golf/code-golf>the Code Golf repository</a>.",
},
{
"🐍", "ouroboros", "Ouroboros",
"Solve <a href=/quine#python>Quine in Python</a>.",
},
{
"💾", "patches-welcome", "Patches Welcome",
"Contribute a merged PR to <a href=//github.com/code-golf/code-golf>the Code Golf repository</a>.",
},
{
"🔣", "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
Expand Down Expand Up @@ -171,8 +106,4 @@ func init() {

holeByID[hole.ID] = holes[i]
}

for _, trophy := range trophies {
trophiesByID[trophy.ID] = trophy
}
}
6 changes: 4 additions & 2 deletions routes/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"net/http"
"time"

"github.com/code-golf/code-golf/trophy"
)

// User serves GET /users/{user}
Expand All @@ -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{},
}

Expand Down
75 changes: 75 additions & 0 deletions trophy/trophy.go
Original file line number Diff line number Diff line change
@@ -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 <a href=//github.com/code-golf/code-golf/commit/4b44>2 Oct</a>.",
},
{
"👋", "hello-world", "Hello, World!",
"Solve any hole in any language.",
},
{
"🧠", "inception", "Inception",
"Solve <a href=/brainfuck#brainfuck>Brainfuck in Brainfuck</a>.",
},
{
"💼", "interview-ready", "Interview Ready",
"Solve <a href=/fizz-buzz>Fizz Buzz</a> 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 <a href=//github.com/code-golf/code-golf>the Code Golf repository</a>.",
},
{
"🐍", "ouroboros", "Ouroboros",
"Solve <a href=/quine#python>Quine in Python</a>.",
},
{
"💾", "patches-welcome", "Patches Welcome",
"Contribute a merged PR to <a href=//github.com/code-golf/code-golf>the Code Golf repository</a>.",
},
{
"🔣", "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.",
},
}
6 changes: 3 additions & 3 deletions views/golfer-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ <h1>{{ .Name }}</h1>
<div><p>💰<p class=yellow>Sponsor</div>
{{ end }}

<div><p><p class=red>{{ .Holes }} / 42<!-- TODO --> Holes</div>
<div><p><p class=red>{{ .Holes }} / {{ .HolesTotal }} Holes</div>

<div><p>🔣<p class=green>{{ .Langs }} / 23<!-- TODO --> Languages</div>
<div><p>🔣<p class=green>{{ .Langs }} / {{ .LangsTotal }} Languages</div>

<div><p>🏆<p class=blue>{{ .Trophies }} / 14<!-- TODO --> Trophies</div>
<div><p>🏆<p class=blue>{{ .Trophies }} / {{ .TrophiesTotal }} Trophies</div>

<div><p>📅<p class=orange>Teed off {{ time .TeedOff }}</div>
</div>
Expand Down

0 comments on commit 0d67bac

Please sign in to comment.