Ejemplo n.º 1
0
func main() {
	var players = []*game.Player{
		&game.Player{1, "Player One"},
		&game.Player{2, "Player Two"},
	}
	game := collection.NewForbiddenIsland()
	session, err := session.NewSession(game, players)
	if nil != err {
		fmt.Printf("Error creating game session: %s", err)
		os.Exit(1)
	}

	step(session, players[0])
	step(session, players[0])
	step(session, players[1])
	step(session, players[0])
	step(session, players[1])
	step(session, players[0])
	step(session, players[1])
	step(session, players[1])
	step(session, players[0])
	fmt.Println()

	fmt.Println("== Final session state ==========================================")
	session.Print()
	fmt.Println()

	fmt.Println("== Setup rules ==================================================")
	printSetupRules(game)
}
Ejemplo n.º 2
0
// Persist a new session
func (handler SessionCreateHandler) marshalFunc() func(*url.URL, http.Header, *SessionCreateRequest) (int, http.Header, *session.Session, error) {
	return func(u *url.URL, h http.Header, rq *SessionCreateRequest) (int, http.Header, *session.Session, error) {
		var err error

		var game_id uint64
		game_id, err = strconv.ParseUint(rq.Session.Game, 10, 64)
		if nil != err {
			return http.StatusBadRequest, nil, nil, errors.New("Expected integer game ID")
		}

		player_ids := make([]int, 0)
		for _, player_id_str := range rq.Session.Players {
			player_id, err := strconv.ParseInt(player_id_str, 10, 32)
			if nil != err {
				return http.StatusBadRequest, nil, nil, errors.New("Expected integer player ID")
			}
			player_ids = append(player_ids, (int)(player_id))
		}

		var players []*game.Player
		players, err = fetchPlayersById(handler.db, player_ids)
		if nil != err {
			return http.StatusInternalServerError, nil, nil, err
		}

		var _session *session.Session
		_session, err = session.NewSession(gameIndex[game_id], players)
		if nil != err {
			return http.StatusInternalServerError, nil, nil, err
		}
		_session.StepAllPlayers()

		err = record.NewSessionRecord(_session).Create(handler.db)
		if nil != err {
			return http.StatusInternalServerError, nil, nil, err
		}

		sessions = append(sessions, _session)
		sessionIndex[(uint64)(_session.Id)] = _session

		for _, step := range _session.SetupSteps {
			glog.Printf("Session #%d: Created with step %s\n", _session.Id, _session.StepWithAssigneeString(step))
		}

		return http.StatusCreated, nil, _session, nil
	}
}