Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added AstroGame with its own TCPClientServer
- MessageFactory now registeres new message types
- Loading branch information
Showing
13 changed files
with
296 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// import PubSub from '../PubSub'; | ||
import Director, { DirectorTopic } from '../../Director'; | ||
import GameWorld, { GameWorldType, GameWorldState } from '../GameWorld'; | ||
import Lobby, { LobbyOptions } from '../Lobby'; | ||
import ClientProxy from '../../ClientProxy'; | ||
import PlayerAccount, { PlayerLocation } from '../../PlayerAccount'; | ||
import Database from '../../Database'; | ||
import MessageFactory from '../../message/MessageFactory'; | ||
import Msg_Astro from './Msg_Astro'; | ||
|
||
import AstroTCPClientServer from './AstroTCPClientServer'; | ||
import AstroTCPClientSession from './AstroTCPClientSession'; | ||
|
||
const now = require("performance-now"); | ||
|
||
export default class AstroGame extends GameWorld { | ||
|
||
public maxTime: number = 3000; | ||
public server: AstroTCPClientServer; | ||
public connectionPort: number; | ||
|
||
private _startTime: number; | ||
|
||
constructor(options?: any) { | ||
super(options); | ||
this.connectionPort = 9595; | ||
if (options && options.connectionPort) { | ||
this.connectionPort = options.connectionPort; | ||
} | ||
MessageFactory.registerMessageClass(Msg_Astro, Msg_Astro.type); | ||
this.start(); | ||
} | ||
|
||
tick(): number { | ||
this.startTick(); | ||
// if ((now() - this._startTime) >= this.maxTime) { | ||
// let clients: ClientProxy[] = Array.from(this._clients.values()); | ||
// this.dispose(); | ||
// Director.Instance().handleAstroGameOver(this, clients[0], clients[1]); | ||
// } | ||
this.endTick(); | ||
return 0; | ||
} | ||
|
||
start(): void { | ||
this._startTime = now(); | ||
this.server = new AstroTCPClientServer(this.connectionPort); | ||
|
||
} | ||
|
||
dispose(): void { | ||
super.dispose(); | ||
MessageFactory.registerMessageClass(Msg_Astro, Msg_Astro.type); | ||
this.server.dispose(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/renderer/matchmaker/game/astrogame/AstroTCPClientServer.ts
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import WebSocket = require('ws'); | ||
import TCPClientServer from '../../connection/TCPClientServer'; | ||
import TCPClientSession, { MockWebSocket } from '../../connection/TCPClientSession'; | ||
import AstroTCPClientSession from './AstroTCPClientSession'; | ||
import Msg_Auth from '../../message/Msg_Auth'; | ||
|
||
export default class AstroTCPClientServer extends TCPClientServer { | ||
|
||
constructor(port: number) { | ||
super(port) | ||
} | ||
|
||
onConnection(socket: WebSocket | MockWebSocket): TCPClientSession { | ||
let client: AstroTCPClientSession = new AstroTCPClientSession(socket); | ||
// console.log(`${client.ip} : ${client.port} connected to the server.`); | ||
this.clients.set(client, socket); | ||
let authMsg: Msg_Auth = new Msg_Auth({ | ||
command: 'connected' | ||
}); | ||
client.sendMessage(authMsg); | ||
return client; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/renderer/matchmaker/game/astrogame/AstroTCPClientSession.ts
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// import WebSocket = require('ws'); | ||
import TCPClientSession from '../../connection/TCPClientSession'; | ||
import Msg_Auth from '../../message/Msg_Auth'; | ||
import Msg_Chat from '../../message/Msg_Chat'; | ||
import Msg_Astro from './Msg_Astro'; | ||
|
||
// import PubSub, { PubSubClient } from '../../PubSub'; | ||
import Message from '../../message/Message'; | ||
import MessageFactory from '../../message/MessageFactory'; | ||
// import Msg_Chat from '../../message/Msg_Chat'; | ||
// import TCPClientServer from '../../connection/TCPClientServer'; | ||
import Director from '../../Director'; | ||
|
||
const now = require("performance-now"); | ||
|
||
export default class AstroTCPClientSession extends TCPClientSession { | ||
|
||
onMessage(message: any): void { | ||
// console.log(`AstroTCPClientSession: onMessage: `, message); | ||
this.lastMessageReceivedTime = now(); | ||
let rinfo: any = {address: this._ip, port: this._port}; | ||
let msg: Message = MessageFactory.parse(message, rinfo); | ||
if (msg) { | ||
let message_type: number = msg.getType(); | ||
|
||
switch (message_type) { | ||
case Msg_Auth.type: | ||
let authMsg: Msg_Auth = msg as Msg_Auth; | ||
authMsg.host = this._ip; | ||
authMsg.port = this._port; | ||
console.log(` --> AstroTCPClientSession: received Msg_Auth: `, authMsg.command); | ||
this.userUUID = `PLAYER-${Math.floor(Math.random()*1000)}`; //Director.Instance().authenticateUser(authMsg); //TODO: add real authentication flow | ||
authMsg.userUUID = this.userUUID; | ||
authMsg.password = ''; | ||
authMsg.authToken = '<AUTH-TOKEN>'; | ||
authMsg.command = 'authorized'; | ||
this.sendMessage(authMsg); // ACK | ||
break; | ||
case Msg_Chat.type: | ||
console.log(` --> AstroTCPClientSession: received Msg_Chat: `); | ||
//this.publish(message); | ||
this.sendMessage(msg); // ECHO | ||
break; | ||
case Msg_Astro.type: | ||
console.log(` --> AstroTCPClientSession: received Msg_Astro: `); | ||
//this.publish(message); | ||
this.sendMessage(msg); // ECHO | ||
break; | ||
default: | ||
console.log(" --> AstroTCPClientSession: unrecognized message type."); | ||
break; | ||
} | ||
} else { | ||
console.log(` --> AstroTCPClientSession: unrecognized message format: `, message); | ||
} | ||
} | ||
|
||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import Message from '../../message/Message'; | ||
|
||
const sp = require('schemapack'); | ||
|
||
const messageSchema = sp.build({ | ||
__type: "uint8", | ||
id: "string", | ||
sourceUUID: "string", | ||
targetUUID: "string", | ||
body: "string", | ||
direct: "boolean" | ||
}); | ||
|
||
export type ChatMessageOptions = { | ||
id?: string; | ||
sourceUUID?: string; | ||
targetUUID?: string; | ||
body?: string; | ||
direct?: boolean; | ||
} | ||
|
||
export default class Msg_Astro extends Message { | ||
|
||
static type: number = 3; | ||
|
||
public sourceUUID: string; | ||
public targetUUID: string; | ||
public body: string; | ||
public direct: boolean; | ||
|
||
constructor(options?: ChatMessageOptions) { | ||
super(); | ||
options = options || {}; | ||
let defaultOptions: ChatMessageOptions = { | ||
id: '', | ||
sourceUUID: '', | ||
targetUUID: '', | ||
body: '', | ||
direct: false | ||
} | ||
options = Object.assign(defaultOptions, options); | ||
|
||
this._id = options.id; | ||
this.sourceUUID = options.sourceUUID; | ||
this.targetUUID = options.targetUUID; | ||
this.body = options.body; | ||
this.direct = options.direct; | ||
} | ||
|
||
public getBytes(): any { | ||
var message = { | ||
__type: this.getType(), | ||
id: this._id, | ||
sourceUUID: this.sourceUUID, | ||
targetUUID: this.targetUUID, | ||
body: this.body, | ||
direct: this.direct | ||
}; | ||
return messageSchema.encode(message); | ||
} | ||
|
||
public load(buffer: any): void { // throws PacketDataNotApplicableException { | ||
var payload = messageSchema.decode(buffer); | ||
if (payload) { | ||
if (payload.__type == this.getType()) { | ||
this._id = payload.id; | ||
this.sourceUUID = payload.sourceUUID, | ||
this.targetUUID = payload.targetUUID, | ||
this.body = payload.body, | ||
this.direct = payload.direct; | ||
} else { | ||
console.log(`Expecting MessageType: ${this.getType()} but got: ${payload.__type}`) | ||
} | ||
} else { | ||
console.log(`Unable to decode message buffer.`); | ||
} | ||
} | ||
|
||
public getType(): number { | ||
return Msg_Astro.type; | ||
} | ||
} |
This file contains 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 characters
Oops, something went wrong.