Skip to content

Commit

Permalink
Merge pull request #197 from zond/johnpooch/add-public-load-game
Browse files Browse the repository at this point in the history
Add public load game
  • Loading branch information
johnpooch authored Jun 17, 2024
2 parents 1b1e1b7 + c13cc10 commit e4d9155
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,17 @@ func createGame(w ResponseWriter, r Request) (*Game, error) {
return game, nil
}

// Like `game.Redact`, but for use with unauthenticated requests.
// Removes game master invitations and anonymizes all members.
func (g *Game) RedactPublic(r Request) {
for index := range g.GameMasterInvitations {
g.GameMasterInvitations[index].Email = ""
}
for index := range g.Members {
g.Members[index].Anonymize(r)
}
}

func (g *Game) Redact(viewer *auth.User, r Request) {
if viewer.Id == g.GameMaster.Id {
return
Expand Down Expand Up @@ -1378,11 +1389,36 @@ func gameMasterUpdateGame(w ResponseWriter, r Request) (*Game, error) {
return game, nil
}

func loadGame(w ResponseWriter, r Request) (*Game, error) {
func loadGamePublic(w ResponseWriter, r Request) (*Game, error) {
ctx := appengine.NewContext(r.Req())

gameID, err := datastore.DecodeKey(r.Vars()["id"])
if err != nil {
return nil, err
}

game := &Game{}
if err := datastore.Get(ctx, gameID, game); err != nil {
return nil, err
}
game.ID = gameID
for i := range game.NewestPhaseMeta {
game.NewestPhaseMeta[i].Refresh()
}

game.Refresh()

game.RedactPublic(r)

return game, nil
}

func loadGameAuthenticated(w ResponseWriter, r Request) (*Game, error) {
ctx := appengine.NewContext(r.Req())

user, ok := r.Values()["user"].(*auth.User)
if !ok {
log.Errorf(ctx, "loadGameAuthenticated called without user - this should never happen")
return nil, HTTPErr{"unauthenticated", http.StatusUnauthorized}
}

Expand Down Expand Up @@ -1433,3 +1469,17 @@ func loadGame(w ResponseWriter, r Request) (*Game, error) {

return &filtered[0], nil
}

func loadGame(w ResponseWriter, r Request) (*Game, error) {
ctx := appengine.NewContext(r.Req())

_, ok := r.Values()["user"].(*auth.User)
if !ok {
log.Infof(ctx, "Loading game without user - calling loadGamePublic")
return loadGamePublic(w, r)
} else {
log.Infof(ctx, "Loading game with user - calling loadGameAuthenticated")
return loadGameAuthenticated(w, r)
}

}

0 comments on commit e4d9155

Please sign in to comment.