Skip to content

Exports & events

Public API of Cuxial Dance Class: server exports and events to read classes and react to them.

Read the open classes, close one, or react when a class starts, a student joins or a dance ends. Everything here runs on the server; there are no client exports.

Server exports ​

GetRooms ​

Returns every open class.

lua
local rooms = exports.cuxial_danceclass:GetRooms()

Returns: table[], sorted by id. Each entry is a room summary.

lua
for _, room in ipairs(exports.cuxial_danceclass:GetRooms()) do
    print(room.name, room.studentCount .. '/' .. room.maxStudents)
end

GetPlayerRoom ​

Returns the class a player is in and their role.

lua
local room, role = exports.cuxial_danceclass:GetPlayerRoom(source)
ParameterTypeDescription
sourcenumberServer id of the player.

Returns: table | nil, string | nil. The room summary and the role, 'instructor' or 'student'. nil when the player is not in a class.

lua
local room, role = exports.cuxial_danceclass:GetPlayerRoom(source)
if role == 'instructor' then
    print(('Teaching %s'):format(room.name))
end

IsInClass ​

Tells whether a player is in a class, as instructor or student.

lua
local inClass = exports.cuxial_danceclass:IsInClass(source)
ParameterTypeDescription
sourcenumberServer id of the player.

Returns: boolean.

lua
if exports.cuxial_danceclass:IsInClass(source) then return end

CloseRoom ​

Closes a class from another resource. If at least one dance was played, the class ends with its podium and the history is saved; otherwise it just closes and fees are refunded as set in rooms.refundIfNoRound.

lua
local closed = exports.cuxial_danceclass:CloseRoom(id)
ParameterTypeDescription
idnumberId of the class.

Returns: boolean. false when no class has that id.

lua
for _, room in ipairs(exports.cuxial_danceclass:GetRooms()) do
    exports.cuxial_danceclass:CloseRoom(room.id)
end

Room summary ​

FieldTypeDescription
idnumberId of the class.
namestringName of the class.
difficultystringDifficulty id: easy, normal or hard.
difficultyLabelstringDifficulty name from the config.
feenumberEntry fee. 0 = free.
maxStudentsnumberCapacity.
instructortable{ id, name, points }.
studentstable[]{ id, name, points, rounds, accuracy, bestAccuracy }, sorted by points. Accuracy goes from 0 to 1.
studentCountnumberStudents in the class.
phasestringlobby (not started), countdown, round or results (between dances).
roundIndexnumberDances played so far.
maxRoundsnumberDances allowed per class.
playliststring[] | nilDances picked by the instructor. nil = random.
currentDancetable | nil{ command, label } of the current or last dance.
rolestring | nilOnly with GetPlayerRoom: role of that player.

Server events ​

Each event is triggered on the server with one table. Listen with AddEventHandler.

EventPayloadWhen
cuxial_danceclass:roomCreated{ id, instructor, difficulty }A class opens. instructor is a server id.
cuxial_danceclass:roomClosed{ id, reason }A class closes. reason: closed, left, ended or disconnected.
cuxial_danceclass:studentJoined{ id, source }A student joins.
cuxial_danceclass:studentLeft{ id, source, reason }A student leaves a class that stays open. reason: left, zone, kicked or disconnected.
cuxial_danceclass:roundStarted{ id, index, dance }A dance starts. dance is its command.
cuxial_danceclass:roundEnded{ id, index, results }A dance ends.
cuxial_danceclass:classEnded{ id, rounds, ranking }A class with at least one dance ends, right before roomClosed.

Each entry of results has id, name, perfect, good, miss, total, accuracy, maxStreak, points and totalPoints. Each entry of ranking has id, name, points, rounds, accuracy and bestAccuracy.

studentLeft is not sent for the students still inside when a class closes. Use roomClosed for that case.

lua
AddEventHandler('cuxial_danceclass:roundEnded', function(data)
    for _, result in ipairs(data.results) do
        if result.accuracy >= 0.9 then
            print(('%s nailed dance %d'):format(result.name, data.index))
        end
    end
end)

on / off ​

The same events as hooks, without the prefix. on returns a handle and off removes the hook.

lua
local handle = exports.cuxial_danceclass:on(event, fn)
local removed = exports.cuxial_danceclass:off(handle)
ParameterTypeDescription
eventstringroomCreated, roomClosed, studentJoined, studentLeft, roundStarted, roundEnded or classEnded.
fnfunctionReceives the same table as the event.
handlenumberValue returned by on.

Returns: on returns number, or nil when the arguments are not valid. off returns boolean.

lua
local handle = exports.cuxial_danceclass:on('classEnded', function(data)
    local winner = data.ranking[1]
    if winner then print(('%s won the class'):format(winner.name)) end
end)

© Cuxial