Exemplo n.º 1
0
func startHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	size := parseSize(r)
	g := <-nextGame
	if g.state == nil {
		g.state = game.New(size, 180)
		g.players = map[GameID]game.Color{}
		g.turn = make(chan game.Color, 1)
		g.turn <- game.Black
	}
	id := masterID.next()
	var c game.Color
	switch {
	case len(g.players) == 0:
		c = game.Black
		nextGame <- g
	case len(g.players) == 1:
		c = game.White
		nextGame <- &Game{}
	}
	gameMap[id] = g
	g.players[id] = c
	s := struct {
		ID    GameID     `json: "id"`
		Color game.Color `json:"color"`
	}{
		id, c,
	}
	writeJSON(w, &s)
}
Exemplo n.º 2
0
func TestStartHandler(t *testing.T) {
	r, _ := http.NewRequest("GET", "/", nil)
	tests := []struct {
		input    *http.Request
		expected map[GameID]*Game
		reason   string
	}{
		{
			r,
			map[GameID]*Game{
				1: &Game{
					state:   game.New(19, 180),
					players: map[GameID]game.Color{1: game.Black},
				},
			},
			"One person has joined",
		},
		{
			r,
			map[GameID]*Game{
				1: &Game{
					state:   game.New(19, 180),
					players: map[GameID]game.Color{1: game.Black, 2: game.White},
				},
				2: &Game{
					state:   game.New(19, 180),
					players: map[GameID]game.Color{1: game.Black, 2: game.White},
				},
			},
			"Two people have joined",
		},
	}
	for _, test := range tests {
		startHandler(&testWriter{}, test.input)
		if !gameEqual(test.expected, gameMap) {
			t.Errorf("%s not equal:\nexpected:%v\nactual:%v", test.reason, test.expected, gameMap)
		}
	}

}