-
Notifications
You must be signed in to change notification settings - Fork 1
/
roomListTemplet.go
87 lines (73 loc) · 1.87 KB
/
roomListTemplet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package src
import (
logtool "github.com/adimax2953/log-tool"
"github.com/pkg/errors"
)
// RoomList function - keys, args[] string - return interface{} , error
func (s *MyScriptor) RoomList(
keys []string,
) (interface{}, error) {
res, err := s.Scriptor.ExecSha(RoomListID, keys, nil)
if err != nil {
logtool.LogError("RoomList ExecSha Error", err)
return "", errors.WithStack(err)
}
return res, nil
}
// RoomList - 減少數值
const (
RoomListID = "RoomList"
RoomListTemplate = `
local DBKey = tonumber(KEYS[1])
local ProjectKey = KEYS[2]
local TagKey = KEYS[3]
local function log(v)
local s = ""
if type(v) == 'table' then
s = cjson.encode(v)
else
s = tostring(v)
end
redis.call("RPUSH", "log", s)
end
---@param key string
---@return table
local function hgetall(key)
local entries = redis.call('HGETALL', key)
local result = {}
for i = 1, #entries, 2 do
result[entries[i]] = entries[i + 1]
end
return result
end
---@param str string
---@param separator string
---@return string[]
local function split(str, separator)
local t = {}
for s in string.gmatch(str, "([^" .. separator .. "]+)") do
table.insert(t, s)
end
return t
end
redis.call("SELECT", DBKey)
local roomIds = redis.call("SMEMBERS", ProjectKey .. "/rooms")
if roomIds == false or next(roomIds) == nil then
return "[]"
end
local rooms = {}
for _, roomId in ipairs(roomIds) do
local parts = split(roomId, "/")
local platformId = parts[1]
local gameId = parts[2]
local countrycode = parts[3]
local roomIdLocal = parts[4]
local key = table.concat(
{ProjectKey, platformId, gameId, countrycode, "room", roomIdLocal}, "/"
)
local room = hgetall(key)
table.insert(rooms, room)
end
return cjson.encode(rooms)
`
)