-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.html
More file actions
322 lines (279 loc) · 10.5 KB
/
Copy pathclient.html
File metadata and controls
322 lines (279 loc) · 10.5 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>iV is your friend</title>
<script type="text/javascript" src="/daimio_composite.js"></script>
<script type="text/javascript" src="/public/js/markdown.js"></script>
<link href="/public/css/bootstrap.css" rel="stylesheet">
<link href="/public/css/styles.css" rel="stylesheet">
</head>
<body style="display:none" id="game">
<div style="position:relative;">
<div id="rooms" data-daimio-template="build-room-display">
{begin video_template | >$video-template | ""}
<video id="{_key}" class="fullscreen hidden" style="" loop="true" width="{$game.width}" height="{$game.height}">
<source src="{_value.urls.control}" type="video/webm">
Your browser does not support the video tag.
</video>
{end video_template}
{begin image_template | >$image-template | ""}
<div id="{_key}"
class="fullscreen hidden gamespace"
style="background-image: url({_value.urls.control | tap});
height: {$game.height}px;
width: {$game.width}px; "></div>
{end image_template}
{begin text_template | >$text-template | ""}
<div id="{_key}"
class="fullscreen text hidden gamespace"
style="background-image: url({_value.style});
height: {$game.height}px;
width: {$game.width}px; ">
<div class="game-text">
{_value.text | string markdown | string iv_parser rooms $game.rooms | unquote | run}
</div>
</div>
{end text_template}
{begin selection | each data $game.rooms}
{ _value.urls.control
| string slice start -3 | >extension
| cond (
{_value.type | eq :text} $text-template
{_extension | is in (:ebm :m4v :mp4 :mov)} $video-template
{:otherwise } $image-template
)
| run with {* (:key _key :value _value)} }
{end selection}
<div id="music" class="music">
{begin music}
<audio id="game-audio" src="{$game.music}" loop autoplay>
Your browser does not support the audio element.
</audio>
{end music}
</div>
</div> <!-- TODO: examine this totally freaky webkit whitespace issue -->
<div id="divs" data-daimio-template="show-divs">
{begin divs | merge data {$game.rooms.{$room-id}.exits} | if {$game.rooms.{$room-id}.type | eq :text} then "" else __}
<div class="link" data-value="{_room}"
style="top: {_top | or 0}px; left: {_left | or 0}px;
height: {_height | or 100}px; width: {_width | or 100}px;
position: absolute; background-color:green; opacity: {$game.debug | then 0.4 else 0}"></div>
{end divs}
{begin globaldivs | merge data $game.exits}
<div class="link" data-value="{_room}"
style="top: {_top | or 0}px; left: {_left | or 0}px;
height: {_height | or 100}px; width: {_width | or 100}px;
position: absolute; background-color:green; opacity: {$game.debug | then 0.4 else 0}"></div>
{end globaldivs}
{begin music}
<div id="room-music">
<input type="hidden" id="room-music-value" value="{if {$game.rooms.{$room-id}.urls.music} then {$game.rooms.{$room-id}.urls.music} else $game.music}"/>
</div>
{end music}
</div>
</div>
<script id="control" type="text/daml" class="spaceseeds">
outer
$game {}
$room-id 0
// GET DATA
reader {"gamedata/{__in}" | run with __in | external server-read url | list from-json}
@init from-js
@server-read
@init -> reader -> store-game
reader.server-read* -> @server-read
// DISPLAY
change-room {__ | >$room-id}
store-game {__ | >$game | __.rooms | list keys | __.#1}
build-room-display
show-divs
@set-divs dom-set-html divs
@set-room dom-set-html rooms
// @set-music dom-set-html music
@room-play
store-game -> build-room-display -> @set-room
store-game -> change-room -> show-divs -> @set-divs
change-room -> @room-play
// INPUTS
@link dom-on-click .link
@link -> change-room
@autoswitch from-js
@autoswitch -> change-room
</script>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
D.import_port_flavour('room-play', {
//// A port for controlling rooms
dir: 'out',
outside_exit: function(ship) {
try {
// first just pause everything
;[].slice.call(document.getElementsByTagName('video'))
.forEach(function(item) {item.pause()})
var cur = document.getElementsByClassName('visible')[0]
var nxt = document.getElementById(ship)
if(cur && cur.classList) {
cur.classList.add('hidden')
cur.classList.remove('visible') // for use with selectors
if(cur.pause)
cur.pause()
}
nxt.classList.add('visible')
nxt.classList.remove('hidden')
if(nxt.currentTime)
nxt.currentTime = 0
if(nxt.play)
nxt.play()
// switch the music if necessary
var audio = document.getElementById('game-audio')
var roomMusic = OuterSpace.state.game.rooms[ship].urls.music
var gameMusic = OuterSpace.state.game.music
if(roomMusic && audio.src != roomMusic) {
audio.src=roomMusic
audio.load()
}
else if (!roomMusic && audio.src != gameMusic) {
audio.src=gameMusic
audio.load()
}
// HACKYHACKHACK
var room = OuterSpace.state.game.rooms[ship]
if(room.type == 'text') {
// console.log('argh')
var roomdiv = document.getElementById(ship)
var dstring = '{$text-template | run with {* (:key _key :value _value)}}'
var scope = {key: ship, value: room}
D.run(dstring, OuterSpace, scope, function(value) {
// console.log(value, dstring)
roomdiv.outerHTML = value
roomdiv = document.getElementById(ship)
roomdiv.classList.add('visible')
roomdiv.classList.remove('hidden')
})
}
} catch(e) {}
}
})
document.addEventListener('DOMContentLoaded', function() {
//// Do all this on load.
// D.Etc.socket = io.connect('/') // Leave this, it's for multiplayer
OuterSpace = D.make_me_a_space_as_fast_as_you_can()
// activate init port
var game_id = window.location.pathname.replace(/\/$/, '').split('/')[1]
D.send_value_to_js_port(OuterSpace, 'init', game_id)
setTimeout(function() { D.send_value_to_js_port(OuterSpace, 'autoswitch', 0) }, 300)
})
// touchy feely stuff. http://stackoverflow.com/questions/5186441/javascript-drag-and-drop-for-touch-devices
function touchHandler(event) {
var first = event.changedTouches[0]
, ttype = event.type
, types = [].concat(ttype == 'touchstart' ? ['mousedown'] : [])
.concat(ttype == 'touchmove' ? ['mousemove'] : [])
.concat(ttype == 'touchend' ? ['mouseup', 'click'] : [])
types.forEach(function(type) {
var simulatedEvent = document.createEvent("MouseEvent")
simulatedEvent.initMouseEvent( type, true, true, window, 1
, first.screenX, first.screenY
, first.clientX, first.clientY
, false, false, false, false, 0, null)
first.target.dispatchEvent(simulatedEvent)
})
//if(dragging) event.preventDefault()
}
~function init() {
document.addEventListener("touchend", touchHandler, true)
document.addEventListener("touchmove", touchHandler, true)
document.addEventListener("touchstart", touchHandler, true)
document.addEventListener("touchcancel", touchHandler, true)
}()
D.import_models({
string: {
methods: {
markdown: {
desc: 'Convert markdown to html',
params: [
{
key: 'value',
desc: 'A markdown string',
type: 'string',
required: true
},
],
fun: function(value) {
return markdown.toHTML(value)
}
},
iv_parser: {
desc: 'Look for special iV flags',
params: [
{
key: 'value',
desc: 'A string',
type: 'string',
required: true
},
{
key: 'rooms',
desc: 'List of rooms in game',
type: 'list',
required: true
}
],
fun: function(value, rooms) {
var roomnames = {}
for (var key in rooms) {
roomnames[rooms[key].name] = key
}
return value.replace( /\[\[(.*?):(.*?)\]\]/g
, function (match, p1, p2) {
return '<a href="" data-value="' + roomnames[p1]
+ '" class="link ham">' + p2 + '</a>' })
}
}
}
}
})
D.import_models({
external: {
desc: 'Commands that reach outside our universe',
methods: {
"server-read": {
desc: 'Get JSON over an XHR connection to the server',
port: 'server-read',
params: [
{
key: 'url',
desc: 'A url on the server',
type: 'string',
required: true
},
],
fun: function(url, port, prior_starter) {
port.sync(url, prior_starter)
return NaN
}
}
}
}
})
D.import_port_flavour('server-read', {
dir: 'down',
outside_exit: function(ship, callback) {
xhr_get(ship, callback)
}
})
// load daimio file
function xhr_get(target, callback) {
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
callback(xhr.responseText)
}
}
xhr.open('GET', target, true)
xhr.send(null)
}
</script>
</body>
</html>