-- @file PlayerStatusReporter.lua
-- @description 玩家状态上报
-- @author Q1968023646
-- @version 1.1
-- @date 2026-03-26
local PlayerStatusReporter = {}
local API_URL = "http://192.168.2.38:8000/api/player/"
-- 用于事件节流的时间戳
local lastEventReportTime = 0
local THROTTLE_INTERVAL = 3 -- 秒
-- http请求的回调
local function httpCB(status, msg)
-- SL:Print("Failed to report player status: " .. tostring(status) .. " - " .. tostring(msg))
-- if status >= 200 and status < 300 then
-- --SL:Print("Player status reported successfully.")
-- else
-- SL:Print("Failed to report player status: " .. tostring(status) .. " - " .. tostring(msg))
-- end
end
-- 收集并上报玩家数据
function PlayerStatusReporter:reportStatus()
local userid = SL:GetMetaValue("USER_ID")
local playerData = {
serveridx = SL:GetMetaValue("SERVER_ID"),
account = SL:GetMetaValue("UID"),
userid = userid,
username = SL:GetMetaValue("USER_NAME"),
job = SL:GetMetaValue("JOB"),
level = SL:GetMetaValue("LEVEL"),
relevel = SL:GetMetaValue("RELEVEL"),
job_name = SL:GetMetaValue("JOB_NAME"),
gender = SL:GetMetaValue("SEX"),
-- drop = SL:GetMetaValue("DROP"),
maxhp = SL:GetMetaValue("MAXHP"),
maxac = SL:GetMetaValue("MAXAC"),
maxmac = SL:GetMetaValue("MAXMAC"),
maxdc = SL:GetMetaValue("MAXDC"),
maxmc = SL:GetMetaValue("MAXMC"),
maxsc = SL:GetMetaValue("MAXSC"),
exp = SL:GetMetaValue("EXP"),
dress = SL:GetMetaValue("DRESS"),
weapon = SL:GetMetaValue("WEAPON"),
righthand = SL:GetMetaValue("RIGHTHAND"),
helmet = SL:GetMetaValue("HELMET"),
necklace = SL:GetMetaValue("NECKLACE"),
ring_r = SL:GetMetaValue("RINGR"),
ring_l = SL:GetMetaValue("RINGL"),
armring_r = SL:GetMetaValue("ARMRINGR"),
armring_l = SL:GetMetaValue("ARMRINGL"),
bujuk = SL:GetMetaValue("BUJUK"),
belt = SL:GetMetaValue("BELT"),
boots = SL:GetMetaValue("BOOTS"),
charm = SL:GetMetaValue("CHARM"),
shield = SL:GetMetaValue("EQUIPBYPOS", 16),
gold = SL:GetMetaValue("MONEY", 1),
gamegold = SL:GetMetaValue("MONEY", 2),
bgamegold = SL:GetMetaValue("MONEY", 4),
gamediamond = SL:GetMetaValue("MONEY", 5),
bgamediamond = SL:GetMetaValue("MONEY", 17),
gamegird = SL:GetMetaValue("MONEY", 7),
paypoint = SL:GetMetaValue("MONEY", 11),
spend = SL:GetMetaValue("SERVER_VALUE", "U3") ,
earn = SL:GetMetaValue("SERVER_VALUE", "U4"),
activity = tonumber(SL:GetMetaValue("SERVER_VALUE", "HUMAN(沙城活跃)")) or 0,
maptitle = SL:GetMetaValue("MAP_NAME") or "",
guild = SL:GetMetaValue("ACTOR_GUILD_NAME", userid),
x = SL:GetMetaValue("X"),
y = SL:GetMetaValue("Y"),
}
local jsonData = json.encode(playerData)
SL:HTTPRequestPost(API_URL, httpCB, jsonData, { ["Content-Type"] = "application/json" })
end
-- LUA_EVENT_ENTER_WORLD: 进入游戏事件,延迟6秒等待服务器变量加载后上报
SL:RegisterLUAEvent("LUA_EVENT_ENTER_WORLD", "LUA_EVENT_ENTER_WORLD", function()
SL:ScheduleOnce(function()
PlayerStatusReporter:reportStatus()
-- LUA_EVENT_MONEY_CHANGE: 钱包变更事件,节流3秒上报
SL:RegisterLUAEvent("LUA_EVENT_MONEYCHANGE", "LUA_EVENT_MONEYCHANGE", function()
local currentTime = os.time()
if currentTime - lastEventReportTime > THROTTLE_INTERVAL then
lastEventReportTime = currentTime
PlayerStatusReporter:reportStatus()
end
end)
end
, 6)
end)
-- LUA_EVENT_LEAVE_WORLD: 离开游戏事件,立即上报
SL:RegisterLUAEvent("LUA_EVENT_LEAVE_WORLD", "LUA_EVENT_LEAVE_WORLD", function()
PlayerStatusReporter:reportStatus()
SL:UnRegisterLUAEvent("LUA_EVENT_MONEYCHANGE", "LUA_EVENT_MONEYCHANGE")
end)
-- LUA_EVENT_RECONNECT: 重连事件,立即上报
SL:RegisterLUAEvent("LUA_EVENT_RECONNECT", "LUA_EVENT_RECONNECT", function()
PlayerStatusReporter:reportStatus()
end)
-- LUA_EVENT_DISCONNECT: 断开连接事件,立即上报
SL:RegisterLUAEvent(LUA_EVENT_DISCONNECT, "LUA_EVENT_DISCONNECT", function()
PlayerStatusReporter:reportStatus()
SL:UnRegisterLUAEvent(LUA_EVENT_MONEYCHANGE, "LUA_EVENT_MONEYCHANGE")
end)
return PlayerStatusReporter