/* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/
* MIT Licensed.
*
* See: http://ejohn.org/blog/simple-javascript-inheritance/
*/
(function()
{
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
this.Class = function(){};
// Create a new Class that inherits from this class
Class.extend = function(prop) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function(name, fn){
return function() {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if ( !initializing && this.init )
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.prototype.constructor = Class;
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
})();
//--- Import SFS2X names from Java scope ---------------------------------------
/**
*
* The BanMode enum lists all possible user banning modes.
*
*
Available BanMode values are:
*In addition to this API, a number of other specialized APIs provide the following features:
*Using the {@link SFSApi#newHttpGetRequest} or {@link SFSApi#newHttpPostRequest} factory methods instead of this constructor is recommended. See the methods description for usage examples.
* * @param {string} url The address of the HTTP server to make the request to. * @param {object} params An object containing the parameters to send to the HTTP server. * @param {SFSApi.HttpMode} mode The HTTP request mode (GET or POST). * @param {httpCallbackFn} httpCallbackFn The callback function that will process the response sent by the HTTP server. * * @class * The HttpRequest class represents a request to be sent to an external HTTP/HTTPS server. * *HTTP/S requests can be of type POST or GET. They are useful to retrieve data from or send data to external services.
* *Using the {@link SFSApi#newScheduler} factory method instead of this constructor is recommended. See the method description for a usage example.
* * @param {number} threadPoolSize The number of threads backing the scheduler (recommended value between 1 and 4). * * @class * The TaskScheduler class allows to manage multiple delayed or repeating activities. * *In games, a delayed scheduled task can be useful to set the duration of a match, or set a time limit to a user action, etc. * An interval-based task instead can be used to spawn new NPC enemies on a time basis, or divide the game in rounds, etc.
* *This is a factory method to create a {@link SFSApi.TaskScheduler} instance.
* *This is a factory method to create a {@link SFSApi.HttpRequest} instance.
* *This is a factory method to create a {@link SFSApi.HttpRequest} instance.
* *true
if the password is correct, false
otherwise.
*/
SFSApi.prototype.checkSecurePassword = function(session, originalPass, encryptedPass)
{
return this._javaApi.checkSecurePassword(session, originalPass, encryptedPass);
}
/**
* Logs a user out of the current Zone.
*
* This method can be useful to force a user leave the current Zone and join a new one.
* * @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to be logged out. */ SFSApi.prototype.logout = function(user) { this._javaApi.logout(user); } /** * Creates a connection-less Non-Player Character (NPC). * *The server handles NPCs just like any other regular user, although there is no real, physical connection to the server (in other words no TCP connection is established).
* NPCs can be recognized from the isNpc flag on the SFSUser object representing them, which is always set to true
.
true
is passed, that user will be disconnected before creating the NPC.
*
* @return {SFSUser} The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the NPC.
*
* @throws A [SFSLoginException]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/exceptions/SFSLoginException.html} exception if an error occurs during the NPC creation.
*/
SFSApi.prototype.createNPC = function(userName, zone, forceLogin)
{
return this._javaApi.createNPC(userName, zone, forceLogin);
}
/**
* Kicks a user out of the server.
*
* This operation allows to send a moderator message to the client, providing a number of seconds for reading it before the connection is closed.
* * @param {SFSUser} userToKick The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to be kicked out. * @param {SFSUser} modUser The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing a moderator/administrator user executing the action; can benull
to indicate the "Server" generically.
* @param {string} kickMessage A message from the moderator/administrator to be displayed to the user right before kicking him.
* @param {number} delaySeconds The delay in seconds before the disconnection is executed.
*/
SFSApi.prototype.kickUser = function(userToKick, modUser, kickMessage, delaySeconds)
{
this._javaApi.kickUser(userToKick, modUser, kickMessage, delaySeconds);
}
/**
* Bans a user, preventing his reconnection for a configurable amount of time.
*
* This operation allows to send a moderator message to the client, providing a number of seconds for reading it before the connection is closed.
* The length of the banishment and the rules under which the ban can be removed are specified in the Zone configuration.
null
to indicate the "Server" generically.
* @param {string} banMessage A message from the moderator/administrator to be displayed to the user right before banishing him.
* @param {BanMode} mode A ban mode among those provided by the BanMode enum.
* @param {number} delaySeconds The delay in seconds before the disconnection is executed.
*/
SFSApi.prototype.banUser = function(userToBan, modUser, banMessage, mode, durationMinutes, delaySeconds)
{
this._javaApi.banUser(userToBan, modUser, banMessage, mode, durationMinutes, delaySeconds);
}
/**
* Disconnect a user from the server.
*
* @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to be disconnected.
* @param {ClientDisconnectionReason} [reason] One of the disconnection reasons provided in the ClientDisconnectionReason enum; the reason will be provided to the client in the client-side CONNECTION_LOST event.
*/
SFSApi.prototype.disconnectUser = function(user, reason)
{
if (reason == null)
this._javaApi.disconnectUser(user);
else
this._javaApi.disconnectUser(user, reason);
}
/**
* Removes a Session object, disconnecting the user associated with it (if one exists).
*
* @param {Session} session The client's [Session]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/bitswarm/sessions/Session.html} object to be disconnected.
*/
SFSApi.prototype.disconnectSession = function(session)
{
this._javaApi.disconnect(session);
}
/**
* Creates a new Room.
*
* This method can create both regular Rooms and MMO Rooms, depending on the passed settings object.
* *null
is passed, the "Server" will be the owner.
* @param {boolean} [joinIt=false] If true
, the Room will be joined by the owner right after the creation.
* @param {SFSRoom} [roomToLeave=null] The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room to leave if the owner is supposed to join the new one; if null
is passed, no previous Room will be left.
* @param {boolean} [fireClientEvent=false] If true
, a client-side ROOM_ADD event will be fired to notify the Room creation.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.ROOM_ADDED]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#ROOM_ADDED} will be fired to notify the Room creation.
*
* @example
* 0
, all matching users are returned.
*
* @return {ArrayList} A java.util.ArrayList Java collection containing the list of matching [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} objects. This can be treated like native JavaScript array.
*/
SFSApi.prototype.findUsers = function(userList, expression, limit)
{
if (limit == null)
limit = 10;
// Convert native JS array
if (userList instanceof Array)
userList = $$Util.toList(userList);
return this._javaApi.findUsers(userList, expression, limit);
}
/**
* Find one or more Rooms in a list of Rooms matching the conditions set in a Match Expression.
*
* @param {SFSRoom[]} roomList An array of [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} objects to search in.
* @param {MatchExpression} expression The Match Expression setting the conditions to match.
* @param {number} [limit=10] When this limit is reached, the search is stopped; if set to 0
, all matching Rooms are returned.
*
* @return {ArrayList} A java.util.ArrayList Java collection containing the list of matching [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} objects. This can be treated like native JavaScript array.
*/
SFSApi.prototype.findRooms = function(roomList, expression, limit)
{
if (limit == null)
limit = 10;
// Convert native JS array
if (roomList instanceof Array)
roomList = $$Util.toList(roomList);
return this._javaApi.findRooms(roomList, expression, limit);
}
/**
* Retrieves a SFSUser object from its id property.
*
* @param {number} userId The id of the user to be retrieved.
*
* @return {SFSUser} The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the requested user.
*/
SFSApi.prototype.getUserById = function(userId)
{
return this._javaApi.getUserById(userId);
}
/**
* Retrieves a SFSUser object in a Zone from its name property.
*
* @param {SFSZone} zone The [SFSZone]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSZone.html} object representing the Zone from which to retrieve the user.
* @param {string} name The name of the user to be retrieved.
*
* @return {SFSUser} The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the requested user.
*/
SFSApi.prototype.getUserByName = function(zone, name)
{
return zone.getUserByName(name);
}
/**
* Retrieves a SFSUser object from its client session.
*
* @param {Session} session The [Session]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/bitswarm/sessions/Session.html} object of the user to be retrieved.
*
* @return {SFSUser} The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the requested user.
*/
SFSApi.prototype.getUserBySession = function(session)
{
return this._javaApi.getUserBySession(session);
}
/**
* Joins a user in a Room.
*
* @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to join in the Room.
* @param {SFSRoom} roomToJoin The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room to make the user join.
* @param {string} [password=null] The Room password, in case it is a private Room.
* @param {boolean} [asSpectator=false] If true
, the user will join the Room as a spectator.
* @param {SFSRoom} [roomToLeave=null] The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room that the user must leave after joining the new one; if null
is passed, no previous Room is left.
* @param {boolean} [fireClientEvent=false] If true
, the client-side ROOM_JOIN and USER_ENTER_ROOM events will be fired to notify the Room joining.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.USER_JOIN_ROOM]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#USER_JOIN_ROOM} will be fired to notify the Room joining.
*
* @example
* true
, a client-side USER_EXIT_ROOM event will be fired to notify the action.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.USER_LEAVE_ROOM]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#USER_LEAVE_ROOM} will be fired to notify the action.
*/
SFSApi.prototype.leaveRoom = function(user, room, fireClientEvent, fireServerEvent)
{
this._javaApi.leaveRoom
(
user,
room,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Removes a Room from its Zone.
*
* @param {SFSRoom} room The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room that the user is going to leave.
* @param {boolean} [fireClientEvent=false] If true
, a client-side ROOM_REMOVE event will be fired to notify the Room removal.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.ROOM_REMOVED]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#ROOM_REMOVED} will be fired to notify the Room removal.
*/
SFSApi.prototype.removeRoom = function(room, fireClientEvent, fireServerEvent)
{
this._javaApi.removeRoom
(
room,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Sends a public chat message from a user.
*
* The message is broadcast to all users in the target Room, including the sender himself.
* * @param {SFSRoom} targetRoom The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room to send the message to. * @param {SFSUser} sender The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user sending the message to the target Room. * @param {string} message The chat message to send. * @param {SFSObject} [params] A SFSObject containing custom parameters to be attached to the message (e.g. text color, font size, etc). */ SFSApi.prototype.sendPublicMessage = function(targetRoom, sender, message, params) { this._javaApi.sendPublicMessage ( targetRoom, sender, message, (params === undefined ? null : params) ); } /** * Sends a private chat message from a user to another one. * *The message is sent to both the sender and the recipient. The sender and recipient can be in any Room, or even not joined in any Room at all.
* * @param {SFSUser} sender The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user sending the message. * @param {SFSUser} recipient The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the message recipient. * @param {string} message The chat message to send. * @param {SFSObject} [params] A SFSObject containing custom parameters to be attached to the message (e.g. text color, font size, etc). */ SFSApi.prototype.sendPrivateMessage = function(sender, recipient, message, params) { this._javaApi.sendPrivateMessage ( sender, recipient, message, (params === undefined ? null : params) ); } /** * Sends a message from a moderator to a list of clients. * *The sender must be either an actual user with "Super User" privileges, or null
. In this case the sender becomes the "Server" itself.
null
, the sender is the "server" itself.
* @param {string} message The message to send.
* @param {SFSObject} params A SFSObject containing custom parameters to be attached to the message.
* @param {Session[]} recipients An array of [Session]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/bitswarm/sessions/Session.html} objects representing the clients to deliver the message to.
*/
SFSApi.prototype.sendModeratorMessage = function(sender, message, params, recipients)
{
// Convert native JS array
if (recipients instanceof Array)
recipients = $$Util.toList(recipients);
this._javaApi.sendModeratorMessage(sender, message, params, recipients);
}
/**
* Sends a message from an administrator to a list of clients.
*
* The sender must be either an actual user with "Super User" privileges, or null
. In this case the sender becomes the "Server" itself.
null
, the sender is the "server" itself.
* @param {string} message The message to send.
* @param {SFSObject} params A SFSObject containing custom parameters to be attached to the message.
* @param {Session[]} recipients An array of [Session]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/bitswarm/sessions/Session.html} objects representing the clients to deliver the message to.
*/
SFSApi.prototype.sendAdminMessage = function(sender, message, params, recipients)
{
// Convert native JS array
if (recipients instanceof Array)
recipients = $$Util.toList(recipients);
this._javaApi.sendAdminMessage(sender, message, params, recipients);
}
/**
* Sends a data object from a user to a list of other users in a Room.
*
* This method sends a custom SFSObject that can contain any data. Typically this is used to send game moves to players or other game/app related updates.
* By default the message is sent to all users in the specified target Room, but a list of SFSUser object can be passed to specify which user in that Room should receive the message. The sender must be joined in the target Room too.
Only new/updated variables are broadcast to the users in the target Room. A variable can also be deleted by setting it to null
.
null
, the ownership is assigned to the "Server" itself.
* @param {SFSRoom} targetRoom The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room in which to set the Room Variables.
* @param {SFSRoomVariable[]} variables An array of {@link SFSRoomVariable} objects to set.
* @param {boolean} [fireClientEvent=false] If true
, a client-side ROOM_VARIABLES_UPDATE event will be fired to notify the Room Variables creation/update.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.ROOM_VARIABLES_UPDATE]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#ROOM_VARIABLES_UPDATE} will be fired to notify the Room Variables creation/update.
* @param {boolean} [overrideOwnership=false] If true
, the ownership of variables can be overridden.
*/
SFSApi.prototype.setRoomVariables = function(user, targetRoom, variables, fireClientEvent, fireServerEvent, overrideOwnership)
{
// Convert native JS array
if (variables instanceof Array)
variables = $$Util.toList(variables);
this._javaApi.setRoomVariables
(
user,
targetRoom,
variables,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent),
(overrideOwnership === undefined ? false : overrideOwnership)
);
}
/**
* Sets the User Variables for the passed user.
*
* Only new/updated variables are broadcast to the users that can "see" the owner. A variable can also be deleted by setting it to null
.
true
, a client-side USER_VARIABLES_UPDATE event will be fired to notify the User Variables creation/update.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.USER_VARIABLES_UPDATE]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#USER_VARIABLES_UPDATE} will be fired to notify the User Variables creation/update.
*/
SFSApi.prototype.setUserVariables = function(owner, variables, fireClientEvent, fireServerEvent)
{
// Convert native JS array
if (variables instanceof Array)
variables = $$Util.toList(variables);
this._javaApi.setUserVariables
(
owner,
variables,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Renames a Room.
*
* @param {SFSUser} owner The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the owner of the Room; if null
, the authorization of the user to change the Room name is not checked.
* @param {SFSRoom} targetRoom The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room to change the name to.
* @param {string} newName The new Room name.
*
* @throws A [SFSRoomException]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/exceptions/SFSRoomException.html} exception if the new name is already assigned to another Room, its length is out of the range allowed by the Zone configuration or it contains bad words (if the Words Filter is active).
*/
SFSApi.prototype.changeRoomName = function(owner, targetRoom, newName)
{
this._javaApi.changeRoomName(owner, targetRoom, newName);
}
/**
* Changes the Room password and the Room password-protection state.
*
* The password-protection state indicates if a Room is private and requires a password to access it. Passing a null
or empty string makes the Room public. Passing a valid string as the password makes the Room private.
null
, the authorization of the user to change the Room password-protection state is not checked.
* @param {SFSRoom} targetRoom The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room to change the password-protection state to.
* @param {string} newPassword The new password; null
or empty string to remove the password protection.
*
* @throws A [SFSRoomException]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/exceptions/SFSRoomException.html} exception if the password-protection state couldn't be modified.
*/
SFSApi.prototype.changeRoomPassword = function(owner, targetRoom, newPassword)
{
this._javaApi.changeRoomPassword(owner, targetRoom, newPassword);
}
/**
* Changes the capacity (maximum number of users/players and spectators) of the Room.
*
* In case the Room size is shrunk, extra players or spectators will not be kicked out of the Room.
* * @param {SFSUser} owner The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the owner of the Room; ifnull
, the authorization of the user to change the Room capacity is not checked.
* @param {SFSRoom} targetRoom The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room to change the capacity to.
* @param {number} maxUsers The new maximum number of users (aka players in Game Rooms).
* @param {number} maxSpectators The new maximum number of spectators (for Game Rooms only).
*
* @throws A [SFSRoomException]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/exceptions/SFSRoomException.html} exception if the Room capacity couldn't be modified.
*/
SFSApi.prototype.changeRoomCapacity = function(owner, targetRoom, maxUsers, maxSpectators)
{
this._javaApi.changeRoomCapacity(owner, targetRoom, maxUsers, maxSpectators);
}
/**
* Subscribes a user to a Room Group.
*
* This action enables the user to receive events related to the Rooms belonging the passed Group.
* * @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to subscribe to a Room Group. * @param {string} groupId The name of the Group to subscribe. */ SFSApi.prototype.subscribeRoomGroup = function(user, groupId) { this._javaApi.subscribeRoomGroup(user, groupId); } /** * Unsubscribes a user from a Room Group. * *This action disables the user to receive events related to the Rooms belonging the passed Group.
* * @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to unsubscribe from a Room Group. * @param {string} groupId The name of the Group to unsubscribe. */ SFSApi.prototype.unsubscribeRoomGroup = function(user, groupId) { this._javaApi.unsubscribeRoomGroup(user, groupId); } /** * Turns a spectator in a Game Room to player. * * @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to turn from spectator to player. * @param {SFSRoom} targetRoom The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room in which the spectator will be turned into a player. * @param {boolean} [fireClientEvent=false] Iftrue
, a client-side SPECTATOR_TO_PLAYER event will be fired to notify the change.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.SPECTATOR_TO_PLAYER]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#SPECTATOR_TO_PLAYER} will be fired to notify the change.
*
* @throws A [SFSRoomException]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/exceptions/SFSRoomException.html} exception if no player slot is available in the Game Room.
*/
SFSApi.prototype.spectatorToPlayer = function(user, targetRoom, fireClientEvent, fireServerEvent)
{
this._javaApi.spectatorToPlayer
(
user,
targetRoom,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Turns a player in a Game Room to spectator.
*
* @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to turn from player to spectator.
* @param {SFSRoom} targetRoom The [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room in which the player will be turned into a spectator.
* @param {boolean} [fireClientEvent=false] If true
, a client-side PLAYER_TO_SPECTATOR event will be fired to notify the change.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.PLAYER_TO_SPECTATOR]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#PLAYER_TO_SPECTATOR} will be fired to notify the change.
*
* @throws A [SFSRoomException]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/exceptions/SFSRoomException.html} exception if no spectator slot is available in the Game Room.
*/
SFSApi.prototype.playerToSpectator = function(user, targetRoom, fireClientEvent, fireServerEvent)
{
this._javaApi.playerToSpectator
(
user,
targetRoom,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
//==============================================================================
//--- Buddy API class ----------------------------------------------------------
//==============================================================================
/**
* Developers never istantiate the BuddyApi class: this is done internally by the SmartFoxServer 2X API; get a reference to it using the Extension's {@link getBuddyApi} method.
*
* @class
* The BuddyApi class provides all the features to manage the users' Buddy Lists.
* It contains methods to initialize the Buddy List of a user, add and remove buddies, set the online status and more.
*
* This causes saved data (the list of buddies and their persistent Buddy Variables) to be loaded from the Buddy List storage.
* * @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user whose Buddy List should be loaded. * @param {boolean} [fireServerEvent=false] Iftrue
, a server-side event of type [SFSEventType.BUDDY_LIST_INIT]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#BUDDY_LIST_INIT} will be fired to notify the Buddy List initialization success.
*
* @return {SFSBuddyList} A [SFSBuddyList]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/buddylist/SFSBuddyList.html} class instance representing the usere's Buddy List.
*
* @throws An IOException Java exception if the Buddy List data couldn't be retrieved from the storage.
*/
BuddyApi.prototype.initBuddyList = function(user, fireServerEvent)
{
return this._javaApi.initBuddyList
(
user,
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Activates/deactivates the ONLINE status of the user in the Buddy List system.
*
* All clients who have the user as their Buddy will see him as online or offline respectively.
* * @param {SFSUser} user The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user to set online/offline in the Buddy List system. * @param {boolean} online Iftrue
, the user is set as online in the Buddy List system; if false
, he is set as offline.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.BUDDY_ONLINE_STATE_UPDATE]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#BUDDY_ONLINE_STATE_UPDATE} will be fired to notify the user's online state change.
*/
BuddyApi.prototype.goOnline = function(user, online, fireServerEvent)
{
this._javaApi.goOnline
(
user,
online,
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Adds a new buddy to the Buddy List of the passed user.
*
* @param {SFSUser} owner The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the owner of the Buddy List to add a new buddy to.
* @param {string} buddyName The name of the user to add as a buddy.
* @param {boolean} isTemp If true
, the buddy is only temporary and will be removed when the owner logs out of the system.
* @param {boolean} [fireClientEvent=false] If true
, a client-side BUDDY_ADD event will be fired to notify the action.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.BUDDY_ADD]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#BUDDY_ADD} will be fired to notify the action.
*
* @throws A [SFSBuddyListException]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/exceptions/SFSBuddyListException.html} exception if the Buddy List of the user is full or the buddy is already in the Buddy List.
*/
BuddyApi.prototype.addBuddy = function(owner, buddyName, isTemp, fireClientEvent, fireServerEvent)
{
this._javaApi.addBuddy
(
owner,
buddyName,
isTemp,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Sets the Buddy Variables for the passed user.
*
* Only new/updated variables are broadcast to the users that have the owner in their Buddy list. A variable can also be deleted by setting it to null
.
true
, a client-side BUDDY_VARIABLES_UPDATE event will be fired to notify the Buddy Variables creation/update.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.BUDDY_VARIABLES_UPDATE]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#BUDDY_VARIABLES_UPDATE} will be fired to notify the Buddy Variables creation/update.
*
* @throws A [SFSBuddyListException]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/exceptions/SFSBuddyListException.html} exception if the limit on the number of allowed Buddy Variables is exceeded.
*/
BuddyApi.prototype.setBuddyVariables = function(owner, variables, fireClientEvent, fireServerEvent)
{
// Convert native JS array
if (variables instanceof Array)
variables = $$Util.toList(variables);
this._javaApi.setBuddyVariables
(
owner,
variables,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Removes a buddy from the Buddy List of the passed user.
*
* @param {SFSUser} owner The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the owner of the Buddy List to remove a buddy from.
* @param {string} buddyName The name of the buddy to remove.
* @param {boolean} [fireClientEvent=false] If true
, a client-side BUDDY_REMOVE event will be fired to notify the buddy removal.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.BUDDY_REMOVE]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#BUDDY_REMOVE} will be fired to notify the buddy removal.
*/
BuddyApi.prototype.removeBuddy = function(owner, buddyName, fireClientEvent, fireServerEvent)
{
this._javaApi.removeBuddy
(
owner,
buddyName,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Blocks/unblocks a buddy in the Buddy List of a user.
*
* Blocked buddies won't be able to see the user online status and send him messages or Buddy Variables updates.
* * @param {SFSUser} owner The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the owner of the Buddy List in which the buddy should be blocked/unblocked. * @param {string} buddyName The name of the buddy to block/unblock. * @param {boolean} isBlocked Iftrue
, the buddy will be blocked in the Buddy List of the user; if false
, the block will be removed.
* @param {boolean} [fireClientEvent=false] If true
, a client-side BUDDY_BLOCK event will be fired to notify the change.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.BUDDY_BLOCK]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#BUDDY_BLOCK} will be fired to notify the change.
*/
BuddyApi.prototype.blockBuddy = function(owner, buddyName, isBlocked, fireClientEvent, fireServerEvent)
{
this._javaApi.blockBuddy
(
owner,
buddyName,
isBlocked,
(fireClientEvent === undefined ? false : fireClientEvent),
(fireServerEvent === undefined ? false : fireServerEvent)
);
}
/**
* Sends a Buddy Message from a user to one of his buddies.
*
* A Buddy Message is similar to a regular private chat message, but it is meant to work with the Buddy List system, taking into account the buddy blocked state, online presence, etc.
* The message is sent to both the sender and the receiver. The recipient must be in the sender's Buddy List.
The SFSGame class extends the normal capabilities of a Room, adding the ability to set the game as private and provide a list of users that the system will invite to play automatically. * Additionally the system is be able to invite more users if the number of players is not sufficient to start the game.
* *null
is passed, the "Server" will be the owner.
* @param {boolean} [fireClientEvent=false] If true
, a client-side ROOM_ADD event will be fired to notify the SFSGame creation.
* @param {boolean} [fireServerEvent=false] If true
, a server-side event of type [SFSEventType.ROOM_ADDED]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/core/SFSEventType.html#ROOM_ADDED} will be fired to notify the SFSGame creation.
*
* @example
* When this method is called, the API:
*An invitation can be sent for various purposes, such as joining a Room (both regular and game ones), adding a friend to the Buddy List, etc.
* *Replying to an invitation means to accept or refuse it.
* *true
, a client-side INVITATION_REPLY event will be fired to notify the reply.
*/
GameApi.prototype.replyToInvitation = function(invitedUser, invitationId, reply, params, fireClientEvent)
{
this._javaApi.replyToInvitation
(
invitedUser,
invitationId,
reply,
(params === undefined ? null : params),
(fireClientEvent === undefined ? false : fireClientEvent)
);
}
/**
* Invites a user to join an existing Room of any type.
*
* If the invitation is accepted, the invitee will be automatically joined in the target Room.
* * @param {SFSRoom} target A [SFSRoom]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSRoom.html} object representing the Room to invite the invitees to. * @param {SFSUser} inviter The [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} object representing the user sending the invitation. * @param {SFSUser[]} invitees A list of [SFSUser]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/entities/SFSUser.html} objects representing the recipients of the invitation. * @param {number} expirySeconds The amount of time allowed to each invitee to accept or refuse the invitation. * @param {boolean} [asSpect=false] Iftrue
, the provided list of invitees will be joined as spectators (where applicable, i.e. Rooms of type game).
* @param {boolean} [leaveLastJoinedRoom=false] If true
, the users joining the target Room will leave the previously joined Room.
* @param {SFSObject} [params] A SFSObject containing custom parameters to be attached to the invitation (e.g. a message).
*/
GameApi.prototype.sendJoinRoomInvitation = function(target, inviter, invitees, expirySeconds, asSpect, leaveLastJoinedRoom, params)
{
if (invitees instanceof Array)
invitees = $$Util.toList(invitees);
this._javaApi.sendJoinRoomInvitation
(
target,
inviter,
invitees,
expirySeconds,
(asSpect === undefined ? false : asSpect),
(leaveLastJoinedRoom === undefined ? false : leaveLastJoinedRoom),
(params === undefined ? null : params)
);
}
//==============================================================================
//--- MMO API class ------------------------------------------------------------
//==============================================================================
/**
* Developers never istantiate the MMOApi class: this is done internally by the SmartFoxServer 2X API; get a reference to it using the Extension's {@link getMMOApi} method.
*
* @class
* The MMOApi class provides specialized API calls for advanced MMO-related functionalities.
* All the methods are targeted at an MMORoom and take its Area of Interest into account in order to dispatch events to the clients.
*
* This method sends a custom SFSObject that can contain any data. Typically this is used to send game moves to players or other game/app related updates.
* The difference with the regular version of this method (see {@link SFSApi#sendObjectMessage} method) is that it works with the AOI set for the target MMORoom.
* Also, instead of using the default AOI, a custom AOI can be provided. This must be smaller than the default one; attempting to use a larger AOI is not possible.
* The sender must be joined in the target MMORoom too.
The difference with the regular version of this method (see {@link SFSApi#sendPublicMessage} method) is that it works with the AOI set for the target MMORoom.
* Also, instead of using the default AOI, a custom AOI can be provided. This must be smaller than the default one; attempting to use a larger AOI is not possible.
* The sender must be joined in the target MMORoom too.
The target MMORoom is not required by this method because the system already keeps track of which MMOItem belongs to which MMORoom.
* * @param {MMOItem} item The MMOItem object representing the MMOItem to be removed from the MMORoom where it is located. */ MMOApi.prototype.removeMMOItem = function(item) { this._javaApi.removeMMOItem(item); } /** * Sets the MMOItem Variables for the passed MMOItem. * *Only new/updated variables are broadcast to the users that are within the range defined by the MOORoom's Area of Interest from the target MMOItem. A variable can also be deleted by setting it to null
.
true
, a client-side MMOITEM_VARIABLES_UPDATE event will be fired to notify the MMOItem Variables creation/update.
*/
MMOApi.prototype.setMMOItemVariables = function(item, variables, fireClientEvent)
{
// Convert native JS array
if (variables instanceof Array)
variables = $$Util.toList(variables);
this._javaApi.setMMOItemVariables
(
item,
variables,
(fireClientEvent === undefined ? false : fireClientEvent)
);
}
//--- MMOApi helper object -----------------------------------------------------
/**
* A factory object containing an helper method to generate [Vec3D]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/mmo/Vec3D.html} instances.
* @namespace
*/
Vectors = {}
/**
* Creates an instance of [Vec3D]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/mmo/Vec3D.html}, which defines a set of coordinates in a virtual world represented by a MMORoom.
*
* @param {number} x The position along the X axis.
* @param {number} y The position along the Y axis.
* @param {number} z The position along the Z axis.
* @param {boolean} isFloat Force the coordinates to be treated as floating point numbers (even if integers are passed).
*
* @return {Vec3D} A [Vec3D]{@link http://docs2x.smartfoxserver.com/api-docs/javadoc/server/com/smartfoxserver/v2/mmo/Vec3D.html} instance.
*
* @static
*/
Vectors.newVec3D = function (x, y, z, isFloat)
{
if (isFloat)
return $$Helper.makeFloatVector(x, y, z);
else
return $$Helper.makeIntVector(x, y, z);
}
//==============================================================================
//--- File API class -----------------------------------------------------------
//==============================================================================
/**
* Developers never istantiate the FileApi class: this is done internally by the SmartFoxServer 2X API; get a reference to it using the Extension's {@link getFileApi} method.
*
* @class
* The FileApi class provides specialized API calls to interact with the file system.
*
* Typically this method will return a string like "extensions/{name-of-extension}/". * The path is relative to the server root folder and it can be used to load external data files that are stored together with the Extension's JavaScript file(s).
* * @return {string} The path of the current Extension folder. */ FileApi.prototype.getCurrentFolder = function() { return $$WrapperExt.getCurrentFolder(); } /** * Copies a file to a new location. * *This method copies the contents of the specified source path to the specified destination path. * The folder holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
* * @param {string} srcPath The path to an existing file to copy. * @param {string} destPath The path of the destination file. * * @throws An IOException Java exception in case the source or destination is invalid, or an IO error occurs during copying. */ FileApi.prototype.copyFile = function(srcPath, destPath) { this._javaApi.copyFile(new java.io.File(srcPath), new java.io.File(destPath)); } /** * Moves a file to a new location. * * @param {string} srcPath The path to an existing file to be moved. * @param {string} destPath The path of the destination file. * * @throws An IOException Java exception in case the source or destination is invalid, or an IO error occurs during copying. * @throws An FileExistsException Java exception in case the destination file already exists. */ FileApi.prototype.moveFile = function(srcPath, destPath) { this._javaApi.moveFile(new java.io.File(srcPath), new java.io.File(destPath)); } /** * Deletes a file, never throwing an exception. * * @param {string} filePath The path of the file to delete. * * @return {boolean}true
if the file was deleted, false
otherwise.
*/
FileApi.prototype.deleteFile = function(filePath)
{
return this._javaApi.deleteQuietly(new java.io.File(filePath));
}
/**
* Deletes a folder including all its sub-folders, never throwing an exception.
*
* @param {string} dirPath The path of the folder to delete.
*
* @return {boolean} true
if the folder was deleted, false
otherwise.
*/
FileApi.prototype.deleteDirectory = function(dirPath)
{
return this.deleteFile(dirPath);
}
/**
* Makes a directory, including any necessary but nonexistent parent directories.
*
* @param {string} fullPath The path of the direcotry to create.
*
* @throws An IOException Java exception in case the directory cannot be created or the file already exists but is not a directory.
*/
FileApi.prototype.makeDirectory = function(fullPath)
{
this._javaApi.forceMkdir(new java.io.File(fullPath));
}
/**
* Reads the contents of a file into an array of bytes. The file is always closed.
*
* @param {string} filePath The path of the file to read.
*
* @return {byte[]} The file contents as a Java byte array (byte[]). The content of the array can be accessed using the methods of native JavaScript arrays.
*
* @throws An IOException Java exception in case of an I/O error.
*/
FileApi.prototype.readBinaryFile = function(filePath)
{
return this._javaApi.readFileToByteArray(new java.io.File(filePath));
}
/**
* Writes a Java byte array (byte[]) to a file, creating the file if it does not exist.
*
* @param {string} filePath The path of the file to write.
* @param {byte[]} data The Java byte array to write to the file.
*
* @throws An IOException Java exception in case of an I/O error.
*/
FileApi.prototype.writeBinaryFile = function(filePath, data)
{
this._javaApi.writeByteArrayToFile(new java.io.File(filePath), data);
}
/**
* Tests whether the file denoted by the passed path is a normal file.
*
* @param {string} filePath The path of the file to check.
*
* @return {boolean} true
if and only if the file denoted by the passed path exists and is a normal file; false
otherwise.
*/
FileApi.prototype.isFile = function(filePath)
{
return (new java.io.File(filePath).isFile());
}
/**
* Tests whether the file denoted by the passed path is a directory.
*
* @param {string} filePath The path of the file to check.
*
* @return {boolean} true
if and only if the file denoted by the passed path exists and is directory; false
otherwise.
*/
FileApi.prototype.isDirectory = function(filePath)
{
return (new java.io.File(filePath).isDirectory());
}
/**
* Returns the length of the file denoted by the passed path.
*
* @param {string} filePath The path of the file to get the size of.
*
* @return {number} The length, in bytes, of the file denoted by the passed path.
*/
FileApi.prototype.getFileSize = function(filePath)
{
return (new java.io.File(filePath).length());
}