-- local function _ensure_sql() -- if type(_G.Sql_con) == "function" then -- return true -- end -- pcall(require, "Sql_con") -- if type(_G.Sql_con) == "function" then -- return true -- end -- pcall(dofile, "server_data/script/Sql_con.lua") -- if type(_G.Sql_con) == "function" then -- return true -- end -- pcall(dofile, "../server_data/script/Sql_con.lua") -- return type(_G.Sql_con) == "function" -- end -- local function _now() -- return os.date("%Y-%m-%d %H:%M:%S") -- end -- -- Logging was used for debugging; keep stubs (no-op) to avoid spam in production. -- local function _log(_) end -- local function _log_ok(_, _) end -- local function _log_val(_, _, _) end -- local function _sql_escape(v) -- local s = tostring(v or "") -- s = s:gsub("\\", "\\\\") -- s = s:gsub("'", "''") -- return s -- end -- local function _to_int(v) -- local n = tonumber(v) -- if n == nil then -- return 0 -- end -- return math.floor(n) -- end -- local function _get_method(obj, name) -- if obj == nil then -- return nil -- end -- local t = type(obj) -- if t ~= "userdata" and t ~= "table" then -- return nil -- end -- local ok, v = pcall(function() -- return obj[name] -- end) -- if not ok then -- return nil -- end -- if type(v) == "function" then -- return v -- end -- return nil -- end -- local function _cursor_fetch1(cur) -- local fetch = _get_method(cur, "fetch") -- if not fetch then -- return nil -- end -- local ok, v = pcall(fetch, cur) -- if not ok then -- return nil -- end -- if type(v) == "table" then -- return v[1] -- end -- return v -- end -- local function _cursor_fetch2(cur) -- local fetch = _get_method(cur, "fetch") -- if not fetch then -- return nil, nil -- end -- local ok, v1, v2 = pcall(fetch, cur) -- if not ok then -- return nil, nil -- end -- if type(v1) == "table" then -- return v1[1], v1[2] -- end -- return v1, v2 -- end -- local function _cursor_close(cur) -- local close = _get_method(cur, "close") -- if close then -- pcall(close, cur) -- end -- end -- local function _exec_cursor(con, sql) -- local ok, r1, r2 = pcall(function() -- return con:execute(sql) -- end) -- if ok and r1 ~= nil then -- _G._magda_sql_last_error = nil -- return r1 -- end -- local reason = ok and r2 or r1 -- _G._magda_sql_last_error = tostring(reason) -- _G.con = nil -- local con2 = (type(_G.Sql_con) == "function") and _G.Sql_con() or nil -- if con2 and type(con2.execute) == "function" then -- ok, r1, r2 = pcall(function() -- return con2:execute(sql) -- end) -- if ok and r1 ~= nil then -- _G._magda_sql_last_error = nil -- return r1 -- end -- reason = ok and r2 or r1 -- _G._magda_sql_last_error = tostring(reason) -- end -- return nil -- end -- local function _exec_select1(con, sql) -- local cur = _exec_cursor(con, sql) -- local v = _cursor_fetch1(cur) -- _cursor_close(cur) -- return v -- end -- local function _exec(con, sql) -- local cur = _exec_cursor(con, sql) -- if cur == nil then -- return false -- end -- -- con:execute for non-SELECT may return a number (affected rows). Treat cursor return as success too. -- _cursor_close(cur) -- return true -- end -- local function _get_top(con, table_name) -- local rating_count = 0 -- local RoleID_rating = {} -- local RoleID_point = {} -- local count_sql = string.format( -- "SELECT Count(RoleID) As Count FROM (SELECT RoleID FROM %s ORDER BY Reiting DESC LIMIT 10) as t", -- table_name -- ) -- rating_count = _to_int(_exec_select1(con, count_sql)) -- local top_sql = string.format("SELECT RoleID, Reiting FROM %s ORDER BY Reiting DESC LIMIT 10", table_name) -- local cur = _exec_cursor(con, top_sql) -- if cur and type(cur.fetch) == "function" then -- local i = 1 -- while true do -- local rid, pts = _cursor_fetch2(cur) -- if rid == nil then -- break -- end -- RoleID_rating[i] = rid -- RoleID_point[i] = pts -- i = i + 1 -- end -- _cursor_close(cur) -- end -- return rating_count, RoleID_rating, RoleID_point -- end -- local function _get_gift_flag(con, table_name, RoleID) -- if RoleID == nil then -- return 0 -- end -- local rid = _to_int(RoleID) -- if rid <= 0 then -- return 0 -- end -- local sql = string.format("SELECT Gift FROM %s WHERE RoleID = '%s' LIMIT 1", table_name, _sql_escape(rid)) -- return _to_int(_exec_select1(con, sql)) -- end -- local function _get_my_points(con, table_name, RoleID) -- local rid = _to_int(RoleID) -- if rid <= 0 then -- return 0 -- end -- local sql = string.format("SELECT Reiting FROM %s WHERE RoleID = '%s' LIMIT 1", table_name, _sql_escape(rid)) -- return _to_int(_exec_select1(con, sql)) -- end -- local function _exists_role(con, table_name, RoleID) -- local rid = _to_int(RoleID) -- if rid <= 0 then -- return false -- end -- local sql = string.format("SELECT RoleID FROM %s WHERE RoleID = '%s' LIMIT 1", table_name, _sql_escape(rid)) -- local v = _exec_select1(con, sql) -- return v ~= nil -- end -- local function _upsert_add_points(con, table_name, RoleID, add_points) -- local rid = _to_int(RoleID) -- local pts = _to_int(add_points) -- if rid <= 0 or pts == 0 then -- return -- end -- if not _exists_role(con, table_name, rid) then -- local ins = string.format( -- "INSERT INTO %s (RoleID, Reiting, Gift) VALUES ('%s', '%d', 0)", -- table_name, -- _sql_escape(rid), -- pts -- ) -- if not _exec(con, ins) then -- _log(string.format("UPSERT INSERT failed: table=%s RoleID=%d pts=%d err=%s", table_name, rid, pts, tostring(_G._magda_sql_last_error))) -- end -- else -- local upd = string.format( -- "UPDATE %s SET Reiting = Reiting + %d WHERE RoleID = '%s'", -- table_name, -- pts, -- _sql_escape(rid) -- ) -- if not _exec(con, upd) then -- _log(string.format("UPSERT UPDATE failed: table=%s RoleID=%d pts=%d err=%s", table_name, rid, pts, tostring(_G._magda_sql_last_error))) -- end -- end -- end -- local function _set_gift(con, table_name, RoleID) -- local rid = _to_int(RoleID) -- if rid <= 0 then -- return -- end -- local upd = string.format("UPDATE %s SET Gift = 1 WHERE RoleID = '%s'", table_name, _sql_escape(rid)) -- _exec(con, upd) -- end -- local function _getinfo(table_name, RoleID, Reiting, add) -- _ensure_sql() -- local con = Sql_con() -- if not con or type(con.execute) ~= "function" then -- return 0, {}, {}, 0, 0 -- end -- if add == 1 then -- _upsert_add_points(con, table_name, RoleID, Reiting) -- elseif add == 2 then -- _set_gift(con, table_name, RoleID) -- end -- local rating_count, RoleID_rating, RoleID_point = _get_top(con, table_name) -- local my_rating_point = _get_my_points(con, table_name, RoleID) -- local gift_flag = _get_gift_flag(con, table_name, RoleID) -- return rating_count, RoleID_rating, RoleID_point, my_rating_point, gift_flag -- end -- function Magda_getinfo(RoleID, Reiting, add) -- return _getinfo("sm_data.magda_reiting", RoleID, Reiting, add) -- end -- function Magda_getinfo2(RoleID, Reiting, add) -- return _getinfo("sm_data.magda_reiting_new", RoleID, Reiting, add) -- end -- function clear_Magda_reiting() -- _ensure_sql() -- local con = Sql_con() -- if not con or type(con.execute) ~= "function" then -- return false -- end -- return _exec(con, "TRUNCATE sm_data.magda_reiting") -- end -- function clear_Magda_reiting2() -- _ensure_sql() -- local con = Sql_con() -- if not con or type(con.execute) ~= "function" then -- return false -- end -- return _exec(con, "TRUNCATE sm_data.magda_reiting_new") -- end -- function MagdaUpdate(RoleID, Glory) -- _ensure_sql() -- local con = Sql_con() -- if not con or type(con.execute) ~= "function" then -- return false -- end -- _upsert_add_points(con, "sm_data.magda_reiting_new", RoleID, Glory) -- return true -- end -- return { -- Magda_getinfo = Magda_getinfo, -- Magda_getinfo2 = Magda_getinfo2, -- clear_Magda_reiting = clear_Magda_reiting, -- clear_Magda_reiting2 = clear_Magda_reiting2, -- MagdaUpdate = MagdaUpdate, -- }