Public API¶
You can access and modify these objects through your definition.js file.
Room¶
-
class
Room()¶ This object is frequently an argument for
definition.jsfiles. You can check a full list of features inlib/rooms.js.Rooms are created (and destroyed) for you by the framework. You just have to interract with it.
-
Room.id¶ String
A unique identifier for the room.
-
Room.players¶ Array[Socket]
Contains players that is in this room.
Warning
This is an array of
socketobjects, notplayer! It’s very important: to access the player objects, write something like this:var player0 = room.players[0].player;
The first element of this array is the room creator.
-
Room.name¶ String
The current name of the room.
-
Room.size¶ Number
The number of available seats. If the room is started, it should be the total number of players.
-
Room.started¶ Boolean
If the room is started (not in waiting stage).
-
Room.currentStage¶ String
The name of the active stage, or
nullif not started.
-
Room.broadcast([channel, ]event[, data])¶ Arguments: - event (string) – Send an event to players browsers
- channel (string) – Send the event to this channel. If null, send to everyone in the room.
- data – Data to send through the event
Here is some examples of available events:
chatMessage({sender: String, message: String}): print a new message in game logclearChat(): clear the game logsetGameInfo(String): change the content of the box in the left-top on the game-screenpreloadSound(Sound): preload a sound in player browsers to avoid further latencyplaySound(Sound)stopSound(Sound)
-
Room.playerInfo([channel, ]player, value)¶ Send more information about a player. Client-side, it will be displayed in players list.
Arguments: - player (Player|Socket) – The player to update
- value (string) – The new value to display (html allowed)
- channel (string) – Send the event to this channel. If null, send to everyone in the room.
-
Room.message([channel, ]message)¶ Send a chat message to players (system message)
Arguments: - message (string) – The message to send (html allowed)
- channel (string) – Send the event to this channel. If null, send to everyone in the room.
-
Room.nextStage(stage[, callback])¶ End the current stage and start another one
Arguments: - stage (string) – The new stage name
- callback (function) –
-
Room.endStage()¶ End the current stage, without starting another one
-
Room.setStageDuration(duration)¶ Change current stage duration
Arguments: - duration (number) – duration in seconds
-
Room.getRemainingTime()¶ Returns: Milliseconds before next stage. Can be “Infinity”
-
Room.resolveUsername(username)¶ - Get player object from username
Arguments: - username (string) –
Returns: Socket associated to this username, or null
Player¶
-
class
Player()¶ One player object is created and associated for each user at room startup.
-
Player.roles¶ Object[Role]Roles of this user. You should not modify this object directly.
-
Player.channels¶ Object[Channels]Subscribed channels for this user, overrides
Player.rolesones. You should not modify this object directly.
-
Player.actions¶ Object[Actions]Subscribed actions for this user, overrides
Player.rolesones. You should not modify this object directly.
-
Player.socket¶ Socket
-
Player.room¶ Room
-
Player.username¶ String
-
Player.setRole(role, value)¶ Add, update or remove a role for a player. Actions and channels attached to the role are silently added for the player.
Arguments: - role (String) – The name of the role (should be consistent)
- value (Role) – Role data, or
nullto remove the role
-
Player.setAction(name, value)¶ Add, update or remove an action for a player
Arguments: - name (String) – The name of the action (should be consistent)
- value (Action) – Action data, or
nullto remove the action
-
Player.setChannel(name, value)¶ Add, update or remove a channel for a player
Arguments: - role (String) – The name of the channel (should be consistent)
- value (Channel) – Channel data, or
nullto remove the player
-
Player.sendAvailableActions()¶ Call this function to update one’s available actions (after updating some properties for instance).
-
Player.emit(event, data)¶ Emit an event for one player only
-
Player.message(m)¶ Send a chat message for one player only
Action¶
-
class
Action()¶ This object contains all mandatory data to build dynamic forms for players ingame.
-
Action.isAvailable¶ function(player) {}Must return
trueif the action is available for the player.
-
Action.type¶ Stringbuttonselect
-
Action.options¶ ObjectContains additionnal information for specific actions.
submit: String(for all): the submit message printed on the buttonchoices: String | Function | Array(for select): the list of available choices for select actions. If the value isplayers, default choices is players’ usernames.
-
Action.execute¶ function(player[, choice])Called during action execution by a player. You don’t need to check the availability, OpenParty does it for you :)
Examples:
var action1 = {
isAvailable: function(player) {
return true;
},
type: "button",
options: {
submit: "BOUM",
},
execute: function(player) {
player.room.message("EVERYTHING IS EXPLODED!");
}
};
var action2 = {
isAvailable: function(player) {
return true;
},
type: "select",
options: {
choices: ["One", "Two"],
submit: "Choose",
},
execute: function(player, choice) {
player.room.message(choice);
}
};
var action3 = {
isAvailable: function(player) {
return player.room.currentStage === "stageA";
},
type: "select",
options: {
choices: function() { return [1,2,3]; },
submit: "Choose",
},
execute: function(player, choice) {
player.room.message("general", choice);
}
};
Channel¶
-
class
Channel()¶ A very simple object for channel management. A channel is a virtual chat room: players can read and/or speak in that channel.
By default, each player is in
generalchannel (read and write accesses). You can remove this behavior by executing the following code:room.players.forEach(function(p) { p.player.setChannel("general", null); });
Each player is also in a private channel (read-only). The name of the channel is
player-<username>
with <username> replaced by the effective username of the user. This feature is just an helper for gamemaster features or private messages (for instance).
-
Channel.r¶ BooleanDetermines read access
-
Channel.w¶ BooleanDetermines write access
-
Channel.n¶ StringThe channel name. Players will see this name on their game screens.
-
Channel.p¶ NumberThe channel priority. Highest priority element is in the top in channels list, and selected by default. It is an optional parameter.
Example of read-only channel:
var channel = {r: true, w: false, n: "My Channel", p: 10};
Role¶
-
class
Role()¶ A role is a combination of some channels and some actions. Because in roleplay games, some players could share the same channels and actions...
-
Role.channels¶ Object[Channel]
-
Role.actiond¶ Object[Action]
Example:
var role = {
channels: {
"channelA": {...},
"channelB": {...}
},
actions: {
"actionA": {...},
"actionB": {...}
}
}
Sound¶
-
class
Sound()¶ A sound is a minimal object containing several information for browsers.
-
Sound.id¶ StringA unique identifier for the sound.
-
Sound.path¶ StringRelative, or absolute path of the sound. You should store your sounds in /public directory.
-
Sound.distant¶ BooleanIs it an absolute path, or not ? Defaults to false.
-
Sound.loop¶ BooleanDefine if the sound should be restarted at the end or not.
-
Sound.volume¶ NumberA number between 0 and 1 for setting sound volume.
Global objects¶
Some usefull objects are loaded as global variables by OpenParty.
-
GET_RANDOM(from, to)¶ Arguments: - from (number) –
- to (number) –
Returns: A random integer between from (included) and to (included).
-
__app¶ The sockpress app for OpenParty. You can use it to add custom routes if required. Check the documentation.