Пример #1
0
func gameSrvActivationHandler(response http.ResponseWriter, request *http.Request) {
	if !(utils.CheckValue(request, KEY_SERVER_NAME, utils.Rule{Min: 3, Max: 100}) &&
		utils.CheckValue(request, KEY_PROTOCOL_VERSION, utils.Rule{Number: true, Min: 1, Max: 1000}) &&
		utils.CheckValue(request, KEY_ACTIVE, utils.Rule{Values: []string{"true", "false"}})) {
		http.Error(response, utils.GetLastValidationError().Error(), http.StatusBadRequest)
		return
	}

	protocolVersion, _ := strconv.Atoi(request.FormValue(KEY_PROTOCOL_VERSION))
	if protocolVersion < base.CURRENT_PROTOCOL_VERSION {
		http.Error(response, ERR_WRONG_PROTOCOL, http.StatusNotAcceptable)
		return
	}

	context := appengine.NewContext(request)

	err := pingGameServer(context, request.RemoteAddr+DEFAULT_GAME_SERVER_PORT)
	if err != nil {
		http.Error(response, err.Error(), http.StatusInternalServerError)
		return
	}

	gameServer := new(model.GameServer)
	key, err := datastore.DecodeKey(request.FormValue(KEY_SERVER_ID))
	if err == nil {
		err = datastore.Get(context, key, gameServer)
		if err != nil {
			log.Println("Can't find game server with id: making new one")
		}
	}
	gameServer.Name = request.FormValue(KEY_SERVER_NAME)
	gameServer.Address = request.RemoteAddr + DEFAULT_GAME_SERVER_PORT
	gameServer.Active = request.FormValue(KEY_ACTIVE) == "true"
	gameServer.ProtocolVersion = protocolVersion
	gameServer.LastActivationChange = time.Now()
	if err != nil {
		gameServer.RegisteredAt = time.Now()
		key = datastore.NewIncompleteKey(context, model.DB_KIND_GAME_SERVER, nil)
	}

	key, err = datastore.Put(context, key, gameServer)
	if err != nil {
		log.Fatal(err)
	}

	utils.WriteJsonResponseObject(response, GameServerActivationResult{Id: key.Encode()})
}
Пример #2
0
func gameSrvQueryConnectionHandler(response http.ResponseWriter, request *http.Request) {
	if !(utils.CheckValue(request, KEY_SERVER_ID, utils.Rule{Min: 3, Max: 1000})) {
		http.Error(response, utils.GetLastValidationError().Error(), http.StatusBadRequest)
		return
	}

	context := appengine.NewContext(request)

	gameServer := new(model.GameServer)
	key, err := datastore.DecodeKey(request.FormValue(KEY_SERVER_ID))
	if err != nil {
		http.Error(response, ERR_WRONG_SERVER_ID, http.StatusBadRequest)
		return
	}
	err = datastore.Get(context, key, gameServer)
	if err != nil {
		log.Fatal(err)
	}

	utils.WriteJsonResponseObject(response, &GameServerQueryConnectionResult{Active: gameServer.Active})
}