vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 GATHERER_TOKEN_SEPARATOR = ";";
2 GATHERER_ADDON_MESSAGE_PREFIX = "gatherer_p2p";
3  
4 function Gatherer_BroadcastGather(gather, gatherType, gatherC, gatherZ, gatherX, gatherY, iconIndex, gatherEventType)
5 assert(type(iconIndex) == 'number')
6 local message = Gatherer_EncodeGather(GetUnitName("player"), gather, gatherType, gatherC, gatherZ, gatherX, gatherY, iconIndex, gatherEventType);
7  
8 if Gatherer_Settings.debug then
9 local prettyNodeName = gather;
10 local prettyZoneName = GatherRegionData[gatherC][gatherZ].name;
11 Gatherer_ChatNotify(
12 "Broadcasting new " .. prettyNodeName .. " node found in " .. prettyZoneName .. ".",
13 Gatherer_ENotificationType.sending
14 );
15 Gatherer_ChatNotify(
16 "gatherType: " .. gatherType .. ", gatherEventType: " .. gatherEventType .. ".",
17 Gatherer_ENotificationType.sending
18 );
19 end
20  
21 Gatherer_SendRawMessage(message);
22 end
23  
24  
25 VERSION_REG_EXP = '(%d+)\.(%d+)\.(%d+)';
26  
27  
28 local function extractVersion(str)
29 -- type(Text) -> Tuple[int, int, int]
30 local major, minor, fix = unpack(Gatherer_split(str, '.'))
31 return {tonumber(major), tonumber(minor), tonumber(fix)}
32 end
33  
34  
35 local function validPrefix(prefix)
36 -- type: (Text) -> bool
37 -- Return true if prefix has correct format and acceptable version.
38 -- Message format is considered broken *every tenth* minor version.
39 -- E.g. 1.0.x and 1.9.x are compatible, 1.0.x and 1.10.x are not.
40 -- Thus when breaking message format don't forget to switch to the next ten
41 -- in the minor version.
42  
43 -- check overall prefix format
44 local prefixPos = strfind(
45 prefix, '^'..GATHERER_ADDON_MESSAGE_PREFIX..GATHERER_TOKEN_SEPARATOR..VERSION_REG_EXP..'$'
46 );
47 if (not prefixPos) then
48 return
49 end
50  
51 local versionPos = strfind(prefix, VERSION_REG_EXP)
52 local prefixVersionStr = strsub(prefix, versionPos)
53 -- check version components
54 local prefixVersion = extractVersion(prefixVersionStr);
55 local currentVersion = extractVersion(GATHERER_VERSION);
56 Gatherer_ChatNotify(
57 'Message version: '..table.concat(prefixVersion, ', ')..' vs '..table.concat(currentVersion, ', '),
58 Gatherer_ENotificationType.debug
59 )
60 if (
61 (prefixVersion[1] ~= currentVersion[1])
62 or (floor(prefixVersion[2]/10) ~= floor(currentVersion[2]/10))
63 ) then
64 return
65 end
66 return true
67 end
68  
69  
70 function Gatherer_AddonMessageEvent(prefix, message, type)
71 if (not Gatherer_Settings.p2p) or (not validPrefix(prefix)) then
72 return
73 end
74 Gatherer_ReceiveBroadcast(message);
75 end
76  
77 function Gatherer_EncodeGather(sender, gather, gatherType, gatherC, gatherZ, gatherX, gatherY, gatherIcon, gatherEventType)
78 return sender .. GATHERER_TOKEN_SEPARATOR ..
79 gather .. GATHERER_TOKEN_SEPARATOR ..
80 gatherType .. GATHERER_TOKEN_SEPARATOR ..
81 gatherC .. GATHERER_TOKEN_SEPARATOR ..
82 gatherZ .. GATHERER_TOKEN_SEPARATOR ..
83 gatherX .. GATHERER_TOKEN_SEPARATOR ..
84 gatherY .. GATHERER_TOKEN_SEPARATOR ..
85 gatherIcon .. GATHERER_TOKEN_SEPARATOR ..
86 gatherEventType .. GATHERER_TOKEN_SEPARATOR;
87 end
88  
89 function Gatherer_DecodeGather(message)
90 local function eatToken(str)
91 local sep = string.find(str, GATHERER_TOKEN_SEPARATOR);
92 local arg = string.sub(str, 1, sep-1);
93 local rest = string.sub(str, sep+1);
94 return arg, rest
95 end
96  
97 local sender, rest = eatToken(message);
98 local gather, rest = eatToken(rest);
99 local gatherType, rest = eatToken(rest);
100 local gatherC, rest = eatToken(rest);
101 local gatherZ, rest = eatToken(rest);
102 local gatherX, rest = eatToken(rest);
103 local gatherY, rest = eatToken(rest);
104 local iconIndex, rest = eatToken(rest);
105 local gatherEventType, rest = eatToken(rest);
106  
107 -- correct types
108 gatherType = tonumber(gatherType);
109 gatherC = tonumber(gatherC);
110 gatherZ = tonumber(gatherZ);
111 gatherX = tonumber(gatherX);
112 gatherY = tonumber(gatherY);
113 iconIndex = tonumber(iconIndex);
114 gatherEventType = tonumber(gatherEventType);
115  
116 return sender, gather, gatherType, gatherC, gatherZ, gatherX, gatherY, iconIndex, gatherEventType;
117 end
118  
119  
120 function Gatherer_ReceiveBroadcast(message)
121 local sender, gather, gatherType, gatherC, gatherZ, gatherX, gatherY, iconIndex, gatherEventType = Gatherer_DecodeGather(message);
122 assert(type(iconIndex) == 'number')
123 if sender ~= GetUnitName("player") then
124 if Gatherer_Settings.debug then
125 local prettyNodeName = gather;
126 local prettyZoneName = GatherRegionData[gatherC][gatherZ].name;
127 Gatherer_ChatNotify(
128 Gatherer_coloredText(
129 sender, {170, 115, 255}
130 ) .. " discovered a new " .. prettyNodeName .. " node in " .. prettyZoneName .. ".",
131 Gatherer_ENotificationType.receiving
132 );
133 Gatherer_ChatNotify(
134 'gatherType: '..gatherType..', iconIndex: '.. iconIndex ..', gatherEventType: '..gatherEventType,
135 Gatherer_ENotificationType.receiving
136 )
137 end
138 local newNodeFound = Gatherer_AddGatherToBase(gather, gatherType, gatherC, gatherZ, gatherX, gatherY, iconIndex, gatherEventType, false);
139 if Gatherer_Settings.debug then
140 if newNodeFound then
141 Gatherer_ChatNotify('It was a new node!', Gatherer_ENotificationType.info)
142 else
143 Gatherer_ChatNotify('It was a duplicate.', Gatherer_ENotificationType.warning)
144 end
145 end
146 end
147 end
148  
149 function Gatherer_SendRawMessage(message)
150 SendAddonMessage(
151 GATHERER_ADDON_MESSAGE_PREFIX..GATHERER_TOKEN_SEPARATOR..GATHERER_VERSION,
152 message, "GUILD"
153 );
154 end