Skip to content

Commit

Permalink
Fixed server & client data consistency; maze at 15 levels
Browse files Browse the repository at this point in the history
- The gkEventSystem now calls app.update( before dispatching an event. Clients always run an update before handing networked events. App.update now accepts a boolean that, when true, will not trigger a loop/network update.
- Maze has the first 15 levels. I do not plan to do any more.
- Cleaned up some console statements.
  • Loading branch information
voces committed Nov 18, 2017
1 parent b5ae0cf commit 969595e
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 56 deletions.
224 changes: 188 additions & 36 deletions examples/games/maze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ function start() {

cleanup();

app.state.levelIndex = ( app.state.levelIndex + 1 ) % levels.length;
// app.state.levelIndex = Math.floor( app.random() * levels.length );
app.state.levelIndex = Math.floor( app.random() * levels.length );

const level = levels[ app.state.levelIndex ];
if ( level.checkpoints === undefined ) calculateCheckpoints();
Expand Down Expand Up @@ -128,6 +127,10 @@ function start() {

}

if ( level.stills )
for ( let i = 0; i < level.stills.length; i ++ )
new app.Enemy( Object.assign( { z: 1 }, base, offset( level.stills[ i ] ) ) );

if ( level.patrols )
for ( let i = 0; i < level.patrols.length; i ++ ) {

Expand Down Expand Up @@ -245,6 +248,7 @@ function tick( time ) {
if ( typeof level.tick === "function" ) level.tick( time, delta );

}
tick.count = 0;

function point( players ) {

Expand Down Expand Up @@ -685,30 +689,28 @@ const levels = [
"███████████"
],
patrols: [
...[
{ x: - 2, y: 2, dX: 1 },
{ x: - 1, y: 1, dX: - 1 },
{ x: - 2, y: - 0, dX: 1 },
{ x: - 1, y: - 1, dX: - 1 },
{ x: - 3, y: - 1, dX: - 1 },
{ x: - 4, y: - 2, dX: 1 },
{ x: - 3, y: - 3, dX: - 1 },
{ x: - 2, y: - 4, dY: 1 },
{ x: - 1, y: - 3, dY: - 1 },
{ x: 0, y: - 4, dY: 1 },
{ x: 1, y: - 3, dY: - 1 },
{ x: 2, y: - 4, dY: 1 },
{ x: 3, y: - 3, dX: 1 },
{ x: 4, y: - 2, dX: - 1 },
{ x: 3, y: - 1, dX: 1 },
{ x: 1, y: - 1, dX: 1 },
{ x: 2, y: 0, dX: - 1 },
{ x: 1, y: 1, dX: 1 },
{ x: 2, y: 2, dX: - 1 }
].map( p => [
{ x: p.x, y: p.y },
{ x: p.x + ( p.dX || 0 ), y: p.y + ( p.dY || 0 ) } ] )
]
{ x: - 2, y: 2, dX: 1 },
{ x: - 1, y: 1, dX: - 1 },
{ x: - 2, y: - 0, dX: 1 },
{ x: - 1, y: - 1, dX: - 1 },
{ x: - 3, y: - 1, dX: - 1 },
{ x: - 4, y: - 2, dX: 1 },
{ x: - 3, y: - 3, dX: - 1 },
{ x: - 2, y: - 4, dY: 1 },
{ x: - 1, y: - 3, dY: - 1 },
{ x: 0, y: - 4, dY: 1 },
{ x: 1, y: - 3, dY: - 1 },
{ x: 2, y: - 4, dY: 1 },
{ x: 3, y: - 3, dX: 1 },
{ x: 4, y: - 2, dX: - 1 },
{ x: 3, y: - 1, dX: 1 },
{ x: 1, y: - 1, dX: 1 },
{ x: 2, y: 0, dX: - 1 },
{ x: 1, y: 1, dX: 1 },
{ x: 2, y: 2, dX: - 1 }
].map( p => [
{ x: p.x, y: p.y },
{ x: p.x + ( p.dX || 0 ), y: p.y + ( p.dY || 0 ) } ] )
},

// Level 11
Expand Down Expand Up @@ -739,14 +741,14 @@ const levels = [
const circles = [];
for ( let v = 0.75; v < 5; v += 0.667 )
circles.push(
{ x: 0, y: 0, duration: 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: - 0.25, y: v } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: - 0.25, y: v } ) },
{ x: 0, y: 0, duration: 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: 0.25, y: v } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: 0.25, y: v } ) },
{ x: 0, y: 0, duration: 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: v, y: - 0.25 } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: v, y: - 0.25 } ) },
{ x: 0, y: 0, duration: 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: v, y: 0.25 } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: v, y: 0.25 } ) },
{ x: 0, y: 0, duration: 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: - 0.25, y: - v } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: - 0.25, y: - v } ) },
{ x: 0, y: 0, duration: 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: 0.25, y: - v } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: 0.25, y: - v } ) },
{ x: 0, y: 0, duration: 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: - v, y: - 0.25 } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: - v, y: - 0.25 } ) },
{ x: 0, y: 0, duration: 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: - v, y: 0.25 } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: - v, y: 0.25 } ) } );
{ x: 0, y: 0, duration: - 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: - 0.25, y: v } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: - 0.25, y: v } ) },
{ x: 0, y: 0, duration: - 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: 0.25, y: v } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: 0.25, y: v } ) },
{ x: 0, y: 0, duration: - 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: v, y: - 0.25 } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: v, y: - 0.25 } ) },
{ x: 0, y: 0, duration: - 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: v, y: 0.25 } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: v, y: 0.25 } ) },
{ x: 0, y: 0, duration: - 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: - 0.25, y: - v } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: - 0.25, y: - v } ) },
{ x: 0, y: 0, duration: - 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: 0.25, y: - v } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: 0.25, y: - v } ) },
{ x: 0, y: 0, duration: - 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: - v, y: - 0.25 } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: - v, y: - 0.25 } ) },
{ x: 0, y: 0, duration: - 4, offset: angleBetweenPoints( { x: 0, y: 0 }, { x: - v, y: 0.25 } ) / Math.PI / 2 * 4, radius: distanceBetweenPoints( { x: 0, y: 0 }, { x: - v, y: 0.25 } ) } );

