Skip to content

Commit

Permalink
Finish rework session routes and draw route
Browse files Browse the repository at this point in the history
  • Loading branch information
klydra committed Feb 21, 2023
1 parent 5877d51 commit bb973dc
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 157 deletions.
26 changes: 19 additions & 7 deletions server/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// defaults
// ---------------------------------------------------------------

func defaultPlayer(name string) Player {
func defaultPlayerStruct(name string) Player {
return Player{
Name: name,
Cards: 0,
Expand All @@ -20,19 +20,23 @@ func defaultPlayer(name string) Player {
}

func defaultPlayers(name string) string {
players, _ := json.Marshal([]Player{defaultPlayer(name)})
players, _ := json.Marshal([]Player{defaultPlayerStruct(name)})
return string(players)
}

func defaultGlobals() string {
globals, _ := json.Marshal(Globals{
globals, _ := json.Marshal(defaultGlobalsStruct())
return string(globals)
}

func defaultGlobalsStruct() Globals {
return Globals{
Live: "",
Direction: true,
Stacking: false,
Swapping: false,
Drawable: false,
})
return string(globals)
}
}

func defaultRules() string {
Expand Down Expand Up @@ -75,6 +79,14 @@ func playersFromStruct(players []Player) (string, *apis.ApiError) {
return string(marshalled), nil
}

func rulesFromString(rules string) (Rules, *apis.ApiError) {
var unmarshalled Rules
if err := json.Unmarshal([]byte(rules), &unmarshalled); err != nil {
return Rules{}, apis.NewApiError(500, "Can't parse rules.", err)
}
return unmarshalled, nil
}

func rulesFromStruct(rules Rules) (string, *apis.ApiError) {
marshalled, err := json.Marshal(rules)
if err != nil {
Expand Down Expand Up @@ -103,9 +115,9 @@ func stackFromStruct(stack []string) (string, *apis.ApiError) {
// records to structs
// ---------------------------------------------------------------

func handFromPlayer(game *models.Record) ([]string, *apis.ApiError) {
func handFromPlayer(player *models.Record) ([]string, *apis.ApiError) {
var hand []string
if err := json.Unmarshal([]byte(game.GetString("players")), &hand); err != nil {
if err := json.Unmarshal([]byte(player.GetString("hand")), &hand); err != nil {
return nil, apis.NewApiError(500, "Can't parse hand.", err)
}
return hand, nil
Expand Down
28 changes: 21 additions & 7 deletions server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@ func playerIndexByName(name string, players []Player) (int, *apis.ApiError) {
return -1, apis.NewApiError(500, "Can't find player in struct.", nil)
}

func nextPlayer(name string, players []Player, globals Globals) int {
func nextPlayer(name string, players []Player, globals Globals) (int, *apis.ApiError) {
if globals.Direction {
for i := 0; i < len(players); i++ {
if players[i].Name == name {
if i == len(players)-1 {
return 0
return 0, nil
} else {
return i + 1
return i + 1, nil
}
}
}
} else {
for i := len(players) - 1; i >= 0; i-- {
if players[i].Name == name {
if i == 0 {
return len(players) - 1
return len(players) - 1, nil
} else {
return i - 1
return i - 1, nil
}
}
}
}

return -1
return -1, apis.NewApiError(500, "Could not evaluate next player.", nil)
}

func resetCard(card string) string {
Expand All @@ -63,7 +63,21 @@ func resetCard(card string) string {

func participating(player *models.Record) *apis.ApiError {
if len(player.GetString("game")) == 0 {
return apis.NewBadRequestError("You are not participating in this game.", err)
return apis.NewBadRequestError("You are not participating in this game.", nil)
}
return nil
}

func hosting(player *models.Record, players []Player) *apis.ApiError {
if players[0].Name != player.GetString("name") {
return apis.NewBadRequestError("You are not the host.", nil)
}
return nil
}

func playing(player *models.Record, globals Globals) *apis.ApiError {
if globals.Live == player.GetString("name") {
return apis.NewBadRequestError("You are not the host.", nil)
}
return nil
}
Loading

0 comments on commit bb973dc

Please sign in to comment.