Skip to content

Profiles, items & progress ​

Three additions of 1.1 that let the np_* resources move their old minigames onto np_minigames without losing what they did around them.

Profiles + receipts ​

For flows that run on the client ("the client runs a skill check, then tells the server success"): prison jobs, crafting, digging. The server registers what the player must play, the client starts it, and the server checks the outcome with a one-time receipt. The client can't pick an easier game, difficulty or params, and it can't forge a success.

lua
-- server, once at start. The name is namespaced with the calling resource unless it already contains ':'.
local name = exports.np_minigames:RegisterProfile('dig', {
  game = 'skillcheck', levels = { 'easy', 'medium', 'medium' },   -- any game opts: difficulty, params, label, sub,
                                                                   -- icon, tint, item, maxMove, cancelable, onProgress
  cooldownMs = 0,                                                  -- per-player time between two runs
})                                                                 -- -> 'np_prison:dig' | nil, err
exports.np_minigames:UnregisterProfile('dig')                      -- optional; profiles go away when you stop

-- client: blocks. A server-validated session on the profile's terms; the client can only add label / sub / title.
local ok, result = exports.np_minigames:PlayProfile('np_prison:dig', { sub = 'Tunnel' })
TriggerServerEvent('np_prison:dig:finish', result.receipt)

-- server
local result, err = exports.np_minigames:Redeem(src, receipt, 'dig')   -- profile name: short or full, optional
if result and result.success then GiveLoot(src) end
Export Side Returns Errors
RegisterProfile(name, profile) server full name invalid_name, invalid_profile, unknown_game, invalid_item, taken (another resource owns the name)
UnregisterProfile(name) server boolean
PlayProfile(name, cosmetic?) client ok, result unknown_profile, cooldown, busy, no_item (no receipt)
Redeem(src, receipt, profile?) server result | nil, err no_receipt (unknown or forged), used, expired, wrong_player, wrong_profile
  • Every profile session that ends gets a receipt, failures included (result.success == false), so a fail can be punished on the server too. There is none for dropped and api (no outcome).
  • The receipt is in the submit reply and in the close event, so PlayProfile gets it whichever way the session ends.
  • Redeem works once, only for the player who played, and only within Config.Security.receiptTtlMs (120 s). It returns the full result, with item, profile and receipt.
  • A player's receipts are dropped when they leave. A resource's profiles are removed when it stops, and its running profile sessions end with api. Register them again when np_minigames restarts (see Integrating → Profile + receipt).

Items ​

opts.item on Play, Start, Chain, Team, TeamAsync and profiles:

lua
item = 'lockpick'   -- short for { name = 'lockpick' }
item = {
  name = 'lockpick',
  count = 1,          -- needed, and removed when the rule matches (default 1)
  require = true,     -- refuse with reason 'no_item' when the player has fewer (default true)
  remove = 'fail',    -- 'never' (default) | 'fail' | 'success' | 'always'
  chance = 0.35,      -- probability the removal happens when `remove` matches (default 1)
  degrade = 20,       -- durability points taken per use, on every outcome (np_inventory / ox_inventory / qb)
}
  • The item is checked before the game starts. Nothing is used up when the session ends with dropped, api or replaced.
  • degrade lowers the first stack's durability (np_inventory and ox_inventory metadata.durability, qb info.quality), clamped at 0. It is skipped when the item was removed. With np_inventory it goes through DegradeItem.
  • Chain: an item in the shared opts belongs to the whole chain. It is checked before the first step and used up once on the chain's outcome, reported on the last result. An item inside a step's opts applies to that step.
  • Team: every member needs the item; the round is refused before anyone starts. Each member's own item is used up by their own result.
  • The inventory is detected at start: np_inventory, then ox_inventory, then qb-inventory (ps- / lj- forks), then the framework (es_extended, qbx_core, qb-core player functions, no durability). Config.Inventory forces one. Without any inventory, opts.item is ignored and a warning is printed once per item.

The result reports what happened: result.item = { name, removed, degraded, durability }.

Resources that own their item

Several np_* resources keep their tool items to themselves (np_banking's thermite and laptop, np_passwords' code breaker, np_inventory's lockpick) and don't pass opts.item: they check and use up the item around the game, as before. opts.item is for resources that want np_minigames to do it.

Progress ​

Every game reports a cosmetic 0..100 progress (cells solved, pins set, rounds cleared, depth drilled). When the caller asks for it (onProgress, progress = true, or a team game), the client forwards it in 5 % steps, at most every 0.4 s; the server calls onProgress(p) and fires np_minigames:progress. Use it for things like an ATM screen's trace bar (np_banking does exactly that). Never pay out on progress: it comes from the client.

lua
exports.np_minigames:Play(src, 'hexhack', { onProgress = function(p) SetAtmTrace(atm, p) end })
AddEventHandler('np_minigames:progress', function(src, p, gameId, invokingResource) end)   -- server-local

The client Play / PlayAsync exports take onProgress too (called on the client).