vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --[[ |
2 | I want to go a lot further with voting so this gets it's own file. |
||
3 | For now (1.5.beta.1) it's just the stuff needed for a SyncReset vote |
||
4 | Some things might seem like overkill, but as said, i want to go further with this :P |
||
5 | |||
6 | Note to self: |
||
7 | using meta tables like in SM_CVVote i could easly store "objects" and make them persistant |
||
8 | just make sure to add metatables again in VARIABLES_LOADED, could be interesting when making the |
||
9 | parsing engine its own mod to use for other mods. |
||
10 | |||
11 | --]] |
||
12 | |||
13 | SW_VTL = 0; |
||
14 | SW_VTB = 60; -- buffer in secs to avoid people spamming votes |
||
15 | |||
16 | SW_ActiveVotes = {}; |
||
17 | |||
18 | SW_CVote = |
||
19 | { |
||
20 | new = function (self, o) |
||
21 | o = o or {}; |
||
22 | setmetatable(o, self); |
||
23 | self.__index = self; |
||
24 | return o; |
||
25 | end, |
||
26 | |||
27 | newIVData = function (self, data, from) |
||
28 | o = {}; |
||
29 | setmetatable(o, self); |
||
30 | self.__index = self; |
||
31 | o.issuedBy = from; |
||
32 | o:InitFromData(data); |
||
33 | return o; |
||
34 | end, |
||
35 | InitFromData = function (self, data) |
||
36 | self.vID = data[1]; |
||
37 | self.totalRun = tonumber(data[2]); |
||
38 | self.sID = data[3]; |
||
39 | self.question = SW_SyncQueue:DecStr(data[4]); |
||
40 | self.answerCount = tonumber(data[5]); |
||
41 | self.extraData = {}; |
||
42 | self.timeRunning = 0; |
||
43 | self.onDoneCalled = false; |
||
44 | self.onHideGUICalled = false; |
||
45 | |||
46 | for i=6, table.getn(data) do |
||
47 | table.insert(self.extraData, data[i]); |
||
48 | end |
||
49 | self:initSpecial(); |
||
50 | end, |
||
51 | |||
52 | initSpecial = function (self) |
||
53 | local LIs = SW_SyncGetLIs(); |
||
54 | if self.sID == "SWRV" then |
||
55 | self.question = string.format(SW_STR_RV, self.issuedBy); |
||
56 | self.answerCount = 2; |
||
57 | self.answers = {YES,NO}; |
||
58 | |||
59 | local points =0; |
||
60 | local peers = LIs["SW_PC"]; |
||
61 | local tmp = 0; |
||
62 | for k,v in pairs(LIs) do |
||
63 | |||
64 | if SW_Friends[k] ~= nil and SW_Friends[k]["Rank"] ~= nil then |
||
65 | if SW_Friends[k]["Rank"] == 2 then |
||
66 | v = peers * 100; |
||
67 | elseif SW_Friends[k]["Rank"] == 1 then |
||
68 | v = peers * 10; |
||
69 | else |
||
70 | v=1; |
||
71 | end |
||
72 | LIs[k] = v; |
||
73 | points = points + v; |
||
74 | end |
||
75 | end |
||
76 | self.availablePoints = points; |
||
77 | self.pointLookup = LIs; |
||
78 | self.OnDone = SW_ResetVoteDone; |
||
79 | self.OnHideGUI = SW_ResetVoteHideGUI; |
||
80 | else |
||
81 | self.answers = {}; |
||
82 | self.availablePoints = LIs["SW_PC"]; |
||
83 | self.pointLookup = LIs; |
||
84 | end |
||
85 | self.results = {}; |
||
86 | self.hasAnswered = {}; |
||
87 | self.currentPoints = 0; |
||
88 | |||
89 | if self.sID == "SWRV" then |
||
90 | StaticPopupDialogs["SW_ResetSyncVote"]["text"] = self.question; |
||
91 | StaticPopupDialogs["SW_ResetSyncVote"]["vID"] = self.vID; |
||
92 | StaticPopup_Show("SW_ResetSyncVote"); |
||
93 | end |
||
94 | end, |
||
95 | addVote = function (self, i, from) |
||
96 | -- stop double answers |
||
97 | if self.hasAnswered[from] == nil then |
||
98 | self.hasAnswered[from] = true; |
||
99 | else |
||
100 | return; |
||
101 | end |
||
102 | local index = tonumber(i); |
||
103 | if self.results[index] == nil then |
||
104 | self.results[index] = 0; |
||
105 | end |
||
106 | |||
107 | if self.pointLookup[from] ~= nil then |
||
108 | self.results[index] = self.results[index] + self.pointLookup[from]; |
||
109 | self.currentPoints = self.currentPoints + self.pointLookup[from]; |
||
110 | end |
||
111 | end, |
||
112 | |||
113 | addAnswer = function (self, i, aStr) |
||
114 | local index = tonumber(i); |
||
115 | if index <= self.answerCount then |
||
116 | self.answers[index] = SW_SyncQueue:DecStr(aStr); |
||
117 | end |
||
118 | end, |
||
119 | --called to see if this vote can safely be deleted |
||
120 | expired = function (self) |
||
121 | -- when we delete it isn't to important so just give |
||
122 | -- any "stuff" enough time to finish |
||
123 | if self.timeRunning > 2* self.totalRun then |
||
124 | return true; |
||
125 | end |
||
126 | return false; |
||
127 | end, |
||
128 | |||
129 | updateTimePassed = function (self, sec) |
||
130 | self.timeRunning = self.timeRunning + sec; |
||
131 | |||
132 | |||
133 | if self.timeRunning > self.totalRun + 5 then -- add a little buffer for the last answers to arrive |
||
134 | if self.OnDone ~= nil and not self.onDoneCalled then |
||
135 | self.OnDone(self); |
||
136 | self.onDoneCalled = true; |
||
137 | end |
||
138 | elseif self.timeRunning > self.totalRun then |
||
139 | if self.OnHideGUI ~= nil and not self.onHideGUICalled then |
||
140 | self.OnHideGUI(self); |
||
141 | self.onHideGUICalled = true; |
||
142 | end |
||
143 | elseif self.sID == "SWRV" then |
||
144 | -- we arent interested in the "real" result, just if it is over 50% |
||
145 | if self.currentPoints * 2 > self.availablePoints then |
||
146 | if self.OnHideGUI ~= nil and not self.onHideGUICalled then |
||
147 | self.OnHideGUI(self); |
||
148 | self.onHideGUICalled = true; |
||
149 | end |
||
150 | if self.OnDone ~= nil and not self.onDoneCalled then |
||
151 | self.OnDone(self); |
||
152 | self.onDoneCalled = true; |
||
153 | end |
||
154 | end |
||
155 | end |
||
156 | end, |
||
157 | AnswerVal = function (self, questionNr) |
||
158 | if self.results[questionNr] == nil then |
||
159 | return 0; |
||
160 | else |
||
161 | return self.results[questionNr]; |
||
162 | end |
||
163 | end, |
||
164 | dumpData = function (self) |
||
165 | SW_DumpTable(self); |
||
166 | end, |
||
167 | |||
168 | }; |
||
169 | |||
170 | function SW_DumpVotes() |
||
171 | for k,v in pairs(SW_ActiveVotes) do |
||
172 | v:dumpData(); |
||
173 | end |
||
174 | end |
||
175 | function SW_UpdateVoteTimers(elapsed) |
||
176 | local expIDs = {}; |
||
177 | for k,v in pairs(SW_ActiveVotes) do |
||
178 | v:updateTimePassed(elapsed); |
||
179 | if v:expired() then |
||
180 | table.insert(expIDs, v.vID); |
||
181 | end |
||
182 | end |
||
183 | for i,v in ipairs(expIDs) do |
||
184 | SW_ActiveVotes[v] = nil; |
||
185 | end |
||
186 | end |
||
187 | function SW_SyncVoteAnswer(data, from) |
||
188 | local vID = data[1]; |
||
189 | |||
190 | if SW_ActiveVotes[vID] ~= nil then |
||
191 | SW_ActiveVotes[vID]:addVote(data[2], from); |
||
192 | else |
||
193 | SW_printStr("SW_SyncVoteAnswer vID not defined, can't add vote"); |
||
194 | end |
||
195 | end |
||
196 | |||
197 | function SW_SyncIssuedVote(data, from) |
||
198 | local vID = data[1]; |
||
199 | |||
200 | if SW_ActiveVotes[vID] == nil then |
||
201 | SW_ActiveVotes[vID] = SW_CVote:newIVData(data, from); |
||
202 | else |
||
203 | SW_printStr("SW_SyncIssuedVote vID in use?!?."); |
||
204 | end |
||
205 | end |
||
206 | function SW_SyncIssuedVoteAnswer(data, from) |
||
207 | local vID = data[1]; |
||
208 | |||
209 | if SW_ActiveVotes[vID] ~= nil then |
||
210 | SW_ActiveVotes[vID]:addAnswer(data[2], data[3]); |
||
211 | else |
||
212 | SW_printStr("SW_SyncIssuedVoteAnswer vID not defined, can't add answer"); |
||
213 | end |
||
214 | end |
||
215 | function SW_SendVote(question, runS, answers, data, specialID) |
||
216 | if (time() - SW_VTL) < SW_VTB then |
||
217 | DEFAULT_CHAT_FRAME:AddMessage(SW_STR_VOTE_WARN); |
||
218 | return; |
||
219 | end |
||
220 | SW_VTL = time(); |
||
221 | |||
222 | local vID = date("%H%M%S"..SW_SELF_STRING); |
||
223 | local ID = "N"; |
||
224 | if specialID ~= nil then |
||
225 | ID = specialID; |
||
226 | end |
||
227 | local seconds; |
||
228 | if runS == nil then |
||
229 | seconds = 30; |
||
230 | else |
||
231 | seconds = tonumber(runS); |
||
232 | if seconds < 15 then |
||
233 | seconds = 15; |
||
234 | end |
||
235 | end |
||
236 | local request = {}; |
||
237 | table.insert(request, vID); |
||
238 | table.insert(request, seconds); |
||
239 | table.insert(request, ID); |
||
240 | if question == nil or type(question) ~= "string" then |
||
241 | table.insert(request, "Q"); |
||
242 | else |
||
243 | table.insert(request, SW_SyncQueue:EncStr(question)); |
||
244 | end |
||
245 | if type(answers) == "table" then |
||
246 | table.insert(request, table.getn(answers)); |
||
247 | else |
||
248 | table.insert(request, 0); |
||
249 | end |
||
250 | |||
251 | if type(data) == "table" then |
||
252 | for i,v in ipairs(data) do |
||
253 | table.insert(request, SW_SyncQueue:EncStr(v)); |
||
254 | end |
||
255 | end |
||
256 | |||
257 | -- hmm 255 char limit, ok for now but dont forget later on |
||
258 | SW_SyncQueue:PushSWInfo("IV", request); |
||
259 | |||
260 | if type(answers) == "table" then |
||
261 | for i,v in ipairs(answers) do |
||
262 | -- hmm 255 char limit, ok for now but dont forget later on |
||
263 | SW_SyncQueue:PushSWInfo("IVA", {vID,i,SW_SyncQueue:EncStr(v),math.random(100)}); |
||
264 | end |
||
265 | end |
||
266 | end |
||
267 | |||
268 | function SW_ResetVote() |
||
269 | SW_RebuildFriendList(); |
||
270 | SW_SendVote(nil, nil, nil,nil,"SWRV"); |
||
271 | --SW_SendVote("My question~%+??",43,{'answer"1"', "answer~2"},{"data1",2},"N"); |
||
272 | end |
||
273 | function SW_ResetVoteHideGUI(vote) |
||
274 | StaticPopup_Hide("SW_ResetSyncVote"); |
||
275 | end |
||
276 | function SW_ResetVoteDone(vote) |
||
277 | StaticPopup_Hide("SW_ResetSyncVote"); |
||
278 | if vote:AnswerVal(1) > vote:AnswerVal(2) then |
||
279 | --SW_SyncReset(newSessID, issuedBy, selfForce) |
||
280 | SW_SyncReset(nil, nil, true); |
||
281 | DEFAULT_CHAT_FRAME:AddMessage(SW_STR_RV_PASSED); |
||
282 | else |
||
283 | DEFAULT_CHAT_FRAME:AddMessage(SW_STR_RV_FAILED); |
||
284 | end |
||
285 | |||
286 | end |
||
287 |