ESLINT, style and spelling fixes

This commit is contained in:
Thijs Wenker 2017-04-26 18:51:55 +02:00
parent c7ab4994ea
commit a25584c90f

View file

@ -1,6 +1,6 @@
"use strict";
//
// makeUserConnetion.js
// makeUserConnection.js
// scripts/system
//
// Created by David Kelly on 3/7/2017.
@ -11,31 +11,31 @@
//
(function() { // BEGIN LOCAL_SCOPE
const label = "makeUserConnection";
const MAX_AVATAR_DISTANCE = 0.2; // m
const GRIP_MIN = 0.05; // goes from 0-1, so 5% pressed is pressed
const MESSAGE_CHANNEL = "io.highfidelity.makeUserConnection";
const STATES = {
inactive : 0,
waiting: 1,
connecting: 2,
makingConnection: 3
var LABEL = "makeUserConnection";
var MAX_AVATAR_DISTANCE = 0.2; // m
var GRIP_MIN = 0.05; // goes from 0-1, so 5% pressed is pressed
var MESSAGE_CHANNEL = "io.highfidelity.makeUserConnection";
var STATES = {
INACTIVE: 0,
WAITING: 1,
CONNECTING: 2,
MAKING_CONNECTION: 3
};
const STATE_STRINGS = ["inactive", "waiting", "connecting", "makingConnection"];
const WAITING_INTERVAL = 100; // ms
const CONNECTING_INTERVAL = 100; // ms
const MAKING_CONNECTION_TIMEOUT = 800; // ms
const CONNECTING_TIME = 1600; // ms
const PARTICLE_RADIUS = 0.15; // m
const PARTICLE_ANGLE_INCREMENT = 360/45; // 1hz
const HANDSHAKE_SOUND_URL = "https://s3-us-west-1.amazonaws.com/hifi-content/davidkelly/production/audio/4beat_sweep.wav";
const SUCCESSFUL_HANDSHAKE_SOUND_URL = "https://s3-us-west-1.amazonaws.com/hifi-content/davidkelly/production/audio/3rdbeat_success_bell.wav";
const HAPTIC_DATA = {
var STATE_STRINGS = ["inactive", "waiting", "connecting", "makingConnection"];
var WAITING_INTERVAL = 100; // ms
var CONNECTING_INTERVAL = 100; // ms
var MAKING_CONNECTION_TIMEOUT = 800; // ms
var CONNECTING_TIME = 1600; // ms
var PARTICLE_RADIUS = 0.15; // m
var PARTICLE_ANGLE_INCREMENT = 360/45; // 1hz
var HANDSHAKE_SOUND_URL = "https://s3-us-west-1.amazonaws.com/hifi-content/davidkelly/production/audio/4beat_sweep.wav";
var SUCCESSFUL_HANDSHAKE_SOUND_URL = "https://s3-us-west-1.amazonaws.com/hifi-content/davidkelly/production/audio/3rdbeat_success_bell.wav";
var HAPTIC_DATA = {
initial: { duration: 20, strength: 0.6 }, // duration is in ms
background: { duration: 100, strength: 0.3 }, // duration is in ms
success: { duration: 60, strength: 1.0 } // duration is in ms
};
const PARTICLE_EFFECT_PROPS = {
var PARTICLE_EFFECT_PROPS = {
"alpha": 0.8,
"azimuthFinish": Math.PI,
"azimuthStart": -1*Math.PI,
@ -61,7 +61,7 @@ const PARTICLE_EFFECT_PROPS = {
"dimensions": {"x":0.05, "y": 0.05, "z": 0.05},
"type": "ParticleEffect"
};
const MAKING_CONNECTION_PARTICLE_PROPS = {
var MAKING_CONNECTION_PARTICLE_PROPS = {
"alpha": 0.07,
"alphaStart": 0.011,
"alphaSpread": 0,
@ -93,7 +93,7 @@ const MAKING_CONNECTION_PARTICLE_PROPS = {
};
var currentHand;
var state = STATES.inactive;
var state = STATES.INACTIVE;
var connectingInterval;
var waitingInterval;
var makingConnectionTimeout;
@ -102,7 +102,6 @@ var connectingId;
var connectingHand;
var waitingList = {};
var particleEffect;
var waitingBallScale;
var particleRotationAngle = 0.0;
var makingConnectionParticleEffect;
var makingConnectionEmitRate = 2000;
@ -115,7 +114,8 @@ var successfulHandshakeSound;
function debug() {
var stateString = "<" + STATE_STRINGS[state] + ">";
var connecting = "[" + connectingId + "/" + connectingHand + "]";
print.apply(null, [].concat.apply([label, stateString, JSON.stringify(waitingList), connecting], [].map.call(arguments, JSON.stringify)));
print.apply(null, [].concat.apply([LABEL, stateString, JSON.stringify(waitingList), connecting],
[].map.call(arguments, JSON.stringify)));
}
function cleanId(guidWithCurlyBraces) {
@ -156,8 +156,10 @@ function request(options, callback) { // cb(error, responseOfCorrectContentType)
if (options.body && (options.method === 'GET')) { // add query parameters
var params = [], appender = (-1 === options.uri.search('?')) ? '?' : '&';
for (key in options.body) {
if (options.body.hasOwnProperty(key)) {
params.push(key + '=' + options.body[key]);
}
}
options.uri += appender + params.join('&');
delete options.body;
}
@ -167,8 +169,10 @@ function request(options, callback) { // cb(error, responseOfCorrectContentType)
options.body = JSON.stringify(options.body);
}
for (key in options.headers || {}) {
if (options.headers.hasOwnProperty(key)) {
httpRequest.setRequestHeader(key, options.headers[key]);
}
}
httpRequest.open(options.method, options.uri, true);
httpRequest.send(options.body);
}
@ -184,9 +188,9 @@ function handToString(hand) {
}
function stringToHand(hand) {
if (hand == "RightHand") {
if (hand === "RightHand") {
return Controller.Standard.RightHand;
} else if (hand == "LeftHand") {
} else if (hand === "LeftHand") {
return Controller.Standard.LeftHand;
}
debug("stringToHand called with bad hand string:", hand);
@ -245,8 +249,7 @@ function shakeHandsAnimation(animationProperties) {
if (headIndex) {
offset = 0.8 * MyAvatar.getAbsoluteJointTranslationInObjectFrame(headIndex).y;
}
var handPos = Vec3.multiply(offset, {x: -0.25, y: 0.8, z: 1.3});
result.rightHandPosition = handPos;
result.rightHandPosition = Vec3.multiply(offset, {x: -0.25, y: 0.8, z: 1.3});
result.rightHandRotation = Quat.fromPitchYawRollDegrees(90, 0, 90);
return result;
}
@ -287,7 +290,7 @@ function calcParticlePos(myHand, otherHand, otherOrientation, reset) {
// this is called frequently, but usually does nothing
function updateVisualization() {
if (state == STATES.inactive) {
if (state === STATES.INACTIVE) {
deleteParticleEffect();
deleteMakeConnectionParticleEffect();
// this should always be true if inactive, but just in case:
@ -306,16 +309,14 @@ function updateVisualization() {
}
}
var wrist = MyAvatar.getJointPosition(MyAvatar.getJointIndex(handToString(currentHand)));
var d = Math.min(MAX_AVATAR_DISTANCE, Vec3.distance(wrist, myHandPosition));
switch (state) {
case STATES.waiting:
case STATES.WAITING:
// no visualization while waiting
deleteParticleEffect();
deleteMakeConnectionParticleEffect();
stopHandshakeSound();
break;
case STATES.connecting:
case STATES.CONNECTING:
var particleProps = {};
// put the position between the 2 hands, if we have a connectingId. This
// helps define the plane in which the particles move.
@ -343,13 +344,20 @@ function updateVisualization() {
makingConnectionParticleEffect = Entities.addEntity(props, true);
} else {
makingConnectionEmitRate *= 0.5;
Entities.editEntity(makingConnectionParticleEffect, {emitRate: makingConnectionEmitRate, position: myHandPosition, isEmitting: 1});
Entities.editEntity(makingConnectionParticleEffect, {
emitRate: makingConnectionEmitRate,
position: myHandPosition,
isEmitting: true
});
}
break;
case STATES.makingConnection:
case STATES.MAKING_CONNECTION:
particleEmitRate = Math.max(50, particleEmitRate * 0.5);
Entities.editEntity(makingConnectionParticleEffect, {emitRate: 0, isEmitting: 0, position: myHandPosition});
Entities.editEntity(particleEffect, {position: calcParticlePos(myHandPosition, otherHand, otherOrientation), emitRate: particleEmitRate});
Entities.editEntity(particleEffect, {
position: calcParticlePos(myHandPosition, otherHand, otherOrientation),
emitRate: particleEmitRate
});
break;
default:
debug("unexpected state", state);
@ -397,7 +405,7 @@ function findNearestWaitingAvatar() {
function startHandshake(fromKeyboard) {
if (fromKeyboard) {
debug("adding animation");
// just in case order of press/unpress is broken
// just in case order of press/release is broken
if (animHandlerId) {
animHandlerId = MyAvatar.removeAnimationStateHandler(animHandlerId);
}
@ -405,7 +413,7 @@ function startHandshake(fromKeyboard) {
}
debug("starting handshake for", currentHand);
pollCount = 0;
state = STATES.waiting;
state = STATES.WAITING;
connectingId = undefined;
connectingHand = undefined;
// just in case
@ -444,7 +452,7 @@ function endHandshake() {
// only be done here, unless we change how the triggering works,
// as we ignore the key release event when inactive. See updateTriggers
// below.
state = STATES.inactive;
state = STATES.INACTIVE;
connectingId = undefined;
connectingHand = undefined;
stopWaiting();
@ -460,7 +468,8 @@ function endHandshake() {
debug("removing animation");
MyAvatar.removeAnimationStateHandler(animHandlerId);
}
// No-op if we were successful, but this way we ensure that failures and abandoned handshakes don't leave us in a weird state.
// No-op if we were successful, but this way we ensure that failures and abandoned handshakes don't leave us
// in a weird state.
request({uri: requestUrl, method: 'DELETE'}, debug);
}
@ -476,7 +485,7 @@ function updateTriggers(value, fromKeyboard, hand) {
var isGripping = value > GRIP_MIN;
if (isGripping) {
debug("updateTriggers called - gripping", handToString(hand));
if (state != STATES.inactive) {
if (state !== STATES.INACTIVE) {
return;
} else {
startHandshake(fromKeyboard);
@ -484,7 +493,7 @@ function updateTriggers(value, fromKeyboard, hand) {
} else {
// TODO: should we end handshake even when inactive? Ponder
debug("updateTriggers called -- no longer gripping", handToString(hand));
if (state != STATES.inactive) {
if (state !== STATES.INACTIVE) {
endHandshake();
} else {
return;
@ -502,11 +511,11 @@ function lookForWaitingAvatar() {
// as soon as you have a connectingId (which means you got their
// message before noticing they were in range in this loop)
// just in case we reenter before stopping
// just in case we re-enter before stopping
stopWaiting();
debug("started looking for waiting avatars");
waitingInterval = Script.setInterval(function () {
if (state == STATES.waiting && !connectingId) {
if (state === STATES.WAITING && !connectingId) {
// find the closest in-range avatar, and send connection request
var nearestAvatar = findNearestWaitingAvatar();
if (nearestAvatar.avatar) {
@ -536,15 +545,22 @@ var result, requestBody, pollCount = 0, requestUrl = location.metaverseServerUrl
function connectionRequestCompleted() { // Final result is in. Do effects.
if (result.status === 'success') { // set earlier
if (!successfulHandshakeInjector) {
successfulHandshakeInjector = Audio.playSound(successfulHandshakeSound, {position: getHandPosition(MyAvatar, currentHand), volume: 0.5, localOnly: true});
successfulHandshakeInjector = Audio.playSound(successfulHandshakeSound, {
position: getHandPosition(MyAvatar, currentHand),
volume: 0.5,
localOnly: true
});
} else {
successfulHandshakeInjector.restart();
}
Controller.triggerHapticPulse(HAPTIC_DATA.success.strength, HAPTIC_DATA.success.duration, handToHaptic(currentHand));
Controller.triggerHapticPulse(HAPTIC_DATA.success.strength, HAPTIC_DATA.success.duration,
handToHaptic(currentHand));
// don't change state (so animation continues while gripped)
// but do send a notification, by calling the slot that emits the signal for it
Window.makeConnection(true, result.connection.new_connection ? "You and " + result.connection.username + " are now connected!" : result.connection.username);
UserActivityLogger.makeUserConnection(connectingId, true, result.connection.new_connection ? "new connection" : "already connected");
Window.makeConnection(true, result.connection.new_connection ?
"You and " + result.connection.username + " are now connected!" : result.connection.username);
UserActivityLogger.makeUserConnection(connectingId, true, result.connection.new_connection ?
"new connection" : "already connected");
return;
} // failed
endHandshake();
@ -557,7 +573,8 @@ var POLL_INTERVAL_MS = 200, POLL_LIMIT = 5;
function handleConnectionResponseAndMaybeRepeat(error, response) {
// If response is 'pending', set a short timeout to try again.
// If we fail other than pending, set result and immediately call connectionRequestCompleted.
// If we succceed, set result and call connectionRequestCompleted immediately (if we've been polling), and otherwise on a timeout.
// If we succeed, set result and call connectionRequestCompleted immediately (if we've been polling),
// and otherwise on a timeout.
if (response && (response.connection === 'pending')) {
debug(response, 'pollCount', pollCount);
if (pollCount++ >= POLL_LIMIT) { // server will expire, but let's not wait that long.
@ -586,7 +603,7 @@ function handleConnectionResponseAndMaybeRepeat(error, response) {
result = response;
if (pollCount++) {
connectionRequestCompleted();
} else { // Wait for other guy, so that final succcess is at roughly the same time.
} else { // Wait for other guy, so that final success is at roughly the same time.
Script.setTimeout(connectionRequestCompleted, MAKING_CONNECTION_TIMEOUT);
}
}
@ -601,12 +618,12 @@ function makeConnection(id) {
connectionId: id
});
state = STATES.makingConnection;
state = STATES.MAKING_CONNECTION;
// continue the haptic background until the timeout fires. When we make calls, we will have an interval
// probably, in which we do this.
Controller.triggerHapticPulse(HAPTIC_DATA.background.strength, MAKING_CONNECTION_TIMEOUT, handToHaptic(currentHand));
requestBody = {node_id: cleanId(MyAvatar.sessionUUID), proposed_node_id: cleanId(id)}; // for use when repeating
requestBody = {'node_id': cleanId(MyAvatar.sessionUUID), 'proposed_node_id': cleanId(id)}; // for use when repeating
// It would be "simpler" to skip this and just look at the response, but:
// 1. We don't want to bother the metaverse with request that we know will fail.
@ -616,13 +633,13 @@ function makeConnection(id) {
return;
}
// This will immediately set response if successfull (e.g., the other guy got his request in first), or immediate failure,
// and will otherwise poll (using the requestBody we just set).
// This will immediately set response if successful (e.g., the other guy got his request in first),
// or immediate failure, and will otherwise poll (using the requestBody we just set).
request({ //
uri: requestUrl,
method: 'POST',
json: true,
body: {user_connection_request: requestBody}
body: {'user_connection_request': requestBody}
}, handleConnectionResponseAndMaybeRepeat);
}
@ -636,11 +653,15 @@ function startConnecting(id, hand) {
// do we need to do this?
connectingId = id;
connectingHand = hand;
state = STATES.connecting;
state = STATES.CONNECTING;
// play sound
if (!handshakeInjector) {
handshakeInjector = Audio.playSound(handshakeSound, {position: getHandPosition(MyAvatar, currentHand), volume: 0.5, localOnly: true});
handshakeInjector = Audio.playSound(handshakeSound, {
position: getHandPosition(MyAvatar, currentHand),
volume: 0.5,
localOnly: true
});
} else {
handshakeInjector.restart();
}
@ -655,8 +676,9 @@ function startConnecting(id, hand) {
connectingInterval = Script.setInterval(function () {
count += 1;
Controller.triggerHapticPulse(HAPTIC_DATA.background.strength, HAPTIC_DATA.background.duration, handToHaptic(currentHand));
if (state != STATES.connecting) {
Controller.triggerHapticPulse(HAPTIC_DATA.background.strength, HAPTIC_DATA.background.duration,
handToHaptic(currentHand));
if (state !== STATES.CONNECTING) {
debug("stopping connecting interval, state changed");
stopConnecting();
} else if (!isNearby(id, hand)) {
@ -713,7 +735,8 @@ function messageHandler(channel, messageString, senderID) {
break;
case "connectionRequest":
delete waitingList[senderID];
if (state == STATES.waiting && message.id == MyAvatar.sessionUUID && (!connectingId || connectingId == senderID)) {
if (state === STATES.WAITING && message.id === MyAvatar.sessionUUID &&
(!connectingId || connectingId === senderID)) {
// you were waiting for a connection request, so send the ack. Or, you and the other
// guy raced and both send connectionRequests. Handle that too
connectingId = senderID;
@ -723,43 +746,39 @@ function messageHandler(channel, messageString, senderID) {
id: senderID,
hand: handToString(currentHand)
});
} else {
if (state == STATES.waiting && connectingId == senderID) {
} else if (state === STATES.WAITING && connectingId === senderID) {
// the person you are trying to connect sent a request to someone else. See the
// if statement above. So, don't cry, just start the handshake over again
startHandshake();
}
}
break;
case "connectionAck":
delete waitingList[senderID];
if (state == STATES.waiting && (!connectingId || connectingId == senderID)) {
if (message.id == MyAvatar.sessionUUID) {
if (state === STATES.WAITING && (!connectingId || connectingId === senderID)) {
if (message.id === MyAvatar.sessionUUID) {
// start connecting...
connectingId = senderID;
connectingHand = message.hand;
stopWaiting();
startConnecting(senderID, message.hand);
} else {
if (connectingId) {
} else if (connectingId) {
// this is for someone else (we lost race in connectionRequest),
// so lets start over
startHandshake();
}
}
}
// TODO: check to see if we are waiting for this but the person we are connecting sent it to
// someone else, and try again
break;
case "connecting":
delete waitingList[senderID];
if (state == STATES.waiting && senderID == connectingId) {
if (state === STATES.WAITING && senderID === connectingId) {
// temporary logging
if (connectingHand != message.hand) {
if (connectingHand !== message.hand) {
debug("connecting hand", connectingHand, "not same as connecting hand in message", message.hand);
}
connectingHand = message.hand;
if (message.id != MyAvatar.sessionUUID) {
if (message.id !== MyAvatar.sessionUUID) {
// the person we were trying to connect is connecting to someone else
// so try again
startHandshake();
@ -770,7 +789,7 @@ function messageHandler(channel, messageString, senderID) {
break;
case "done":
delete waitingList[senderID];
if (state == STATES.connecting && connectingId == senderID) {
if (state === STATES.CONNECTING && connectingId === senderID) {
// if they are done, and didn't connect us, terminate our
// connecting
if (message.connectionId !== MyAvatar.sessionUUID) {
@ -783,10 +802,10 @@ function messageHandler(channel, messageString, senderID) {
} else {
// if waiting or inactive, lets clear the connecting id. If in makingConnection,
// do nothing
if (state != STATES.makingConnection && connectingId == senderID) {
if (state !== STATES.MAKING_CONNECTION && connectingId === senderID) {
connectingId = undefined;
connectingHand = undefined;
if (state != STATES.inactive) {
if (state !== STATES.INACTIVE) {
startHandshake();
}
}
@ -817,12 +836,14 @@ function makeGripHandler(hand, animate) {
}
function keyPressEvent(event) {
if ((event.text === "x") && !event.isAutoRepeat && !event.isShifted && !event.isMeta && !event.isControl && !event.isAlt) {
if ((event.text === "x") && !event.isAutoRepeat && !event.isShifted && !event.isMeta && !event.isControl &&
!event.isAlt) {
updateTriggers(1.0, true, Controller.Standard.RightHand);
}
}
function keyReleaseEvent(event) {
if ((event.text === "x") && !event.isAutoRepeat && !event.isShifted && !event.isMeta && !event.isControl && !event.isAlt) {
if ((event.text === "x") && !event.isAutoRepeat && !event.isShifted && !event.isMeta && !event.isControl &&
!event.isAlt) {
updateTriggers(0.0, true, Controller.Standard.RightHand);
}
}
@ -835,7 +856,7 @@ connectionMapping.from(Controller.Standard.RightGrip).peek().to(makeGripHandler(
Controller.keyPressEvent.connect(keyPressEvent);
Controller.keyReleaseEvent.connect(keyReleaseEvent);
// xbox controller cuz that's important
// Xbox controller because that is important
connectionMapping.from(Controller.Standard.RB).peek().to(makeGripHandler(Controller.Standard.RightHand, true));
// it is easy to forget this and waste a lot of time for nothing
@ -861,4 +882,3 @@ Script.scriptEnding.connect(function () {
});
}()); // END LOCAL_SCOPE