return circles;

Expand Down Expand Up @@ -780,12 +782,162 @@ const levels = [
delete this.mode;

}

},

// Level 12
{
origin: { x: 0.25, y: 0.25 },
spawn: 2,
score: 1,
speed: 3,
won: 1,
floormap: [
"████████████████████",
"█ █",
"█ ░░ █",
"█ ░░ █",
"█ █",
"█ █",
"█ ░░ ░░ █",
"█ ░░ ░░ █",
"█ █",
"████████████████████"
],
stills: [].concat( ...[
"••••••••••••••••••••••••••••••••••",
"• •• ••••••••••••••",
"• ••••••••••••••",
"• •••••• • ••••••••••••••",
"• ••••••••••••• ••••••••••••••",
"• ••••••• ••••••••••••••",
"• ••• ••••••••••••••",
"••••••••• ••• ••••••••••••••",
"•••••••••••••••••• ••••••••••••••",
"• ••••••• ••••••• •",
"• •• •• •",
"• • •• •",
"• ••• •••••••• •",
"••••••••••••••••••••••••••••••••••"
].map( ( row, y ) => row.split( "" ).map( ( symbol, x ) =>
symbol === "•" ? { x: x / 2 - 8.5, y: - y / 2 + 3 } : null ).filter( still => still ) ) ),
patrols: [
[ { x: - 5, y: - 3.5 }, { x: - 5, y: 3 } ],
[ { x: - 4.5, y: - 3.5 }, { x: - 4.5, y: 3 } ],
[ { x: 0.5, y: - 3.5 }, { x: 0.5, y: 3 } ],
[ { x: 1, y: - 3.5 }, { x: 1, y: 3 } ],
[ { x: - 2, y: 3 }, { x: - 2, y: - 3.5 } ],
[ { x: - 1.5, y: 3 }, { x: - 1.5, y: - 3.5 } ],
[ { x: 4, y: 3 }, { x: 4, y: - 3.5 } ],
[ { x: 4.5, y: 3 }, { x: 4.5, y: - 3.5 } ]
],
food: [ { x: - 1.75, y: - 0.25 } ]
},

