Skip to content

Commit 0c5c729

Browse files
author
Max Carlson
committed
Add presence tracking for selector and navigator roles
1 parent 8e453cc commit 0c5c729

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

WebSites/spacetime/index.html

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@
66
<title>Spacecraft in Unity</title>
77
<link rel="icon" href="https://brand.publiccode.net/logo/mark-128w128h.png">
88
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js"></script>
9+
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js"></script>
910
<script src="qrcode.min.js"></script>
1011
<script>
1112
const qrcodeDefaults = {dim: 100, pad: 1, pal: ['#000','#fff']}
12-
const getQrcodeLink = (url) => {
13+
const getQrcodeLink = (url, id) => {
1314
const el = document.createElement('a');
1415
el.href = url;
1516
el.target = "_new";
1617
el.className = "qrcode";
18+
el.id = id;
1719
// pass {msg: 'http://...'} and/or overrides for qrcodeDefaults
1820
const svg = QRCode( {...qrcodeDefaults, msg: url})
1921
el.appendChild(svg);
22+
const label = document.createElement('div');
23+
label.innerText = id;
24+
label.className = 'label';
25+
el.appendChild(label);
2026
return el;
2127
}
2228
</script>
2329
<style>
2430
body {
31+
font-family: "Mulish", Helvetica;
2532
text-align: right;
2633
padding: 0;
2734
border: 0;
@@ -37,6 +44,13 @@
3744
width: 100px;
3845
height: 100px;
3946
padding: 10px;
47+
opacity: .60;
48+
transition: opacity 1s;
49+
}
50+
.label {
51+
color: white;
52+
margin-top: 0;
53+
margin-bottom: 50px;
4054
}
4155
</style>
4256
</head>
@@ -60,20 +74,27 @@
6074

6175
// handlers to send events into Unity
6276
function handlePos(payload) {
63-
console.log('handlePos', payload)
77+
console.debug('handlePos', payload)
6478
}
6579

6680
function handleZoom(zoom) {
67-
console.log('handleZoom', zoom)
81+
console.debug('handleZoom', zoom)
82+
}
83+
84+
function handlePresence(id, payload) {
85+
const el = document.getElementById(id);
86+
console.debug('handlePresence', id, payload, el)
87+
userCount = Object.keys(payload).length
88+
el.style.opacity = userCount ? 1 : '';
6889
}
6990

7091
function handleSelectPos(payload) {
71-
console.log('handleSelectPos', payload)
92+
console.debug('handleSelectPos', payload)
7293
}
7394

7495
// handlers to send events into Unity
7596
function handleSelectZoom(payload) {
76-
console.log('handleSelectZoom', payload)
97+
console.debug('handleSelectZoom', payload)
7798
}
7899

79100
// Subscribe to the navigatorChannel
@@ -94,12 +115,16 @@
94115
{ event: 'zoom' },
95116
(data) => handleZoom(data.payload.zoom)
96117
)
118+
.on('presence', { event: 'sync' }, () => {
119+
const newState = navigatorChannel.presenceState()
120+
handlePresence('navigator', newState)
121+
})
97122

98123
const selectorChannel = client.channel("selectors")
99124
// Subscribe to the selectorChannel
100125
selectorChannel
101126
.subscribe((status) => {
102-
console.log('subscribe', status)
127+
console.debug('subscribe', status)
103128
if (status === 'SUBSCRIBED') {
104129
subscribed = true;
105130
}
@@ -114,6 +139,10 @@
114139
{ event: 'zoom' },
115140
(data) => handleSelectZoom(data.payload.zoom)
116141
)
142+
.on('presence', { event: 'sync' }, () => {
143+
const newState = selectorChannel.presenceState()
144+
handlePresence('selector', newState)
145+
})
117146
</script>
118147
<canvas id="unity-canvas" width=960 height=600 tabindex="-1" style="width: 100%; height: 100%; position: fixed; top: 0; left: 0; background: #1F1F20"></canvas>
119148
<script src="Build/CraftSpace.loader.js"></script>
@@ -148,8 +177,8 @@
148177
</script>
149178
<script>
150179
// add navigator and selector qr codes
151-
document.body.appendChild( getQrcodeLink( window.location + 'navigator.html') );
152-
document.body.appendChild( getQrcodeLink( window.location + 'selector.html') );
180+
document.body.appendChild( getQrcodeLink( window.location + 'navigator.html', 'navigator') );
181+
document.body.appendChild( getQrcodeLink( window.location + 'selector.html', 'selector') );
153182
</script>
154183
</body>
155184
</html>

WebSites/spacetime/navigator.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@
4141

4242
const channel = client.channel("navigators")
4343

44+
var isMobile = navigator.userAgent.toLowerCase().match(/mobile/i),
45+
isTablet = navigator.userAgent.toLowerCase().match(/tablet/i),
46+
isAndroid = navigator.userAgent.toLowerCase().match(/android/i),
47+
isiPhone = navigator.userAgent.toLowerCase().match(/iphone/i),
48+
isiPad = navigator.userAgent.toLowerCase().match(/ipad/i);
49+
50+
let user = isAndroid ? 'Android' : isiPhone ? 'iPhone' : isAndroid ? 'Android' : isTablet ? 'Tablet' : isMobile ? 'Mobile' : 'Computer';
51+
52+
let userStatus = {
53+
user,
54+
online_at: new Date().toISOString()
55+
};
56+
4457
let subscribed = false;
4558
channel.subscribe((status) => {
4659
console.log('subscribe', status)
4760
if (status === 'SUBSCRIBED') {
61+
channel.track(userStatus);
4862
const el = document.getElementById("label");
4963
el.style.opacity = '';
5064
subscribed = true;

WebSites/spacetime/selector.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@
4141

4242
const channel = client.channel("selectors")
4343

44+
var isMobile = navigator.userAgent.toLowerCase().match(/mobile/i),
45+
isTablet = navigator.userAgent.toLowerCase().match(/tablet/i),
46+
isAndroid = navigator.userAgent.toLowerCase().match(/android/i),
47+
isiPhone = navigator.userAgent.toLowerCase().match(/iphone/i),
48+
isiPad = navigator.userAgent.toLowerCase().match(/ipad/i);
49+
50+
let user = isAndroid ? 'Android' : isiPhone ? 'iPhone' : isAndroid ? 'Android' : isTablet ? 'Tablet' : isMobile ? 'Mobile' : 'Computer'
51+
52+
let userStatus = {
53+
user,
54+
online_at: new Date().toISOString()
55+
};
56+
4457
let subscribed = false;
4558
channel.subscribe((status) => {
4659
console.log('subscribe', status)
4760
if (status === 'SUBSCRIBED') {
61+
channel.track(userStatus);
4862
const el = document.getElementById("label");
4963
el.style.opacity = '';
5064
subscribed = true;

0 commit comments

Comments
 (0)