Skip to content

Commit

Permalink
fix identifying when filters are being used. bump 0.11.25
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Dec 17, 2019
1 parent 49f1c96 commit 059c874
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "colyseus",
"version": "0.11.24",
"version": "0.11.25",
"description": "Multiplayer Game Server for Node.js.",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down
19 changes: 12 additions & 7 deletions src/serializer/SchemaSerializer.ts
Expand Up @@ -54,7 +54,7 @@ export class SchemaSerializer<T> implements Serializer<T> {
send[Protocol.ROOM_STATE_PATCH](client, this.state.encodeFiltered(client));
}

// this.state.markAsUnchanged();
this.state.discardAllChanges();

}
}
Expand All @@ -74,28 +74,33 @@ export class SchemaSerializer<T> implements Serializer<T> {
}

private hasFilter(schema: Definition, filters: any = {}) {
let hasFilter = false;

for (const fieldName in schema) {
// skip if a filter has been found
if (hasFilter) { break; }

if (filters[fieldName]) {
return true;
hasFilter = true;

} else if (typeof (schema[fieldName]) === 'function') {
const childSchema = (schema[fieldName] as typeof Schema)._schema;
const childFilters = (schema[fieldName] as typeof Schema)._filters;
return this.hasFilter(childSchema, childFilters);
hasFilter = this.hasFilter(childSchema, childFilters);

} else if (Array.isArray(schema[fieldName])) {
const childSchema = (schema[fieldName][0] as typeof Schema)._schema;
const childFilters = (schema[fieldName][0] as typeof Schema)._filters;
return this.hasFilter(childSchema, childFilters);
hasFilter = this.hasFilter(childSchema, childFilters);

} else if ((schema[fieldName] as any).map) {
const childSchema = ((schema[fieldName] as any).map as typeof Schema)._schema;
const childFilters = ((schema[fieldName] as any).map as typeof Schema)._filters;
return this.hasFilter(childSchema, childFilters);
hasFilter = this.hasFilter(childSchema, childFilters);

}

}
return false;

return hasFilter;
}
}
2 changes: 1 addition & 1 deletion src/transport/WebSocketTransport.ts
Expand Up @@ -83,7 +83,7 @@ export class WebSocketTransport extends Transport {
}, pingInterval);
}

protected onConnection = async (client: Client, req?: http.IncomingMessage & any) => {
async onConnection (client: Client, req?: http.IncomingMessage & any) {
// prevent server crashes if a single client had unexpected error
client.on('error', (err) => debugAndPrintError(err.message + '\n' + err.stack));
client.on('pong', heartbeat);
Expand Down
16 changes: 8 additions & 8 deletions usage/DummyRoom.ts
Expand Up @@ -40,7 +40,7 @@ export class DummyRoom extends Room<State> {
try {
if (consented) throw new Error("just close!");

await this.allowReconnection(client, 10);
// await this.allowReconnection(client, 10);
console.log("CLIENT RECONNECTED");

} catch (e) {
Expand Down Expand Up @@ -69,13 +69,13 @@ export class DummyRoom extends Room<State> {
onDispose () {
console.log("Disposing ChatRoom...");

// perform async tasks to disconnect all players
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("async task finished, let's dispose the room now!")
resolve();
}, 1000);
});
// // perform async tasks to disconnect all players
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// console.log("async task finished, let's dispose the room now!")
// resolve();
// }, 1000);
// });
}

}
6 changes: 3 additions & 3 deletions usage/ServerBarebones.ts
Expand Up @@ -2,16 +2,16 @@
* Barebones server without express.
*/
import { Server } from "../src";
import { ChatRoom } from "./ChatRoom";
import { DummyRoom } from "./DummyRoom";

const port = Number(process.env.PORT || 2567);
const endpoint = "localhost";

// Create HTTP & WebSocket servers
const gameServer = new Server();

// Define ChatRoom as "chat"
gameServer.define("chat", ChatRoom)
// Define DummyRoom as "chat"
gameServer.define("chat", DummyRoom)
// Matchmaking filters
// .filterBy(['progress'])

Expand Down
6 changes: 3 additions & 3 deletions usage/ServerKoa.ts
Expand Up @@ -2,7 +2,7 @@ import http from "http";
import Koa from "koa";

import { Server } from "../src";
import { ChatRoom } from "./ChatRoom";
import { DummyRoom } from "./DummyRoom";

const app = new Koa();
const port = Number(process.env.PORT || 2567);
Expand All @@ -17,8 +17,8 @@ const gameServer = new Server();

gameServer.attach({ server });

// Define ChatRoom as "chat"
gameServer.define("chat", ChatRoom)
// Define DummyRoom as "chat"
gameServer.define("chat", DummyRoom)

gameServer.listen(port);

Expand Down
6 changes: 3 additions & 3 deletions usage/ServerTCP.ts
Expand Up @@ -5,7 +5,7 @@
import * as net from "net";

import { Server } from "../src/Server";
import { ChatRoom } from "./ChatRoom";
import { DummyRoom } from "./DummyRoom";

const port = Number(process.env.PORT || 8181);
const endpoint = "localhost";
Expand All @@ -15,8 +15,8 @@ const engine = net.Server;
// Create TCP server
const gameServer = new Server({ engine });

// Register ChatRoom as "chat"
gameServer.define("chat", ChatRoom);
// Register DummyRoom as "chat"
gameServer.define("chat", DummyRoom);

process.on('unhandledRejection', r => console.log(r));
gameServer.listen(port);
Expand Down

0 comments on commit 059c874

Please sign in to comment.