Skip to content

Commit

Permalink
Cleanup, added humorous message
Browse files Browse the repository at this point in the history
  • Loading branch information
lucashorward committed Feb 11, 2024
1 parent dd0e559 commit 745047f
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,24 @@ type newGame struct {
}

type game struct {
Id string `json:"id"`
Result string `json:"result"`
Ended bool `json:"ended"`
answer int
Id string `json:"id"`
Result string `json:"result"`
Ended bool `json:"ended"`
answer int
Message string `json:"message"`
}

type guess struct {
GameId string `json:"gameId"`
Guess int `json:"guess"`
}

// String - Creating common behavior - give the type a String function
func (d Result) String() string {
return [...]string{"TooLow", "TooHigh", "Equal", "None"}[d]
func (result Result) String() string {
return [...]string{"TooLow", "TooHigh", "Equal", "None"}[result]
}

// EnumIndex - Creating common behavior - give the type a EnumIndex functio
func (d Result) EnumIndex() int {
return int(d)
func (result Result) EnumIndex() int {
return int(result)
}

var games = make(map[string]*game)
Expand All @@ -64,6 +63,7 @@ func createGameHandler(w http.ResponseWriter, r *http.Request) {
Result.String(None),
false,
answer,
"Welcome to the game!",
}
games[gameId] = createdGame
jsonResponse, _ := json.Marshal(createdGame)
Expand All @@ -87,19 +87,24 @@ func guessHandler(w http.ResponseWriter, r *http.Request) {
}
var result Result
ended := false
var message string
if guess.Guess == savedGame.answer {
result = Equal
ended = true
message = "Excelsior, you win!!"
} else if guess.Guess > savedGame.answer {
result = TooHigh
message = "Fiddlesticks, that's a too high"
} else {
result = TooLow
message = "Dingleberries, that's too low!"
}
updatedGame := &game{
savedGame.Id,
Result.String(result),
ended,
savedGame.answer,
message,
}
games[savedGame.Id] = updatedGame
jsonResponse, _ := json.Marshal(updatedGame)
Expand Down

0 comments on commit 745047f

Please sign in to comment.