commonEventCallbacks - wxyz-abcd/node-haxball GitHub Wiki
Default Event Callbacks
There are 6 types of event callbacks in this API.
1. Host-Only Events
These events can only be received by a host room.
RoomLink(link: string)
1.1. The room link was received from Haxball's backend server. Called some time after a room is created successfully. Also called when your room stops sending signal to Haxball's backend server, and starts it again after some time. This can happen if your connection gets interrupted or the room somehow becomes unstable due to bugs.
Parameters:
-
link: string
The room link that was just received.
BansClear()
1.2. All bans have been cleared.
RoomRecaptchaModeChange(on: boolean)
1.3. The room's recaptcha mode was changed.
Parameters:
-
on: boolean
The new value; whether to request recaptcha or not while joining the room.
2. Host-Triggered Events
These events can only be triggered by the room's host.
Announcement(msg: string, color: int, style: int, sound: int)
2.1. An announcement was just made by the room host.
Parameters:
-
msg: string
The announcement message.
-
color: int
The color of the announcement message. Range: -1 <=
color
< 16777216.- The color value can be converted into a rgba string via API's
Utils.numberToColor
function. - The special value
-1
meanstransparent
color.
- The color value can be converted into a rgba string via API's
-
style: int
The style of the announcement message. Must be one of the following:
- 0: use document's default font style.
- 1: fontWeight = "bold".
- 2: fontStyle = "italic".
- 3: fontSize = "12px".
- 4: fontWeight = "bold", fontSize = "12px".
- 5: fontWeight = "italic", fontSize = "12px".
-
sound: int
The sound of the announcement message. Must be one of the following:
- 0: no sound.
- 1: chat sound.
- 2: highlight sound.
PlayerHeadlessAvatarChange(id: int, value: string)
2.2. A player has just changed his/her headless avatar.
Parameters:
-
id: int
Id of the player who triggered this event.
-
value: string
The new headless avatar value.
PlayersOrderChange(idList: Array<int>, moveToTop: boolean)
2.3. The order of players have just been changed.
Parameters:
idList: Array<int>
The ids of players that were removed from the room's players list, reordered to match the order in idList
and added back to the room's players list.
moveToTop: boolean
Whether to add the players to the top or bottom of the room's players list.
SetDiscProperties(id: int, type: int, data1: Array<number>, data2: Array<int>)
2.4. A disc's properties have just been modified.
Parameters:
-
id: int
The id of player or disc whose disc properties have changed.
-
type: int
The type of object. Must be one of the following:
- 0: disc.
- 1: player.
-
data1: Array<number>
Must consist of the following properties of the disc in the same order:[x, y, xspeed, yspeed, xgravity, ygravity, radius, bCoeff, invMass, damping]
. -
data2: Array<int>
Must consist of the following properties of the disc in the same order:[color, cMask, cGroup]
.
PingData(array: Array<int>)
2.5. Ping values for all players have just been updated.
Parameters:
-
array: Array<int>
The updated list of ping values for each player in the same order as the player list in the current room's RoomState object.
RoomPropertiesChange(props: object)
2.6. The room's properties have just been changed.
Parameters:
-
props: object
The properties that were changed. The current structure of this object is as follows:
props = { name: string | null, password: string | null, fakePassword: boolean | null, geo: GeoLocation | null, playerCount: int | null, maxPlayerCount: int | null }
Note that only the changed keys will show up in
props
.
3. Individually-Triggered Events
3.1. Game Events
PlayerBallKick(playerId: int)
3.1.1. The ball has just been kicked.
Parameters:
-
playerId: int
Id of the player who kicked the ball.
TeamGoal(teamId: int)
3.1.2. A goal has just been scored.
Parameters:
-
teamId: int
Id of the team who scored the goal.
GameEnd(winningTeamId: int)
3.1.3. The game has just ended.
Parameters:
-
winningTeamId: int
Id of the team who won the game.
GameTick()
3.1.4. A game tick has just occurred.
Note:
This will run a lot of times per second. Be careful not to make too many calculations here, otherwise the game might slow down.
KickOff()
3.1.5. The game has just kicked-off.
TimeIsUp()
3.1.6. The game has just ended by timeout.
PositionsReset()
3.1.7. The positions have just been reset. This event happens just after a new game has been started or a goal has been scored. The player positions are reset to their corresponding spawn points defined in the current room's Stadium object.
LocalFrame(localFrameNo: int)
3.1.8. A new game frame number has just occurred. This is just like a game tick, but with a reference frame no value. In the future, this event might be merged with the GameTick event.
Parameters:
-
localFrameNo: int
An independent and always increasing frame number value.
CollisionDiscVsDisc(discId1: int, discPlayerId1: int, discId2: int, discPlayerId2: int)
3.1.9. A collision has just happened between two discs.
Parameters:
-
discId1: int
Id of the first collided disc.
-
playerId1: int
The player's id that the first disc belongs to. If the disc is not a player's disc, this value will be null.
-
discId2: int
Id of the second collided disc.
-
playerId2: int
The player's id that the second disc belongs to. If the disc is not a player's disc, this value will be null.
CollisionDiscVsSegment(discId: int, discPlayerId: int, segmentId: int)
3.1.10. A collision has just happened between a disc and a segment.
Parameters:
-
discId: int
Id of the collided disc.
-
discPlayerId: int
The player's id that the disc belongs to. If the disc is not a player's disc, this value will be null.
-
segmentId: int
Id of the collided segment.
CollisionDiscVsPlane(discId: int, discPlayerId: int, planeId: int)
3.1.11. A collision has just happened between a disc and a plane.
Parameters:
-
discId: int
Id of the collided disc.
-
discPlayerId: int
The player's id that the disc belongs to. If the disc is not a player's disc, this value will be null.
-
planeId: int
Id of the collided plane.
3.2. Local Events
ExtrapolationChange(value: int)
3.2.1. The local extrapolation value has just been changed.
Parameters:
-
value: int
The new extrapolation value in milliseconds. Range:
-200 <= value <= 200
.
HandicapChange(value: int)
3.2.2. The local ping handicap value has just been changed.
Parameters:
-
value: int
The new ping handicap value in milliseconds. Range:
0 <= value <= 300
.
RoomRecordingChange(value: true | ArrayBuffer)
3.2.3. Room recording has just been started or stopped.
Parameters:
-
value: true | ArrayBuffer
- If
value
istrue
, recording has just been started, - Else, the recording has just stopped and the recorded data is returned in
value
as anArrayBuffer
. You might want to write the contents to a version-3 replay file.
- If
3.3. API Events
PluginActiveChange(plugin: Plugin)
3.3.1. A plugin has just been activated or deactivated.
Parameters:
-
plugin: Plugin
The plugin which was activated or deactivated. This property stores the current activation status of the plugin:
plugin.active
.
ConfigUpdate(oldRoomConfigObj: RoomConfig, newRoomConfigObj: RoomConfig)
3.3.2. The room's config object has just been replaced by a new one.
Parameters:
-
oldRoomConfigObj: RoomConfig
The old RoomConfig object.
-
newRoomConfigObj: RoomConfig
The new RoomConfig object.
RendererUpdate(oldRendererObj: Renderer, newRendererObj: Renderer)
3.3.3. The room's renderer object has just been replaced by a new one.
Parameters:
-
oldRendererObj: Renderer
The old Renderer object.
-
newRendererObj: Renderer
The new Renderer object.
PluginUpdate(oldPluginObj: Plugin, newPluginObj: Plugin)
3.3.4. An old plugin object has just been replaced by a new one.
Parameters:
-
oldPluginObj: Plugin
The old Plugin object.
-
newPluginObj: Plugin
The new Plugin object.
LanguageChange(abbr: string)
3.3.5. API's language has just been changed.
Parameters:
-
abbr: string
The new language's abbreviation value.
4. Common Events
PlayerSyncChange(playerId: int, value: boolean)
4.1. A player's synchronization status has just changed.
Parameters:
-
playerId: int
Id of the player whose synchronization status has changed.
-
value: boolean
The new synchronization status.
AutoTeams(playerId1: int, teamId1: int, playerId2: int | null, teamId2: int | null, byId: int)
4.2. An "auto" event has just been triggered to automatically move at least one, at most two players from spectators to teams.
Parameters:
-
playerId1: int
Id of the first player affected by this event.
-
teamId1: int
Id of the team which the first player was moved into.
-
playerId2: int | null
Id of the second player affected by this event, or
null
if there was only one spectator when this event was triggered. -
teamId2: int | null
Id of the team which the second player was moved into, or
null
if there was only one spectator when this event was triggered. -
byId: int
Id of the player who has triggered the event.
ScoreLimitChange(value: int, byId: int)
4.3. The score limit has just been changed.
Parameters:
-
value: int
The new score limit value.
-
byId: int
Id of the player who has triggered this event.
TimeLimitChange(value: int, byId: int)
4.4. The time limit has just been changed.
Parameters:
-
value: int
The new time limit value.
-
byId: int
Id of the player who has triggered this event.
PlayerAdminChange(id: int, isAdmin: boolean, byId: int)
4.5. A player's admin rights have just been given/taken.
Parameters:
-
id: int
Id of the player whose admin rights have been given/taken.
-
isAdmin: boolean
The new admin rights status of the player whose id is
id
. -
byId: int
Id of the player who has triggered this event.
PlayerAvatarChange(id: int, value: string)
4.6. A player has just changed his/her avatar.
Parameters:
-
id: int
Id of the player who has changed his/her avatar.
-
value: string
The new avatar value.
PlayerTeamChange(id: int, teamId: int, byId: int)
4.7. A player has been moved to a different team.
Parameters:
-
id: int
Id of the player who has been moved to a different team.
-
teamId: int
Id of the player's new team.
-
byId: int
Id of the player who has triggered this event.
StadiumChange(stadium: Stadium, byId: int)
4.8. The room's current stadium has just been changed.
Parameters:
-
stadium: Stadium
The room's new Stadium object.
-
byId: int
Id of the player who has triggered this event.
TeamsLockChange(value: boolean, byId: int)
4.9. The current room's teams have just been locked/unlocked.
Parameters:
-
value: boolean
The room's new teams lock value.
-
byId: int
Id of the player who has triggered this event.
PlayerObjectCreated(playerObj: Player)
4.10. A player object has just been created. This callback can be used to define custom
Parameters:
-
playerObj: Player
The new Player object that has just been created.
PlayerJoin(playerObj: Player)
4.11. A player has just joined the room.
Parameters:
-
playerObj: Player
The Player object that has just been created.
GamePauseChange(isPaused: boolean, byId: int)
4.12. The game has just been paused or resumed.
Parameters:
-
isPaused: boolean
Whether the game has been paused or not.
-
byId: int
Id of the player who has triggered this event.
PlayerChat(id: int, message: string)
4.13. A chat message has been received.
Parameters:
-
id: int
Id of the player who has sent the chat message.
-
message: string
The chat message.
PlayerInputChange(id: int, value: int)
4.14. A player's input has just been changed.
Parameters:
-
id: int
Id of the player whose input has been changed.
-
value: int
The new input value of the player.
PlayerChatIndicatorChange(id: int, value: boolean)
4.15. A player has just activated or deactivated his/her chat indicator.
Parameters:
-
id: int
Id of the player whose chat indicator has been activated/deactivated.
-
value: boolean
Whether the chat indicator has been activated or not.
PlayerLeave(playerObj: Player, reason: string | null, isBanned: boolean, byId: int)
4.16. A player has just left the room.
Parameters:
-
playerObj: Player
The player who has left the room.
-
reason: string | null
The reason of leaving the room. If
null
, the player has left by himself/herself. -
isBanned: boolean
Whether the player has been banned or not. If
reason
isnull
, this value is ignored. -
byId: int
Id of the player who has kicked/banned the player. If
reason
isnull
, this value is ignored.
TeamColorsChange(teamId: int, value: TeamColors, byId: int)
4.17. A player has just left the room.
Parameters:
-
teamId: int
The team whose colors have been changed.
-
value: TeamColors
The new team colors value.
-
byId: int
Id of the player who has changed the team colors.
KickRateLimitChange(min: int, rate: int, burst: int, byId: int)
4.18. The room's kick rate limit has just been changed.
Parameters:
-
min: int
The new
min
part of kick rate limit. -
rate: int
The new
rate
part of kick rate limit. -
burst: int
The new
burst
part of kick rate limit. -
byId: int
Id of the player who has changed the kick rate limit.
GameStart(byId: int)
4.19. The game has just been started.
Parameters:
-
byId: int
Id of the player who has started the game.
GameStop(byId: int)
4.20. The game has just been stopped.
Parameters:
-
byId: int
Id of the player who has stopped the game.
5. Modifier Callbacks
Beware that these callbacks do not exist in Renderer objects, but they do in all RoomConfig and Plugin objects.
[modifiedName, modifiedFlag, modifiedAvatar] = modifyPlayerData(playerId: int, name: string, flag: string, avatar: string, conn: string, auth: string)
5.1. If defined, modifies the player's properties just before the player has joined the room.
Parameters:
-
playerId: int
Id of the new player who is about to join the room.
-
name: int
Name of the new player who is about to join the room.
-
flag: string
Country code of the new player who is about to join the room.
-
avatar: string
Avatar of the new player who is about to join the room.
-
conn: string
Connection string of the new player who is about to join the room.
-
auth: string
Auth string of the new player who is about to join the room.
Expected return values:
-
return null
:Blocks the player from joining the room.
-
return [modifiedName: string, modifiedFlag: string, modifiedAvatar: string]
:Modifies the name, flag and avatar values.
Example usage:
this.modifyPlayerData = function(playerId, name, flag, avatar, conn, auth){
if (name=="abc")
return null; // block anyone trying to join the room with name "abc", before he can join the room.
return [
"[" + playerId + "] " + name, // prefix everyone's name with [playerId]
"tr", // set everyone's flag to tr
avatar // do not change avatars
];
};
newPing = modifyPlayerPing(playerId: int, ping: int)
5.2. If defined, runs for all players except host in a host room. Modifies the ping
value of the player whose id is playerId
.
NOTE:
Host player's ping will not change using this callback. In order to change host player's ping, you need to modify room.hostPing
value directly.
Parameters:
-
playerId: int
Id of the current player.
-
ping: int
Current ping value of the current player.
Expected return values:
-
return newPing: int
:Sets the ping of the current player to
newPing
.
Example usage:
this.modifyPlayerPing = function(playerId, ping){
return 100000 + ping*ping*ping; // set everybody(except host)'s ping to 100000 + its original value cubed.
};
newPing = modifyClientPing(ping: int)
5.3. If defined, runs only for the current player in a client room and modifies its ping
value.
Parameters:
-
ping: int
Current ping value of the current player.
Expected return values:
-
return newPing: int
:Sets the ping of the current player to
newPing
.
Example usage:
this.modifyClientPing = function(ping){
return 100000 + ping*ping*ping; // set your ping to 100000 + its original value cubed;
};
newFrameNo = modifyFrameNo(frameNo: int)
5.4. If defined, expects us to return the physics engine's new current frameNo
value, which tells the physics engine that it is currently on a different frame than expected, which causes your player to look laggy to your opponents, especially on extrapolated clients.
Parameters:
-
frameNo: int
Current frameNo value of the physics engine.
Expected return values:
-
return newFrameNo: int
:Sets the frameNo of the physics engine to
newFrameNo
.
Example usage: (This is also known as the "opMode" cheat)
this.modifyFrameNo = function(frameNo){
return frameNo - 3; // This tells the physics engine that it is 3 frames behind the actual reference frame.
};
acceptEvent = onOperationReceived(type: int, msg: object, globalFrameNo: int, clientFrameNo: int)
5.5. If defined, runs for each message received from all clients in a host room.
Parameters:
-
type: int
Type of the received message. We have to look it up in the global
OperationType
object to understand what type of message it actually is. -
msg: object
The original message object. We can directly modify all contents of this object here as we wish.
-
globalFrameNo: int
The global frame no that host's physics engine is at, at the time that the message is received.
-
clientFrameNo: int
The frame no that this client's physics engine is at, at the time that the message is received.
Expected return values:
-
true
: accept event. -
false
: block message from being processed. -
throw exception
: break the connection of the sender of this message.
Example usage:
this.onOperationReceived = function(type, msg, globalFrameNo, clientFrameNo, customData){
switch (type){
case OperationType.KickBanPlayer: { // if someone is leaving or being kicked/banned by someone else
var reason = msg.reason; // get the reason. this is null if the player is leaving by himself/herself.
if (reason!=null && playerId!=0) // if any player sends a kick/ban event message other than room host
return false; // block the event message
break;
}
}
return true;
};
6. Custom Events
These events will only work on host or client rooms that are using this API. The other players will still be able to play the game normally unless these events trigger an important physical change. Synchronization issues might arise in such cases, and the solution to these issues is to create your own Haxball website with this API and invite people there.
CustomEvent(type: int, data: object, byId: int)
6.1. A custom event has just been triggered.
Parameters:
-
type: int
Any integer value to hold the type of the custom event.
-
data: object
Any JSON object to store the properties of the custom event. This object is converted to a string and sent/received by Haxball's original event mechanism.
-
byId: int
Id of the player who has triggered this custom event.