-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsitServer.js
More file actions
110 lines (89 loc) · 3 KB
/
Copy pathsitServer.js
File metadata and controls
110 lines (89 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// sitServer.js
//
// Created by Robin Wilson 1/17/2019
//
// Copyright 2017 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// sitServer.js is the gate to allow an avatar to sit. Multiple avatars can not sit in the same chair.
//
/* globals Entities Script */
(function() {
var isOccupied;
var entityID = null;
var currentClientSessionID = null;
var HEARTBEAT_INTERVAL_TIME_MS = 10000; // ms
var RESOLVED_TIMEOUT_TIME_MS = 1000; // ms
var resolved = false;
var heartbeatInterval = null;
function SitServer() {
}
function checkClient() {
Entities.callEntityClientMethod(
currentClientSessionID,
entityID,
"check"
);
// If the check call to the client script does not return checkResolved
// Will open the chair to other avatars to sit
Script.setTimeout(function (){
if (resolved === true){
// Seat is occupied
resolved = false;
} else {
// Seat is not occupied
isOccupied = false;
currentClientSessionID = null;
}
}, RESOLVED_TIMEOUT_TIME_MS);
}
SitServer.prototype = {
remotelyCallable: [
"onSitDown",
"onStandUp",
"checkResolved"
],
preload: function (id) {
entityID = id;
isOccupied = false;
resolved = false;
// Every 10 seconds will check the client that was sitting in the chair
heartbeatInterval = Script.setInterval(function () {
if (isOccupied) {
checkClient();
}
}, HEARTBEAT_INTERVAL_TIME_MS);
},
checkResolved: function () {
// Called by remote client script
// indicating avatar is still sitting in chair
resolved = true;
},
// Called from client to check if chair is occupied
// If seat is not occupied, server script calls the client method that begins the sit down process
onSitDown: function (id, param) {
var clientSessionID = param[0];
if (isOccupied === false){
currentClientSessionID = clientSessionID;
isOccupied = true;
Entities.callEntityClientMethod(
clientSessionID,
entityID,
"startSitDown"
);
}
},
// Called from client to open the chair to other avatars
onStandUp: function () {
isOccupied = false;
},
unload: function () {
isOccupied = false;
Script.clearInterval(heartbeatInterval);
}
};
return new SitServer();
});