Skip to content

Commit

Permalink
asdf
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Jul 8, 2024
1 parent a12e6d5 commit 33bbadc
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 9 deletions.
24 changes: 24 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# flyctl launch added from .gitignore
# Binaries for programs and plugins
**/*.exe
**/*.exe~
**/*.dll
**/*.so
**/*.dylib

# Test binary, built with `go test -c`
**/*.test

# Output of the go coverage tool, specifically when used with LiteIDE
**/*.out

# Go workspace file
**/go.work

### Go Patch ###
vendor
Godeps

# GoReleaser
**/dist
fly.toml
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ARG GO_VERSION=1
FROM golang:${GO_VERSION}-alpine as builder

WORKDIR /usr/src/app
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o /run-app .

FROM alpine:latest

COPY --from=builder /run-app /usr/local/bin/
CMD ["run-app", "-serve", "0.0.0.0:3000"]
68 changes: 59 additions & 9 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"time"

"github.com/google/renameio/maybe"
"github.com/google/uuid"
"github.com/pkg/errors"
)

// db is the file where the game statistics are stored.
type db struct {
// Guesses stores the win statistics of each game. Guesses[0] is the number
// of games that were lost, Guesses[1] is the number of games that were won
// with 1 guess, etc.
Guesses [numGuesses + 1]int `json:"guesses"`
Games []Game
}

type Game struct {
ID string
Guesses int
CompletedAt time.Time
}

// loadDb reads the database from dbPath.
Expand Down Expand Up @@ -56,12 +62,56 @@ func (db *db) addWin(guesses int) {
if !(1 <= guesses && guesses <= numGuesses) {
panic(fmt.Sprintf("guesses out of range: %d", guesses))
}
db.Guesses[guesses]++

id, err := uuid.NewUUID()
if err != nil {
panic(err)
}

completedAt := time.Now().UTC()

game := Game{
ID: id.String(),
Guesses: guesses,
CompletedAt: completedAt,
}
db.Games = append(db.Games, game)
}

// addLoss adds a loss to the game statistics.
func (db *db) addLoss() {
db.Guesses[0]++
id, err := uuid.NewUUID()
if err != nil {
panic(err)
}

completedAt := time.Now().UTC()

game := Game{
ID: id.String(),
Guesses: 0,
CompletedAt: completedAt,
}
db.Games = append(db.Games, game)
}

func (db *db) GetHistogram() map[int]int {
histogram := make(map[int]int)
for _, game := range db.Games {
histogram[game.Guesses]++
}
return histogram
}

// IMPORTANT: this assumes that the games are sorted by completion time.
func (db *db) GetStreak() int {
sort.Slice(db.Games, func(i, j int) bool {
return db.Games[i].CompletedAt.After(db.Games[j].CompletedAt)
})

for _, game := range db.Games {

Check failure on line 112 in db.go

View workflow job for this annotation

GitHub Actions / lint

game declared and not used

}
}

Check failure on line 115 in db.go

View workflow job for this annotation

GitHub Actions / lint

missing return (typecheck)

// score returns the current total score based on the statistics.
Expand All @@ -70,8 +120,8 @@ func (db *db) addLoss() {
// points, plus an extra 10 points for each remaining guess.
func (db *db) score() int {
score := 0
for i, guess := range db.Guesses[1:] {
score += (100 - 10*i) * guess
}
// for i, guess := range db.Guesses[1:] {
// score += (100 - 10*i) * guess
// }
return score
}
25 changes: 25 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# fly.toml app configuration file generated for clidle on 2024-02-14T17:52:47+05:30
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'clidle'
primary_region = 'sjc'

[build]
[build.args]
GO_VERSION = '1.13'

[[services]]
internal_port = 3000
protocol = "tcp"
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1

[[services.ports]]
port = 3000

[[vm]]
memory = '256mb'
size = 'shared-cpu-1x'
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ require (
github.com/charmbracelet/wish v0.2.0
github.com/gliderlabs/ssh v0.3.6
github.com/google/renameio v1.0.1
github.com/google/uuid v1.6.0 // indirect
github.com/pkg/errors v0.9.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU=
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
Expand Down
Empty file added schema.sql
Empty file.

0 comments on commit 33bbadc

Please sign in to comment.