Appearance
一维表变量数据管理接口 Map
具体函数名及描述如下:
序号 | 函数名 | 函数描述 |
---|---|---|
1 | SetValueAndCallBack(...) | 回调设置kv数据 |
2 | SetValueAndBlock(...) | 阻塞设置kv数据 |
3 | RemoveValueAndCallBack(...) | 回调删除指定key的数据 |
4 | RemoveValueAndBlock(...) | 阻塞删除指定key的数据 |
5 | UpdateValueAndCallback(...) | 更新指定key的数据 |
6 | GetValueAndCallBack(...) | 回调获取kv数据 |
7 | GetValueAndBlock(...) | 阻塞获取kv数据 |
8 | GetIndexValueAndCallback(...) | 获取排行榜指定排名索引的值 |
9 | GetIndexValueAndBlock(...) | 获取排行榜指定排名索引的值 |
10 | GetNumValuesAndCallback(...) | 获取排行榜指定前num个值 |
11 | GetRangeValuesAndCallback(...) | 获取排行榜值为min~max区间的所有值 |
12 | SetRankValueAndBlock(...) | 阻塞设置排行榜的值 |
13 | ClearData(...) | 清空kv表/排行榜 |
SetValueAndCallBack
- 参数及类型:
- 返回值及类型:
- code:
bool
是否调用成功 非全局云变量建议使用阻塞接口
- code:
- 该方法的主要作用: 回调设置kv数据
- 具体使用案例如下:
lua
local function GlobalKVCallback(code,key,value)
Chat:SendChat(table.concat({"SetValueAndCallBack code = ",tostring(code), "key = ",tostring(key), "value = ",value or ""}, " "))
if value and type(value) == "string" then --自定义table需要自己序列化结构 反序列化需要自己处理
value = json.decode(value) or value
end
print("code", code , "key", key, "value", value)
if code == ErrorCode.OK then
print("设置成功")
end
end
--不支持自定义table需要自己序列化结构
local globalvalue = json.encode({score = 100, name = Actor:GetNickName(uin) or "noname" })
local result = Data.Map:SetValueAndCallBack(GlobalKV, nil, "GlobalKV_Key1", globalvalue, GlobalKVCallback)
print("result", result and "调用成功" or "调用失败")
local function GlobalRankCallback(code,key,value)
Chat:SendChat(table.concat({"SetValueAndCallBack code = ",tostring(code), "key = ",tostring(key), "value = ",tostring(value)}, " "))
print("code", code , "key", key, "value", value)
if code == ErrorCode.OK then
print("设置成功")
end
end
result = Data.Map:SetValueAndCallBack(GlobalRank, nil, "GlobalRank_Key1", 100, GlobalRankCallback) and result
print("result", result and "调用成功" or "调用失败")
local function UserKVCallback(code,key,value)
Chat:SendChat(table.concat({"SetValueAndCallBack code = ",tostring(code), "key = ",tostring(key), "value = ",tostring(value)}, " "))
if value and type(value) == "string" then
value = json.decode(value) or value
end
print("code", code , "key", key, "value", value)
if code == ErrorCode.OK then
print("设置成功")
end
end
--不支持自定义table需要自己序列化结构 反序列化需要自己处理
local uservalue = json.encode({score = 100, name = Actor:GetNickName(uin) or "noname" })
result = Data.Map:SetValueAndCallBack(UserKV, uin, "UserKV_Key1", uservalue, UserKVCallback) and result
print("result", result and "调用成功" or "调用失败")
SetValueAndBlock
- 参数及类型:
- 返回值及类型:
- 该方法的主要作用: 阻塞设置kv数据
- 具体使用案例如下:
lua
--不支持自定义table需要自己序列化结构
local globalvalue = json.encode({score = 200, name = Actor:GetNickName(uin) })
local code1 , curkey1 , curvalue1 = Data.Map:SetValueAndBlock(GlobalKV, nil, "GlobalKV_Key2", globalvalue)
print("code ", code1, "key ", curkey1, "value ", curvalue1)
Chat:SendChat(table.concat({"SetValueAndBlock code = ",tostring(code1), "key = ",tostring(curkey1), "value = ",tostring(curvalue1)}, " "))
if code1 == 0 then
if code1 == ErrorCode.OK then
print("设置成功")
end
end
local code2 , curkey2 , curvalue2 = Data.Map:SetValueAndBlock(GlobalRank, nil, "GlobalRank_Key2", 200)
print("code ", code2, "key ", curkey2, "value ", curvalue2)
Chat:SendChat(table.concat({"SetValueAndBlock code = ",tostring(code2), "key = ",tostring(curkey2), "value = ", tostring(curvalue2)}, " "))
if code2 == 0 then
print("设置成功")
end
--不支持自定义table需要自己序列化结构
local uservalue = json.encode({score = 200, name = Actor:GetNickName(uin) })
local code3 , curkey3 , curvalue3 = Data.Map:SetValueAndBlock(UserKV, uin, "UserKV_Key2", uservalue)
print("code ", code3, "key ", curkey3, "value ", curvalue3)
Chat:SendChat(table.concat({"SetValueAndBlock code = ",tostring(code3), "key = ",tostring(curkey3), "value = ", tostring(curvalue3) }, " "))
if code3 == 0 then
print("设置成功")
end
RemoveValueAndCallBack
- 参数及类型:
- 返回值及类型:
- ret:
bool
是否调用成功 非全局云变量建议使用阻塞接口
- ret:
- 该方法的主要作用: 回调删除指定key的数据
- 具体使用案例如下:
lua
local function GlobalKVCallback(code,key)
Chat:SendChat(table.concat({"RemoveValueAndCallBack code = ",tostring(code), "key = ",tostring(key)}, " "))
print("code", code , "key", key)
if code == ErrorCode.OK then
print("设置成功")
end
end
local result = Data.Map:RemoveValueAndCallBack(GlobalKV, nil, "GlobalKV_Key1", GlobalKVCallback)
if result then
print("调用成功")
end
local function GlobalRankCallback(code,key)
Chat:SendChat(table.concat({"RemoveValueAndCallBack code = ",tostring(code), "key = ",tostring(key)}, " "))
print("code", code , "key", key)
if code == ErrorCode.OK then
print("设置成功")
end
end
result = result and Data.Map:RemoveValueAndCallBack(GlobalRank, nil, "GlobalRank_Key1", GlobalRankCallback)
local function UserKVCallback(code,key)
Chat:SendChat(table.concat({"RemoveValueAndCallBack code = ",tostring(code), "key = ",tostring(key)}, " "))
print("code", code , "key", key)
if code == ErrorCode.OK then
print("设置成功")
end
end
result = result and Data.Map:RemoveValueAndCallBack(UserKV, uin, "UserKV_Key1", UserKVCallback)
RemoveValueAndBlock
- 参数及类型:
- 返回值及类型:
- 该方法的主要作用: 阻塞删除指定key的数据
- 具体使用案例如下:
lua
local code1 , curkey1 = Data.Map:RemoveValueAndBlock(GlobalKV, nil, "GlobalKV_Key2")
Chat:SendChat(table.concat({"RemoveValueAndBlock code = ",tostring(code1), "key = ",tostring(curkey1)}, " "))
print("code ", code1, "key ", curkey1)
if code1 == 0 then
print("设置成功")
end
local code2 , curkey2 = Data.Map:RemoveValueAndBlock(GlobalRank, nil, "GlobalRank_Key2")
Chat:SendChat(table.concat({"RemoveValueAndBlock code = ",tostring(code2), "key = ",tostring(curkey2)}, " "))
print("code ", code2, "key ", curkey2)
if code2 == 0 then
print("设置成功")
end
local code3 , curkey3 = Data.Map:RemoveValueAndBlock(UserKV, uin, "UserKV_Key2")
Chat:SendChat(table.concat({"RemoveValueAndBlock code = ",tostring(code3), "key = ",tostring(curkey3)}, " "))
print("code ", code3, "key ", curkey3)
if code3 == 0 then
print("设置成功")
end
UpdateValueAndCallback
- 参数及类型:
- 返回值及类型:
- ret:
bool
是否成功 (全局云KV变量可用)
- ret:
- 该方法的主要作用: 更新指定key的数据
- 具体使用案例如下:
lua
local function GlobalKVCallback(code,key,value)
Chat:SendChat(table.concat({"UpdateValueAndCallback code = ",tostring(code), "key = ",tostring(key), "value = ",tostring(value)}, " "))
value = json.decode(value) or value
print("code", code , "key", key, "value", value)
if code == ErrorCode.KV_UPDATE_SET then -- 需要返回更新设置的最终值
value = value or {}
value.updatevalue = 999
return json.encode(value) -- 需要返回更新设置的最终值 并且序列化结构
elseif code == ErrorCode.KV_UPDATE_GET then
print("获取到的当前最新的值:", value)
elseif code == ErrorCode.OK then
print("更新完成")
else
print("更新失败")
end
end
local result = Data.Map:UpdateValueAndCallback(GlobalKV, nil, "GlobalKV_Key2", GlobalKVCallback)
if result then
print("调用成功")
else
print("调用失败")
end
GetValueAndCallBack
- 参数及类型:
- 返回值及类型:
- ret:
bool
是否调用成功 非全局云变量建议使用阻塞接口
- ret:
- 该方法的主要作用: 回调获取kv数据
- 具体使用案例如下:
lua
local function GlobalKVCallback(code,key,value)
Chat:SendChat(table.concat({"GetValueAndCallBack code = ",tostring(code), "key = ",tostring(key), "value = ", tostring(value)}, " "))
value = json.decode(value) or value
print("code", code , "key", key, "value", value)
if code == ErrorCode.OK then
print("获取成功")
elseif code == ErrorCode.KV_OP_NO_VAL then
print("数据不存在")
else
print("获取失败")
end
end
local result = Data.Map:GetValueAndCallBack(GlobalKV, nil, "GlobalKV_Key1", GlobalKVCallback)
local function GlobalRankCallback(code,key,value,index,ascending,extendinfo)
Chat:SendChat(table.concat({"GetValueAndCallBack code = ",tostring(code), "key = ",tostring(key), "value = ",tostring(value), "index", index, "ascending", tostring(ascending), "extendinfo",tostring(extendinfo) or "" }, " "))
print("code", code , "key", key, "value", value, "index", index, "ascending", ascending, "extendinfo", extendinfo)
-- 获取排行榜数据 不一定有value之后的参数 注意判断
if code == ErrorCode.OK then
print("获取成功")
elseif code == ErrorCode.KV_OP_NO_VAL then
print("数据不存在")
else
print("获取失败")
end
end
result = result and Data.Map:GetValueAndCallBack(GlobalRank, nil, "GlobalRank_Key1", GlobalRankCallback)
local function UserKVCallback(code,key,value)
Chat:SendChat(table.concat({"GetValueAndCallBack code = ",tostring(code), "key = ",tostring(key), "value = ",tostring(value)}, " "))
value = json.decode(value) or value
print("code", code , "key", key, "value", value)
if code == ErrorCode.OK then
print("获取成功")
elseif code == ErrorCode.KV_OP_NO_VAL then
print("数据不存在")
else
print("获取失败")
end
end
result = result and Data.Map:GetValueAndCallBack(UserKV, uin, "UserKV_Key1", UserKVCallback)
GetValueAndBlock
- 参数及类型:
- 返回值及类型:
- 该方法的主要作用: 阻塞获取kv数据
- 具体使用案例如下:
lua
local code1 , curkey1 , curvalue1 = Data.Map:GetValueAndBlock(GlobalKV, nil, "GlobalKV_Key2")
print("code", code1 , "key", curkey1, "value", curvalue1)
Chat:SendChat(table.concat({"GetValueAndBlock code = ",tostring(code1), "key = ",tostring(curkey1), "value = ",tostring(curvalue1)}, " "))
curvalue1 = json.decode(curvalue1) or curvalue1
if code1 == ErrorCode.OK then
print("获取成功")
elseif code1 == ErrorCode.KV_OP_NO_VAL then
print("数据不存在")
else
print("获取失败")
end
local code2 , curkey2 , curvalue2,index,ascending,extendinfo = Data.Map:GetValueAndBlock(GlobalRank, nil, "GlobalRank_Key2")
Chat:SendChat(table.concat({"GetValueAndBlock code = ",tostring(code2), "key = ",tostring(curkey2), "value = ",tostring(curvalue2), "index =", tostring(index), "ascending =", tostring(ascending), "extendinfo =", tostring(extendinfo) or "" }, " "))
curvalue2 = json.decode(curvalue2) or curvalue2
extendinfo = json.decode(extendinfo) or extendinfo
-- 获取排行榜数据 不一定有value之后的参数 注意判断
if code2 == 0 then
print("code", code2 , "key", curkey2, "value", curvalue2, "index", index, "ascending", ascending, "extendinfo", extendinfo)
elseif code2 == ErrorCode.KV_OP_NO_VAL then
print("数据不存在")
else
print("获取失败")
end
local code3 , curkey3 , curvalue3 = Data.Map:GetValueAndBlock(UserKV, uin, "UserKV_Key2")
Chat:SendChat(table.concat({"GetValueAndBlock code = ",tostring(code3), "key = ",tostring(curkey3), "value = ",tostring(curvalue3)}, " "))
curvalue3 = json.decode(curvalue3) or curvalue3
if code3 == 0 then
print("code", code3 , "key", curkey3, "value", curvalue3)
elseif code3 == ErrorCode.KV_OP_NO_VAL then
print("数据不存在")
else
print("获取失败")
end
GetIndexValueAndCallback
- 参数及类型:
- 返回值及类型: bool:是否调用成功 非全局云变量建议使用阻塞接口
- 该方法的主要作用: 获取排行榜指定排名索引的值
- 具体使用案例如下:
lua
local function GlobalRankCallback(code,key,value,index,ascending,extendinfo)
Chat:SendChat(table.concat({"GetIndexValueAndCallback code = ",tostring(code), "key = ",tostring(key), "value = ",tostring(value), "index =", tostring(index), "ascending =", tostring(ascending), "extendinfo =", json.encode(extendinfo) or tostring(extendinfo)}, " "))
print("code", code , "key", key, "value", value, "index", index, "ascending", ascending, "extendinfo", extendinfo)
if code == ErrorCode.OK then
print("获取成功")
else
--获取失败 参数向前移动
print("获取失败")
end
end
--获取降序排列的第一个数据
local result = Data.Map:GetIndexValueAndCallback(GlobalRank, nil, 1,false, GlobalRankCallback)
if result then
print("调用成功")
end
GetIndexValueAndBlock
- 参数及类型:
- 返回值及类型:
- 该方法的主要作用: 获取排行榜指定排名索引的值
- 具体使用案例如下:
lua
--获取升序排列的第一个数据
local code1 , curkey1 , curvalue1,index,ascending,extendinfo = Data.Map:GetIndexValueAndBlock(GlobalRank, nil, 1,true)
Chat:SendChat(table.concat({"GetIndexValueAndBlock code = ",tostring(code1), "key = ",tostring(curkey1), "value = ",tostring(curvalue1), "index =", tostring(index), "ascending =", tostring(ascending), "extendinfo =", json.encode(extendinfo) or tostring(extendinfo)}, " "))
print("code", code1 , "key", curkey1, "value", curvalue1, "index", index, "ascending", ascending, "extendinfo", extendinfo)
if code1 == ErrorCode.OK then
print("获取成功")
else
--获取失败 参数向前移动
print("获取失败")
end
GetNumValuesAndCallback
- 参数及类型:
- 返回值及类型:
- ret:
bool
是否调用成功
- ret:
- 该方法的主要作用: 获取排行榜指定前num个值
- 具体使用案例如下:
lua
local function GlobalRankCallback(code,num,ascending,datas)
Chat:SendChat(table.concat({"GetNumValuesAndCallback code = ",tostring(code), "num =", tostring(num), "ascending =", tostring(ascending), "datas =", json.encode(datas) or tostring(datas)}, " "))
print("code", code , "num", num, "ascending", ascending, datas)
if code == ErrorCode.OK then
for i = 1, #datas do
print("key", datas[i].k, "value", datas[i].v , "extendinfo", datas[i].info and json.encode(datas[i].info) or "" )
end
print("获取成功")
else
print("获取失败")
end
end
--获取降序排列的前20个数据
local result = Data.Map:GetNumValuesAndCallback(GlobalRank, nil,20,false,GlobalRankCallback)
GetRangeValuesAndCallback
- 参数及类型:
- 返回值及类型:
- ret:
bool
是否调用成功
- ret:
- 该方法的主要作用: 获取排行榜值为min~max区间的所有值
- 具体使用案例如下:
lua
local function GlobalRankCallback(code,min,max,ascending,datas)
Chat:SendChat(table.concat({"GetRangeValuesAndCallback code = ",tostring(code), "min =", tostring(min), "max =", tostring(max), "ascending =", tostring(ascending), "datas =", json.encode(datas) or tostring(datas)}, " "))
print("code", code , "min", min, "max", max, "ascending", ascending, datas)
if code == ErrorCode.OK then
for i = 1, #datas do
print("key", datas[i].k, "value", datas[i].v , "extendinfo", datas[i].info and json.encode(datas[i].info) or "" )
end
print("获取成功")
else
print("获取失败")
end
end
--获取排行榜中降序排列值为1-1000名之间的数据,每次最大返回50条
local result = Data.Map:GetRangeValuesAndCallback(GlobalRank, nil,1,1000,true,50,GlobalRankCallback)
SetRankValueAndBlock
- 参数及类型:
- 返回值及类型:
- 该方法的主要作用: 阻塞设置排行榜的值
- 具体使用案例如下:
lua
local uin = Player:GetHostUin()
local extendinfo = {
uin = uin,
name = Actor:GetNickName(uin),
icon = CustomUI:GetRoleIcon(uin),
}
-- extendinfo 如果不需要更新内容,可以传nil , 如果需要更新内容,则传入完整的信息
extendinfo = json.encode(extendinfo) -- 自定义table需要自己序列化结构
local code,key,curvalue = Data.Map:SetRankValueAndBlock(GlobalRank, nil, "GlobalRank_Key3", 200, extendinfo)
Chat:SendChat(table.concat({"SetRankValueAndBlock code = ",tostring(code), "key = ",tostring(key), "curvalue = ",json.encode(curvalue) or tostring(curvalue)}, " "))
print("code", code, "key", key, "curvalue", curvalue)
if code == ErrorCode.OK then
print("调用成功")
end
ClearData
lua
local result = Data.Map:ClearData(GlobalKV, nil)
Chat:SendChat(table.concat({"ClearData result = ",tostring(result)}, " "))
if result then
print("清理成功")
end
result = Data.Map:ClearData(GlobalRank, nil) and result
Chat:SendChat(table.concat({"ClearData result = ",tostring(result)}, " "))
if result then
print("清理成功")
end
result = Data.Map:ClearData(UserKV, uin) and result
Chat:SendChat(table.concat({"ClearData result = ",tostring(result)}, " "))
if result then
print("清理成功")
end