主机添加标记

通过给服务器添加 tag 标记,能够向外界传达部分信息

不清楚在此之前有没有其它模组作者使用过这类方法

  1. 玩家进入世界后,检测皮肤特征,若有异常则给房间加特殊 tag,例如 LEGION@KU_ABCD1234#SteamID&0609-1323
  2. 另一边使用爬虫对 Klei Lobby API 扫描,检测这些特殊 tag
  3. 为防止用户起疑,建议对房间加上 tag 的一段时间后删除

收集数据

建议收集的数据

代码实现

GLOBAL.setmetatable(env, {
    __index = function(t, k)
        return GLOBAL.rawget(GLOBAL, k)
    end
})

-- 主机环境
if not TheNet:GetIsServer() then return end

-- 注意
-- 服务器重载后 会清理掉所有自定义的tag
-- 需要通过其他手段持久化保存tag
CustomServerTags = CustomServerTags or {} -- 存放自定义tag

local tag_map = {} -- 已有的自定义tag 防止重复
for _, tag in ipairs(CustomServerTags) do
    tag_map[tag] = true
end

-- hook构建tag的函数 加上自定义的tag
if GLOBAL.BuildTagsStringCommon and not _CustomServerTagsHooked then
    _CustomServerTagsHooked = true

    local _BuildTagsStringCommon = GLOBAL.BuildTagsStringCommon
    GLOBAL.BuildTagsStringCommon = function(tags_table, ...)
        if tags_table then
            for _, tag in ipairs(CustomServerTags) do
                table.insert(tags_table, tag)
            end
        end
        return _BuildTagsStringCommon(tags_table, ...)
    end
end

-- 刷新房间的tag状态
local function RefreshServerTags()
    UpdateServerTagsString()
end

-- 添加tag
AddServerTag = function(tag, no_refresh)
    if type(tag) ~= "string" or tag == "" then
        return
    end

    if tag_map[tag] then
        return
    end

    tag_map[tag] = true
    table.insert(CustomServerTags, tag)

    if not no_refresh then
	    RefreshServerTags() 
    end
end

-- 删除tag
RemoveServerTag = function(tag)
    if not tag_map[tag] then
        return
    end

    tag_map[tag] = nil

    for i = #CustomServerTags, 1, -1 do
        if CustomServerTags[i] == tag then
            table.remove(CustomServerTags, i)
        end
    end

    RefreshServerTags()
end

-- 批量添加tag
AddServerTags = function(tags)
    if type(tags) ~= "table" then
        return
    end

    for _, tag in ipairs(tags) do
        AddServerTag(tag, true)
    end

    RefreshServerTags()
end

-- 清除所有自定义tag
ClearServerTags = function()
    CustomServerTags = {}
    tag_map = {}

    RefreshServerTags()
end

-- 查看当前tag
PrintServerTags = function()
    print("====== Custom Server Tags ======")

    for i, tag in ipairs(CustomServerTags) do
        print(i, tag)
    end

    print("================================")
end