-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOriginalTwineStoryJavascript.js
104 lines (81 loc) · 2.3 KB
/
OriginalTwineStoryJavascript.js
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
// via https://furkleindustries.com/fictions/twine/twine2_resources/twine2_macros/
var _state = State;
window._state = _state;
function getHarloweVariable(prop) {
if (typeof(prop) === typeof(undefined) ||
prop === '') {
return;
}
return prop[0] === '$' ?
_state.variables[prop.slice(1, prop.length)] : _state.variables[prop];
}
window.getHarloweVariable = getHarloweVariable;
function setHarloweVariable(prop, val) {
if (typeof(prop) === typeof(undefined) ||
prop === '' ||
typeof(val) === typeof(undefined)) {
return;
}
if (prop[0] === '$') {
prop = prop.slice(1, prop.length);
}
_state.variables[prop] = val;
}
window.setHarloweVariable = setHarloweVariable;
// get lat,long
var lat = 0, long = 0;
function storeLatLong(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
}
navigator.geolocation.getCurrentPosition(storeLatLong);
/*
// do temperature stuff
function updateTemperature() {
if( (lat!==0) && (long!==0) ) {
var call = "http://api.openweathermap.org/data/2.5/weather?lat="+String(lat)+"&lon="+String(long)+"&appid=b99ed57bf10a3ea8e76987423fd198e2&units=metric";
var req = XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == XMLHttpRequest.DONE ) {
if (req.status == 200) {
console.log(req);
var data = JSON.parse(req.responseText);
// via https://openweathermap.org/current
setTemperature(data.main.temp);
}
else if (xmlhttp.status == 400) {
console.log('There was an error 400');
}
else {
console.log('something else other than 200 was returned');
}
}
};
req.open("GET", call, true);
req.send();
} else {
setTemperature(-100);
}
}
function setTemperature(t) {
window.setHarloweVariable(
"temperature",t?t:-200 //Math.round((Math.random()*40)-10)
);
}
var timerHandle = setInterval(function(){
updateTemperature();
}, (15*60*1000 ) );
updateTemperature(); // initial ajax call
setTemperature(); // set a default while waiting
*/
var socket = new WebSocket("ws://"+window.location.hostname+":"+window.location.port+"/ws/game");
socket.onopen = function() {
socket.send("init");
}
socket.onmessage = function (event) {
console.log(event);
var msg = JSON.parse(event.data);
for (var key in msg) {
window.setHarloweVariable( key, msg[key] );
}
}