wiki-plugin-mech 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/mech.js +84 -7
- package/package.json +1 -1
package/client/mech.js
CHANGED
|
@@ -189,14 +189,17 @@
|
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
function walk_emit ({elem,command,args,state}) {
|
|
192
|
-
const steps =
|
|
192
|
+
const steps = walks(state.neighborhood)
|
|
193
|
+
const aspects = steps.filter(({graph})=>graph)
|
|
193
194
|
if(state.debug) console.log({steps})
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
elem.innerHTML = command
|
|
196
|
+
const nodes = aspects.map(({graph}) => graph.nodes).flat()
|
|
197
|
+
elem.innerHTML += ` ⇒ ${aspects.length} aspects, ${nodes.length} nodes`
|
|
198
|
+
if(steps.find(({graph}) => !graph)) trouble(elem,"WALK skipped sites with no links in sitemaps")
|
|
196
199
|
const item = elem.closest('.item')
|
|
197
200
|
item.classList.add('aspect-source')
|
|
198
|
-
item.aspectData = () =>
|
|
199
|
-
state.aspect = [{div:item,result:
|
|
201
|
+
item.aspectData = () => aspects
|
|
202
|
+
state.aspect = [{div:item,result:aspects}]
|
|
200
203
|
}
|
|
201
204
|
|
|
202
205
|
function tick_emit ({elem,args,body,state}) {
|
|
@@ -216,7 +219,7 @@
|
|
|
216
219
|
run(body,state)
|
|
217
220
|
clock = setInterval(()=>{
|
|
218
221
|
if(state.debug) console.log({tick:state.tick})
|
|
219
|
-
if(--state.tick > 0)
|
|
222
|
+
if(('tick' in state) && --state.tick > 0)
|
|
220
223
|
run(body,state)
|
|
221
224
|
else
|
|
222
225
|
clock = clearInterval(clock)
|
|
@@ -225,6 +228,38 @@
|
|
|
225
228
|
})
|
|
226
229
|
}
|
|
227
230
|
|
|
231
|
+
function until_emit ({elem,command,args,body,state}) {
|
|
232
|
+
if(!args.length) return trouble(elem,`UNTIL expects an argument, a word to stop running.`)
|
|
233
|
+
if(!state.tick) return trouble(elem,`UNTIL expects to indented below an iterator, like TICKS.`)
|
|
234
|
+
if(!state.aspect) return trouble(elem,`UNTIL expects "aspect", like from WALK.`)
|
|
235
|
+
elem.innerHTML = command + ` ⇒ ${state.tick}`
|
|
236
|
+
const word = args[0]
|
|
237
|
+
for(const {div,result} of state.aspect)
|
|
238
|
+
for(const {name,graph} of result)
|
|
239
|
+
for(const node of graph.nodes)
|
|
240
|
+
if(node.type.includes(word) || node.props.name.includes(word)) {
|
|
241
|
+
if(state.debug) console.log({div,result,name,graph,node})
|
|
242
|
+
delete state.tick
|
|
243
|
+
elem.innerHTML += ' done'
|
|
244
|
+
if (body) run(body,state)
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function forward_emit ({elem,command,args,state}) {
|
|
250
|
+
if(args.length < 1) return trouble(elem,`FORWARD expects an argument, the number of steps to move a "turtle".`)
|
|
251
|
+
if(!('turtle' in state)) state.turtle = new Turtle(elem)
|
|
252
|
+
const position = state.turtle.forward(+args[0])
|
|
253
|
+
elem.innerHTML = command + ` ⇒ ${position.map(n => (n-200).toFixed(1)).join(', ')}`
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function turn_emit ({elem,command,args,state}) {
|
|
257
|
+
if(args.length < 1) return trouble(elem,`TURN expects an argument, the number of degrees to turn a "turtle".`)
|
|
258
|
+
if(!('turtle' in state)) state.turtle = new Turtle(elem)
|
|
259
|
+
const direction = state.turtle.turn(+args[0])
|
|
260
|
+
elem.innerHTML = command + ` ⇒ ${direction}°`
|
|
261
|
+
}
|
|
262
|
+
|
|
228
263
|
const blocks = {
|
|
229
264
|
CLICK: {emit:click_emit},
|
|
230
265
|
HELLO: {emit:hello_emit},
|
|
@@ -235,7 +270,10 @@
|
|
|
235
270
|
PREVIEW: {emit:preview_emit},
|
|
236
271
|
NEIGHBORS:{emit:neighbors_emit},
|
|
237
272
|
WALK: {emit:walk_emit},
|
|
238
|
-
TICK: {emit:tick_emit}
|
|
273
|
+
TICK: {emit:tick_emit},
|
|
274
|
+
UNTIL: {emit:until_emit},
|
|
275
|
+
FORWARD: {emit:forward_emit},
|
|
276
|
+
TURN: {emit:turn_emit}
|
|
239
277
|
}
|
|
240
278
|
|
|
241
279
|
function run (nest,state={}) {
|
|
@@ -399,4 +437,43 @@
|
|
|
399
437
|
|
|
400
438
|
}
|
|
401
439
|
|
|
440
|
+
class Turtle {
|
|
441
|
+
constructor(elem) {
|
|
442
|
+
const size = elem
|
|
443
|
+
const div = document.createElement('div')
|
|
444
|
+
elem.closest('.item').firstElementChild.prepend(div)
|
|
445
|
+
div.outerHTML = `
|
|
446
|
+
<div style="border:1px solid black; background-color:#f8f8f8; margin-bottom:16px;">
|
|
447
|
+
<svg viewBox="0 0 400 400" width=100% height=400>
|
|
448
|
+
<circle id=dot r=5 cx=200 cy=200 stroke="#ccc"></circle>
|
|
449
|
+
</svg>
|
|
450
|
+
</div>`
|
|
451
|
+
this.svg = elem.closest('.item').getElementsByTagName('svg')[0]
|
|
452
|
+
this.position = [200,200]
|
|
453
|
+
this.direction = 0
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
forward(steps) {
|
|
457
|
+
const theta = this.direction*2*Math.PI/360
|
|
458
|
+
const [x1,y1] = this.position
|
|
459
|
+
const [x2,y2] = [x1+steps*Math.sin(theta), y1+steps*Math.cos(theta)]
|
|
460
|
+
const line = document.createElementNS("http://www.w3.org/2000/svg", 'line')
|
|
461
|
+
const set = (k,v) => line.setAttribute(k,Math.round(v))
|
|
462
|
+
set("x1",x1); set("y1",400-y1)
|
|
463
|
+
set("x2",x2); set("y2",400-y2)
|
|
464
|
+
line.style.stroke = "black"
|
|
465
|
+
line.style.strokeWidth = "2px"
|
|
466
|
+
this.svg.appendChild(line)
|
|
467
|
+
const dot = this.svg.getElementById('dot')
|
|
468
|
+
dot.setAttribute('cx',Math.round(x2))
|
|
469
|
+
dot.setAttribute('cy',Math.round(400-y2))
|
|
470
|
+
this.position = [x2,y2]
|
|
471
|
+
return this.position
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
turn(degrees) {
|
|
475
|
+
this.direction += degrees
|
|
476
|
+
return this.direction}
|
|
477
|
+
}
|
|
478
|
+
|
|
402
479
|
}).call(this)
|