// handleMove processes a player's move. func (s *Server) handleMove(w http.ResponseWriter, r *http.Request) { s.WithMessage(w, r, func(m *lib.Message) { s.WithGame(w, m, func(g *lib.Game) { s.WithPlayer(w, m, g, func(p *lib.Player) { g.ProcessMove(m) log.Printf("Global game state: %#v", g) fmt.Fprintf(w, lib.EncodeGame(g.CreateState(m.Player))) }) }) }) }
// handleStart begins the game if it has not already begun. func (s *Server) handleStart(w http.ResponseWriter, r *http.Request) { s.WithMessage(w, r, func(m *lib.Message) { s.WithGame(w, m, func(g *lib.Game) { s.WithPlayer(w, m, g, func(p *lib.Player) { if g.Started { fmt.Printf("Attempting to start already started game.") fmt.Fprintf(w, "Error: Attempting to start already started game.") return } g.Start() fmt.Fprintf(w, lib.EncodeGame(g.CreateState(m.Player))) }) }) }) }
// handleJoin handles a new player joining the game. func (s *Server) handleJoin(w http.ResponseWriter, r *http.Request) { s.WithMessage(w, r, func(m *lib.Message) { s.WithGame(w, m, func(g *lib.Game) { player := g.GetPlayerByID(m.Player) // add player if it doesn't exist if player == nil { g.AddPlayer(m.Player) } else { log.Printf("Player already exists: %v", player.ID) } fmt.Fprintf(w, lib.EncodeGame(g.CreateState(m.Player))) }) }) }