interfaces utils - wxyz-abcd/node-haxball GitHub Wiki
This object consists of several helper utility functions for developers.
-
generateAuth(): Promise([authKey: string, authObj: Auth])
Generates a new
player_auth_key
along with its companion auth object(auhObj
) to be used as parameters to the functionRoom.join
. We should store the auth key and use it later if we want to be recognized in Haxball rooms. Any internal error will result in a rejected promise.Parameters: None.
Return value: Promise([authKey: string, authObj: Auth]).
Example:
function joinRoomWithNewAuth(roomId, playerName, playerAvatar){ Utils.generateAuth().then(([authKey, authObj])=>{ Room.join({ id: roomId, }, { storage: { player_name: playerName, avatar: playerAvatar, player_auth_key: authKey, // ... }, authObj: authObj, /* ... other parameters here ... */ }); } }
-
authFromKey(authKey: string): Promise(Auth)
Recreates the auth object from the given
authKey
. The returned object is only to be used as parameter to the functionRoom.join
.- Bad inputs or any internal error will result in a rejected promise.
Parameters:
-
authKey: string
: A simplified string that represents an auth object.
Return value: The Auth object that was generated from the given
authKey
.Example:
function joinRoomWithOldAuth(roomId, authKey, playerName, playerAvatar){ Utils.authFromKey(authKey).then((authObj)=>{ Room.join({ id: roomId, }, { storage: { player_name: playerName, avatar: playerAvatar, player_auth_key: authKey, // ... }, authObj: authObj, /* ... other parameters here ... */ }); }, (error)=>{ console.error("Wrong auth key."); }); }
-
getRoomList(): Promise(roomListArray: RoomData[])
Connects to Haxball's backend server, retrieves the current room list and returns it.
Parameters: None.
Return value: A promise that returns the current list of open rooms returned from the Haxball's backend server.
Example:
Utils.getRoomList().then((list)=>{ list.forEach((room)=>{ console.log(room); }); });
-
calculateAllRoomDistances(geo: GeoLocation, roomList: RoomData[]): void
Calculates the distances to the given GeoLocation
geo
of all rooms in givenroomList
and stores them inside each room'sdist
property.Parameters:
-
geo: GeoLocation
: The location to calculate the distances to. -
roomList: RoomData[]
: The room list to update.
Return value: void.
Example:
function getRoomListSortedByDistanceToGeoLocation(geoLocation){ return Utils.getRoomList().then((list)=>{ calculateAllRoomDistances(geoLocation, list); list.sort((a, b) => (a.dist - b.dist)); return list; }); }
-
-
numberToColor(number: int): string
Returns the html color string (rgba representation) of the given
number
. This function is mostly intended to be used in renderers and map editors. Bad inputs will return a bad string output.Parameters:
-
number: int
: A number in the range [0, 16777215].
Return value: The rgba representation of the color that was generated from the given
number
.Example:
function drawTextWithColor(canvas, x, y, text, color){ var ctx = canvas.getContext("2d"); ctx.fillStyle = Utils.numberToColor(color); ctx.fillText(text, x, y); return canvas; }
-
-
colorToNumber(color: string): int
Returns the number representation of the given html color string. (rgba representation) This function is mostly intended to be used in renderers and map editors.
- Bad inputs will return
undefined
. - Alpha value of the input color is not used. (The game engine currently assigns
255
to all alpha values by default.)
Parameters:
-
color: string
: The rgba representation of a color.
Return value: An integer that represents the given
color
.Example:
function createCustomSegment(x1, y1, x2, y2, curve, rgbColor){ return room.state.createSegmentFromObj({ v0: room.state.createVertex({x: x1, y: y1}), v1: room.state.createVertex({x: x2, y: y2}), curve: curve, color: Utils.colorToNumber(rgbColor) }); }
- Bad inputs will return
-
keyState(dirX: int, dirY: int, kick: boolean): int
Returns an integer key state value, only to be used with
Room.setKeyState
.Parameters:
-
dirX: int
: Desired x direction. One of [-1
:left,0
:still,1
:right]. -
dirY: int
: Desired y direction. One of [-1
:up,0
:still,1
:down]. -
kick: boolean
: Desired pressed state of the kick button.
Return value: An integer in the range [0, 31].
Example:
// This will be interpreted as pressing left direction key and kick key, and releasing all other keys. room.setKeyState(Utils.keyState(-1, 0, true));
-
-
reverseKeyState(state: int): object
Returns the explanation of the given key state.
Parameters:
-
state: int
: An integer in the range [0, 31].
Return value:
An object that has the following keys:
-
dirX: int
: X direction. One of [-1
:left,0
:still,1
:right]. -
dirY: int
: Y direction. One of [-1
:up,0
:still,1
:down]. -
kick: boolean
: State of the kick button.
Example:
console.log(Utils.reverseKeyState(3));
-
-
runAfterGameTick(state: int): object
Runs a
callback
function afterticks
game ticks.Parameters:
-
callback: Function<void>
: The function to run. -
ticks: int
: Number of ticks to wait before running the callback function. Defaults to1
.
Return value: void.
Example:
Utils.runAfterGameTick(()=>{ console.log("This code will run after 3 game ticks."); }, 3);
-
-
getGeo(): Promise(GeoLocation)
Connects to Haxball's backend server, retrieves your location based on IP address using backend's geolocation API and returns it.
- Might throw errors.
Parameters: None.
Return value: A promise that returns a GeoLocation object that has your location data retrieved from the backend server.
Example:
function joinRoomWithNewAuthAndBackendGeo(roomId, playerName, playerAvatar){ Utils.getGeo().then((geo)=>{ Utils.generateAuth().then(([authKey, authObj])=>{ Room.join({ id: roomId, }, { storage: { player_name: playerName, avatar: playerAvatar, player_auth_key: authKey, geo: geo, // ... }, authObj: authObj, /* ... other parameters here ... */ }); } }).catch((ex)=>{ console.error("Error:", ex); }); }
-
getDefaultStadiums(): Stadium[]
Returns the default stadium array.
Parameters: None.
Return value: All of the default stadiums as an array.
-
exportStadium(stadium: Stadium): string
Generate and return text content from a
stadium
object that can be directly written in a .hbs file.Return value: string.
Example:
function exportDefaultStadium(index){ var allDefaultStadiums = Utils.getDefaultStadiums(); return Utils.exportStadium(allDefaultStadiums[index]); }
-
parseStadium(textDataFromHbsFile: string, onError: Function): Stadium | undefined
Parses a string as a Stadium object.
Parameters:
textDataFromHbsFile: string
: The string representation of aStadium
object.onError: Function
: A callback that is called if any error occurs.Return value: The generated
Stadium
object, orundefined
if an error occurs.Example:
function changeStadium(room, fileObj){ var fr = new FileReader(); fr.onload = function () { var stadium = Utils.parseStadium(fr.result, console.warn); if (!stadium) return; room.setCurrentStadium(stadium); }; fr.readAsText(fileObj); }
-
stadiumChecksum(stadium: Stadium): string | null
Returns the checksum for the given stadium.
Parameters:
-
stadium: Stadium
: The stadium object whose checksum is to be calculated.
Return value: The checksum string of the given
stadium
, ornull
for default stadiums. -
-
geoFromJSON(json: object): GeoLocation
Creates and returns a
GeoLocation
object from a json object.Parameters:
-
json: object
: An object that has the following keys:-
lat
: Latitude value. -
lon
: Longitude value. -
flag
: Country code.
-
Return value: A
GeoLocation
object. -
-
geoFromString(jsonStr: string): GeoLocation
Creates and returns a
GeoLocation
object from a stringified json object.Parameters:
-
jsonStr: string
: Stringified version of an object that has the following keys:-
lat
: Latitude value. -
lon
: Longitude value. -
flag
: Country code.
-
Return value: A
GeoLocation
object. -