ฟังก์ชัน Hook
ฟังก์ชัน Hook สำหรับปรับแต่งลำดับการทำงาน (config_function_server.lua / config_function_client.lua)
ไฟล์ config_function_server.lua และ config_function_client.lua รวมฟังก์ชัน Hook ที่ทำงานในลำดับต่าง ๆ ของระบบ เช่น ก่อนซื้อ หลังซื้อ ก่อนเติมเงิน
Hook ฝั่งเซิร์ฟเวอร์ (config_function_server.lua)
ลำดับการซื้อ
ก่อนหักเงิน
Config_Function.OnBeforePurchase = function(xPlayer, item, count, currency)
-- return false เพื่อบล็อกการซื้อ
return true
endซื้อสำเร็จ
Config_Function.OnPurchaseSuccess = function(source, itemname, itemcount)
-- ส่งการแจ้งเตือนอัตโนมัติ
endซื้อล้มเหลว
Config_Function.OnPurchaseFailed = function(source, item, reason)
-- แสดงเหตุผลที่ล้มเหลว
endคืนเงิน (กรณี error)
Config_Function.OnPurchaseRefund = function(xPlayer, item, count, currency, refundAmount)
-- คืน Point/Coin ให้ผู้เล่น
endช่องเก็บของเต็ม
Config_Function.OnInventoryFull = function(source, itemname, itemcount)
-- แจ้งเตือนกรณี inventory เต็ม
endถึงลิมิตการซื้อ
Config_Function.OnPurchaseLimitReached = function(source, name, count, limit_max, limit_count)
-- แจ้งเตือนกรณีซื้อเกินลิมิต
endสินค้าหมด
Config_Function.OnOutOfStock = function(source, itemname, requestedCount)
-- แจ้งเตือนกรณีสต็อกหมด
endลำดับการเติมเงิน
ก่อนเติม
Config_Function.OnBeforeTopup = function(source, amount, method)
return true
endเติม Point สำเร็จ
Config_Function.OnPointTopupSuccess = function(source, point, allpoint)
-- แจ้งเตือนผู้เล่น
endเติม Coin สำเร็จ
Config_Function.OnCoinTopupSuccess = function(source, coin, allcoin)
-- แจ้งเตือนผู้เล่น
endเติมเงิน cancel/refund
Config_Function.OnTopupCancelled = function(source, amount, method, reason)
-- แจ้งเตือนกรณียกเลิก
endลำดับการเปลี่ยนชื่อ/เบอร์
เปลี่ยนชื่อสำเร็จ
Config_Function.OnNameChangeSuccess = function(source, newFirstName, newLastName, pointsUsed, coinsUsed)
-- แจ้งผู้เล่น
endเปลี่ยนเบอร์สำเร็จ
Config_Function.OnPhoneChangeSuccess = function(source, oldPhone, newPhone, pointsUsed, coinsUsed)
-- แจ้งผู้เล่น
endระบบการันตี
ความคืบหน้าการสะสม
Config_Function.OnGuaranteeProgress = function(xPlayer, itemKey, oldProgress, newProgress)
-- ส่ง webhook ทุก 1000 บาท เช่น
endรับรางวัล
Config_Function.OnGuaranteeClaim = function(xPlayer, itemKey, tierAmount, reward)
-- reward = { name, count, label }
endรีเซ็ตยอดสะสม
Config_Function.OnGuaranteeReset = function(xPlayer, itemKey)
-- แจ้งผู้เล่น
endลำดับการตรวจสลิป
สลิปผ่าน
Config_Function.OnSlipVerified = function(source, slipData)
-- ส่ง webhook แจ้งแอดมิน
endสลิปถูกปฏิเสธ
Config_Function.OnSlipRejected = function(source, reason)
-- แจ้งผู้เล่น
endตรวจสอบข้อมูลสลิปเพิ่มเติม
Config_Function.ValidateSlipPayload = function(slipData)
-- ตัวอย่าง: บังคับยอดขั้นต่ำ
-- if (slipData.amount or 0) < 50 then
-- return false, "ยอดต่ำกว่า 50 บาท ไม่อนุมัติ"
-- end
return true
endลำดับการโหลดผู้เล่น
ผู้เล่นโหลดเสร็จ
Config_Function.OnPlayerLoaded = function(xPlayer)
-- sync custom data, fetch external API
endผู้เล่นออกจากเกม
Config_Function.OnPlayerDropped = function(source, identifier)
-- cleanup external session
endการจัดการยานพาหนะ
เพิ่มยานพาหนะเข้า DB
Config_Function.InsertOwnerVehicle = function(xPlayer, vehicle)
-- เก็บลง owned_vehicles
-- sync กับ garage/key system
endสร้างข้อมูลยานพาหนะ
Config_Function.BuildOwnedVehicleData = function(xPlayer, item)
-- สร้าง vehicle props (mod, color, plate)
endสุ่มทะเบียน
Config_Function.GenerateVehiclePlate = function(xPlayer)
-- สุ่มทะเบียน ABC 123
endการจัดการ Inventory
ให้ไอเทม
Config_Function.GiveItem = function(xPlayer, name, count)
-- ค่าเริ่มต้นใช้ xPlayer.addInventoryItem
-- ถ้าใช้ ox_inventory:
-- return exports.ox_inventory:AddItem(xPlayer.source, name, count)
endเอาไอเทมออก
Config_Function.RemoveItem = function(xPlayer, name, count)
-- ค่าเริ่มต้นใช้ xPlayer.removeInventoryItem
endตรวจว่ามีไอเทมเพียงพอ
Config_Function.HasItem = function(xPlayer, name, count)
-- ตรวจสอบจำนวนไอเทมในตัวผู้เล่น
endการตรวจสอบสิทธิ์
Config_Function.IsAdmin = function(source)
-- ตรวจ ace permission ก่อน
-- แล้วตรวจ ESX group
return false
endHook ฝั่ง Client (config_function_client.lua)
ส่งสลิปสำเร็จ
Config_Function.OnSlipUploaded = function()
-- แจ้งเตือนผู้เล่นหลังส่งสลิป
endคำนวณราคาเปลี่ยนชื่อ (UI)
Config_Function.CalculateNameChangePrice = function(xPlayer, newFirstName, newLastName, changeType)
-- ส่งไปที่ NameChangeConfig.CalculatePrice
endการใช้งาน
การทำงานอัตโนมัติ
ฟังก์ชันเหล่านี้ถูกเรียกอัตโนมัติในลำดับที่เหมาะสม คุณเพียงแก้ไข logic ภายใน ไม่ต้องเรียกใช้เอง
ตัวอย่างการใช้งานจริง
เพิ่ม webhook แจ้งเตือนทุกครั้งที่ผู้เล่นซื้อของ:
Config_Function.OnPurchaseSuccess = function(source, itemname, itemcount)
Notify(source, "purchase_success", { name = itemname, count = itemcount or 1 })
-- เพิ่ม webhook ส่วนตัว
local playerName = GetPlayerName(source)
PerformHttpRequest('https://your-webhook-url', function(err, text, headers) end, 'POST',
json.encode({content = playerName .. ' ซื้อ ' .. itemname .. ' x' .. itemcount}),
{ ['Content-Type'] = 'application/json' })
endบล็อกการซื้อบางไอเทมสำหรับผู้เล่นบางกลุ่ม:
Config_Function.OnBeforePurchase = function(xPlayer, item, count, currency)
if item.spawnName == 'WEAPON_PISTOL' and xPlayer.getJob() ~= 'police' then
return false, 'อาวุธนี้ซื้อได้เฉพาะตำรวจเท่านั้น'
end
return true
end