2017-06-30 20:07:13 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-09 21:35:56 +00:00
|
|
|
import EventEmitter from "event-emitter-es6";
|
2017-06-30 20:07:13 +00:00
|
|
|
|
|
|
|
import OtherClient from './OtherClient';
|
2017-07-09 14:28:12 +00:00
|
|
|
import Vector from './Utilities/Vector';
|
2017-06-30 20:07:13 +00:00
|
|
|
|
|
|
|
class ClientManager extends EventEmitter
|
|
|
|
{
|
|
|
|
constructor(inRippleLink)
|
|
|
|
{
|
2017-07-01 12:29:45 +00:00
|
|
|
super();
|
|
|
|
|
2017-07-09 14:28:12 +00:00
|
|
|
this.otherClientCursorSize = 15;
|
|
|
|
this.otherClientCursorWidth = 4;
|
2017-07-09 14:18:45 +00:00
|
|
|
|
2017-06-30 20:07:13 +00:00
|
|
|
this.otherClients = new Map();
|
|
|
|
|
|
|
|
// Handle other clients' state updates
|
2017-07-01 12:29:45 +00:00
|
|
|
inRippleLink.on("ClientStates", this.handlePeerUpdates.bind(this));
|
2017-06-30 20:07:13 +00:00
|
|
|
// Handle line start events from other clients
|
2017-07-01 12:29:45 +00:00
|
|
|
inRippleLink.on("LineStartReflection", this.handleOtherLineStart.bind(this));
|
2017-06-30 20:07:13 +00:00
|
|
|
// Handle line part events from other clients
|
2017-07-01 12:29:45 +00:00
|
|
|
inRippleLink.on("LinePartReflection", this.handleOtherLinePart.bind(this));
|
2017-06-30 20:07:13 +00:00
|
|
|
// Handle line complete events from other clients
|
2017-07-01 12:29:45 +00:00
|
|
|
inRippleLink.on("LineCompleteReflection", this.handleOtherLineComplete.bind(this));
|
2017-06-30 20:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render(canvas, context)
|
|
|
|
{
|
|
|
|
context.save();
|
|
|
|
|
2017-07-09 14:18:45 +00:00
|
|
|
for(let [otherClientId, otherClient] of this.otherClients)
|
2017-06-30 20:07:13 +00:00
|
|
|
{
|
|
|
|
// TODO: Filter rendering by working out if this client is actually inside our viewport or not here
|
2017-07-09 14:18:45 +00:00
|
|
|
otherClient.render(canvas, context, this);
|
2017-06-30 20:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
context.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles peer update messages recieved from the server via the RippleLink.
|
|
|
|
*/
|
|
|
|
handlePeerUpdates(message) {
|
|
|
|
// Update our knowledge about other clients
|
|
|
|
for (let otherClient of message.ClientStates) {
|
|
|
|
// If this client is new, emit an event about it
|
|
|
|
if(!this.otherClients.has(otherClient.Id)) {
|
|
|
|
// Convert the raw object into a class instance
|
|
|
|
let otherClientObj = OtherClient.FromRaw(otherClient);
|
|
|
|
this.otherClients.set(otherClientObj.Id, otherClientObj);
|
|
|
|
|
|
|
|
this.emit("OtherClientConnect", otherClient);
|
|
|
|
}
|
|
|
|
else { // If not, emit a normal update message about it
|
|
|
|
this.emit("OtherClientUpdate", otherClient);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the OtherClient instance to pull in the rest of the data
|
|
|
|
this.otherClients.get(otherClient.Id).update(otherClient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches an OtherClient instance based on its id.
|
|
|
|
* @param {number} targetId The id of the OtherClient instance to fetch.
|
|
|
|
* @return {OtherClient} The OtherClient instance with the
|
|
|
|
* specified id.
|
|
|
|
*/
|
|
|
|
get(targetId) {
|
|
|
|
if(!this.otherClients.has(targetId))
|
2017-10-23 18:51:40 +00:00
|
|
|
throw new Error(`Error: The other client with the id ${targetId} couldn't be found.`);
|
2017-06-30 20:07:13 +00:00
|
|
|
|
|
|
|
return this.otherClients.get(targetId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a line start event from another client.
|
|
|
|
* @param {LineStartReflectionMessage} message The line start message event to process.
|
|
|
|
*/
|
|
|
|
handleOtherLineStart(message) {
|
|
|
|
let fromClient = this.get(message.OtherClientId);
|
|
|
|
|
|
|
|
fromClient.addLine({
|
2017-07-09 14:18:45 +00:00
|
|
|
/** @type {string} */
|
2017-06-30 20:07:13 +00:00
|
|
|
LineId: message.LineId,
|
2017-07-09 14:18:45 +00:00
|
|
|
/** @type {string} */
|
2017-06-30 20:07:13 +00:00
|
|
|
Colour: message.LineColour,
|
2017-07-09 14:18:45 +00:00
|
|
|
/** @type {number} */
|
2017-06-30 20:07:13 +00:00
|
|
|
Width: message.LineWidth
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a line part event from another client.
|
|
|
|
* @param {LinePartReflectionMessage} message The line part reflection message to process.
|
|
|
|
*/
|
|
|
|
handleOtherLinePart(message) {
|
|
|
|
let fromClient = this.get(message.OtherClientId);
|
2017-07-09 14:18:45 +00:00
|
|
|
let vectorPoints = message.Points.map((point) => new Vector(point.X, point.Y));
|
|
|
|
|
|
|
|
fromClient.appendLine(message.LineId, vectorPoints);
|
2017-06-30 20:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a line complete event from another client.
|
|
|
|
* @param {LineCompleteReflectionMessage} message The line complete reflection message to process.
|
|
|
|
*/
|
|
|
|
handleOtherLineComplete(message) {
|
|
|
|
let fromClient = this.get(message.OtherClientId);
|
|
|
|
fromClient.finishLine(message.LineId);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-07-01 12:29:45 +00:00
|
|
|
|
|
|
|
export default ClientManager;
|