Example #1
0
File: hello.go Project: hundt/poker
func goToGame(w http.ResponseWriter, r *http.Request) {
	g, err := poker.LoadGame(r.FormValue("id"), r)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	c := appengine.NewContext(r)
	u := user.Current(c)
	tok, err := channel.Create(c, u.Email+g.Id())
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if addWatcher(g, u.Email) {
		err := g.Save(r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	}
	json, err := g.ClientState(u.Email).JSON()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	defineNames(w)
	if err := gameTemplate.Execute(w, json); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	fmt.Fprintf(w, "<script>channel('%s')</script>", tok)
}
Example #2
0
File: hello.go Project: hundt/poker
func sit(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)
	id := r.FormValue("id")
	g, err := poker.LoadGame(id, r)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	err = datastore.RunInTransaction(c, func(c appengine.Context) error {
		name := r.FormValue("name")
		if name == "" {
			return errors.New("Please choose a name")
		}
		return g.Sit(u.Email, name)
	}, nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err = g.Save(r); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	err = broadcastState(c, g)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Example #3
0
File: hello.go Project: hundt/poker
func restart(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	id := r.FormValue("id")
	g, err := poker.LoadGame(id, r)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	if !g.InGame(user.Current(c).Email) {
		http.Error(w, "You are not in this game", http.StatusInternalServerError)
	}
	if !g.Finished() {
		http.Error(w, "Game is not finished", http.StatusInternalServerError)
	}
	err = datastore.RunInTransaction(c, func(c appengine.Context) error {
		g.NewHand()
		return g.Save(r)
	}, nil)
	err = broadcastState(c, g)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Example #4
0
File: hello.go Project: hundt/poker
func play(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)
	g, err := poker.LoadGame(r.FormValue("id"), r)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	idx, err := strconv.Atoi(r.FormValue("idx"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	pos, err := strconv.Atoi(r.FormValue("pos"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if pos < poker.Back || pos > poker.Front || idx < 0 || idx >= len(g.Showing) {
		http.Error(w, "Invalid play", http.StatusInternalServerError)
	}
	if g.Players[g.Turn] != u.Email {
		http.Error(w, "It is not your turn!", http.StatusInternalServerError)
	}
	hand := g.Hands[g.Turn]
	card := g.Showing[idx]
	switch pos {
	case poker.Back:
		if hand.Back.Count() >= 5 {
			http.Error(w, "Invalid play", http.StatusInternalServerError)
			return
		}
		g.Hands[g.Turn].Back = hand.Back.Add(card)
	case poker.Middle:
		if hand.Middle.Count() >= 5 {
			http.Error(w, "Invalid play", http.StatusInternalServerError)
			return
		}
		g.Hands[g.Turn].Middle = hand.Middle.Add(card)
	case poker.Front:
		if hand.Front.Count() >= 3 {
			http.Error(w, "Invalid play", http.StatusInternalServerError)
			return
		}
		g.Hands[g.Turn].Front = hand.Front.Add(card)
	default:
		http.Error(w, "Invalid play", http.StatusInternalServerError)
		return
	}
	// Remove the card that was placed
	copy(g.Showing[idx:], g.Showing[idx+1:])
	g.Showing = g.Showing[:len(g.Showing)-1]
	if len(g.Showing) == 0 {
		// Someone else's turn
		g.NextTurn()
	}
	if err = g.Save(r); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	err = broadcastState(c, g)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	//fmt.Fprint(w, json)
}