主题
应用示例
组合 SortedMap、HashMap 与 Queue,实现短期日榜和跨服务器匹配的存储流程。
下面是可复用的存储片段,不是完整玩法工程。存储调用应在可等待的服务端任务中执行,网络错误通过 pcall() 处理。UI、业务事件校验、消息重试与持久幂等由玩法负责。
按天分组的临时排行榜
采用每日独立的 SortedMap,key 为商品标识,sortKey 为当日累计销售额,value 为最近一次更新的服务器时间。这里仅展示排名指标,不展开购买系统。
以下采用 UTC 日边界:服务器秒时间戳除以 86,400 取整。业务若使用其他时区,应统一调整日边界,不能由不同客户端各自决定日期。
lua
local MemoryStoreService = game:GetService("MemoryStoreService")
local World = game:GetService("World")
local now = math.floor(World:GetServerTime())
local day = math.floor(now / 86400)
local ranks = MemoryStoreService:GetSortedMap("DailyRevenue_" .. tostring(day))
local itemId = "item_01"
local delta = 100
local success, result = pcall(function()
local options = game:CreateData("MemoryStoreSortedMapSetOptions")
options.INCR = true
options.EXAT = (day + 3) * 86400
ranks:SetAsync(itemId, now, delta, options)
local pages = ranks:GetRangeAsync(Enums.SortDirection.Descending, 10)
for rank, entry in ipairs(pages:GetCurrentPage()) do
print(rank, entry.Key, entry.SortKey, entry.Value)
end
end)
if not success then
print("日榜操作失败", result)
end本例在该日结束后的 48 小时统一过期,距写入时刻约 48 至 72 小时。与每次写入 EX=48*3600 不同,更新不会让旧日榜不断向后延期。
INCR 只累计排序键,value=now 每次覆盖。榜单读取从 entry.SortKey 取排名指标,entry.Value 仅作为更新记录。服务端可短期缓存榜单,避免每个客户端打开界面都发起请求。
累计榜使用独立的 OrderedDataStore,见持久排行榜。同时更新两个榜不构成跨服务事务,应根据业务要求设计重试、补偿和防重复。
跨服务器匹配的组合方式
Queue 提供临时待办与不可见窗口,HashMap 租约用于降低多服务器争抢,MessageService 用于发送结果。消息通知不是持久完成凭证;图中的流程不保证恰好一次。
玩家请求入队
业务验证玩家仍在场、允许匹配后,在可等待任务中调用下面函数。requestId 是业务生成的稳定请求标识,serverId 是可路由到发起服务器的标识,均不是此处虚构的引擎获取方法。
lua
local MemoryStoreService = game:GetService("MemoryStoreService")
local queue = MemoryStoreService:GetQueue("GuideMatch_2v2", 30)
local function enqueueMatch(requestId, userId, serverId)
return pcall(function()
local options = game:CreateData("MemoryStoreQueueAddOptions")
options.EX = 120
return queue:AddAsync({
RequestId = requestId,
UserId = userId,
ServerId = serverId,
}, 0, options)
end)
end超时不等于匹配成功,重复调用也不自动去重。记录稳定请求标识、处理取消与过期,是业务必须补齐的部分。
竞争与续期仲裁租约
以下函数同样在可等待任务中调用。ownerId 必须是本次运行唯一的非空字符串,不应复用其他服务器或旧运行的身份。
lua
local MemoryStoreService = game:GetService("MemoryStoreService")
local leases = MemoryStoreService:GetHashMap("GuideMatchLeases")
local function acquireLease(ownerId)
local success, value = pcall(function()
local options = game:CreateData("MemoryStoreHashMapSetOptions")
options.NX = true
options.EX = 90
return leases:SetAsync("leader_2v2", ownerId, options)
end)
return success and value == ownerId
end
local function renewLease(ownerId)
local success, value = pcall(function()
local options = game:CreateData("MemoryStoreHashMapSetOptions")
options.XX = true
options.EQT = ownerId
options.EX = 90
return leases:SetAsync("leader_2v2", ownerId, options)
end)
return success and value == ownerId
end只对成功取得租约的处理者调度匹配循环,并在到期前续期,例如每 60 秒续期一次。失败或结果不确定时停止工作。XX 禁止续期时重新创建已过期租约,EQT 验证持有者;两项都需要。
不要在放弃时无条件删除租约,因为它可能已经属于另一服务器。可停止续期让它过期。90 秒租约只是示例参数,不代表严格互斥或 fencing 保证。
领取一组匹配请求
业务为每个模式设定所需人数,这里示例读取 4 项。由仍有效的仲裁者在可等待任务中调用:
lua
local MemoryStoreService = game:GetService("MemoryStoreService")
local queue = MemoryStoreService:GetQueue("GuideMatch_2v2", 30)
local function takeMatchBatch()
return pcall(function()
local options = game:CreateData("MemoryStoreQueueReadOptions")
options.AllOrNothing = true
options.WaitTimeout = 0
return queue:ReadAsync(4, options)
end)
end调用结果依次为 success, readId, items。成功且 readId=nil 表示当前不足一组,不需要紧循环重试。读取前的数量检查不能代替实际领取结果。
获取批次后,业务必须:
- 再验证玩家请求没有取消、过期或已经完成,避免同一玩家重复组队。
- 建立稳定的匹配标识与幂等完成状态;租约不是幂等凭证。
- 通过已配置的 MessageService 路由通知各玩家服务器;接收方按匹配标识去重。
- 只有批次处理确认成功后,在同一队列调用
RemoveAsync(readId)。 - 明确部分通知成功、处理超时、确认删除失败后的重试与补偿。
这里不提供直接“发通知后立即删除”的完整代码,以免将异步通知误当成可靠提交。超过不可见窗口或租约期限的暂停恢复,会产生并行处理风险;重要结果应由持久幂等与业务授权约束。
发布前检查
- 业务指标和匹配请求只由服务端验证后写入。
- 日期、TTL、队列等待与租约期限采用同一套服务端规则。
- 重复事件、重复读取和响应丢失有明确处理方式。
- 单场景与地图全局 RU 预算满足预估峰值,见用量限制。
