Last active
September 6, 2017 06:32
-
-
Save mikolalysenko/34f29a29b1dbdfdaf29916a7460eb9aa to your computer and use it in GitHub Desktop.
Revisions
-
mikolalysenko revised this gist
Sep 6, 2017 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -71,8 +71,6 @@ function clientMain () { } function serverMain () { const server = require('heldb/server')({ model: require('./schema'), net: require('helnet/server')({ -
mikolalysenko revised this gist
Sep 6, 2017 . 1 changed file with 13 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,9 +31,18 @@ function clientMain () { net: require('helnet/client')() }) function randColor () { return `rgb(${Math.random() * 256},${Math.random() * 256},${Math.random() * 256})` } client.start({ ready(err) { const player = client.state.head player.x = 0.5 player.y = 0.5 player.color = randColor() client.state.commit() canvas.addEventListener('mousemove', ({clientX, clientY}) => { player.x = clientX / canvas.width player.y = clientY / canvas.height @@ -43,16 +52,16 @@ function clientMain () { canvas.addEventListener('click', ({clientX, clientY}) => { client.actions.splat({ x: clientX / canvas.width, y: clientY / canvas.height, color: randColor() }) }) function render () { client.peers.forEach((peer) => { const {x, y, color} = peer.state.head context.fillStyle = color context.fillRect(canvas.width * x - 10, canvas.height * y - 10, 20, 20) }) window.requestAnimationFrame(render) } -
mikolalysenko revised this gist
Sep 6, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ const EntitySchema = HelStruct({ color: HelString }) const protocol = { client: { state: EntitySchema }, @@ -76,7 +76,7 @@ function serverMain () { let splatCount = 0 server.start({ event: { spawn (splat, client) { server.state.head[++splatCount] = EntitySchema.clone(splat) server.state.commit() -
mikolalysenko created this gist
Sep 6, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,92 @@ const HelNumber = require('helschema/number') const HelString = require('helschema/string') const HelStruct = require('helschema/struct') const HelDictionary = require('helschema/dictionary') const EntitySchema = HelStruct({ x: HelNumber, y: HelNumber, color: HelString }) const Protocol = { client: { state: EntitySchema }, server: { state: HelDictionary(EntitySchema), events: { splat: EntitySchema } } } function clientMain () { const canvas = document.createElement('canvas') document.body.appendChild(canvas) const context = canvas.getContext('2d') const client = require('heldb/client')({ protocol, net: require('helnet/client')() }) client.start({ ready(err) { const player = client.state.head canvas.addEventListener('mousemove', ({clientX, clientY}) => { player.x = clientX / canvas.width player.y = clientY / canvas.height client.state.commit() }) canvas.addEventListener('click', ({clientX, clientY}) => { client.actions.splat({ x: clientX / canvas.width, y: clientY / canvas.height }) }) function render () { client.peers.forEach((peer) => { const {x, y, color} = peer.state.head // draw player at x/y with color context.fillRect(x - 10, y - 10, 20, 20) }) window.requestAnimationFrame(render) } render() } }) } function serverMain () { const path = require('path') const server = require('heldb/server')({ model: require('./schema'), net: require('helnet/server')({ client: __filename, live: true, debug: true }) }) let splatCount = 0 server.start({ message: { spawn (splat, client) { server.state.head[++splatCount] = EntitySchema.clone(splat) server.state.commit() } } }) } if (process.env.HEL_CLIENT) { clientMain() } else { serverMain() }