diff --git a/cardshifter.html b/cardshifter.html index 92a56bc..d9461f3 100644 --- a/cardshifter.html +++ b/cardshifter.html @@ -1,28 +1,28 @@ -
-
- * A login message from a client to add a user to the available users on the server.
- * This login message is required before any other action or message can be performed between a client and a server.
- * @constructor
- * @param username the incoming user name passed from client to server, not null
- * @example Message: { "command":"login","username":"JohnDoe" }
- */
- LoginMessage: function(username) {
- this.username = username;
- },
+ window.CardshifterServerAPI = {
+ socket: null,
+ incomingMessages: [],
+ messageTypes: {
+ /**
+ * Incoming login message.
+ *
+ * A login message from a client to add a user to the available users on the server.
+ * This login message is required before any other action or message can be performed between a client and a server.
+ * @constructor
+ * @param username the incoming user name passed from client to server, not null
+ * @example Message: { "command":"login","username":"JohnDoe" }
+ */
+ LoginMessage: function(username) {
+ this.username = username;
+ },
- /**
- * Request available targets for a specific action to be performed by an entity.
- *
- * These in-game messages request a list of al available targets for a given action and entity. - * The client uses this request in order to point out targets (hopefully with a visual aid such as highlighting targets) - * that an entity (such as a creature card, or a player) can perform an action on (for example attack or enchant a card. - * @constructor - * @param gameId The Id of this game currently being played - * @param id The Id of this entity which requests to perform an action - * @param action The name of this action requested to be performed - */ - RequestTargetsMessage: function(gameId, id, action) { - this.gamdId = gameId; - this.id = id; - this.action = action; - }, + /** + * Request available targets for a specific action to be performed by an entity. + *
+ * These in-game messages request a list of al available targets for a given action and entity. + * The client uses this request in order to point out targets (hopefully with a visual aid such as highlighting targets) + * that an entity (such as a creature card, or a player) can perform an action on (for example attack or enchant a card. + * @constructor + * @param gameId The Id of this game currently being played + * @param id The Id of this entity which requests to perform an action + * @param action The name of this action requested to be performed + */ + RequestTargetsMessage: function(gameId, id, action) { + this.gamdId = gameId; + this.id = id; + this.action = action; + }, - /** - * Make a specific type of request to the server. - *
- * This is used to request an action from the server which requires server-side information. - * @constructor - * @param request This request - * @param message The message accompanying this request - */ - ServerQueryMessage: function(request, message) { - this.request = request; - this.message = message; - - this.toString = function() { - return "ServerQueryMessage: Request" + this.request + " message: " + this.message; - }; - }, - - - /** - * Request to start a new game. - *
- * This is sent from the Client to the Server when this player invites another player (including AI) to start a new game of a chosen type. - * @constructor - * @param opponent The Id of the player entity being invited by this player - * @param gameType The type / mod of the game chosen by this player - */ - StartGameRequest: function(opponent, gameType) { - this.opponent = opponent; - this.gameType = gameType; - }, - - /** - * Serialize message from JSON to byte. - *
- * Primarily used for libGDX client. - * Constructor. - * @param type This message type - */ - TransformerMessage: function(type) { - this.type = type; - }, - - /** - * Message for a game entity to use a certain ability. - *
- * Game entities (e.g., cards, players) may have one or more ability actions that they can perform. - * Certain abilities can have multiple targets, hence the use of an array. - * @constructor. (multiple targets) - *
- * Used for multiple target actions. - * - * @param gameId This current game - * @param entity This game entity performing an action - * @param action This action - * @param targets The set of multiple targets affected by this action - */ - UseAbilityMessage: function(gameId, id, action, targets) { - this.gameId = gameId; - this.id = id; - this.action = action; - this.targets = targets; - - this.toString = function() { - return "UseAbilityMessage" + - "[id=" + this.id + - ", action=" + this.action + - ", gameId=" + this.gameId + - ", targets=" + this.targets.toString() + - "]"; - }; - }, - - /** - * Chat message in game lobby. - *
- * These are messages printed to the game lobby which are visible to all users present at the time the message is posted. - * @constructor - * @param message The content of this chat message - */ - ChatMessage: function(message) { - this.chatId = 1; - this.from = "unused"; - this.message = message; - - this.toString = function() { - return "ChatMessage [chatId=" + chatId + ", message=" + message + ", from=" + from + "]"; - }; - }, - - /** - * Request to invite a player to start a new game. - * @constructor - * @param id The Id of this invite request - * @param name The name of the player being invited - * @param gameType The game type of this invite request - */ - InviteRequest: function(id, name, gameType) { - this.id = id; - this.name = name; - this.gameType = gameType; - }, - - /** - * Response to an InviteRequest message. - * @constructor - * @param inviteId Id of this incoming InviteRequest message - * @param accepted Whether or not the InviteRequest is accepted - */ - InviteResponse: function(inviteId, accepted) { - this.inviteId = inviteId; - this.accepted = accepted; - }, - - /** - * Player configuration for a given game. - * @constructor - * @param gameId This game - * @param modName The mod name for this game - * @param configs Map of player name and applicable player configuration - */ - PlayerConfigMessage: function(gameId, modName, configs) { - this.gameId = gameId; - this.modName = modName; - this.configs = configs; - - this.toString = function() { - return "PlayerConfigMessage{" + - "configs=" + configs + - ", gameId=" + gameId + - ", modName='" + modName + '\'' + - '}'; - }; - } - }, - /** - * Initializes the API for use. - * - * @param server:string -- The server that to be connected to. - * @param isSecure:boolean -- Whether or not the server is being connected to with wss - * - * This sets up all the message types to inherit the main `Message` class, and sets - * up the websocket that will be used to communicate to the server, and to recieve - * information from the server. - * - */ - init: function(server, isSecure, onReady, onError) { - var types = this.messageTypes; - var self = this; // for the events - - types.LoginMessage.prototype = new Message("login"); - types.RequestTargetsMessage.prototype = new Message("requestTargets"); - types.ServerQueryMessage.prototype = new Message("query"); - types.StartGameRequest.prototype = new Message("startgame"); - types.TransformerMessage.prototype = new Message("serial"); - types.UseAbilityMessage.prototype = new Message("use"); - types.ChatMessage.prototype = new Message("chat"); - types.InviteRequest.prototype = new Message("inviteRequest"); - types.InviteResponse.prototype = new Message("inviteResponse"); - types.PlayerConfigMessage.prototype = new Message("playerconfig"); - NotInitializedException.prototype = new Error(); - SocketNotReadyException.prototype = new Error(); - - // secure websocket is wss://, rather than ws:// - var secureAddon = (isSecure ? "s" : ""); - // if the protocol is not found in the string, store the correct protocol (is secure?) - var protocolAddon = (wsProtocolFinder.test(server) ? "" : "ws" + secureAddon + "://"); - var socket = new WebSocket(protocolAddon + server); + /** + * Make a specific type of request to the server. + *
+ * This is used to request an action from the server which requires server-side information. + * @constructor + * @param request This request + * @param message The message accompanying this request + */ + ServerQueryMessage: function(request, message) { + this.request = request; + this.message = message; + + this.toString = function() { + return "ServerQueryMessage: Request" + this.request + " message: " + this.message; + }; + }, + + + /** + * Request to start a new game. + *
+ * This is sent from the Client to the Server when this player invites another player (including AI) to start a new game of a chosen type. + * @constructor + * @param opponent The Id of the player entity being invited by this player + * @param gameType The type / mod of the game chosen by this player + */ + StartGameRequest: function(opponent, gameType) { + this.opponent = opponent; + this.gameType = gameType; + }, + + /** + * Serialize message from JSON to byte. + *
+ * Primarily used for libGDX client. + * Constructor. + * @param type This message type + */ + TransformerMessage: function(type) { + this.type = type; + }, + + /** + * Message for a game entity to use a certain ability. + *
+ * Game entities (e.g., cards, players) may have one or more ability actions that they can perform. + * Certain abilities can have multiple targets, hence the use of an array. + * @constructor. (multiple targets) + *
+ * Used for multiple target actions. + * + * @param gameId This current game + * @param entity This game entity performing an action + * @param action This action + * @param targets The set of multiple targets affected by this action + */ + UseAbilityMessage: function(gameId, id, action, targets) { + this.gameId = gameId; + this.id = id; + this.action = action; + this.targets = targets; + + this.toString = function() { + return "UseAbilityMessage" + + "[id=" + this.id + + ", action=" + this.action + + ", gameId=" + this.gameId + + ", targets=" + this.targets.toString() + + "]"; + }; + }, + + /** + * Chat message in game lobby. + *
+ * These are messages printed to the game lobby which are visible to all users present at the time the message is posted. + * @constructor + * @param message The content of this chat message + */ + ChatMessage: function(message) { + this.chatId = 1; + this.from = "unused"; + this.message = message; - socket.onmessage = function(message) { - self.incomingMessages.push(JSON.parse(message.data)); - } + this.toString = function() { + return "ChatMessage [chatId=" + chatId + ", message=" + message + ", from=" + from + "]"; + }; + }, - socket.onopen = onReady; + /** + * Request to invite a player to start a new game. + * @constructor + * @param id The Id of this invite request + * @param name The name of the player being invited + * @param gameType The game type of this invite request + */ + InviteRequest: function(id, name, gameType) { + this.id = id; + this.name = name; + this.gameType = gameType; + }, - socket.onerror = function() { - onError(); - this.socket = null; - } + /** + * Response to an InviteRequest message. + * @constructor + * @param inviteId Id of this incoming InviteRequest message + * @param accepted Whether or not the InviteRequest is accepted + */ + InviteResponse: function(inviteId, accepted) { + this.inviteId = inviteId; + this.accepted = accepted; + }, - this.socket = socket; - }, - - /** - * Sends a message to the server. - * - * @param message:Message -- The message to send. - * @param onReceive:Function (OPTIONAL) -- A function to run when a message has returned - * @error NotInitializedException -- If the API has not been initialized yet. - * - * This will use websocket setup by `this.init` to send a message to the server. - * - * NOTE: The onReceive function will NOT NECESSARILY be run when the desired request - * is received. However, it is likely. - */ - sendMessage: function(message, onReceive) { - var socket = this.socket; - var self = this; - if(socket) { - if(socket.readyState === SOCKET_OPEN) { - this.socket.send(JSON.stringify(flatten(message))); - if(onReceive) { - this.socket.onmessage = function(msg) { - onReceive(JSON.parse(msg.data)); - this.onmessage = function(msg2) { - self.incomingMessages.push(JSON.parse(msg2.data)); - } - } - } - } else { - throw new SocketNotReadyException("The Websocket is not yet ready to be used", socket.readyState); - } - } else { - throw new NotInitializedException("The API has not yet been initialized."); - } - }, + /** + * Player configuration for a given game. + * @constructor + * @param gameId This game + * @param modName The mod name for this game + * @param configs Map of player name and applicable player configuration + */ + PlayerConfigMessage: function(gameId, modName, configs) { + this.gameId = gameId; + this.modName = modName; + this.configs = configs; - /** - * Gets a message from the recieved message queue. - * - * @return Message -- The first message in the incoming messages queue - * - * This will .shift() the incomingMessages queue and return the value. - * This allows the API to handle the messages from the server so the - * main game code can access the messages as it needs to. - * - * Although the function is very simple, the name make is clear what is - * happening. - */ - getMessage: function() { - return this.incomingMessages.shift(); - } - }; + this.toString = function() { + return "PlayerConfigMessage{" + + "configs=" + configs + + ", gameId=" + gameId + + ", modName='" + modName + '\'' + + '}'; + }; + } + }, + /** + * Initializes the API for use. + * + * @param server:string -- The server that to be connected to. + * @param isSecure:boolean -- Whether or not the server is being connected to with wss + * + * This sets up all the message types to inherit the main `Message` class, and sets + * up the websocket that will be used to communicate to the server, and to recieve + * information from the server. + * + */ + init: function(server, isSecure, onReady, onError) { + var types = this.messageTypes; + var self = this; // for the events + + types.LoginMessage.prototype = new Message("login"); + types.RequestTargetsMessage.prototype = new Message("requestTargets"); + types.ServerQueryMessage.prototype = new Message("query"); + types.StartGameRequest.prototype = new Message("startgame"); + types.TransformerMessage.prototype = new Message("serial"); + types.UseAbilityMessage.prototype = new Message("use"); + types.ChatMessage.prototype = new Message("chat"); + types.InviteRequest.prototype = new Message("inviteRequest"); + types.InviteResponse.prototype = new Message("inviteResponse"); + types.PlayerConfigMessage.prototype = new Message("playerconfig"); + NotInitializedException.prototype = new Error(); + SocketNotReadyException.prototype = new Error(); + + // secure websocket is wss://, rather than ws:// + var secureAddon = (isSecure ? "s" : ""); + // if the protocol is not found in the string, store the correct protocol (is secure?) + var protocolAddon = (wsProtocolFinder.test(server) ? "" : "ws" + secureAddon + "://"); + var socket = new WebSocket(protocolAddon + server); + + socket.onmessage = function(message) { + self.incomingMessages.push(JSON.parse(message.data)); + } + + socket.onopen = onReady; + + socket.onerror = function() { + onError(); + this.socket = null; + } + + this.socket = socket; + }, + + /** + * Sends a message to the server. + * + * @param message:Message -- The message to send. + * @param onReceive:Function (OPTIONAL) -- A function to run when a message has returned + * @error NotInitializedException -- If the API has not been initialized yet. + * + * This will use websocket setup by `this.init` to send a message to the server. + * + * NOTE: The onReceive function will NOT NECESSARILY be run when the desired request + * is received. However, it is likely. + */ + sendMessage: function(message, onReceive) { + var socket = this.socket; + var self = this; + if(socket) { + if(socket.readyState === SOCKET_OPEN) { + this.socket.send(JSON.stringify(flatten(message))); + if(onReceive) { + this.socket.onmessage = function(msg) { + onReceive(JSON.parse(msg.data)); + this.onmessage = function(msg2) { + self.incomingMessages.push(JSON.parse(msg2.data)); + } + } + } + } else { + throw new SocketNotReadyException("The Websocket is not yet ready to be used", socket.readyState); + } + } else { + throw new NotInitializedException("The API has not yet been initialized."); + } + }, + + /** + * Gets a message from the recieved message queue. + * + * @return Message -- The first message in the incoming messages queue + * + * This will .shift() the incomingMessages queue and return the value. + * This allows the API to handle the messages from the server so the + * main game code can access the messages as it needs to. + * + * Although the function is very simple, the name make is clear what is + * happening. + */ + getMessage: function() { + return this.incomingMessages.shift(); + } + }; })(Function("return this")()); \ No newline at end of file