diff --git a/libcredit_mobile.lua b/libcredit_mobile.lua index eeae436..c569eeb 100644 --- a/libcredit_mobile.lua +++ b/libcredit_mobile.lua @@ -31,6 +31,41 @@ local function sendRequestAndWaitForReply(messageTable, replyHandler, timeout) return replyReceived end +function M.createAccount(username, associatedDevice, callback) + if type(username) ~= "string" or #username == 0 then + error("Username must be a non-empty string.", 2) + end + if type(callback) ~= "function" then + error("Callback must be a function.", 2) + end + if associatedDevice and type(associatedDevice) ~= "number" then + error("Associated device must be a number if provided.", 2) + end + + local message = { + type = "createAccount", + user = username + } + if associatedDevice then + message.associatedDevice = associatedDevice + end + + return sendRequestAndWaitForReply(message, function(reply) + if reply.type == "accountCreationConfirmation" and reply.user == username then + if reply.status == "success" then + print("Account '" .. username .. "' created successfully. New credit: " .. tostring(reply.newCredit)) + callback(true, reply.newCredit) + else + print("Account creation failed for '" .. username .. "'. Reason: " .. tostring(reply.reason)) + callback(false, reply.reason) + end + else + print("Unexpected reply for createAccount request: " .. textutils.serialiseJSON(reply)) + callback(false, "unexpected_reply") + end + end) +end + function M.getCredit(username, callback) local message = { type = "getCredit",