Esc to clear, Enter to open the best match, click an export to open it in its page
whip_autos

Learning from whip_autos

whip_autos is Whiplash, the car customisation shop, with turbos and nitrous. This page shows how a shop that takes money for work on a car is built on WHIP; server owners want whip_autos/README.md.

Skip the camera, the 3D page (html/js/view.js, kit.js), the gearbox computer and the stance maths: they teach nothing about WHIP. fxmanifest.lua lists every file and follows Calling whip_core.

The server prices the basket again

The page sends what was picked and the total it showed, never a price. The purchase is a SecureEvent: near keeps it to the shop floor, and onReject replies to the page even when it's refused early, so the pay button never hangs.

-- server/main.lua (trimmed)
exports.whip_core:SecureEvent('whip_autos:server:purchase', {
    rateLimit = 3000, burst = 2,
    near = SHOP_NEAR,   -- its `at` is a function: a /whiplash visit's floor is wherever its car is
    onReject = function(src, why)
        TriggerClientEvent('whip_autos:client:purchaseResult', src, false, tostring(why))
    end,
    params = {
        { 'vehicleId', 'id' },
        { 'method', 'enum', { 'cash', 'bank' } },
        { 'basket', 'list', { { 'slot', 'any' }, { 'index', 'id', -1, 255 } --[[ , ... ]] }, 40 },
        { 'shown', 'int', 0, 1000000000, -1 },
    },
}, onPurchase)

Paid and fitted in one write

The handler prices every line again from the server's Config, drops parts the car already has (ShopFitted), and refuses a total above the one shown. ChargePlayer, called straight inside the SecureEvent, lands in the same transaction as the car's new look, and a failed fit gives the money back (RefundPlayer).

-- server/main.lua, onPurchase (trimmed)
if shown >= 0 and total > shown then   -- a price raised in /whip mid-visit is refused, not charged
    return fail('The prices changed while you were here. Leave and come back to see them.')
end
local receipt
if total > 0 then
    local payErr
    receipt, payErr = exports.whip_core:ChargePlayer(source, total, { method = method, reason = 'Whiplash customisation' })
    if not receipt then return fail(payErr or 'Payment failed') end
end
if not TuningServer.write(vehicleId, tuningChanges, propsFromBasket(lines)) then   -- one SetVehicleAppearance
    if receipt then exports.whip_core:RefundPlayer(receipt) end
    return fail('Something went wrong fitting that')
end

A claim that every exit gives back

Trying parts on changes the real car for everyone near, so the server keeps one session per car and per player. Every way a visit ends puts the stored look back: the page closing, leaving the driver's seat (whip_core:vehicle:exited), playerDropped, whip_autos stopping. The client calls Whip.core:HoldVehicleAppearance(vId, true) so WHIP doesn't report the unpaid preview as the car's look.

Data on a car that every game applies

An engine swap, stance, handling setup and drivetrain aren't GTA mods, so the server writes them as vehicle data. Every game applies them with WatchVehicleData: onGained/onChanged/onLost for every car in view, or driverOnly with onStart/onStop for the driver alone (a handling layer).

-- client/tuning.lua (trimmed), inside Whip.register: the swapped engine's power, on the car this player drives
exports.whip_core:WatchVehicleData(TUNING_ENGINE_KEY, {
    id = 'enginePower', driverOnly = true,
    onStart = function(entity, value)
        local set = TuningEngineSet(value, GetVehicleHandlingFloat(entity, 'CHandlingData', 'fMass'))
        if set then exports.whip_core:SetVehicleHandlingLayer({ entity = entity }, 'engine', { set = set }) end
    end,
    onStop = function() exports.whip_core:ClearVehicleHandlingLayer('engine') end,
})

Two kinds of vehicle stat

The turbo's boost is a live stat (RegisterVehicleStat): the driver's game simulates it, whip_core caps how fast it rises, and the gauge reads it from whileHeld (Watching vehicle stats). Nitrous is a consume stat: the game bills what it burns, and whip_core caps it and clears the bottle when empty. Fitting a bottle is a declared timed use, on the car the server sees the player in (server/power/nitrous.lua).

Who may change a car

Config.WhoCanModify lets anyone work on a car, only the owner for performance and tuning, or only the owner at all (anyone may still repair). One function both sides load decides it. The server asks it at the claim, to hide shelves, and again at payment: hiding a shelf is for the player, the server is the rule.

-- shared/shop.lua
function ShopMayChange(charId, ownerCharId)
    local rule = Config.WhoCanModify
    if rule ~= 'ownerTuning' and rule ~= 'owner' then return 'all' end
    if ownerCharId == nil or ownerCharId == charId then return 'all' end
    if rule == 'owner' then return 'repair' end
    return 'looks'
end

A settings page, and what's worked out from it

shared/settings.lua puts the prices, power and slider ranges on a page in /whip (Whip.configPage), written into Config when saved. A lookup built from Config is rebuilt in place with Whip.onConfigChanged, so every file holding it sees the change.

-- shared/shop.lua
local function buildServices() refill(SHOP_SERVICES, Config.Services, function(t, _, x) t[x.id] = true end) end
buildServices()
Whip.onConfigChanged('Services', buildServices)

A page of its own

The shop is whip_autos' own NUI page. nui_callback_strict_mode 'true' lets only that page call its callbacks (the purchase spends money), and every callback replies, even on an error, or the page's fetch hangs (cb, client/ui.lua). Whip.padScreen gives it a controller and cursor, and SetScreenOpen stands WHIP's HUD down while it's up.

Director cards

whip_autos adds two Whip.cards, a car task and a builder button (tasks and buttons) to the Director; the files are in the table below.

Where to look#

To see how toRead
Take money for work the server prices itselfserver/main.lua: onPurchase, priceOf, its Whip.register block
Keep a request to a place that can moveserver/main.lua: SHOP_NEAR
Claim a shared thing and give it back on every exitserver/main.lua: onClaim, endSession; client/session.lua: Session.open, teardownLocal
Save data on a car that every game appliesserver/tuning.lua: TuningServer.write; client/tuning.lua: RegisterWithWhip
A stat the driver's game simulatesserver/power/turbo.lua: registerBoostStat; client/power/turbo.lua: whileHeld
A stat the game burns down, and an item fitted under a barserver/power/nitrous.lua: registerNitroStat, registerInstall; client/power/nitrous.lua: billBurn
A handling layer on the car this player drivesclient/tuning.lua; client/power/handling.lua: PowerHandling.driverLayer (every car driven)
A setting that decides who may do whatshared/shop.lua: ShopMayChange; client/catalogue.lua: Catalogue.looksOnly
A settings page that rebuilds what it worked outshared/settings.lua; shared/shop.lua: refill
An NUI page of your ownclient/ui.lua: cb, show; html/js/app.js
An entry on the F1 wheelclient/main.lua: RegisterWithWhip (AddAction with radial)
Director cards, a task and a builder buttonserver/director.lua, shared/director.lua, client/look.lua, server/look.lua
Developer commands, kept out of the shop's filesclient/dev.lua, server/dev.lua