vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | GUILDADSCOMM_DEBUG = false; -- Is debug printf enabled? |
2 | |||
3 | GUILDADS_MSG_TYPE_ANNONCE = 0; |
||
4 | GUILDADS_MSG_TYPE_REQUEST = 1; |
||
5 | GUILDADS_MSG_TYPE_AVAILABLE = 2; |
||
6 | GUILDADS_MSG_TYPE_SKILL = 3; |
||
7 | GUILDADS_MSG_TYPE_EVENT = 4; |
||
8 | GUILDADS_MSG_TYPE_INVENTORY = 5; |
||
9 | GUILDADS_MSG_TYPE_NOTE = 6; |
||
10 | GUILDADS_MSG_TYPE_EVENT_SUBSCRIPTION = 7; |
||
11 | GUILDADS_MSG_TYPE_IGNORE = "i"; |
||
12 | |||
13 | GUILDADS_VERSION_PROTOCOL = "26"; |
||
14 | |||
15 | GUILDADS_MSG_PREFIX = "<GA"..GUILDADS_VERSION_PROTOCOL..">"; |
||
16 | GUILDADS_MSG_PREFIX_NOVERSION = "<GA"; |
||
17 | GUILDADS_MSG_PREFIX_TOKEN = '\29'; |
||
18 | GUILDADS_MSG_PREFIX_CHATCOMM = '\29'.."AD"..'\29'..GUILDADS_VERSION_PROTOCOL..'>'; |
||
19 | |||
20 | GUILDADS_MSG_ADD = "a"; |
||
21 | GUILDADS_MSG_REMOVE = "r"; |
||
22 | GUILDADS_MSG_REMOVE_ALL = "R"; |
||
23 | GUILDADS_MSG_REQUEST_ADS = "?"; |
||
24 | GUILDADS_MSG_REQUEST_INSPECT = "?i"; |
||
25 | GUILDADS_MSG_REQUEST_OFFLINES = "?o"; |
||
26 | GUILDADS_MSG_SENDING_UPDATE = "U"; |
||
27 | GUILDADS_MSG_SENDING_ALL = "S"; |
||
28 | GUILDADS_MSG_SENDING_ALL_END = "E"; |
||
29 | GUILDADS_MSG_LASTSEEN = "l"; |
||
30 | GUILDADS_MSG_LASTSEEN_END = "le"; |
||
31 | GUILDADS_MSG_META = "m"; |
||
32 | GUILDADS_MSG_CHATFLAG = "chatFlag"; |
||
33 | |||
34 | GUILDADS_STATE_UNKNOW = "unknow"; |
||
35 | GUILDADS_STATE_SYNC_ONLINE = "s_online"; |
||
36 | GUILDADS_STATE_SYNC_OFFLINE = "s_offline"; |
||
37 | GUILDADS_STATE_OK = "ok"; |
||
38 | |||
39 | local playerName = ""; |
||
40 | local MonitorAds = {}; -- record updated ads (GUILDADS_MSG_SENDING_ALL) |
||
41 | local MetaPlayers = {}; -- state, onlineSince, version |
||
42 | local MyState = GUILDADS_STATE_UNKNOW; |
||
43 | local LastSeens = {}; -- Players we are listen to give theirs offline players. |
||
44 | local WaitingOfflinesAds = { }; -- Players are we waiting for offlines ads to be GUILDADS_STATE_OK |
||
45 | local StartTime; |
||
46 | local OnMessageCommand = { }; |
||
47 | local OnMessageAd = {}; |
||
48 | local WatingForUpdate = { }; |
||
49 | |||
50 | local function DEBUG_MSG(msg) |
||
51 | if (GUILDADSCOMM_DEBUG) |
||
52 | then |
||
53 | ChatFrame1:AddMessage("GAC: "..msg, 1.0, 1.0, 0.5); |
||
54 | end |
||
55 | end |
||
56 | |||
57 | local function GAC_GetGuildChatFrame() |
||
58 | for i=1,NUM_CHAT_WINDOWS,1 do |
||
59 | local DefaultMessages = { GetChatWindowMessages(i) }; |
||
60 | for k, channel in DefaultMessages do |
||
61 | if channel == "GUILD" then |
||
62 | return getglobal("ChatFrame"..i); |
||
63 | end |
||
64 | end |
||
65 | end |
||
66 | return DEFAULT_CHAT_FRAME; |
||
67 | end |
||
68 | |||
69 | function GAC_GetMeta(player) |
||
70 | return MetaPlayers[player]; |
||
71 | end |
||
72 | |||
73 | function GAC_GetMetas() |
||
74 | return MetaPlayers; |
||
75 | end |
||
76 | |||
77 | GAC_GetFlag = SimpleComm_GetFlag; |
||
78 | |||
79 | function GAC_AddChatMessage(msg) |
||
80 | local info = ChatTypeInfo["CHANNEL"..GetChannelName( SimpleComm_Channel )]; |
||
81 | SimpleComm_ChatFrame:AddMessage(msg, info.r, info.g, info.b); |
||
82 | end |
||
83 | |||
84 | function GAC_InitVariablesLoaded() |
||
85 | SimpleComm_PreInit(GuildAds_FilterText); |
||
86 | end |
||
87 | |||
88 | function GAC_Init(playername, channel, password) |
||
89 | DEBUG_MSG("GAC_Init("..playername..","..channel..")"); |
||
90 | |||
91 | playerName = playername; |
||
92 | if (not StartTime) then |
||
93 | StartTime = GAS_currentTime(); |
||
94 | end |
||
95 | |||
96 | SimpleComm_Init( |
||
97 | channel, |
||
98 | password, |
||
99 | GAC_GetGuildChatFrame(), |
||
100 | GAC_Synchronize, |
||
101 | GAC_OnChannelLeave, |
||
102 | GAC_OnMessage, |
||
103 | GuildAds_Serialize, |
||
104 | GuildAds_Unserialize, |
||
105 | GuildAds_FilterText |
||
106 | ); |
||
107 | |||
108 | local command, alias = GuildAdsConfig_GetChannelAlias() |
||
109 | SimpleComm_InitAlias(command, alias); |
||
110 | |||
111 | SimpleComm_SetFlagListener(GAC_OnChatFlagChange); |
||
112 | |||
113 | -- Init after the channel is joined (GAC_Synchronize called by SimpleComm) |
||
114 | end |
||
115 | |||
116 | function GAC_Reinit(channel, password) |
||
117 | -- Reset internal variables |
||
118 | MonitorAds = {}; |
||
119 | LastSeens = {}; |
||
120 | WaitingOfflinesAds = { }; |
||
121 | WatingForUpdate = { }; |
||
122 | MyState = GUILDADS_STATE_UNKNOW; |
||
123 | GuildAdsSystem.SynchronizeOfflinesTimer = nil; |
||
124 | GuildAdsSystem.SynchronizeOfflinesTimerEnd = nil; |
||
125 | |||
126 | -- Reinit GuildAdsComm |
||
127 | SimpleComm_SetChannel(channel, password); |
||
128 | |||
129 | -- Init after the channel is joined (GAC_Synchronize called by SimpleComm) |
||
130 | end |
||
131 | |||
132 | function GAC_OnChannelLeave() |
||
133 | GuildAdsPlugin_OnChannelLeave(); |
||
134 | end |
||
135 | |||
136 | function GAC_Synchronize() |
||
137 | DEBUG_MSG("GAC_Synchronize"); |
||
138 | |||
139 | -- call plugin init |
||
140 | GuildAdsPlugin_OnChannelJoin(); |
||
141 | |||
142 | -- reset local variables |
||
143 | MonitorAds = {}; |
||
144 | MetaPlayers = {}; |
||
145 | LastSeens = {}; |
||
146 | WaitingOfflinesAds = {}; |
||
147 | WatingForUpdate = { }; |
||
148 | |||
149 | -- Send status |
||
150 | MyState = GUILDADS_STATE_SYNC_ONLINE; |
||
151 | GAC_SendMeta(nil); |
||
152 | |||
153 | -- Send chat status : detected by SimpleComm |
||
154 | GAC_SendChatFlag(playerName); |
||
155 | |||
156 | -- Now, send to all my ads |
||
157 | GAC_SendAllAdsType(nil, nil, nil); |
||
158 | |||
159 | -- Ask everyone to send me their ads |
||
160 | GAC_SendRequestAds(nil); |
||
161 | |||
162 | -- Wait 30 seconds and synchronize offlines |
||
163 | GuildAdsSystem.SynchronizeOfflinesCount = 0; |
||
164 | GuildAdsSystem.SynchronizeOfflinesTimer = 30; |
||
165 | GuildAdsSystem.SynchronizeOfflinesTimerEnd = false; |
||
166 | end |
||
167 | |||
168 | function GAC_SynchronizeOfflines(numberOfTries) |
||
169 | DEBUG_MSG("GAC_SynchronizeOfflines("..numberOfTries..")"); |
||
170 | |||
171 | --[[ |
||
172 | moi à player> envois de GUILDADS_MSG_REQUEST_OFFLINES |
||
173 | player à moi> envois de GUILDADS_MSG_LASTSEEN pour chacun de ses offlines |
||
174 | moi> pour chaque GUILDADS_MSG_LASTSEEN reçu : |
||
175 | si plus ancien -> envois de mes annonces |
||
176 | si plus récent -> demande les annonces à player |
||
177 | player à moi> envois de GUILDADS_MSG_LASTSEEN_END |
||
178 | envois de ses offlines non mis à jour |
||
179 | ]] |
||
180 | |||
181 | -- Ask offlines to someone who is online |
||
182 | local oneOnlinePlayer, canTryLater = GAC_GetRandomOnline(); |
||
183 | if (oneOnlinePlayer) then |
||
184 | -- Il y au moins un autre joueur |
||
185 | if (canTryLater) and (numberOfTries<=20) then |
||
186 | --[[ aucun joueur connecté n'est synchronisé |
||
187 | attends à nouveau 30 secondes |
||
188 | sauf s'il y a plus de 20 essais (donc 10 minutes) |
||
189 | nombre d'essais limité pour éviter le deadlock si |
||
190 | deux joueurs se connectent au même moment |
||
191 | onelinePlayer = true |
||
192 | ]] |
||
193 | GuildAdsSystem.SynchronizeOfflinesTimerEnd = false; |
||
194 | GuildAdsSystem.SynchronizeOfflinesCount = numberOfTries+1; |
||
195 | GuildAdsSystem.SynchronizeOfflinesTimer = 30; |
||
196 | else |
||
197 | -- Changement d'état |
||
198 | MyState = GUILDADS_STATE_SYNC_OFFLINE; |
||
199 | GAC_SendMeta(nil); |
||
200 | |||
201 | -- Il y a au moins un autre joueur connecté |
||
202 | GAC_SendRequestOfflines(oneOnlinePlayer); |
||
203 | -- 10 minutes avant d'être déclaré synchronisé |
||
204 | GuildAdsSystem.SynchronizeOfflinesTimer = 60*10; |
||
205 | GuildAdsSystem.SynchronizeOfflinesCount = 0; |
||
206 | GuildAdsSystem.SynchronizeOfflinesTimerEnd = true; |
||
207 | end |
||
208 | else |
||
209 | -- Aucun online |
||
210 | -- Synchronisation terminée |
||
211 | MyState = GUILDADS_STATE_OK; |
||
212 | GAC_SendMeta(nil); |
||
213 | end |
||
214 | end |
||
215 | |||
216 | function GAC_SynchronizeOfflinesEnd() |
||
217 | MyState = GUILDADS_STATE_OK; |
||
218 | GAC_SendMeta(nil); |
||
219 | end |
||
220 | |||
221 | function GAC_OnChatFlagChange(flag, message) |
||
222 | SimpleComm_SendMessage( |
||
223 | nil, |
||
224 | { |
||
225 | command = GUILDADS_MSG_CHATFLAG; |
||
226 | flag = flag; |
||
227 | text = message; |
||
228 | } |
||
229 | ); |
||
230 | end |
||
231 | |||
232 | --[[ |
||
233 | Retourne le nom d'un joueur connecté et synchronisé. |
||
234 | Si aucun joueur connecté n'est synchronisé, retourne true |
||
235 | Si aucun joueur n'est connecté, retourne nil |
||
236 | ]] |
||
237 | function GAC_GetRandomOnline() |
||
238 | local canTryLater = false; |
||
239 | local ready = {}; |
||
240 | for name, metainfo in MetaPlayers do |
||
241 | if (name ~= playerName) then |
||
242 | if (metainfo.state == GUILDADS_STATE_OK) then |
||
243 | tinsert(ready, name); |
||
244 | elseif (metainfo.state == GUILDADS_STATE_SYNC_OFFLINE) or (metainfo.state == GUILDADS_STATE_SYNC_ONLINE) then |
||
245 | canTryLater = true; |
||
246 | end |
||
247 | end |
||
248 | end |
||
249 | local s = table.getn(ready); |
||
250 | if (s > 0) then |
||
251 | return ready[math.random(s)], false; |
||
252 | else |
||
253 | if (canTryLater) then |
||
254 | return nil, true; |
||
255 | else |
||
256 | return nil, false; |
||
257 | end |
||
258 | end |
||
259 | end |
||
260 | |||
261 | function GAC_SendAd(who, ad_type ,ad, delay) |
||
262 | if (ad.m_Enabled == false) then |
||
263 | return; |
||
264 | end |
||
265 | SimpleComm_SendMessage( |
||
266 | who, |
||
267 | { |
||
268 | command = GUILDADS_MSG_ADD; |
||
269 | adtype = ad_type; |
||
270 | id = ad.id; |
||
271 | text = ad.text; |
||
272 | texture = ad.texture; |
||
273 | count = ad.count; |
||
274 | itemRef = ad.itemRef; |
||
275 | itemName = ad.itemName; |
||
276 | itemColor = ad.itemColor; |
||
277 | skillRank = ad.skillRank; |
||
278 | skillMaxRank = ad.skillMaxRank; |
||
279 | creationtime = ad.creationtime; |
||
280 | owner = ad.owner |
||
281 | }, |
||
282 | delay |
||
283 | ); |
||
284 | end |
||
285 | |||
286 | function GAC_SendSkill(who, id, skillRank, skillMaxRank, delay) |
||
287 | SimpleComm_SendMessage( |
||
288 | who, |
||
289 | { |
||
290 | command = GUILDADS_MSG_ADD; |
||
291 | adtype = GUILDADS_MSG_TYPE_SKILL; |
||
292 | id = id; |
||
293 | skillRank = skillRank; |
||
294 | skillMaxRank = skillMaxRank; |
||
295 | creationtime = GAS_currentTime() |
||
296 | }, |
||
297 | delay |
||
298 | ); |
||
299 | end |
||
300 | |||
301 | function GAC_SendSkills(who, delay) |
||
302 | local grp = ""; |
||
303 | for i = 1, GetNumSkillLines(), 1 do |
||
304 | local skillName, header, isExpanded, skillRank, numTempPoints, skillModifier, skillMaxRank, isAbandonable, stepCost, rankCost, minLevel, skillCostType = GetSkillLineInfo(i); |
||
305 | if (header == 1) then |
||
306 | grp = skillName; |
||
307 | else |
||
308 | local id = GAS_GetSkillId(skillName); |
||
309 | if (id > 0) then |
||
310 | GAC_SendSkill(who, id, skillRank, skillMaxRank, delay) |
||
311 | end |
||
312 | end |
||
313 | end |
||
314 | end |
||
315 | |||
316 | function GAC_SendInventory(who, slot, texture, color, ref, name, count) |
||
317 | SimpleComm_SendMessage( |
||
318 | who, |
||
319 | { |
||
320 | command = GUILDADS_MSG_ADD; |
||
321 | adtype = GUILDADS_MSG_TYPE_INVENTORY; |
||
322 | id = slot; |
||
323 | itemRef = ref; |
||
324 | itemName = name; |
||
325 | itemColor = color; |
||
326 | count = count; |
||
327 | texture = texture; |
||
328 | creationtime = GAS_currentTime() |
||
329 | } |
||
330 | ); |
||
331 | end |
||
332 | |||
333 | function GAC_SendInspect(who) |
||
334 | local buffer = {}; |
||
335 | for slot=1, 19, 1 do |
||
336 | local link = GetInventoryItemLink("player", slot); |
||
337 | if (link) then |
||
338 | -- local title = TEXT(getglobal(strupper(SlotIdText[slot]))); |
||
339 | local texture = GetInventoryItemTexture("player", slot); |
||
340 | local count = GetInventoryItemCount("player", slot); |
||
341 | local color, ref, name = GAS_UnpackLink(link); |
||
342 | tinsert(buffer, { |
||
343 | slot = slot, |
||
344 | texture = texture, |
||
345 | color = color, |
||
346 | ref = ref, |
||
347 | name = name, |
||
348 | count = count |
||
349 | } |
||
350 | ); |
||
351 | end |
||
352 | end |
||
353 | |||
354 | GAC_SendingUpdate(who, table.getn(buffer)); |
||
355 | local index = 1; |
||
356 | while buffer[index] do |
||
357 | GAC_SendInventory(who, buffer[index].slot, buffer[index].texture, buffer[index].color, buffer[index].ref, buffer[index].name, buffer[index].count); |
||
358 | index = index + 1; |
||
359 | end |
||
360 | end |
||
361 | |||
362 | function GAC_SendRequestInspect(who) |
||
363 | SimpleComm_SendMessage( |
||
364 | who, |
||
365 | { |
||
366 | command = GUILDADS_MSG_REQUEST_INSPECT |
||
367 | } |
||
368 | ); |
||
369 | end |
||
370 | |||
371 | function GAC_SendRequestOfflines(who) |
||
372 | DEBUG_MSG("GAC_SendRequestOfflines("..NoNil(who)..")"); |
||
373 | LastSeens[who] = {}; |
||
374 | SimpleComm_SendMessage( |
||
375 | who, |
||
376 | { |
||
377 | command = GUILDADS_MSG_REQUEST_OFFLINES |
||
378 | } |
||
379 | ); |
||
380 | end |
||
381 | |||
382 | function GAC_SendRequestAds(who, owner) |
||
383 | DEBUG_MSG("GAC_SendRequestAds("..NoNil(who)..")"); |
||
384 | SimpleComm_SendMessage( |
||
385 | who, |
||
386 | { |
||
387 | command = GUILDADS_MSG_REQUEST_ADS; |
||
388 | owner = owner |
||
389 | } |
||
390 | ); |
||
391 | end |
||
392 | |||
393 | |||
394 | function GAC_SendAnnonce(who, owner) |
||
395 | local powner, level, race, class, name, guildName, guildRankName, guildRankIndex, creationtime; |
||
396 | if (owner) then |
||
397 | powner = GAS_ProfileGet(owner); |
||
398 | level = powner.level; |
||
399 | race = powner.race; |
||
400 | class = powner.class; |
||
401 | name = owner; |
||
402 | guildName = powner.guild; |
||
403 | creationtime = powner.creationtime; |
||
404 | accountid = powner.accountid; |
||
405 | else |
||
406 | level = UnitLevel("player"); |
||
407 | race = GAS_GetRaceId(UnitRace("player")); |
||
408 | class = GAS_GetClassId(UnitClass("player")); |
||
409 | name = playerName; |
||
410 | guildName, guildRankName, guildRankIndex = GetGuildInfo("player"); |
||
411 | creationtime = GAS_currentTime(); |
||
412 | accountid = GAS_GetAccountId(); |
||
413 | end |
||
414 | |||
415 | SimpleComm_SendMessage( |
||
416 | who, |
||
417 | { |
||
418 | command = GUILDADS_MSG_ADD; |
||
419 | adtype = GUILDADS_MSG_TYPE_ANNONCE; |
||
420 | accountId = accountid; |
||
421 | class = class; |
||
422 | race = race; |
||
423 | level = level; |
||
424 | guild = guildName; |
||
425 | creationtime = creationtime; |
||
426 | owner = owner |
||
427 | } |
||
428 | ); |
||
429 | end |
||
430 | |||
431 | function GAC_SendRemove(who, adtype, id) |
||
432 | SimpleComm_SendMessage( |
||
433 | who, |
||
434 | { |
||
435 | command = GUILDADS_MSG_REMOVE; |
||
436 | adtype = adtype; |
||
437 | id = id |
||
438 | } |
||
439 | ); |
||
440 | end |
||
441 | |||
442 | function GAC_SendRemoveAll(who) |
||
443 | SimpleComm_SendMessage( |
||
444 | who, |
||
445 | { |
||
446 | command = GUILDADS_MSG_REMOVE_ALL; |
||
447 | } |
||
448 | ); |
||
449 | end |
||
450 | |||
451 | function GAC_SendingUpdate(who, count, owner, delay) |
||
452 | SimpleComm_SendMessage( |
||
453 | who, |
||
454 | { |
||
455 | command = GUILDADS_MSG_SENDING_UPDATE, |
||
456 | owner = owner, |
||
457 | count = count |
||
458 | }, |
||
459 | delay |
||
460 | ); |
||
461 | end |
||
462 | |||
463 | function GAC_SendingAll(who, owner) |
||
464 | SimpleComm_SendMessage( |
||
465 | who, |
||
466 | { |
||
467 | command = GUILDADS_MSG_SENDING_ALL; |
||
468 | owner = owner |
||
469 | } |
||
470 | ); |
||
471 | end |
||
472 | |||
473 | function GAC_SendingAllEnd(who, owner, delay) |
||
474 | local creationtime = 0; |
||
475 | if (owner) then |
||
476 | creationtime = GAS_ProfileGetUpdatedDate(owner); |
||
477 | else |
||
478 | creationtime = GAS_ProfileGetUpdatedDate(playerName); |
||
479 | end |
||
480 | SimpleComm_SendMessage( |
||
481 | who, |
||
482 | { |
||
483 | command = GUILDADS_MSG_SENDING_ALL_END; |
||
484 | creationtime = creationtime; |
||
485 | owner = owner; |
||
486 | }, |
||
487 | delay |
||
488 | ); |
||
489 | end |
||
490 | |||
491 | function GAC_SendLastSeen(who, owner, time) |
||
492 | DEBUG_MSG("GAC_SendLastSeen("..NoNil(who)..")"); |
||
493 | SimpleComm_SendMessage( |
||
494 | who, |
||
495 | { |
||
496 | command = GUILDADS_MSG_LASTSEEN; |
||
497 | creationtime = time; |
||
498 | owner = owner |
||
499 | } |
||
500 | ); |
||
501 | end |
||
502 | |||
503 | function GAC_SendLastSeenEnd(who) |
||
504 | DEBUG_MSG("GAC_SendLastSeenEnd("..NoNil(who)..")"); |
||
505 | SimpleComm_SendMessage( |
||
506 | who, |
||
507 | { |
||
508 | command = GUILDADS_MSG_LASTSEEN_END; |
||
509 | } |
||
510 | ); |
||
511 | end |
||
512 | |||
513 | function GAC_SendAllAds(who, owner, adTable, ad_type, delay) |
||
514 | local size = table.getn(adTable); |
||
515 | if owner then |
||
516 | for i = 1, size, 1 do |
||
517 | if adTable[i].owner == owner then |
||
518 | GAC_SendAd(who, ad_type, adTable[i], delay); |
||
519 | end |
||
520 | end |
||
521 | else |
||
522 | for i = 1, size, 1 do |
||
523 | GAC_SendAd(who, ad_type, adTable[i], delay); |
||
524 | end |
||
525 | end |
||
526 | end |
||
527 | |||
528 | function GAC_SendAllAdsType(who, owner, delay) |
||
529 | DEBUG_MSG("GAC_SendAllAdsType("..NoNil(who)..","..NoNil(owner)..")"); |
||
530 | |||
531 | -- On n'envois pas à who ses annonces |
||
532 | -- sauf si c'est nous même, ou si c'est à tout le monde |
||
533 | if (who == owner) and (who ~= playerName) and (who ~= nil) then |
||
534 | return; |
||
535 | end |
||
536 | |||
537 | -- Ads->Start |
||
538 | GAC_SendingAll(who, owner); |
||
539 | |||
540 | -- Ads : Guild |
||
541 | GAC_SendAnnonce(who, owner); |
||
542 | |||
543 | -- Ads : Ask/Have/Event |
||
544 | local adTables = {}; |
||
545 | if (owner) then |
||
546 | adTables = GAS_GetAds(); |
||
547 | else |
||
548 | adTables = GAS_GetMyAds(); |
||
549 | end |
||
550 | |||
551 | GAC_SendAllAds(who, owner, adTables[GUILDADS_MSG_TYPE_REQUEST], GUILDADS_MSG_TYPE_REQUEST, delay); |
||
552 | GAC_SendAllAds(who, owner, adTables[GUILDADS_MSG_TYPE_AVAILABLE], GUILDADS_MSG_TYPE_AVAILABLE, delay); |
||
553 | GAC_SendAllAds(who, owner, adTables[GUILDADS_MSG_TYPE_EVENT], GUILDADS_MSG_TYPE_EVENT, delay); |
||
554 | |||
555 | -- Ads : Skills |
||
556 | if owner then |
||
557 | GAC_SendAllAds(who, owner, adTables[GUILDADS_MSG_TYPE_SKILL], GUILDADS_MSG_TYPE_SKILL, delay); |
||
558 | else |
||
559 | GAC_SendSkills(who, delay); |
||
560 | end |
||
561 | |||
562 | -- Ads->End |
||
563 | GAC_SendingAllEnd(who, owner, delay); |
||
564 | end |
||
565 | |||
566 | function GAC_SendMeta(who) |
||
567 | SimpleComm_SendMessage( |
||
568 | who, |
||
569 | { |
||
570 | command = GUILDADS_MSG_META; |
||
571 | text = GUILDADS_VERSION; |
||
572 | creationtime = StartTime; |
||
573 | id = MyState; |
||
574 | } |
||
575 | ); |
||
576 | end |
||
577 | |||
578 | function GAC_SendChatFlag(who) |
||
579 | local flag, message = SimpleComm_GetFlag(playerName); |
||
580 | SimpleComm_SendMessage( |
||
581 | nil, |
||
582 | { |
||
583 | command = GUILDADS_MSG_CHATFLAG; |
||
584 | flag = flag; |
||
585 | text = message; |
||
586 | } |
||
587 | ); |
||
588 | end |
||
589 | |||
590 | -------------------------------------------------------------------------------- |
||
591 | -- |
||
592 | -- OnMessage |
||
593 | -- |
||
594 | --------------------------------------------------------------------------------- |
||
595 | function GAC_OnMessage(author, message) |
||
596 | if (GUILDADSCOMM_DEBUG) then |
||
597 | DEBUG_MSG("[OnMessage,"..author.."]: "..GuildAds_Serialize(message)); |
||
598 | end |
||
599 | |||
600 | -- Ignore this author ? |
||
601 | if GAS_ProfileIsIgnored(author) then |
||
602 | return; |
||
603 | end |
||
604 | |||
605 | -- Set online |
||
606 | GAS_SetOnlineStatus(author, true); |
||
607 | |||
608 | -- A propos de quelle personne : l'auteur du message (author) ou une autre (message.owner) |
||
609 | local owner = author; |
||
610 | if (message.owner) then |
||
611 | owner = message.owner; |
||
612 | end |
||
613 | |||
614 | -- Mise à jour du dernier message de author |
||
615 | if (owner==author) and ((message.command == GUILDADS_MSG_ADD) or (message.command == GUILDADS_MSG_REMOVE)) then |
||
616 | GAS_ProfileSetUpdatedDate(author, message.creationtime); |
||
617 | end |
||
618 | |||
619 | -- |
||
620 | -- Process all the messages we know about |
||
621 | -- |
||
622 | if (message.command == GUILDADS_MSG_ADD) then |
||
623 | -- Accept only if : |
||
624 | -- this owner isn't ignored |
||
625 | -- AND ( this owner has sent GUILDADS_MSG_SENDING_ALL which was accepted. |
||
626 | -- OR this owner has sent a GUILDADS_MSG_SENDING_UPDATE ) |
||
627 | if not GAS_ProfileIsIgnored(owner) and (MonitorAds[owner] or WatingForUpdate[owner]) then |
||
628 | -- |
||
629 | local adtype = message.adtype; |
||
630 | |||
631 | -- Use this update |
||
632 | if WatingForUpdate[owner] then |
||
633 | WatingForUpdate[owner] = WatingForUpdate[owner] - 1; |
||
634 | if WatingForUpdate[owner]==0 then |
||
635 | WatingForUpdate[owner] = nil; |
||
636 | end |
||
637 | DEBUG_MSG("Count"..NoNil(WatingForUpdate[owner])); |
||
638 | end |
||
639 | |||
640 | -- Call onMessageAd |
||
641 | if OnMessageAd[adtype] then |
||
642 | if not OnMessageAd[adtype](author, message) then |
||
643 | return; |
||
644 | end |
||
645 | end |
||
646 | |||
647 | -- Inventory |
||
648 | if (adtype == GUILDADS_MSG_TYPE_INVENTORY) then |
||
649 | GAS_ProfileSetInventorySlot(message.creationtime, owner, message.id, message.itemColor, message.itemRef, message.itemName, message.texture, message.count); |
||
650 | return false; |
||
651 | end |
||
652 | |||
653 | -- Mise à jour du profile |
||
654 | if (adtype == GUILDADS_MSG_TYPE_ANNONCE) then |
||
655 | GAS_ProfileSetGeneral(message.creationtime, owner, message.race, message.class, message.level, message.guild); |
||
656 | GAS_ProfileSetAccountId(owner, message.accountId); |
||
657 | message.guild = nil; |
||
658 | message.level = nil; |
||
659 | message.class = nil; |
||
660 | message.race = nil; |
||
661 | message.accountId = nil; |
||
662 | message.id = owner; |
||
663 | end |
||
664 | |||
665 | -- Monitor : l'annonce a été mise à jour |
||
666 | -- cf GUILDADS_MSG_SENDING_ALL et GUILDADS_MSG_SENDING_ALL_END |
||
667 | if adtype and owner and MonitorAds[owner] then |
||
668 | if (MonitorAds[owner][adtype] == nil) then |
||
669 | MonitorAds[owner][adtype] = {}; |
||
670 | end |
||
671 | MonitorAds[owner][adtype][message.id] = true; |
||
672 | end |
||
673 | |||
674 | -- Scan for dupes |
||
675 | if ((adtype == GUILDADS_MSG_TYPE_REQUEST) or (adtype == GUILDADS_MSG_TYPE_AVAILABLE) or (adtype == GUILDADS_MSG_TYPE_EVENT)) then |
||
676 | local ads = GAS_GetAds(); |
||
677 | for k,v in ads[adtype] do |
||
678 | if ( v.owner == owner and v.id == message.id and v.text == message.text and v.itemName == message.itemName and v.count == message.count ) then |
||
679 | -- DONT ADD IT AGAIN |
||
680 | DEBUG_MSG("GuildAds_OnMessage: Already in DB"); |
||
681 | return false; |
||
682 | end |
||
683 | end |
||
684 | end |
||
685 | |||
686 | |||
687 | -- Effacer le précédent Id |
||
688 | GAS_RemoveByOwnerAndId(owner, adtype, message.id); |
||
689 | |||
690 | -- Efface les donnees inutiles |
||
691 | message.adtype = nil; |
||
692 | message.command = nil; |
||
693 | message.owner = owner; |
||
694 | message.currenttime = nil; |
||
695 | |||
696 | -- Traduction du nom si necessaire |
||
697 | if message.itemName and message.itemRef then |
||
698 | local info = GAS_GetItemInfo(message.itemRef); |
||
699 | if (info.name) then |
||
700 | message.itemName = info.name; |
||
701 | end |
||
702 | end |
||
703 | |||
704 | -- Ajout de l'annonce |
||
705 | GAS_AddAd(owner, adtype, message); |
||
706 | end |
||
707 | |||
708 | elseif (message.command == GUILDADS_MSG_REMOVE) then |
||
709 | -- Ignore this owner ? |
||
710 | if not GAS_ProfileIsIgnored(owner) then |
||
711 | -- A previously placed add is being removed |
||
712 | GAS_RemoveByOwnerAndId(owner, message.adtype, message.id); |
||
713 | end |
||
714 | |||
715 | elseif (message.command == GUILDADS_MSG_REQUEST_INSPECT) then |
||
716 | if not GAS_ProfileIsIgnored(owner) then |
||
717 | -- Send inspect to the author |
||
718 | -- Always my inventory |
||
719 | GAC_SendInspect(author); |
||
720 | end |
||
721 | |||
722 | elseif (message.command == GUILDADS_MSG_REQUEST_OFFLINES) then |
||
723 | if not GAS_ProfileIsIgnored(owner) then |
||
724 | -- Pour chaque personne offline |
||
725 | -- -> envois la date du dernier message |
||
726 | local ads = GAS_GetAds(); |
||
727 | for i, ad in ads[GUILDADS_MSG_TYPE_ANNONCE] do |
||
728 | if (not GAS_IsOnline(ad.owner)) and (not GAS_ProfileIsIgnored(ad.owner)) then |
||
729 | GAC_SendLastSeen(author, ad.owner, GAS_ProfileGetUpdatedDate(ad.owner)); |
||
730 | end |
||
731 | end |
||
732 | GAC_SendLastSeenEnd(author); |
||
733 | end |
||
734 | |||
735 | elseif (message.command == GUILDADS_MSG_LASTSEEN) then |
||
736 | if (owner ~= playerName) and (LastSeens[author]) and (not GAS_ProfileIsIgnored(owner)) then |
||
737 | LastSeens[author][owner] = true; |
||
738 | local myUpdate = GAS_ProfileGetUpdatedDate(owner); |
||
739 | if (message.creationtime ~= myUpdate) then |
||
740 | if (myUpdate) then |
||
741 | if (message.creationtime==nil or message.creationtime < myUpdate) then |
||
742 | DEBUG_MSG("LastSeen("..owner..")="..NoNil(message.creationtime).."<"..NoNil(myUpdate)); |
||
743 | -- on doit faire la mise à jour pour tout le monde |
||
744 | GAC_SendAllAdsType(nil, owner); |
||
745 | elseif (message.creationtime > myUpdate) then |
||
746 | DEBUG_MSG("LastSeen("..owner..")="..NoNil(message.creationtime)..">"..NoNil(myUpdate)); |
||
747 | -- on doit récupérer la mise à jour |
||
748 | tinsert(WaitingOfflinesAds, owner); |
||
749 | end |
||
750 | else |
||
751 | DEBUG_MSG("LastSeen("..owner..")="..NoNil(message.creationtime).."/"..NoNil(myUpdate)); |
||
752 | -- on doit récupérer la mise à jour |
||
753 | tinsert(WaitingOfflinesAds, owner); |
||
754 | end |
||
755 | end |
||
756 | end |
||
757 | |||
758 | elseif (message.command == GUILDADS_MSG_LASTSEEN_END) then |
||
759 | if LastSeens[author] and not GAS_ProfileIsIgnored(author) then |
||
760 | -- envois les annonces des joueurs non connus. |
||
761 | local ads = GAS_GetAds(); |
||
762 | for i, ad in ads[GUILDADS_MSG_TYPE_ANNONCE] do |
||
763 | -- si ad.owner est offline et non connu par author |
||
764 | -- alors on envois ses informations à propos de ad.owner |
||
765 | if (not GAS_IsOnline(ad.owner) and LastSeens[author][ad.owner]==nil) then |
||
766 | GAC_SendAllAdsType(nil, ad.owner); |
||
767 | end |
||
768 | end |
||
769 | |||
770 | -- Dernier message de author à propos des offlines |
||
771 | -- Donc on n'écoute plus les message de author |
||
772 | LastSeens[author] = nil; |
||
773 | |||
774 | -- |
||
775 | if (table.getn(WaitingOfflinesAds) > 0) then |
||
776 | -- demande de mise à jour pour soi. |
||
777 | for _, owner in WaitingOfflinesAds do |
||
778 | GAC_SendRequestAds(author, owner); |
||
779 | end |
||
780 | else |
||
781 | -- aucune demande de offlines |
||
782 | -- on est donc synchro : passage a l'etat OK |
||
783 | GuildAdsSystem.SynchronizeOfflinesTimer = nil; |
||
784 | MyState = GUILDADS_STATE_OK; |
||
785 | GAC_SendMeta(nil); |
||
786 | end |
||
787 | end |
||
788 | |||
789 | elseif (message.command == GUILDADS_MSG_REQUEST_ADS) then |
||
790 | -- Someone is requesting ads, probably just arrived in the channel |
||
791 | -- if message.owner is set : owner's ads |
||
792 | -- if message.owner is nil : my ads (send my ads in few seconds) |
||
793 | -- owner is meaningless |
||
794 | if GuildAds.Config.PublishMyAds and not GAS_ProfileIsIgnored(owner) then |
||
795 | if (author ~= playerName) then |
||
796 | if message.owner then |
||
797 | GAC_SendAllAdsType(author, message.owner); |
||
798 | else |
||
799 | GAC_SendMeta(author); |
||
800 | GAC_SendChatFlag(author); |
||
801 | GAC_SendAllAdsType(author, nil, math.random(20)); |
||
802 | end |
||
803 | end |
||
804 | end |
||
805 | |||
806 | elseif (message.command == GUILDADS_MSG_SENDING_UPDATE) then |
||
807 | |||
808 | WatingForUpdate[owner] = (WatingForUpdate[owner] or 0) + message.count; |
||
809 | |||
810 | elseif (message.command == GUILDADS_MSG_SENDING_ALL) then |
||
811 | if not GAS_ProfileIsIgnored(owner) then |
||
812 | -- Start recording each update ads |
||
813 | if message.owner then |
||
814 | -- Accept offline ads from owner we need. (see GUILDADS_MSG_LASTSEEN) |
||
815 | i = 1; |
||
816 | while WaitingOfflinesAds[i] and WaitingOfflinesAds[i]~=owner do |
||
817 | i = i+1; |
||
818 | end |
||
819 | if WaitingOfflinesAds[i] and WaitingOfflinesAds[i]==owner then |
||
820 | MonitorAds[owner] = {}; |
||
821 | end |
||
822 | |||
823 | -- accept new ads from author who are in GUILDADS_STATE_SYNC_OFFLINE state. |
||
824 | if MetaPlayers[author] and MetaPlayers[author].state==GUILDADS_STATE_SYNC_OFFLINE then |
||
825 | MonitorAds[owner] = {}; |
||
826 | end |
||
827 | else |
||
828 | -- online synchronization : ok |
||
829 | MonitorAds[owner] = {}; |
||
830 | end |
||
831 | end |
||
832 | |||
833 | elseif (message.command == GUILDADS_MSG_SENDING_ALL_END) then |
||
834 | if MonitorAds[owner] and not GAS_ProfileIsIgnored(owner) then |
||
835 | |||
836 | -- Update profile date |
||
837 | if owner then |
||
838 | GAS_ProfileSetUpdatedDate(owner, message.creationtime); |
||
839 | end |
||
840 | |||
841 | -- for each owner's ads |
||
842 | -- if not updated (ie in MonitorAds), we delete it |
||
843 | local ads = GAS_GetAds(); |
||
844 | for adtype, tads in ads do |
||
845 | if (MonitorAds[owner][adtype] == nil) then |
||
846 | GAS_RemoveByOwner(owner, adtype); |
||
847 | else |
||
848 | for i, ad in tads do |
||
849 | if (owner==ad.owner and MonitorAds[owner][adtype][ad.id] == nil) then |
||
850 | GAS_RemoveByOwnerAndId(owner, adtype, ad.id); |
||
851 | end |
||
852 | end |
||
853 | end |
||
854 | end |
||
855 | |||
856 | -- stop monitoring |
||
857 | MonitorAds[owner] = nil; |
||
858 | |||
859 | -- Unstack owner in WatingOfflineAds |
||
860 | -- if this is the last, we are sync |
||
861 | -- so set state to GUILDADS_STATE_OK |
||
862 | i = 1; |
||
863 | while WaitingOfflinesAds[i] and WaitingOfflinesAds[i]~=owner do |
||
864 | i = i+1; |
||
865 | end |
||
866 | if WaitingOfflinesAds[i] and WaitingOfflinesAds[i]==owner then |
||
867 | table.remove(WaitingOfflinesAds, i); |
||
868 | if (table.getn(WaitingOfflinesAds)==0) then |
||
869 | GuildAdsSystem.SynchronizeOfflinesTimer = nil; |
||
870 | MyState = GUILDADS_STATE_OK; |
||
871 | GAC_SendMeta(nil); |
||
872 | end |
||
873 | end |
||
874 | |||
875 | end |
||
876 | |||
877 | elseif (message.command == GUILDADS_MSG_REMOVE_ALL) then |
||
878 | |||
879 | if not GAS_ProfileIsIgnored(owner) then |
||
880 | -- Remove all the ads from the owner |
||
881 | GAS_RemoveByOwner(owner, GUILDADS_MSG_TYPE_ANNONCE); |
||
882 | GAS_RemoveByOwner(owner, GUILDADS_MSG_TYPE_REQUEST); |
||
883 | GAS_RemoveByOwner(owner, GUILDADS_MSG_TYPE_AVAILABLE); |
||
884 | GAS_RemoveByOwner(owner, GUILDADS_MSG_TYPE_SKILL); |
||
885 | GAS_RemoveByOwner(owner, GUILDADS_MSG_TYPE_EVENT); |
||
886 | end |
||
887 | |||
888 | elseif (message.command == GUILDADS_MSG_META) then |
||
889 | |||
890 | if not GAS_ProfileIsIgnored(author) then |
||
891 | MetaPlayers[author] = { |
||
892 | state = message.id; |
||
893 | onlineSince = message.creationtime; |
||
894 | version = message.text; |
||
895 | }; |
||
896 | end |
||
897 | |||
898 | elseif (message.command == GUILDADS_MSG_CHATFLAG) then |
||
899 | |||
900 | if author~=playerName then |
||
901 | SimpleComm_SetFlag(author, message.flag, message.text); |
||
902 | end |
||
903 | |||
904 | elseif OnMessageCommand[message.command] then |
||
905 | OnMessageCommand[message.command](author, message); |
||
906 | else |
||
907 | -- This message was unknown |
||
908 | end |
||
909 | end |
||
910 | |||
911 | |||
912 | -------------------------------------------------------------------------------- |
||
913 | -- |
||
914 | -- Serialize/Unserialize |
||
915 | -- |
||
916 | --------------------------------------------------------------------------------- |
||
917 | function SerializeId(obj) |
||
918 | if (type(obj) == "nil" ) then |
||
919 | return ""; |
||
920 | else |
||
921 | return obj |
||
922 | end |
||
923 | end |
||
924 | |||
925 | function UnserializeId(str) |
||
926 | return str; |
||
927 | end |
||
928 | |||
929 | function SerializeString(obj) |
||
930 | if (type(obj) == "nil" ) then |
||
931 | return ""; |
||
932 | else |
||
933 | return obj |
||
934 | end |
||
935 | end |
||
936 | |||
937 | function UnserializeString(str) |
||
938 | if (str == "") then |
||
939 | return nil; |
||
940 | else |
||
941 | return str |
||
942 | end |
||
943 | end |
||
944 | |||
945 | function SerializeTexture(obj) |
||
946 | if (type(obj) == "nil" ) then |
||
947 | return ""; |
||
948 | else |
||
949 | return string.gsub(obj, "Interface\\Icons\\", "\@"); |
||
950 | end |
||
951 | end |
||
952 | |||
953 | function UnserializeTexture(str) |
||
954 | if (str == "") then |
||
955 | return nil; |
||
956 | else |
||
957 | return string.gsub(str, "\@", "Interface\\Icons\\"); |
||
958 | end |
||
959 | end |
||
960 | |||
961 | function SerializeItemRef(obj) |
||
962 | if (type(obj) == "nil" ) then |
||
963 | return ""; |
||
964 | else |
||
965 | return string.gsub(string.gsub(obj, "item\:", "\@"), ":0:0:0", "\*"); |
||
966 | end |
||
967 | end |
||
968 | |||
969 | function UnserializeItemRef(str) |
||
970 | if (str == "") then |
||
971 | return nil; |
||
972 | else |
||
973 | return string.gsub(string.gsub(str, "\@", "item\:"), "\*", ":0:0:0"); |
||
974 | end |
||
975 | end |
||
976 | |||
977 | function SerializeColor(obj) |
||
978 | if obj==nil then |
||
979 | return ""; |
||
980 | elseif obj=="ffa335ee" then |
||
981 | return "E"; -- epic |
||
982 | elseif obj=="ff0070dd" then |
||
983 | return "R"; -- rare |
||
984 | elseif obj=="ff1eff00" then |
||
985 | return "U" -- uncommun |
||
986 | elseif obj=="ffffffff" then |
||
987 | return "C" -- common |
||
988 | elseif obj=="ff9d9d9d" then |
||
989 | return "P" -- poor |
||
990 | else |
||
991 | return obj; |
||
992 | end |
||
993 | end |
||
994 | |||
995 | function UnserializeColor(str) |
||
996 | if str==nil then |
||
997 | return nil |
||
998 | elseif str=="E" then |
||
999 | return "ffa335ee"; |
||
1000 | elseif str=="R" then |
||
1001 | return "ff0070dd"; |
||
1002 | elseif str=="U" then |
||
1003 | return "ff1eff00"; |
||
1004 | elseif str=="C" then |
||
1005 | return "ffffffff"; |
||
1006 | elseif str=="P" then |
||
1007 | return "ff9d9d9d"; |
||
1008 | else |
||
1009 | return str; |
||
1010 | end |
||
1011 | end |
||
1012 | |||
1013 | function SerializeInteger(obj) |
||
1014 | if (type(obj) == "nil" ) then |
||
1015 | return ""; |
||
1016 | else |
||
1017 | return obj; |
||
1018 | end |
||
1019 | end |
||
1020 | |||
1021 | function UnserializeInteger(str) |
||
1022 | return tonumber(str); |
||
1023 | end |
||
1024 | |||
1025 | function SerializeTime(obj) |
||
1026 | if (type(obj) == "nil" ) then |
||
1027 | return ""; |
||
1028 | else |
||
1029 | -- convertion en base 52 |
||
1030 | value = ""; |
||
1031 | while (obj ~= 0) do |
||
1032 | i = floor(obj / 52); |
||
1033 | j = obj - i*52; |
||
1034 | if (j>=26) then |
||
1035 | value = string.char(65+j-26)..value; |
||
1036 | else |
||
1037 | value = string.char(96+j)..value; |
||
1038 | end |
||
1039 | obj = i; |
||
1040 | end |
||
1041 | return value; |
||
1042 | end |
||
1043 | end |
||
1044 | |||
1045 | function UnserializeTime(str) |
||
1046 | if (str == "") then |
||
1047 | return nil; |
||
1048 | else |
||
1049 | number = 0; |
||
1050 | for i=1, string.len(str),1 do |
||
1051 | o = string.byte(str, i); |
||
1052 | if (o> 95) then |
||
1053 | j = o-96; |
||
1054 | else |
||
1055 | j = o-65+26; |
||
1056 | end |
||
1057 | number = number*52 + j; |
||
1058 | end |
||
1059 | return number; |
||
1060 | end |
||
1061 | end |
||
1062 | |||
1063 | function SerializeObj(obj) |
||
1064 | if (type(obj) == "nil" ) then |
||
1065 | return ""; |
||
1066 | elseif ( type(obj) == "string" ) then |
||
1067 | return "s"..string.gsub(string.gsub(string.gsub(obj, ">", ">"), "|([cHhr])", "&%1;"), "|", "&p;"); |
||
1068 | elseif ( type(obj) == "number" ) then |
||
1069 | return "n"..obj; |
||
1070 | elseif ( type(obj) == "boolean" ) then |
||
1071 | if (value) then |
||
1072 | return "1"; |
||
1073 | else |
||
1074 | return "0"; |
||
1075 | end |
||
1076 | elseif ( type(obj) == "function" ) then |
||
1077 | return ""; -- nil |
||
1078 | elseif ( type(obj) == "table" ) then |
||
1079 | return ""; -- nil |
||
1080 | end |
||
1081 | return ""; |
||
1082 | end |
||
1083 | |||
1084 | function UnserializeObj(str) |
||
1085 | if (str == "") then |
||
1086 | return nil; |
||
1087 | else |
||
1088 | typeString = string.sub(str, 0, 1); |
||
1089 | valueString = string.sub(str, 2); |
||
1090 | if (typeString == "s") then |
||
1091 | return string.gsub(string.gsub(string.gsub(valueString, ">", ">"), "&(%w);", "|%1"), "&p;", "|"); |
||
1092 | elseif (typeString == "n") then |
||
1093 | return tonumber(valueString); |
||
1094 | elseif (typeString == "1") then |
||
1095 | return true; |
||
1096 | elseif (typeString == "0") then |
||
1097 | return false; |
||
1098 | else |
||
1099 | DEBUG_MSG("GuildAds_Unserialize: Type non reconnu:"..str); |
||
1100 | return nil; |
||
1101 | end |
||
1102 | end |
||
1103 | end |
||
1104 | |||
1105 | local SerializeMeta = { |
||
1106 | [1] = { ["key"] ="command", ["fout"]=SerializeId, ["fin"]=UnserializeId } |
||
1107 | }; |
||
1108 | |||
1109 | local SerializeCommand = { |
||
1110 | [GUILDADS_MSG_ADD] = { |
||
1111 | [1] = { ["key"]="owner", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1112 | [2] = { ["key"]="adtype", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1113 | [3] = { ["key"]="id", ["fout"]=SerializeObj, ["fin"]=UnserializeObj }, |
||
1114 | [4] = { ["key"]="currenttime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime}, |
||
1115 | [5] = { ["key"]="creationtime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime}, |
||
1116 | }, |
||
1117 | [GUILDADS_MSG_REMOVE] = { |
||
1118 | [1] = { ["key"] ="owner", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1119 | [2] = { ["key"] ="adtype", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1120 | [3] = { ["key"] ="id", ["fout"]=SerializeObj, ["fin"]=UnserializeObj }, |
||
1121 | }, |
||
1122 | [GUILDADS_MSG_REMOVE_ALL] = { |
||
1123 | [1] = { ["key"] ="owner", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1124 | }, |
||
1125 | [GUILDADS_MSG_REQUEST_ADS] = { |
||
1126 | [1] = { ["key"] ="owner", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1127 | }, |
||
1128 | [GUILDADS_MSG_REQUEST_OFFLINES] = {}, |
||
1129 | [GUILDADS_MSG_REQUEST_INSPECT] = {}, |
||
1130 | [GUILDADS_MSG_SENDING_UPDATE] = { |
||
1131 | [1] = { ["key"] ="owner", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1132 | [2] = { ["key"] ="count", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1133 | }, |
||
1134 | [GUILDADS_MSG_SENDING_ALL] = { |
||
1135 | [1] = { ["key"] ="owner", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1136 | }, |
||
1137 | [GUILDADS_MSG_SENDING_ALL_END] = { -- currenttime, profiletime |
||
1138 | [1] = { ["key"] ="owner", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1139 | [2] = { ["key"] ="currenttime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime}, |
||
1140 | [3] = { ["key"] ="creationtime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime }, |
||
1141 | }, |
||
1142 | [GUILDADS_MSG_LASTSEEN] = { -- currenttime, profiletime |
||
1143 | [1] = { ["key"] ="owner", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1144 | [2] = { ["key"] ="currenttime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime}, |
||
1145 | [3] = { ["key"] ="creationtime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime }, |
||
1146 | }, |
||
1147 | [GUILDADS_MSG_LASTSEEN_END] = {}, |
||
1148 | [GUILDADS_MSG_META] = { -- currenttime, version, starttime, id |
||
1149 | [1] = { ["key"] ="currenttime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime}, |
||
1150 | [2] = { ["key"] ="text", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1151 | [3] = { ["key"] ="creationtime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime }, |
||
1152 | [4] = { ["key"] ="id", ["fout"]=SerializeId, ["fin"]=UnserializeId }, |
||
1153 | }, |
||
1154 | [GUILDADS_MSG_CHATFLAG] = { -- flag, text |
||
1155 | [1] = { ["key"] ="flag", ["fout"]=SerializeString, ["fin"]=UnserializeString}, |
||
1156 | [2] = { ["key"] ="text", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1157 | }, |
||
1158 | }; |
||
1159 | |||
1160 | |||
1161 | local SerializeAd = { |
||
1162 | [GUILDADS_MSG_TYPE_ANNONCE] = { -- accountId, class, race, level, guild |
||
1163 | [1] = { ["key"] ="accountId", ["fout"]=SerializeObj, ["fin"]=UnserializeObj }, |
||
1164 | [2] = { ["key"] ="class", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1165 | [3] = { ["key"] ="race", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1166 | [4] = { ["key"] ="level", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1167 | [5] = { ["key"] ="guild", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1168 | }, |
||
1169 | [GUILDADS_MSG_TYPE_INVENTORY] = { -- itemRef, itemName, itemColor, count, texture |
||
1170 | [1] = { ["key"] ="itemColor", ["fout"]=SerializeColor, ["fin"]=UnserializeColor }, |
||
1171 | [2] = { ["key"] ="itemRef", ["fout"]=SerializeItemRef, ["fin"]=UnserializeItemRef }, |
||
1172 | [3] = { ["key"] ="itemName", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1173 | [4] = { ["key"] ="count", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1174 | [5] = { ["key"] ="texture", ["fout"]=SerializeTexture, ["fin"]=UnserializeTexture }, |
||
1175 | }, |
||
1176 | [GUILDADS_MSG_TYPE_SKILL] = { -- skillRank , skillMaxRank |
||
1177 | [1] = { ["key"] ="skillRank", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1178 | [2] = { ["key"] ="skillMaxRank", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1179 | }, |
||
1180 | [GUILDADS_MSG_TYPE_REQUEST] = { -- text, itemRef, itemName, itemColor, count, texture |
||
1181 | [1] = { ["key"] ="text", ["fout"]=SerializeObj, ["fin"]=UnserializeObj }, |
||
1182 | [2] = { ["key"] ="itemColor", ["fout"]=SerializeColor, ["fin"]=UnserializeColor }, |
||
1183 | [3] = { ["key"] ="itemRef", ["fout"]=SerializeItemRef, ["fin"]=UnserializeItemRef }, |
||
1184 | [4] = { ["key"] ="itemName", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1185 | [5] = { ["key"] ="count", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1186 | [6] = { ["key"] ="texture", ["fout"]=SerializeTexture, ["fin"]=UnserializeTexture }, |
||
1187 | }, |
||
1188 | [GUILDADS_MSG_TYPE_AVAILABLE] ={ -- text, itemRef, itemName, itemColor, count, texture |
||
1189 | [1] = { ["key"] ="text", ["fout"]=SerializeObj, ["fin"]=UnserializeObj }, |
||
1190 | [2] = { ["key"] ="itemColor", ["fout"]=SerializeColor, ["fin"]=UnserializeColor }, |
||
1191 | [3] = { ["key"] ="itemRef", ["fout"]=SerializeItemRef, ["fin"]=UnserializeItemRef }, |
||
1192 | [4] = { ["key"] ="itemName", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1193 | [5] = { ["key"] ="count", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1194 | [6] = { ["key"] ="texture", ["fout"]=SerializeTexture, ["fin"]=UnserializeTexture }, |
||
1195 | }, |
||
1196 | [GUILDADS_MSG_TYPE_EVENT] = { -- text |
||
1197 | [1] = { ["key"] ="text", ["fout"]=SerializeObj, ["fin"]=UnserializeObj }, |
||
1198 | [2] = { ["key"] ="eventtime", ["fout"]=SerializeTime, ["fin"]=UnserializeTime }, |
||
1199 | [3] = { ["key"] ="note", ["fout"]=SerializeString, ["fin"]=UnserializeString }, |
||
1200 | [4] = { ["key"] ="count", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1201 | [5] = { ["key"] ="minlevel", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1202 | [6] = { ["key"] ="maxlevel", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1203 | }, |
||
1204 | [GUILDADS_MSG_TYPE_EVENT_SUBSCRIPTION] = { |
||
1205 | [1] = { ["key"] ="eventid", ["fout"]=SerializeInteger, ["fin"]=UnserializeInteger }, |
||
1206 | } |
||
1207 | }; |
||
1208 | |||
1209 | |||
1210 | function SerializeTable(spec, obj) |
||
1211 | local tmp = ""; |
||
1212 | local index = table.getn(spec); |
||
1213 | while (index > 0) do |
||
1214 | local info = spec[index]; |
||
1215 | local result = info.fout(obj[info.key]); |
||
1216 | |||
1217 | if (tmp ~= "") or (result ~= "") then |
||
1218 | tmp = result..">"..tmp; |
||
1219 | end; |
||
1220 | |||
1221 | index = index -1; |
||
1222 | end |
||
1223 | return tmp; |
||
1224 | end |
||
1225 | |||
1226 | function GuildAds_SerializeCommand(obj) |
||
1227 | local result = SerializeTable(SerializeMeta, obj) |
||
1228 | if SerializeCommand[obj.command] then |
||
1229 | result = result .. SerializeTable(SerializeCommand[obj.command], obj); |
||
1230 | if obj.command == GUILDADS_MSG_ADD then |
||
1231 | result = result .. SerializeTable(SerializeAd[obj.adtype], obj); |
||
1232 | end |
||
1233 | end |
||
1234 | return result; |
||
1235 | end |
||
1236 | |||
1237 | function GuildAds_UnserializeCommand(text) |
||
1238 | local obj= { }; |
||
1239 | |||
1240 | local spec=SerializeMeta; |
||
1241 | local index=1; |
||
1242 | local size= table.getn(spec); |
||
1243 | local step=1; |
||
1244 | |||
1245 | for str in string.gfind(text, "([^\>]*)>") do |
||
1246 | info = spec[index]; |
||
1247 | obj[info.key] = info.fin(str); |
||
1248 | |||
1249 | index = index + 1; |
||
1250 | if index>size then |
||
1251 | |||
1252 | step=step+1; |
||
1253 | |||
1254 | if step==2 then |
||
1255 | if obj.command and SerializeCommand[obj.command] then |
||
1256 | spec=SerializeCommand[obj.command]; |
||
1257 | else |
||
1258 | spec=nil; |
||
1259 | end |
||
1260 | elseif step==3 then |
||
1261 | if obj.adtype and SerializeAd[obj.adtype] then |
||
1262 | spec=SerializeAd[obj.adtype]; |
||
1263 | else |
||
1264 | spec=nil; |
||
1265 | end |
||
1266 | else |
||
1267 | return obj; |
||
1268 | end |
||
1269 | |||
1270 | if spec then |
||
1271 | index=1; |
||
1272 | size=table.getn(spec); |
||
1273 | else |
||
1274 | return obj |
||
1275 | end |
||
1276 | end |
||
1277 | end |
||
1278 | |||
1279 | return obj; |
||
1280 | end |
||
1281 | |||
1282 | function GuildAds_Serialize(obj) |
||
1283 | obj["currenttime"] = GAS_currentTime(); |
||
1284 | return GUILDADS_MSG_PREFIX..GuildAds_SerializeCommand(obj); |
||
1285 | end |
||
1286 | |||
1287 | function GuildAds_Unserialize(str) |
||
1288 | local iStart, iEnd, msg, params; |
||
1289 | iStart, iEnd, params = string.find(str, GUILDADS_MSG_PREFIX.."(.*)"); |
||
1290 | if (iStart) then |
||
1291 | return GuildAds_UnserializeCommand(params); |
||
1292 | else |
||
1293 | iStart, iEnd, params = string.find(str, GUILDADS_MSG_PREFIX_CHATCOMM.."(.*)"); |
||
1294 | if (iStart) then |
||
1295 | return GuildAds_UnserializeCommand(params); |
||
1296 | else |
||
1297 | return nil |
||
1298 | end |
||
1299 | end |
||
1300 | end |
||
1301 | |||
1302 | function GuildAds_FilterText(text) |
||
1303 | return string.sub(text, 1, string.len(GUILDADS_MSG_PREFIX_NOVERSION)) == GUILDADS_MSG_PREFIX_NOVERSION |
||
1304 | or string.sub(text, 1, string.len(GUILDADS_MSG_PREFIX_TOKEN)) == GUILDADS_MSG_PREFIX_TOKEN; |
||
1305 | end |
||
1306 | |||
1307 | |||
1308 | -------------------------------------------------------------------------------- |
||
1309 | -- |
||
1310 | -- Register/Unregister new command |
||
1311 | -- |
||
1312 | --------------------------------------------------------------------------------- |
||
1313 | function GAC_RegisterCommand(command, serializeInfo, onMessage) |
||
1314 | if SerializeCommand[command] then |
||
1315 | return false, "command("..command..") already registered"; |
||
1316 | else |
||
1317 | if type(onMessage) == "function" then |
||
1318 | if type(serializeInfo) == "table" then |
||
1319 | for _, spec in serializeInfo do |
||
1320 | if type(spec.key)~="string" or type(spec.fin)~="function" or type(spec.fout)~="function" then |
||
1321 | if type(spec.key)=="string" then |
||
1322 | return false, "serializeInfo["..spec.key.."]="..type(spec.fin)..","..type(spec.fout); |
||
1323 | else |
||
1324 | return false, "serializeInfo[??]="..type(spec.fin)..","..type(spec.fout); |
||
1325 | end |
||
1326 | end |
||
1327 | end |
||
1328 | SerializeCommand[command] = serializeInfo; |
||
1329 | OnMessageCommand[command] = onMessage; |
||
1330 | return true; |
||
1331 | else |
||
1332 | return false, "type(serializeInfo)="..type(serializeInfo); |
||
1333 | end |
||
1334 | else |
||
1335 | return false, "type(onMessage)="..type(onMessage); |
||
1336 | end |
||
1337 | end |
||
1338 | end |
||
1339 | |||
1340 | function GAC_UnregisterCommand(command) |
||
1341 | SerializeCommand[command] = nil; |
||
1342 | OnMessageCommand[command] = nil; |
||
1343 | end |
||
1344 | |||
1345 | function GAC_IsRegisteredCommand(command) |
||
1346 | if SerializeCommand[command] then |
||
1347 | return true, SerializeCommand[command], OnMessageCommand[command]; |
||
1348 | else |
||
1349 | return false; |
||
1350 | end |
||
1351 | end |
||
1352 | |||
1353 | -------------------------------------------------------------------------------- |
||
1354 | -- |
||
1355 | -- Register/Unregister new ad type |
||
1356 | -- |
||
1357 | --------------------------------------------------------------------------------- |
||
1358 | function GAC_RegisterAdtype(adtype, serializeInfo, onMessage) |
||
1359 | if SerializeAd[adtype] then |
||
1360 | return false, "adtype("..command..") already registered"; |
||
1361 | else |
||
1362 | if type(onMessage) == "function" then |
||
1363 | if type(serializeInfo) == "table" then |
||
1364 | for _, spec in serializeInfo do |
||
1365 | if type(spec.key)~="string" or type(spec.fin)~="function" or type(spec.fout)~="function" then |
||
1366 | return false, "serializeInfo["..spec.key.."]="..type(spec.fin)..","..type(spec.fout); |
||
1367 | end |
||
1368 | end |
||
1369 | SerializeAd[adtype] = serializeInfo; |
||
1370 | OnMessageAd[adtype] = onMessage; |
||
1371 | return true; |
||
1372 | else |
||
1373 | return false, "type(serializeInfo)="..type(serializeInfo); |
||
1374 | end |
||
1375 | else |
||
1376 | return false, "type(onMessage)="..type(onMessage); |
||
1377 | end |
||
1378 | end |
||
1379 | end |
||
1380 | |||
1381 | function GAC_UnregisterAdtype(adtype) |
||
1382 | SerializeAd[adtype] = nil; |
||
1383 | OnMessageAd[adtype] = nil; |
||
1384 | end |
||
1385 | |||
1386 | function GAC_IsRegisteredAdtype(adtype) |
||
1387 | if SerializeCommand[adtype] then |
||
1388 | return true, SerializeAd[adtype], OnMessageAd[adtype]; |
||
1389 | else |
||
1390 | return false; |
||
1391 | end |
||
1392 | end |
||
1393 | |||
1394 | -- export |
||
1395 | GAC_SerializeObj = SerializeObj; |
||
1396 | GAC_UnserializeObj = UnserializeObj; |
||
1397 | |||
1398 | GAC_SerializeString = SerializeString; |
||
1399 | GAC_UnserializeString = UnserializeString; |
||
1400 | |||
1401 | GAC_SerializeInteger = SerializeInteger; |
||
1402 | GAC_UnserializeInteger = UnserializeInteger; |
||
1403 | |||
1404 | GAC_SerializeColor = SerializeColor; |
||
1405 | GAC_UnserializeColor = UnserializeColor; |