コード例 #1
0
func (this *DataServer) createNewSession(resp *Response, req *http.Request) (err error) {
	userid := req.FormValue(USER_ID_FORM_KEY)
	gameid := req.FormValue(APPLICATION_ID_FORM_KEY)

	userTbl := this.database.GetTable(USERS_TABLE_NAME)
	if userTbl == nil {
		resp.AddValueToKey("message", "Server Error.")
		err = errors.New("Users table doesn't exist.")
		return
	}
	hasKey, dbErr := userTbl.HasPrimaryKey(userid)
	if !hasKey || dbErr.Exists() {
		resp.AddValueToKey("message", "Failed to create session: Invalid userid.")
		err = errors.New(dbErr.Error())
		return
	}

	appTbl := this.database.GetTable(APPLICATIONS_TABLE_NAME)
	if appTbl == nil {
		resp.AddValueToKey("message", "Server Error.")
		err = errors.New("Application table doesn't exist.")
		return
	}
	hasKey, err = appTbl.HasPrimaryKey(gameid)
	if !hasKey || err != nil {
		resp.AddValueToKey("message", "Failed to create session: Invalid game_id.")
		return
	}

	sessionTbl := this.database.GetTable(SESSIONS_TABLE_NAME)
	if sessionTbl == nil {
		resp.AddValueToKey("message", "Server Error.")
		err = errors.New("Sessions table doesn't exist.")
		return
	}

	sessionid := GenerateGUID(userid, gameid, time.Now().String())[:32]

	colVals := sessionTbl.GetColumnMap()
	colVals.Update("guid", sessionid)
	colVals.Update("application_id", gameid)
	colVals.Update("user_id", userid)
	colVals.Update("start_time", mysqldb.CurrentTime())
	colVals.Update("end_time", mysqldb.CurrentTimePlus(time.Minute*5))
	dbErr = sessionTbl.Insert(colVals)
	if dbErr.Exists() {
		resp.AddValueToKey("message", "Failed to create session: Database Error.")
		err = errors.New(dbErr.Error())
		return
	}
	resp.AddValueToKey("message", fmt.Sprintf("Logging in as %v in game %v, session_id=%v", userid, gameid, sessionid))

	return
}
コード例 #2
0
func (this *DataServer) handleRefreshSession(resp *Response, req *http.Request) (err error) {
	sessionid := req.FormValue(SESSION_ID_FORM_KEY)
	if sessionid == "" {
		resp.AddValueToKey("message", "Invalid session id.")
		return
	}

	sessionTbl := this.database.GetTable(SESSIONS_TABLE_NAME)
	if sessionTbl == nil {
		resp.AddValueToKey("error", "Server Error.")
		err = errors.New("Sessions table doesn't exist.")
		return
	}

	expirationStr, dbErr := sessionTbl.SelectColumnValueWithPrimaryKey("end_time", sessionid)
	if dbErr.Exists() {
		resp.AddValueToKey("error", "Database Error.")
		err = errors.New(dbErr.Error())
		return
	}
	expiration, err := time.Parse(mysqldb.MySQLTimeFormat, expirationStr)
	if err != nil {
		resp.AddValueToKey("error", "Database Error.")
		return
	}

	if time.Now().Before(expiration) {
		colVals := sessionTbl.GetColumnMap()
		colVals.Update("guid", sessionid)
		colVals.Update("end_time", mysqldb.CurrentTimePlus(time.Minute*5))
		dbErr = sessionTbl.UpdateWithPrimaryKey(colVals)
		if dbErr.Exists() {
			resp.AddValueToKey("error", "Database Error.")
			err = errors.New(dbErr.Error())
			return
		}
	} else {
		sErr := this.createNewSession(resp, req)
		if sErr != nil {
			resp.AddValueToKey("error", "Failed to create new session.")
			err = sErr
			return
		}
	}

	resp.AddValueToKey("message", "Session refreshed, session_id="+sessionid+"}")
	return
}