Skip to content

Commit

Permalink
Merge 9175940 into 43fec4f
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Reder committed Jul 28, 2018
2 parents 43fec4f + 9175940 commit 240358f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "net64plus-server",
"version": "2.1.0",
"version": "2.1.1",
"compatVersion": "1.0.0",
"description": "Net64+ Dedicated Server",
"main": "dist/index.js",
Expand Down
45 changes: 34 additions & 11 deletions src/Client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ describe('Client', () => {
let client: Client
let wsMock: any
let fnMocks: {[key: string]: (...args: any[]) => Promise<void>}
let majorVersion = 1
let minorVersion = 1

beforeEach(() => {
jest.useFakeTimers()
console.info = jest.fn()
process.env.MAJOR = String(majorVersion)
process.env.MINOR = String(minorVersion)
})

beforeEach(() => {
Expand Down Expand Up @@ -88,11 +92,6 @@ describe('Client', () => {
})

it('should handle gzip decompression error', async () => {
const data = {
messageType: ClientServer.MessageType.PING,
ping: {}
}
const dataMessage = ClientServer.encode(ClientServer.fromObject(data)).finish()
const message: IClientServerMessage = {
compression: Compression.GZIP,
compressedData: new Uint8Array()
Expand All @@ -119,6 +118,32 @@ describe('Client', () => {
})
})

describe('#onHandshake', () => {
describe('on correct handshake', () => {
beforeEach(() => {
const message: IClientServerMessage = {
compression: Compression.NONE,
data: {
messageType: ClientServer.MessageType.HANDSHAKE,
handshake: {
characterId: 0,
major: majorVersion,
minor: minorVersion,
username: 'username'
}
}
}
const encodedMessage = ClientServerMessage.encode(ClientServerMessage.fromObject(message)).finish()

return fnMocks.message(encodedMessage)
})

it('should create player object', () => {
expect(client.player).toBeDefined()
})
})
})

describe('#onPing', () => {
it('should send message back', async () => {
const message: IClientServerMessage = {
Expand All @@ -143,8 +168,6 @@ describe('Client', () => {
beforeEach(() => {
username = 'username'
characterId = 4
process.env.MAJOR = '0'
process.env.MINOR = '0'
})

beforeEach(() => {
Expand All @@ -161,8 +184,8 @@ describe('Client', () => {
messageType: ClientServer.MessageType.HANDSHAKE,
handshake: {
characterId,
major: 0,
minor: 0,
major: majorVersion,
minor: minorVersion,
username
}
}
Expand Down Expand Up @@ -330,8 +353,8 @@ describe('Client', () => {
messageType: ClientServer.MessageType.HANDSHAKE,
handshake: {
characterId,
major: 0,
minor: 0,
major: majorVersion,
minor: minorVersion,
username
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export class Client {
return
}
if (this.isClientUsingWrongVersion(handshake)) {
console.log('WRONG', handshake, process.env.MAJOR, process.env.MINOR)
this.sendWrongVersionMessage()
return
}
Expand All @@ -289,14 +290,16 @@ export class Client {
private createPlayerObject (username?: string, characterId?: number): void {
username = username || this.desiredUsername
if (!username) throw new Error('Player object could not be created, because username is null')
characterId = characterId || this.desiredCharacterId
characterId = characterId != null ? characterId : this.desiredCharacterId
if (characterId == null) throw new Error('Player object could not be created, because characterId is null')
this.player = new Player(this, username, characterId)
webSocketServer.addPlayer(this.player)
}

private isClientUsingWrongVersion (handshake: IClientHandshake): boolean {
return String(handshake.major) !== process.env.MAJOR || String(handshake.minor) !== process.env.MINOR
const major = handshake.major || -1
const minor = handshake.minor || -1
return major !== Number(process.env.MAJOR) || minor < Number(process.env.MINOR)
}

private sendWrongVersionMessage (): void {
Expand Down

0 comments on commit 240358f

Please sign in to comment.