diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 86cf9d5d4d..c547406779 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -11,6 +11,9 @@ #include +#include + +#include #include #include #include @@ -39,6 +42,8 @@ const bool USING_HEAD_LEAN = false; const float SKIN_COLOR[] = {1.0f, 0.84f, 0.66f}; const float DARK_SKIN_COLOR[] = {0.9f, 0.78f, 0.63f}; +const float DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS = 5 * 1000; + MyAvatar::MyAvatar() : Avatar(), _mousePressed(false), @@ -61,6 +66,11 @@ MyAvatar::MyAvatar() : for (int i = 0; i < MAX_DRIVE_KEYS; i++) { _driveKeys[i] = 0.0f; } + + // update our location every 5 seconds in the data-server, assuming that we are authenticated with one + QTimer* locationUpdateTimer = new QTimer(this); + connect(locationUpdateTimer, &QTimer::timeout, this, &MyAvatar::updateLocationInDataServer); + locationUpdateTimer->start(DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS); } MyAvatar::~MyAvatar() { @@ -1167,3 +1177,26 @@ void MyAvatar::resetSize() { qDebug("Reseted scale to %f", _targetScale); } +static QByteArray createByteArray(const glm::vec3& vector) { + return QByteArray::number(vector.x) + ',' + QByteArray::number(vector.y) + ',' + QByteArray::number(vector.z); +} + +void MyAvatar::updateLocationInDataServer() { + // TODO: don't re-send this when it hasn't change or doesn't change by some threshold + // This will required storing the last sent values and clearing them when the AccountManager rootURL changes + + AccountManager& accountManager = AccountManager::getInstance(); + + if (accountManager.isLoggedIn()) { + QString positionString(createByteArray(_position)); + QString orientationString(createByteArray(safeEulerAngles(getOrientation()))); + + // construct the json to put the user's location + QString locationPutJson = QString() + "{\"location\":{\"position\":\"" + + positionString + "\", \"orientation\":\"" + orientationString + "\"}}"; + + accountManager.authenticatedRequest("/api/v1/users/location", QNetworkAccessManager::PutOperation, + JSONCallbackParameters(), locationPutJson.toUtf8()); + } +} + diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 538ca4e0b2..21434f3041 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -77,18 +77,19 @@ public: static void sendKillAvatar(); - void orbit(const glm::vec3& position, int deltaX, int deltaY); AvatarData* getLookAtTargetAvatar() const { return _lookAtTargetAvatar.data(); } void updateLookAtTargetAvatar(glm::vec3& eyePosition); void clearLookAtTargetAvatar(); - + public slots: void goHome(); void increaseSize(); void decreaseSize(); void resetSize(); + + void updateLocationInDataServer(); // Set/Get update the thrust that will move the avatar around void addThrust(glm::vec3 newThrust) { _thrust += newThrust; };