Skip to content

Commit

Permalink
added AstroGame with its own TCPClientServer
Browse files Browse the repository at this point in the history
- MessageFactory now registeres new message types
  • Loading branch information
wwlib committed Aug 8, 2018
1 parent 88f6d3e commit f028c3d
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/renderer/components/Client.tsx
Expand Up @@ -3,7 +3,7 @@ import * as ReactBootstrap from "react-bootstrap";
import WebSocket = require('ws');
import Msg_Auth from '../matchmaker/message/Msg_Auth';
import Msg_Chat from '../matchmaker/message/Msg_Chat';
import Message, { MessageType } from '../matchmaker/message/Message';
import Message from '../matchmaker/message/Message';
import MessageFactory from '../matchmaker/message/MessageFactory';

// const prettyjson = require('prettyjson');
Expand Down Expand Up @@ -116,15 +116,15 @@ export default class Client extends React.Component < ClientProps, ClientState >
let message_type: number = msg.getType();

switch (message_type) {
case MessageType.Auth:
case Msg_Auth.type:
let authMsg: Msg_Auth = msg as Msg_Auth;
console.log(` --> Client: received Msg_Auth: ${authMsg.command}`);
if (authMsg.command === 'authorized') {
this.id = authMsg.id;
this.userUUID = authMsg.userUUID;
}
break;
case MessageType.Chat:
case Msg_Chat.type:
let chatMsg: Msg_Chat = msg as Msg_Chat;
console.log(` --> Client: received Msg_Chat: `, chatMsg.body);
this.setState({messages: this.state.messages + '\n' + chatMsg.sourceUUID + ': ' + chatMsg.body});
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/matchmaker/Director.ts
Expand Up @@ -5,7 +5,9 @@ import Lobby, { Range } from './game/Lobby';
import GameWorld, { GameWorldType } from './game/GameWorld';
import MockGame from './game/MockGame';
import ChatLobby from './game/ChatLobby';
import AstroGame from './game/astrogame/AstroGame';
import ClientProxy from './ClientProxy';
import MessageFactory from './message/MessageFactory';
import Msg_Auth from './message/Msg_Auth';
import PlayerAccount from './PlayerAccount';
import Database from './Database';
Expand Down Expand Up @@ -89,6 +91,7 @@ export default class Director {
this._disposeGameQueue = [];
this._disposedGameCount = 0;
Database.init();
MessageFactory.init();
}

static Instance(options?: DirectorOptions)
Expand Down Expand Up @@ -151,6 +154,12 @@ export default class Director {
return lobby;
}

addAstroGame(): AstroGame {
let game: AstroGame = new AstroGame(9595);
this._lobbies.set(game.uuid, game);
return game;
}

get lobyCount(): number {
return this._lobbies.size;
}
Expand Down
9 changes: 8 additions & 1 deletion src/renderer/matchmaker/connection/TCPClientServer.ts
Expand Up @@ -83,7 +83,7 @@ export default class TCPClientServer {
}

onConnection(socket: WebSocket | MockWebSocket): TCPClientSession {
let client: TCPClientSession = new TCPClientSession(this, socket);
let client: TCPClientSession = new TCPClientSession(socket);
// console.log(`${client.ip} : ${client.port} connected to the server.`);
this.clients.set(client, socket);
let authMsg: Msg_Auth = new Msg_Auth({
Expand Down Expand Up @@ -138,4 +138,11 @@ export default class TCPClientServer {
this.clients.delete(client);

}

dispose(): void {
this.clients.forEach((socket: WebSocket, clientSession: TCPClientSession) => {
clientSession.dispose();
});
this.killServer();
}
}
34 changes: 17 additions & 17 deletions src/renderer/matchmaker/connection/TCPClientSession.ts
@@ -1,10 +1,10 @@
import PubSub, { PubSubClient } from '../PubSub';
import Message, { MessageType } from '../message/Message';
import Message from '../message/Message';
import MessageFactory from '../message/MessageFactory';
import WebSocket = require('ws');
import Msg_Chat from '../message/Msg_Chat';
import Msg_Auth from '../message/Msg_Auth';
import TCPClientServer from './TCPClientServer';
// import TCPClientServer from './TCPClientServer';
import Director from '../Director';

const now = require("performance-now");
Expand All @@ -20,20 +20,20 @@ export type MockWebSocket = {

export default class TCPClientSession {

public clientServer: TCPClientServer
// public clientServer: TCPClientServer
public lastMessageReceivedTime: number;

private _userUUID: string;
private _socket: any; //FIXME: WebSocket;
private _ip: string;
private _port: number;
private _spawnTime: number;
// private _userToken: any;
private _pubClient: PubSubClient;
private _subClient: PubSubClient;

constructor(clientServer: TCPClientServer, socket: WebSocket | MockWebSocket) {
this.clientServer = clientServer;
protected _userUUID: string;
protected _socket: any; //FIXME: WebSocket;
protected _ip: string;
protected _port: number;
protected _spawnTime: number;
// protected _userToken: any;
protected _pubClient: PubSubClient;
protected _subClient: PubSubClient;

constructor(socket: WebSocket | MockWebSocket) { //clientServer: TCPClientServer,
// this.clientServer = clientServer;
this._socket = socket;
this._ip = this._socket.host;
this._port = this._socket.port;
Expand Down Expand Up @@ -103,7 +103,7 @@ export default class TCPClientSession {
let message_type: number = msg.getType();

switch (message_type) {
case MessageType.Auth:
case Msg_Auth.type:
let authMsg: Msg_Auth = msg as Msg_Auth;
authMsg.host = this._ip;
authMsg.port = this._port;
Expand All @@ -115,7 +115,7 @@ export default class TCPClientSession {
authMsg.command = 'authorized';
this.sendMessage(authMsg); // ACK
break;
case MessageType.Chat:
case Msg_Chat.type:
console.log(` --> TCP_c: received Msg_Chat: `);
this.publish(message);
break;
Expand Down Expand Up @@ -157,7 +157,7 @@ export default class TCPClientSession {
}

dispose(): void {
this.clientServer = undefined;
// this.clientServer = undefined;
try {
if (this._socket) {
this._socket.removeAllListeners();
Expand Down
56 changes: 56 additions & 0 deletions src/renderer/matchmaker/game/astrogame/AstroGame.ts
@@ -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 src/renderer/matchmaker/game/astrogame/AstroTCPClientServer.ts
@@ -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 src/renderer/matchmaker/game/astrogame/AstroTCPClientSession.ts
@@ -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);
}
}

}
82 changes: 82 additions & 0 deletions src/renderer/matchmaker/game/astrogame/Msg_Astro.ts
@@ -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;
}
}
7 changes: 2 additions & 5 deletions src/renderer/matchmaker/message/Message.ts
@@ -1,11 +1,6 @@
// import Player from './Player';
import TCPClientSession from '../connection/TCPClientSession';

export enum MessageType {
Auth,
Chat,
}

export type MessageOptions = {
id?: string;
password?: string
Expand All @@ -14,6 +9,8 @@ export type MessageOptions = {

export default abstract class Message {

static type: number = -1;

protected _id: string;
protected _password: string;
public userUUID: string;
Expand Down

0 comments on commit f028c3d

Please sign in to comment.