diff --git a/ticketmachine.lua b/ticketmachine.lua index 18c3cfa..64e49b4 100644 --- a/ticketmachine.lua +++ b/ticketmachine.lua @@ -541,9 +541,15 @@ local function buildFinalCardData(payload, respData) } end +local function generateNumericCode(prefix, digits) + local width = math.max(1, math.floor(tonumber(digits) or 1)) + local maxValue = (10 ^ width) - 1 + local num = string.format('%0' .. tostring(width) .. 'd', math.random(0, maxValue)) + return tostring(prefix or 'ID'):upper() .. '-' .. num +end + local function generateCardId() - local num = string.format('%06d', math.random(0, 999999)) - return 'IC-' .. num + return generateNumericCode('IC', 6) end local function issueTicketFromPeripheral(fromNameEnArg, toNameEnArg, apiType, rides, cost, startStationArg, terminalStationArg, fromNameCnUArg, toNameCnUArg, fallbackTicketId) @@ -2002,12 +2008,7 @@ local function showPrePrintCheck() end local function generateTicketId() - local function randLetter() - return string.char(string.byte('A') + math.random(0, 25)) - end - local prefix = randLetter() .. randLetter() - local num = string.format('%08d', math.random(0, 99999999)) - return prefix .. '-' .. num + return generateNumericCode('TK', 8) end local function ensureTicketIdFormat(id)