|
| 1 | +-- src/lua/endpoints/sort.lua |
| 2 | + |
| 3 | +-- ========================================================================== |
| 4 | +-- Sort Endpoint Params |
| 5 | +-- ========================================================================== |
| 6 | + |
| 7 | +---@class Request.Endpoint.Sort.Params |
| 8 | +---@field by string Sort mode: "rank" or "suit" |
| 9 | + |
| 10 | +-- ========================================================================== |
| 11 | +-- Sort Endpoint |
| 12 | +-- ========================================================================== |
| 13 | + |
| 14 | +---@type Endpoint |
| 15 | +return { |
| 16 | + |
| 17 | + name = "sort", |
| 18 | + |
| 19 | + description = "Sort the hand by rank or suit (mirrors the in-game Sort buttons)", |
| 20 | + |
| 21 | + schema = { |
| 22 | + by = { |
| 23 | + type = "string", |
| 24 | + required = true, |
| 25 | + description = 'Sort mode: "rank" or "suit"', |
| 26 | + }, |
| 27 | + }, |
| 28 | + |
| 29 | + requires_state = { G.STATES.SELECTING_HAND }, |
| 30 | + |
| 31 | + ---@param args Request.Endpoint.Sort.Params |
| 32 | + ---@param send_response fun(response: Response.Endpoint) |
| 33 | + execute = function(args, send_response) |
| 34 | + sendDebugMessage("sort()", "BB.ENDPOINTS") |
| 35 | + |
| 36 | + -- Gate: manual enum check (the validator has no enum support) |
| 37 | + if args.by ~= "rank" and args.by ~= "suit" then |
| 38 | + send_response({ |
| 39 | + message = 'Sort \'by\' must be "rank" or "suit", got "' .. args.by .. '"', |
| 40 | + name = BB_ERROR_NAMES.BAD_REQUEST, |
| 41 | + }) |
| 42 | + return |
| 43 | + end |
| 44 | + |
| 45 | + if args.by == "rank" then |
| 46 | + G.FUNCS.sort_hand_value({}) |
| 47 | + else |
| 48 | + G.FUNCS.sort_hand_suit({}) |
| 49 | + end |
| 50 | + |
| 51 | + sendInfoMessage("Sorting hand by " .. args.by, "BB.ENDPOINTS") |
| 52 | + |
| 53 | + -- Wait for the hand to be sorted (predicate α) |
| 54 | + G.E_MANAGER:add_event(Event({ |
| 55 | + trigger = "condition", |
| 56 | + blocking = false, |
| 57 | + func = function() |
| 58 | + local done = G.STATE == G.STATES.SELECTING_HAND and G.hand ~= nil |
| 59 | + if done then |
| 60 | + sendDebugMessage("sort() → ok", "BB.ENDPOINTS") |
| 61 | + send_response(BB_GAMESTATE.get_gamestate()) |
| 62 | + end |
| 63 | + return done |
| 64 | + end, |
| 65 | + })) |
| 66 | + end, |
| 67 | +} |
0 commit comments