Ejemplo n.º 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()})
}
Ejemplo n.º 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})
}
Ejemplo n.º 3
0
func registerHandler(response http.ResponseWriter, request *http.Request) {
	if !(utils.CheckValue(request, KEY_LOGIN, utils.Rule{Min: 3, Max: 100}) &&
		utils.CheckValue(request, KEY_PASSWORD, utils.Rule{Min: 3, Max: 100}) &&
		utils.CheckValue(request, KEY_ALIAS, utils.Rule{Min: 3, Max: 100}) &&
		utils.CheckValue(request, KEY_EMAIL, utils.Rule{Min: 3, Max: 500, Regex: "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$"}) &&
		utils.CheckValue(request, KEY_CAPTCHA_ID, utils.Rule{Min: 1, Max: 100}) &&
		utils.CheckValue(request, KEY_CAPTCHA_VAL, utils.Rule{Min: 1, Max: 100})) {
		http.Error(response, utils.GetLastValidationError().Error(), http.StatusBadRequest)
		return
	}

	context := appengine.NewContext(request)

	query := datastore.NewQuery(model.DB_KIND_USER).
		Filter("Login ="******"Email =", request.FormValue(KEY_EMAIL)).KeysOnly()
	count, err = query.Count(context)
	if count != 0 {
		http.Error(response, ERR_EMAIL_CONFLICT, http.StatusConflict)
		return
	}

	passed, err := CheckCaptcha(context, request.FormValue(KEY_CAPTCHA_ID), request.FormValue(KEY_CAPTCHA_VAL))
	if !passed {
		if err != nil {
			log.Println(err)
		}
		http.Error(response, ERR_CAPTCHA_NOT_PASS, http.StatusNotAcceptable)
		return
	}

	user := model.User{
		Login:      request.FormValue(KEY_LOGIN),
		Password:   request.FormValue(KEY_PASSWORD),
		Alias:      request.FormValue(KEY_ALIAS),
		Email:      request.FormValue(KEY_EMAIL),
		Verified:   false,
		Registered: time.Now(),
	}
	key, err := datastore.Put(context, datastore.NewIncompleteKey(context, model.DB_KIND_USER, nil), &user)
	if err != nil {
		log.Fatal(err)
		return
	}

	url := "http://" + appengine.DefaultVersionHostname(context) + HANDLER_PATH_CONFIRM + "?" + KEY_USER + "=" + key.Encode()
	msg := &mail.Message{
		Sender:   base.PHLOX_CONNECT_EMAIL,
		To:       []string{request.FormValue(KEY_EMAIL)},
		Subject:  CONFIRM_REGISTRATION,
		HTMLBody: fmt.Sprintf(CONFIRM_MAIL_BODY, url, url),
	}
	err = mail.Send(context, msg)
	if err != nil {
		log.Println(err)
		http.Error(response, fmt.Sprintf(ERR_CANT_SEND_EMAIL, err), http.StatusInternalServerError)
		return
	}

	utils.WriteJsonResponse(response, utils.JSON_RESULT_SUCCESS)
}