-
Notifications
You must be signed in to change notification settings - Fork 0
/
wanderer.php
174 lines (163 loc) · 5.63 KB
/
wanderer.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<!--<input id="section-id" type="text" placeholder="Section ID" />-->
<div id="sse">
<a id="getaggregate" type="button" href="#">Get Aggregate!</a>
<br>
<div>
<p id="flag1" style="color:black;font-size:12px;font-weight:bold"></p>
</div>
<div>
<img id="flagstatus1" width="30" height="30" style="background-color:white" >
</div>
<div >
<p id="flag2" style="color:black;font-size:12px;font-weight:bold"></p>
</div>
<div>
<img id="flagstatus2" width="30" height="30" style="background-color:white" >
</div>
<div >
<p id="flag3" style="color:black;font-size:12px;font-weight:bold"></p>
</div>
<div>
<img id="flagstatus3" width="30" height="30" style="background-color:white" >
</div>
</div>
<script type="text/javascript">
if ("WebSocket" in window) {
<?php
$treasurehunt_1_host = isset($_ENV['TREASUREHUNT_1_HOST']) ? $_ENV['TREASUREHUNT_1_HOST'] : 'localhost';
$treasurehunt_1_port = isset($_ENV['TREASUREHUNT_1_PORT']) ? $_ENV['TREASUREHUNT_1_PORT'] : 9000;
$treasurehunt_2_host = isset($_ENV['TREASUREHUNT_2_HOST']) ? $_ENV['TREASUREHUNT_2_HOST'] : 'localhost';
$treasurehunt_2_port = isset($_ENV['TREASUREHUNT_2_PORT']) ? $_ENV['TREASUREHUNT_2_PORT'] : 9003;
echo "const treasurehunt_1_url = 'ws://${treasurehunt_1_host}:${treasurehunt_1_port}';\n";
echo "const treasurehunt_2_url = 'ws://${treasurehunt_2_host}:${treasurehunt_2_port}';\n";
?>
const getAggregate = function () {
// Create a WebSocket.
var ws = new WebSocket(treasurehunt_1_url);
ws.onopen = function()
{
// console.log('ws.onopen');
};
var f1 = false;
var f2 = false;
var f3 = false;
ws.onmessage = function (evt)
{
if (evt.data == "plus10warmer" || evt.data == "plus10colder" || evt.data == "plus10marker" )
{
console.log("Ignoring the server side aggregate broadcast");
}//ifplusten
else
{
var msg = evt.data.split(",");
if(msg.includes("wanderer"))
{
msg[1] = parseInt(msg[1]);
msg[2] = parseInt(msg[2]);
msg[3] = parseInt(msg[3]);
if(msg[1] > msg[2] && msg[1] > msg[3])
{
document.body.style.backgroundColor = "red";
}
else if(msg[2] > msg[1] && msg[2] > msg[3])
{
document.body.style.backgroundColor = "yellow";
}
else if(msg[3] > msg[1] && msg[3] > msg[2])
{
document.body.style.backgroundColor = "lightblue";
}
else {}
} //ifwanderer
else
{
//{"event":"state","game_on":true,"flag1":true,"flag2":true,"flag3":false,"game_off":false}
var page = JSON.parse(evt.data);
console.log(page.game_on,page.flag1,page.flag2,page.flag3,page.game_off,f1);
//identify reset state
if(page.game_on === false && page.game_off === false)
{
f1=false;
}
if(page.game_on===true)
{
if(f1 && page.flag1===true && page.flag2===false && page.flag3===false)
{
document.getElementById('flag1').innerHTML="Marker-1";
document.getElementById('flagstatus1').src="../images/approved.png";
f1=true;
}
if(f1 && page.flag1===false && page.flag2===false && page.flag3===false)
{
document.getElementById('flag1').innerHTML="Marker-1";
document.getElementById('flagstatus1').src="../images/declined.png";
f1=false;
}
if(f1===false){ f1=true;}
else
{
if(f2 &&page.flag2===true && page.flag1===true && page.flag3===false)
{
document.getElementById('flag2').innerHTML="Marker-2";
document.getElementById('flagstatus2').src="../images/approved.png";
f2=true;
}
if( f2 && page.flag2===false && page.flag1===true && page.flag3===false)
{
document.getElementById('flag2').innerHTML="Marker-2";
document.getElementById('flagstatus2').src="../images/declined.png";
f2=false;
}
if(f2===false){ f2=true;}
else
{
if(f3 && page.flag3===true && page.flag2===true && page.flag1===true)
{
document.getElementById('flag3').innerHTML="Marker-3";
document.getElementById('flagstatus3').src="../images/approved.png";
f3=true;
}
if(f3 && page.flag3===false && page.flag2===true && page.flag1===true)
{
document.getElementById('flag3').innerHTML="Marker-3";
document.getElementById('flagstatus3').src="../images/declined.png";
f3=false;
}
if(f3===false){ f3=true;}
}//f2
}//f1
}//ifgameon
if(page.game_off)
{
document.getElementById('flag1').innerHTML="";
document.getElementById('flagstatus1').src="";
document.getElementById('flag2').innerHTML="";
document.getElementById('flagstatus2').src="";
document.getElementById('flag3').innerHTML="";
document.getElementById('flagstatus3').src="";
}//ifgameoff
} //elsewanderer
} //elseplusten
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
};
const getAggregateBtn = document.getElementById('getaggregate');
getAggregateBtn.addEventListener('click', getAggregate);
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
</script>
</body>
</html>