-
Notifications
You must be signed in to change notification settings - Fork 11
/
gatherchat.lua
302 lines (272 loc) · 7.68 KB
/
gatherchat.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
local ao = require("ao")
local json = require("json")
local utils = require(".utils")
-- Key: Address
Users = Users or {
testUser1 = {
processId = "id",
created = 1713833416559,
lastSeen = 1713833416559,
name = "Test User :)",
avatar = "a1204030b070a01", -- pixel art seed
status = "Hello, World!",
currentWorldId = "WelcomeLobby",
following = {
-- testUser2 = true,
},
},
}
DefaultWorlds = {
"WelcomeLobby",
"ChillZone",
"HolidayHangout",
}
-- Key: World ID
Worlds = Worlds or {
WelcomeLobby = {
created = 1713833416559,
lastActivity = 1713833416559,
name = "WelcomeLobby",
description = "Welcome to the Lobby!",
worldSize = {
w = 21,
h = 12,
},
worldType = "decoratedRoom",
worldTheme = "room_default",
spawnPosition = {
x = 5,
y = 4,
},
playerPositions = {
testUser1 = {
x = 4,
y = 5,
},
},
},
ChillZone = {
created = 1713833416559,
lastActivity = 1713833416559,
name = "ChillZone",
description = "Take it easy ;)",
worldSize = {
w = 12,
h = 14,
},
worldType = "clubbeach",
worldTheme = "clubhouse1",
spawnPosition = {
x = 5,
y = 4,
},
playerPositions = {}
},
HolidayHangout = {
created = 1713833416559,
lastActivity = 1713833416559,
name = "HolidayHangout",
description = "Sun, sand, and sea~",
worldSize = {
w = 18,
h = 10,
},
worldType = "clubbeach",
worldTheme = "beach1",
spawnPosition = {
x = 5,
y = 4,
},
playerPositions = {}
},
}
-- Key: Message ID
Posts = Posts or {
testPost1 = {
created = 1713833416559,
author = "testUser1",
worldId = "WelcomeLobby",
type = "text", -- if "video" or "image" then "TextOrTxId" is a TxId
textOrTxId = "Welcome to GatherChat!",
},
testPost2 = {
created = 1713833416559,
author = "testUser1",
worldId = "HolidayHangout",
type = "text", -- if "video" or "image" then "TextOrTxId" is a TxId
textOrTxId = "I love being on holiday!",
},
}
Handlers.add(
"GetUsers",
Handlers.utils.hasMatchingTag("Action", "GetUsers"),
function(msg)
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(Users) })
end
)
Handlers.add(
"GetWorldIndex",
Handlers.utils.hasMatchingTag("Action", "GetWorldIndex"),
function(msg)
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(DefaultWorlds) })
end
)
Handlers.add(
"GetWorld",
Handlers.utils.hasMatchingTag("Action", "GetWorld"),
function(msg)
if string.len(msg.Data) > 0 then
local data = json.decode(msg.Data)
if type(data) == "table" then
local world = Worlds[data.worldId]
if world then
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(world) })
else
ao.send({ Target = msg.From, Status = "Error", Message = "World not found." })
end
return
end
end
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(Worlds) })
end
)
Handlers.add(
"GetPosts",
Handlers.utils.hasMatchingTag("Action", "GetPosts"),
function(msg)
if string.len(msg.Data) > 0 then
local data = json.decode(msg.Data)
if type(data) == "table" then
local WorldPosts = {}
for postId, post in pairs(Posts) do
if post.worldId == data.worldId then
WorldPosts[postId] = post
end
end
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(WorldPosts) })
return
end
end
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(Posts) })
end
)
Handlers.add(
"Register",
Handlers.utils.hasMatchingTag("Action", "Register"),
function(msg)
local address = msg.Owner;
Users[address] = {}
Users[address].processId = msg.From
Users[address].created = msg.Timestamp
Users[address].lastSeen = msg.Timestamp
Users[address].following = {}
local data = json.decode(msg.Data)
Users[address].name = data.name
Users[address].avatar = data.avatar
Users[address].status = data.status
Users[address].currentWorldId = data.currentWorldId
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(Users[address]) })
end
)
Handlers.add(
"UpdateUser",
Handlers.utils.hasMatchingTag("Action", "UpdateUser"),
function(msg)
local address = msg.Owner;
Users[address].lastSeen = msg.Timestamp
if string.len(msg.Data) > 0 then
local data = json.decode(msg.Data)
if data.name then Users[address].name = data.name end
if data.avatar then Users[address].avatar = data.avatar end
if data.status then Users[address].status = data.status end
if data.currentWorldId then Users[address].currentWorldId = data.currentWorldId end
end
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(Users[address]) })
end
)
Handlers.add(
"UpdatePosition",
Handlers.utils.hasMatchingTag("Action", "UpdatePosition"),
function(msg)
local address = msg.Owner;
Users[address].lastSeen = msg.Timestamp
if string.len(msg.Data) > 0 then
local data = json.decode(msg.Data)
if type(data) == "table" and data.worldId then
if Worlds[data.worldId] then
Users[address].currentWorldId = data.worldId
Worlds[data.worldId].playerPositions[address] = {
x = data.position.x,
y = data.position.y,
}
ao.send({ Target = msg.From, Status = "OK" })
else
ao.send({ Target = msg.From, Status = "Error", Message = "World not found." })
end
return
end
end
ao.send({ Target = msg.From, Status = "Error", Message = "Missing worldId." })
end
)
Handlers.add(
"Follow",
Handlers.utils.hasMatchingTag("Action", "Follow"),
function(msg)
local address = msg.Owner;
Users[address].lastSeen = msg.Timestamp
local data = json.decode(msg.Data)
Users[address].following[data.address] = true
local Notification = {
Source = address,
Type = "Follow",
}
local followedUserProcess = Users[data.address].processId
if followedUserProcess then
ao.send({ Target = followedUserProcess, Status = "OK", Action = "Notification", Data = json.encode(Notification) })
end
ao.send({ Target = msg.From, Status = "OK" })
end
)
Handlers.add(
"Unfollow",
Handlers.utils.hasMatchingTag("Action", "Unfollow"),
function(msg)
local address = msg.Owner;
Users[address].lastSeen = msg.Timestamp
local data = json.decode(msg.Data)
Users[address].following[data.address] = nil
ao.send({ Target = msg.From, Status = "OK" })
end
)
Handlers.add(
"CreatePost",
Handlers.utils.hasMatchingTag("Action", "CreatePost"),
function(msg)
local address = msg.Owner;
Users[address].lastSeen = msg.Timestamp
local postId = msg.Id
Posts[postId] = {}
Posts[postId].created = msg.Timestamp
Posts[postId].author = address
local data = json.decode(msg.Data)
Posts[postId].worldId = data.worldId
Posts[postId].type = data.type
Posts[postId].textOrTxId = data.textOrTxId
-- local Notification = {
-- Source = address,
-- Type = "Post",
-- Post = Posts[postId],
-- }
-- -- Notify all users following this user
-- for follower, isFollowing in pairs(Users[address].following) do
-- if isFollowing then
-- local followerProcess = Users[follower].processId
-- if followerProcess then
-- ao.send({ Target = followerProcess, Status = "OK", Action = "Notification", Data = json.encode(Notification) })
-- end
-- end
-- end
ao.send({ Target = msg.From, Status = "OK", Data = json.encode(Posts[postId]) })
end
)