示例#1
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
	}
}
示例#2
0
func step(session *session.Session, player *game.Player) {
	step := session.Step(player)
	step.Finish()
	fmt.Println(session.StepWithAssigneeString(step))
}