// Level 13
{
spawn: 1,
score: 0,
speed: 5,
floormap: [
"████████████",
"█████░░█████",
"█████░░█████",
"█ █",
"█ █",
"█ █",
"█ █",
"█ █",
"█ █",
"█████░░█████",
"█████░░█████",
"████████████"
],
patrols: [
...Array( 5 ).fill( 0 ).map( ( v, i ) => ( [ { x: i * 2 - 4.5, y: 2.5 }, { x: i * 2 - 4.5, y: - 2.5 } ] ) ),
...Array( 5 ).fill( 0 ).map( ( v, i ) => ( [ { x: i * 2 - 3.5, y: - 2.5 }, { x: i * 2 - 3.5, y: 2.5 } ] ) ),
[ { x: - 4.5, y: 0.5 }, { x: 4.5, y: 0.5 } ],
[ { x: 4.5, y: - 0.5 }, { x: - 4.5, y: - 0.5 } ]
]
},

// Level 14
{
spawn: 1,
score: 0,
speed: 2,
floormap: [
"████████████████████",
"████████████████░░░█",
"████████████████░░░█",
"████████████████░░░█",
"█░░░ █",
"█░░░ █",
"█░░░ █",
"████████████████████"
],
patrols: [
...[ - 4.5, - 0.5, 3.5, 7.5 ].map( x => [ { x, y: - 1.5 } ] ),
[ { x: - 2.5, y: - 0.25 }, { x: - 2.5, y: - 2.25 } ],
[ { x: - 2.5, y: - 0.75 }, { x: - 2.5, y: - 2.75 } ],
[ { x: 5.5, y: - 0.25 }, { x: 5.5, y: - 2.25 } ],
[ { x: 5.5, y: - 0.75 }, { x: 5.5, y: - 2.75 } ],
[ { x: 1.5, y: - 2.25 }, { x: 1.5, y: - 0.25 } ],
[ { x: 1.5, y: - 2.75 }, { x: 1.5, y: - 0.75 } ]
],
circles: [].concat( ...[ - 4.5, - 0.5, 3.5, 7.5 ].map( x => [].concat( ...[ 0, 0.25, 0.5, 0.75 ].map( arm => [ 0.72, 1.44 ].map( radius => ( {
x, y: - 1.5, radius, duration: - 3, offset: 3 * arm
} ) ) ) ) ) )
},

// Level 15
{
origin: { x: - 0.5, y: 0.5 },
spawn: 0,
score: 1,
speed: 6,
floormap: [
"██████████████████████",
"█░░░█ █ █",
"█░░░█ █ █",
"█ ███ █ █",
"█ █ █ █ ██ █",
"█ █ █ █ ██ █",
"█ █ █ █ ██ █",
"█ █ █ █ ██ █",
"█ █ ████ █",
"█ █ ██░░░█",
"█ █ ██░░░█",
"██████████████████████"
],
patrols: [
{ x: - 9, y: 1, dY: - 6 },
{ x: - 8, y: - 5, dY: 6 },
{ x: - 7, y: 1, dY: - 6 },
{ x: - 6, y: - 3, dY: - 2 },
{ x: - 5, y: 4, dY: - 9 },
{ x: - 4, y: - 5, dY: 9 },
{ x: - 3, y: 4, dY: - 9 },
{ x: - 2, y: 4, dY: - 2 },
{ x: - 1, y: 4, dY: - 9 },
{ x: 0, y: - 5, dY: 9 },
{ x: 1, y: 4, dY: - 9 },
{ x: 2, y: - 3, dY: - 2 },
{ x: 3, y: 4, dY: - 9 },
{ x: 4, y: - 5, dY: 9 },
{ x: 5, y: 4, dY: - 9 },
{ x: 6, y: 4, dY: - 2 },
{ x: 7, y: 2, dY: 2 },
{ x: 8, y: - 2, dY: 6 },
{ x: 9, y: 4, dY: - 6 },
{ x: 10, y: - 2, dY: 6 }
].map( p => [
{ x: p.x, y: p.y },
{ x: p.x + ( p.dX || 0 ), y: p.y + ( p.dY || 0 ) } ] )
}

];

