-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.lua
244 lines (217 loc) · 7.96 KB
/
lib.lua
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
---@class Lib
local Lib = {}
local function getConfig(conf)
local ok, result = pcall(Kristal.getLibConfig, "gasterscoolsocialnetwork", conf)
if not ok then return (Kristal.Config["plugins/gcsn"] or {
["domain"] = "serveo.net",
["port"] = 25574,
["chatBind"] = "/"
})[conf] end
return result
end
Game.socket = require("socket")
Game.client = assert(
Game.socket.connect(
getConfig("domain"),
getConfig("port")
)
)
local socket = Game.socket
local json = JSON
local function sendToServer(client, message)
local encodedMessage = json.encode(message)
-- print("[OUT] "..Utils.dump(encodedMessage))
client:send(encodedMessage .. "\n")
end
function Lib:receiveFromServer(client)
local response, err, partial = client:receive()
if partial then
self.partial = self.partial .. partial
elseif response then
local decodedResponse = json.decode(self.partial .. response)
self.partial = ""
return decodedResponse
elseif err ~= "timeout" then
print("Error: ", err)
end
end
local client = Game.client
client:settimeout(0)
-- Throttle interval (in seconds)
local THROTTLE_INTERVAL = 0.05
local HEARTBEAT_INTERVAL = 10.0
local lastHearbeatTime = love.timer.getTime()
local lastUpdateTime = 0
local lastPlayerListTime = 0
function Lib:init()
self.chat_box = ChatInputBox()
self.partial = ""
Utils.hook(World, 'update', function (orig, wld, ...)
orig(wld,...)
self:updateWorld()
end)
Utils.hook(Game, "update", function (orig, ...)
orig(...)
self:update()
end)
Utils.hook(Battle, "update", function (orig, batl, ...)
orig(batl, ...)
self:updateBattle(batl)
end)
end
function Lib:postInit()
Game.stage:addChild(self.chat_box)
self.name = Game.save_name
self.other_players = nil
self.other_players = {} -- Store other players
-- Register player with username and actor
local registerMessage = {
command = "register",
uuid = Game:getFlag("GCSN_UUID"), -- server will generate this if it's nil
username = self.name,
actor = Game.party[1].actor.id or "kris" -- Include actor
}
sendToServer(client, registerMessage)
end
function Lib:update()
local currentTime = love.timer.getTime()
if currentTime - lastHearbeatTime >= HEARTBEAT_INTERVAL then
lastHearbeatTime = currentTime
sendToServer(client, {
command = "heartbeat",
gamestate = Game.state
})
end
end
function Lib:unload()
sendToServer(client, {command = "disconnect"})
client:close()
end
function Lib:updateBattle(batl, ...)
local currentTime = love.timer.getTime()
local data = self:receiveFromServer(client)
if data then
end
-- Throttle player position update packets
if currentTime - lastUpdateTime >= THROTTLE_INTERVAL and batl then
local player = batl.party[1]
local updateMessage = {
command = "battle",
subCommand = "update",
actor = player.actor.id,
username = self.name,
sprite = player.sprite.sprite_options[1],
encounter = batl.encounter.id,
}
sendToServer(client, updateMessage)
lastUpdateTime = currentTime
end
end
function Lib:updateWorld(...)
local player = Game.world.player
-- Update the current time
local currentTime = love.timer.getTime()
-- Receive data from the server (if any)
local data = self:receiveFromServer(client)
if data then
-- print("[NET] "..Utils.dump(data))
if data.command == "register" then
self.uuid = data.uuid
Game:setFlag("GCSN_UUID", self.uuid)
elseif data.command == "update" then
for _, playerData in ipairs(data.players) do
if playerData.uuid ~= self.uuid then
local other_player = self.other_players[playerData.uuid]
if other_player then
-- Smoothly interpolate position update
other_player.targetX = playerData.x
other_player.targetY = playerData.y
other_player.name = playerData.username
if other_player.actor.id ~= playerData.actor then
local success, result = pcall(Other_Player, playerData.actor, 0, 0, 0)
if success then
other_player:setActor(playerData.actor)
else
other_player:setActor("dummy")
end
end
if other_player.sprite.sprite_options[1] ~= playerData.sprite then
other_player:setSprite(playerData.sprite)
end
else
local otherplr
local success, result = pcall(Other_Player, playerData.actor, playerData.x, playerData.y, playerData.username, playerData.uuid)
if success then
otherplr = result
else
otherplr = Other_Player("dummy", playerData.x, playerData.y, playerData.username, playerData.uuid)
end
if playerData.map == (Mod.info.id..":"..Game.world.map.id) then
-- Create a new player if it doesn't exist while making sure It's on the right map
other_player = otherplr
other_player.layer = Game.world.map.object_layer
other_player.mapID = playerData.map
Game.world:addChild(other_player)
self.other_players[playerData.uuid] = other_player
end
end
end
end
elseif data.command == "chat" then
local sender = self.other_players[data.uuid] or Game.world.player
local bubble = ChatBubble(sender.actor, data.message)
bubble:setScale(0.25)
sender:addChild(bubble)
elseif data.command == "RemoveOtherPlayersFromMap" then
for _, uuid in ipairs(data.players) do
if self.other_players[uuid] then
self.other_players[uuid].fadingOut = true
self.other_players[uuid] = nil
end
end
else
Kristal.Console:warn("Unhandled command: " .. (data.command or "<nil>"))
Kristal.Console:log(Utils.dump(data))
end
end
-- Throttle player position update packets
if currentTime - lastUpdateTime >= THROTTLE_INTERVAL then
local updateMessage = {
command = "world",
subCommand = "update",
username = self.name,
x = player.x,
y = player.y,
map = (Mod.info.id..":"..Game.world.map.id) or "null",
actor = player.actor.id,
sprite = player.sprite.sprite_options[1]
}
sendToServer(client, updateMessage)
lastUpdateTime = currentTime
end
-- Throttle current players list packets
if currentTime - lastPlayerListTime >= THROTTLE_INTERVAL then
local playersList = {}
for uuid, _ in pairs(self.other_players) do
table.insert(playersList, uuid)
end
local currentPlayersMessage = {
command = "world",
subCommand = "inMap",
username = self.name,
players = playersList
}
sendToServer(client, currentPlayersMessage)
lastPlayerListTime = currentTime
end
end
function Lib:onKeyPressed(key, is_repeat)
if (
not is_repeat
and key == getConfig("chatBind")
and not self.chat_box.is_open
) then
self.chat_box:open()
end
end
return Lib