summaryrefslogtreecommitdiff
path: root/server/src/vm/label.lua
blob: cf24ce5d9955c0717a37ee9115de101c7afc17cb (plain)
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
88
89
90
91
92
93
local sourceMgr = require 'vm.source'

local Sort = 0

local mt = {}
mt.__index = mt
mt.type = 'label'
mt._infoCount = 0
mt._infoLimit = 10

function mt:getName()
    return self.name
end

function mt:addInfo(tp, source)
    if not source then
        error('No source')
    end
    local id = source.id
    if not id then
        error('Not instanted source')
    end
    if self._info[id] then
        return
    end
    Sort = Sort + 1
    local info = {
        type = tp,
        source = id,
        _sort = Sort,
    }

    self._info[id] = info
    self._infoCount = self._infoCount + 1

    if self._infoCount > self._infoLimit then
        for srcId in pairs(self._info) do
            local src = sourceMgr.list[srcId]
            if not src then
                self._info[srcId] = nil
                self._infoCount = self._infoCount - 1
            end
        end
        self._infoLimit = self._infoCount * 2
        if self._infoLimit < 10 then
            self._infoLimit = 10
        end
    end
end

function mt:eachInfo(callback)
    local list = {}
    for srcId, info in pairs(self._info) do
        local src = sourceMgr.list[srcId]
        if src then
            list[#list+1] = info
        else
            self._info[srcId] = nil
            self._infoCount = self._infoCount - 1
        end
    end
    table.sort(list, function (a, b)
        return a._sort < b._sort
    end)
    for i = 1, #list do
        local info = list[i]
        local res = callback(info, sourceMgr.list[info.source])
        if res ~= nil then
            return res
        end
    end
    return nil
end

function mt:getSource()
    return sourceMgr.list[self.source]
end

return function (name, source)
    if not source then
        error('No source')
    end
    local id = source.id
    if not id then
        error('Not instanted source')
    end
    local self = setmetatable({
        name = name,
        source = id,
        _info = {},
    }, mt)
    return self
end