app.state.levelIndex = levels.length - 2;

for ( let i = 0; i < levels.length; i ++ ) {

levels[ i ].height = levels[ i ].floormap.length;
Expand Down
4 changes: 2 additions & 2 deletions src/core/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class App extends EventDispatcher {

}

update() {
update( consistencyUpdate = false ) {

if ( env.isServer ) {

Expand Down Expand Up @@ -519,7 +519,7 @@ class App extends EventDispatcher {

}

if ( env.isServer ) {
if ( env.isServer && ! consistencyUpdate ) {

this.network.send( this.time );
setTimeout( () => this.update(), 25 );
Expand Down
3 changes: 0 additions & 3 deletions src/core/EventDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ class EventDispatcher {

if ( typeof type !== "string" ) {

// console.warn( "dispatchEvent( <object> ) has been deprecated, please use dispatchEvent( <string>, <object> )" );
console.trace( "dispatchEvent( <object> ) has been deprecated, please use dispatchEvent( <string>, <object> )" );

args.unshift( event );
event = type;
type = type.type;
Expand Down
1 change: 0 additions & 1 deletion src/misc/Rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ class Rect extends EventDispatcher {

if ( ! this.area ) return;

// console.log( this.candidateUnits );
const units = this.candidateUnits.filter( unit => this.contains( unit ) ).sort( ( a, b ) => a.id > b.id );

const [ enters, leaves ] = diff( units, this.units, "id" );
Expand Down
2 changes: 0 additions & 2 deletions src/misc/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const defaultReplacer = ( prop, value ) => value;

function stringify( value, replacer = defaultReplacer, toJSON = "toJSON", memory = new Set() ) {

// console.log( "stringify", memory.size, typeof value, value instanceof Handle, value.toString().slice( 0, 100 ) );

if ( value == null ) return "null";
if ( typeof value === "number" ) return replacer( undefined, isFinite( value ) ? value.toString() : "null" );
if ( typeof value === "boolean" ) return replacer( undefined, value.toString() );
Expand Down
5 changes: 5 additions & 0 deletions src/presets/events/gkEventSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function clientJoinHandler( app, e ) {
local: player.toJSON()
}, "toState" );

app.update( true );
app.dispatchEvent( "playerJoin", { player } );

}
Expand All @@ -46,6 +47,8 @@ function clientLeaveHandler( app, e ) {
const player = app.players.dict[ "p" + e.client.id ];

app.network.send( { type: "playerLeave", player } );

app.update( true );
app.dispatchEvent( "playerLeave", { player } );

}
Expand All @@ -70,6 +73,8 @@ function clientMessageHandler( app, e ) {
e.message.time = app.time;

app.network.send( e.message );

app.update( true );
app.dispatchEvent( e.message.type, e.message );

}
Expand Down
2 changes: 0 additions & 2 deletions src/presets/networks/GenericClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class GenericClient extends EventDispatcher {

this.socket.addEventListener( "message", e => {

// if ( isNaN( e.data ) ) console.log( JSON.parse( e.data ) );

e = JSON.parse( e.data, this.reviver );

if ( typeof e === "number" ) {
Expand Down
10 changes: 0 additions & 10 deletions src/presets/networks/GenericServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ class GenericServer extends EventDispatcher {
this.clients = props.clients || new Collection();
this.ws = props.ws && props.ws.constructor !== Object && props.ws || this.createWS( props );

this.charsSent = 0;

setInterval( () => {

if ( ! this.charsSent ) return;
// console.log( this.charsSent );
this.charsSent = 0;

}, 1000 );

}

send( data, toJSON ) {
Expand Down

0 comments on commit 969595e

Please sign in to comment.