-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMbApi.lua
More file actions
69 lines (52 loc) · 1.58 KB
/
MbApi.lua
File metadata and controls
69 lines (52 loc) · 1.58 KB
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
--- Main API initializer for Transformice.
--- @class mousebase.MbApi:mousebase.EventEmitter
--- @field tfmEvent mousebase.MbTfmEvent
local MbPlayer = require("MbPlayer")
--- @return mousebase.MbApi
local createApi = function()
-- Private vars
local TfmEvent = require("MbTfmEvent")
local players = {}
--- @type mousebase.MbApi
local Api = require("EventEmitter"):extend("MbApi")
Api._init = function(self)
Api._parent._init(self)
self.tfmEvent = TfmEvent
players = {}
self:hookTfmEvents()
end
Api.hookTfmEvents = function(self)
TfmEvent:onCrucial("NewPlayer", function(pn)
local p = MbPlayer:new(pn)
players[pn] = p
self:emit("newPlayer", p)
end)
TfmEvent:onCrucial("PlayerLeft", function(pn)
local p = players[pn]
if not p then return end
players[pn] = nil
end)
end
Api.emitExistingPlayers = function(self)
for name, rp in pairs(tfm.get.room.playerList) do
local p = MbPlayer:new(name)
players[name] = p
self:emit("newPlayer", p)
end
end
Api.start = function(self)
self:emitExistingPlayers()
self:emit("ready")
end
return Api:new()
end
--- @type mousebase.MbApi
local cached_api
if cached_api then
error("Tried to create more than one Api instance! This should only be created in the main program file, and optionally cached for future use.")
return nil
end
return function()
cached_api = createApi()
return cached_api
end