annotate Lists.lua @ 9:daed0597deba

Pretty printing for lists A decision on how to index them up Bug about reverse list maintenance fixed. Next step noted.
author John@Doomsday
date Wed, 07 Mar 2012 14:56:25 -0500
parents b05fcb225c4a
children 433d31ea992a
rev   line source
John@0 1 -- lists consist of three things
John@0 2 -- 1) a base state - agreed on by one or more list holders
John@0 3 -- 2) change sets - incremental list changes (can be rolled forwards or
John@0 4 -- backwards)
John@0 5 -- 3) working state - not saved because it can be so easily calculated
John@0 6 --
John@0 7 -- A separate user list is held - lists index into this
John@0 8
John@0 9
John@0 10 -- TODO: rename player
John@5 11 -- TODO: list trimming
John@0 12
John@4 13 -- notes on list storage:
John@7 14 -- Using names as keys as I do now is atrocious.
John@4 15 -- It prevents insertions (twss) to the middle of the list because then it acts
John@4 16 -- as a side effect onto all the others. ie ABCD -> AXBCD would be phrased as
John@4 17 -- "insert X and shift down B,C,D" which sucks. BCD haven't really been affected
John@4 18 -- (yet) because their relative positions to the others are still intact - ie
John@4 19 -- they are still below A right where they belong. But really X hasn't done
John@4 20 -- anything to affect their relative standing.
John@4 21 --
John@4 22 -- Ok so we can't use names.
John@4 23 --
John@4 24 -- We can't use monotonic integers either because it suffers the same problem.
John@4 25 -- Also consider, randoming in someone to a list of ABCD. Say they roll spot 2.
John@4 26 -- What if someone else runs a separate raid and also randoms someone into slot
John@4 27 -- 2? How do you handle that conflict? Difficult. Also, consider this:
John@4 28 -- List of ABCD on night 1.
John@4 29 -- Admin 1 on night 2 rolls in 30 new people. ABCD's indexes are shuffled to be
John@4 30 -- between 1-35.
John@4 31 -- Admin 2 on night 3 rolls in 5 new ones and people ABCD and PQRST now all have
John@4 32 -- indexes between 1-9.
John@4 33 -- When these two are resolved against one another, do the 1-9 peopole end up on
John@4 34 -- top of the list compared to those other 30?
John@4 35 --
John@4 36 -- Solution:
John@4 37 -- Need a huge random space with purposely left gaps to leave plenty of room for
John@4 38 -- conflicts.
John@4 39 -- So if ABCD had randomed on a space of say, 10,000 and then were sorted into
John@4 40 -- order, then the next 30 could roll into that same space and have a proper
John@4 41 -- ordering. Then the next 5, etc.
John@4 42 --
John@4 43 -- Handling conflicts:
John@4 44 --
John@9 45 -- Executive decision: random on a range of [0,1], ie math.random
John@9 46 -- then on an add-to-end event just do last + .1
John@9 47 -- disallow random after any add-to-end event occurs
John@9 48 -- because the list either elongates beyond 1 OR becomes
John@9 49 -- ridiculously bottom heavy, thus meaning that randoms
John@9 50 -- don't get an even distibution from then on (in fact
John@9 51 -- they'll end up getting top favor)
John@9 52 -- * if a stream contains a random-add after an add-to-end
John@9 53 -- it is declared invalid. tough tits. it's just not a fair
John@9 54 -- distribution at that point.
John@0 55
John@8 56 -- todo: list-of-lists must not use int indices. those will lead to peril.
John@0 57 bsk.lists = {}
John@8 58 bsk.persons = {}
John@0 59
John@8 60 local raidList = {}
John@8 61 local reserveList = {}
John@8 62 local activeListKey = 1 -- temporary
John@8 63 local personsReverse = {}
John@0 64
John@0 65 local tinsert = table.insert
John@0 66 local sformat = string.format
John@0 67 local getn = table.getn
John@0 68
John@0 69 function bsk:tcopy(to, from)
John@0 70 for k,v in pairs(from) do
John@0 71 if(type(v)=="table") then
John@0 72 to[k] = {}
John@0 73 bsk:tcopy(to[k], v);
John@0 74 else
John@0 75 to[k] = v;
John@0 76 end
John@0 77 end
John@0 78 end
John@0 79 local shallowCopy = function(t)
John@0 80 local u = { }
John@0 81 for k, v in pairs(t) do u[k] = v end
John@0 82 return setmetatable(u, getmetatable(t))
John@0 83 end
John@0 84
John@1 85 -- Debugging {{{
John@9 86 function bsk:PrettyPrintList(listIndex)
John@9 87 local list = bsk.lists[listIndex]
John@9 88 bsk:Print("List: " .. list.name .. " (" .. list.time .. ")")
John@9 89 for i = 1,#list do
John@9 90 bsk:Print(" " .. i .. " - " .. bsk.persons[list[i]].main)
John@9 91 end
John@9 92 end
John@9 93 function bsk:PrettyPrintLists()
John@9 94 for i,_ in pairs(bsk.lists) do
John@9 95 bsk:PrettyPrintList(i)
John@9 96 end
John@9 97 end
John@1 98 function bsk:PrintLists()
John@1 99 bsk:PrintTable(bsk.lists)
John@1 100 end
John@1 101 function bsk:PrintChanges()
John@1 102 bsk:PrintTable(bsk.db.profile.changes)
John@1 103 end
John@8 104 function bsk:PrintPersons()
John@8 105 bsk:PrintTable(bsk.persons)
John@1 106 end
John@0 107 function bsk:PrintTable(table, depth)
John@0 108 depth = depth or ""
John@0 109 if not table then return end
John@0 110 for i,v in pairs(table) do
John@0 111 if( type(v) == "string" ) then
John@0 112 self:Print(depth .. i .. " - " .. v)
John@0 113 elseif( type(v) == "number" ) then
John@0 114 self:Print(depth .. i .. " - " .. tostring(v))
John@0 115 elseif( type(v) == "table" ) then
John@0 116 self:Print(depth .. i .." - ")
John@0 117 self:PrintTable(v,depth.." ")
John@0 118 elseif( type(v) == "boolean" ) then
John@0 119 self:Print(depth .. i .. " - " .. tostring(v))
John@0 120 else
John@0 121 self:Print(depth .. i .. " - not sure how to print type: " .. type(v) )
John@0 122 end
John@0 123 end
John@0 124 end
John@0 125
John@0 126 --}}}
John@0 127
John@9 128 function bsk:UpdatePersonsReverse()
John@9 129 for i,v in pairs(bsk.persons) do
John@9 130 if i ~= "time" then
John@9 131 personsReverse[v.main] = i
John@9 132 end
John@9 133 end
John@9 134 end
John@9 135
John@5 136 function bsk:CreateWorkingStateFromChanges(changes)
John@8 137 local personsBase = self.db.profile.persons
John@0 138 local listBase = self.db.profile.listBase
John@0 139
John@0 140 -- copy the base to the working state
John@0 141 wipe(bsk.lists)
John@8 142 wipe(bsk.persons)
John@8 143 wipe(personsReverse)
John@8 144
John@0 145 bsk:tcopy(bsk.lists,listBase)
John@8 146 bsk:tcopy(bsk.persons,personsBase)
John@0 147
John@0 148 -- now just go through the changes list applying each
John@5 149 for i,v in ipairs(changes) do
John@0 150 bsk:ProcessChange(v)
John@0 151 end
John@9 152
John@9 153 -- update the persons reverse list
John@9 154 bsk:UpdatePersonsReverse()
John@0 155 end
John@0 156
John@0 157 function bsk:CreateChange(change)
John@0 158 -- sanity
John@0 159 assert(change)
John@0 160 assert(change.action)
John@0 161 assert(change.arg)
John@0 162
John@0 163 bsk:StartChange(change)
John@0 164 bsk:CommitChange(change)
John@0 165 end
John@0 166
John@0 167 function bsk:StartChange(change)
John@0 168 local changes = self.db.profile.changes
John@0 169 change.time = time()
John@0 170 local n = getn(changes)
John@0 171 if n > 0 then
John@0 172 if changes[n].time >= change.time then
John@0 173 change.time = changes[n].time + 1
John@0 174 end
John@0 175 end
John@0 176 end
John@0 177
John@0 178 function bsk:CommitChange(change)
John@0 179 local changes = self.db.profile.changes
John@0 180 tinsert(changes,change)
John@0 181 -- TODO: broadcast change
John@0 182 end
John@0 183
John@0 184
John@0 185 -- timestamp logic:
John@0 186 -- use time() for comparisons - local clients use date() to make it pretty. only
John@0 187 -- dowisde - we can't have a server timestamp. Which kind of sucks, but it turns
John@0 188 -- out you can change timezones when you enter an instance server, so you really
John@0 189 -- never know what time it is.
John@0 190 -- There's unfortunately no hard-and-proven method for determining the true time
John@0 191 -- difference between local time and server time. You can't just query the two
John@0 192 -- and compare them because your server timezone can change (!) if you go into
John@0 193 -- an instance server with a different timezone. This is apparently a big
John@0 194 -- problem on Oceanic realms.
John@0 195 --
John@0 196 -- Timestamp handling (brainstorming how to deal with drift):
John@0 197 -- (not an issue) if someone sends you time in the future, update your offset so you won't
John@0 198 -- send out events in the "past" to that person
John@0 199 -- (not an issue - using local UTC now) on change-zone-event: check if you've changed timezones - might need update
John@0 200 -- each time you add a change, check the tail of the change list; if this is
John@0 201 -- less than that, you have a problem. Print a message. if this is equal, then
John@0 202 -- that's ok, just bump it by 1 second. This could happen in the case of, say,
John@0 203 -- spam-clicking the undo button or adding names to the list. The recipients
John@0 204 -- should be ok with this since they'll follow the same algorithm. The only
John@0 205 -- real chance for a problem is if two people click within the 1 second window?
John@0 206 -- if someone sends you a past event,
John@0 207 -- it's ok if it's newer than anything in the changes list
John@0 208 -- otherwise ... causality has been violated.
John@0 209 -- Whenever an admin signon event happens, have the admins each perform a
John@0 210 -- timestamp check. Issue warnings for anyone with a clock that's more than
John@0 211 -- X seconds out of sync with the others. Seriously, why isn't NTP a standard
John@0 212 -- setting on all operating systems ...
John@0 213
John@0 214 function bsk:ProcessChange(change)
John@8 215 if change.action == "AddPerson" then
John@8 216 bsk:DoAddPerson(change)
John@0 217 elseif change.action == "CreateList" then
John@0 218 bsk:DoCreateList(change)
John@8 219 elseif change.action == "AddPersonToList" then
John@8 220 bsk:DoAddPersonToList(change)
John@8 221 elseif change.action == "SuicidePerson" then
John@8 222 bsk:DoSuicidePerson(change)
John@0 223 else
John@0 224 bsk:Print("Unknown message encountered")
John@0 225 bsk:PrintTable(change)
John@0 226 assert(false)
John@0 227 end
John@0 228 end
John@0 229
John@1 230 -- Action and DoAction defs {{{
John@0 231 --
John@0 232 -- The actual actions for changes start here
John@0 233 --
John@0 234 -- Each action occurs as a pair of functions. The bsk:Action() function is from
John@0 235 -- a list admin's point of view. Each will check for admin status, then create a
John@0 236 -- change bundle, call the handler for that change (ie the DoAction func), and
John@0 237 -- then record/transmist the bundle. These are simple and repetitive functions.
John@0 238 --
John@0 239 -- The bsk:DoAction() function is tasked with executing the bundle and is what
John@0 240 -- non-admins and admins alike will call to transform their working state via a
John@0 241 -- change packet. Each Do() function will accept *only* a change packet, and
John@0 242 -- it's assumed that the change has been vetted elsewhere. These are very blunt
John@0 243 -- routines.
John@0 244 --
John@0 245 -- Note that "undo" has no special voodoo to it. It's basically a change that
John@0 246 -- reverses the prior change on the stack.
John@0 247
John@8 248 -- persons list
John@8 249 function bsk:DoAddPerson(change)
John@0 250 assert(change)
John@8 251 assert(change.arg.id)
John@0 252 local arg = change.arg
John@0 253 -- require admin
John@8 254 local persons = bsk.persons
John@0 255 local name = arg.name
John@8 256 local id = arg.id
John@8 257 assert(persons[id]==nil)
John@8 258 persons[id] = {main=name}
John@8 259 persons.time=change.time
John@8 260 personsReverse[name] = id
John@0 261 return true
John@0 262 end
John@0 263
John@8 264 function bsk:AddPerson(name)
John@8 265 local persons = bsk.persons
John@0 266 local guid = UnitGUID(name)
John@0 267 -- TODO: check guid to be sure it's a player
John@0 268 if not guid then
John@0 269 self:Print(sformat("Could not add player %s - they must be in range or group",name))
John@0 270 return
John@0 271 end
John@8 272 local id = string.sub(guid,6) -- skip at least 0x0580 ...
John@8 273 id = id:gsub("^0*(.*)","%1") -- nom all leading zeroes remaining
John@8 274
John@8 275 if persons[id] and persons[id] ~= name then
John@8 276 self:Print(sformat("Namechange detected for %s - new is %s, please rename the existing entry", persons[id], name))
John@0 277 return
John@0 278 end
John@8 279 if persons[id] ~= nil then
John@8 280 self:Print(sformat("%s is already in the persons list; disregarding", name))
John@0 281 return
John@0 282 end
John@8 283 local change = {action="AddPerson",arg={name=name,id=id}}
John@8 284 if bsk:DoAddPerson(change) then
John@0 285 bsk:CreateChange(change)
John@0 286 end
John@0 287 end
John@0 288
John@0 289 function bsk:DoCreateList(change)
John@0 290 -- TODO: this segment will probably be useful as bsk:SearchForListByName
John@0 291 local lists = bsk.lists
John@0 292 for i,v in pairs(lists) do
John@0 293 if v.name == change.arg.name then
John@0 294 self:Print(sformat("List %s already exists",v.name))
John@0 295 return false
John@0 296 end
John@0 297 end
John@0 298 tinsert(lists,{name=change.arg.name,time=change.time})
John@0 299 return true
John@0 300 end
John@0 301
John@0 302 function bsk:CreateList(name)
John@0 303 -- require admin
John@0 304 local change={action="CreateList",arg={name=name}}
John@0 305 bsk:StartChange(change)
John@0 306 self:Print("Creating ... " .. name)
John@0 307 if bsk:DoCreateList(change) then
John@0 308 bsk:CommitChange(change)
John@0 309 end
John@0 310 end
John@0 311
John@9 312 -- TODO: break into AddPersonToListEnd and AddPersonToListRandom
John@8 313 function bsk:DoAddPersonToList(change)
John@0 314 local listIndex = change.arg.listIndex
John@0 315 local slist = change.arg.slist
John@0 316 local list = bsk.lists[listIndex]
John@0 317
John@0 318 if #slist == 1 then -- end of list insertion - just one person
John@0 319 tinsert(list,slist[1])
John@0 320 list.time = change.time
John@0 321 else
John@0 322 self:Print("Adding to middle of list is not yet supported")
John@0 323 return false
John@0 324 end
John@0 325 return true
John@0 326 end
John@0 327
John@8 328 function bsk:AddPersonToList(name,list)
John@0 329 -- require admin
John@0 330 local listIndex = bsk:GetListIndex(list)
John@8 331 local id = personsReverse[name]
John@9 332 bsk:Print(sformat("Adding %s (%s) to list %s (%s)", name, id, list, listIndex))
John@8 333 local slist = {id} -- TODO: support adding to elsewhere besides the end
John@8 334 local change = {action="AddPersonToList",arg={id=id,listIndex=listIndex,slist=slist}}
John@0 335 bsk:StartChange(change)
John@8 336 if bsk:DoAddPersonToList(change) then
John@0 337 bsk:CommitChange(change)
John@0 338 end
John@0 339 end
John@0 340
John@8 341 function bsk:DoRemovePerson(change)
John@0 342
John@0 343 -- return true
John@0 344 end
John@0 345
John@8 346 function bsk:RemovePerson(name)
John@8 347 -- from both persons and lists
John@0 348 end
John@0 349
John@8 350 function bsk:DoSuicidePerson(change)
John@0 351 local listIndex = change.arg.listIndex
John@0 352 local list = bsk.lists[listIndex]
John@0 353 local slist = shallowCopy(change.arg.list)
John@0 354 -- the goal here is to rotate the suicide list by 1
John@0 355 -- then we can just mash it on top of the intersection between the original
John@0 356 -- list and the working copy
John@0 357 local stemp = shallowCopy(change.arg.list)
John@0 358 local temp = table.remove(stemp,1) -- pop
John@0 359 tinsert(stemp,temp) -- push_back
John@0 360 --bsk:Print(sformat("Before suicide of %s on list %s",slist[1],list.name))
John@0 361 --bsk:PrintTable(list)
John@0 362 for i = 1, #list do
John@0 363 if list[i] == slist[1] then
John@0 364 table.remove(slist,1)
John@0 365 list[i] = stemp[1]
John@0 366 table.remove(stemp,1)
John@0 367 end
John@0 368 end
John@0 369 list.time=change.time
John@0 370 --bsk:Print("After")
John@0 371 --bsk:PrintTable(list)
John@0 372 return true
John@0 373 end
John@0 374
John@8 375 function bsk:SuicidePerson(name,list)
John@0 376 -- require admin
John@0 377 bsk:PopulateRaidList()
John@0 378 local listIndex = bsk:GetListIndex(list)
John@1 379 local slist=bsk:GetSuicideList(name,bsk.lists[listIndex])
John@8 380 local change = {action="SuicidePerson",arg={names=names,list=slist,listIndex=listIndex}}
John@0 381 bsk:StartChange(change)
John@8 382 if bsk:DoSuicidePerson(change) then
John@0 383 bsk:CommitChange(change)
John@0 384 end
John@0 385 end
John@5 386
John@5 387 function bsk:TrimLists(time)
John@5 388 if not bsk:CheckListCausality() then
John@5 389 self:Print("Unable to trim lists due to violated causality")
John@5 390
John@5 391 return false
John@5 392 end
John@5 393
John@5 394 if type(time) ~= "number" then
John@5 395 time = tonumber(time)
John@5 396 end
John@5 397
John@5 398 -- bisect the changes list by "time"
John@5 399 local before = {}
John@5 400 for i,v in ipairs(self.db.profile.changes) do
John@5 401 if v.time <= time then
John@5 402 tinsert(before,v)
John@5 403 else
John@5 404 break
John@5 405 end
John@5 406 end
John@5 407
John@5 408 -- apply first half
John@5 409 bsk:CreateWorkingStateFromChanges(before)
John@5 410
John@5 411 -- save this state permanently; trim the changes permanently
John@8 412 bsk:tcopy(bsk.db.profile.persons,bsk.persons)
John@5 413 bsk:tcopy(bsk.db.profile.listBase,bsk.lists)
John@8 414 while bsk.db.profile.changes ~= nil and bsk.db.profile.changes[1] ~= nil and bsk.db.profile.changes[1].time <= time do
John@5 415 table.remove(bsk.db.profile.changes,1)
John@5 416 end
John@5 417
John@5 418 -- using the trimmed list and the new bases, recreate the working state
John@5 419 bsk:CreateWorkingStateFromChanges(bsk.db.profile.changes)
John@5 420 end
John@5 421
John@1 422 --}}}
John@1 423 -- Higher order actions (ie calls other Doers){{{
John@8 424 function bsk:AddMissingPersons()
John@1 425 bsk:PopulateRaidList()
John@1 426 local t = {}
John@8 427 for _,v in pairs(bsk.persons) do
John@8 428 t[v.main] = true
John@8 429 -- TODO: also add alts here
John@1 430 end
John@8 431 for i,_ in pairs(raidList) do
John@2 432 if t[i] == nil then
John@8 433 bsk:Print(sformat("Person %s is missing from the persons list - adding",i))
John@8 434 bsk:AddPerson(i)
John@1 435 end
John@1 436 end
John@1 437 -- TODO: batch into a single op - no need to spam 25 messages in a row
John@1 438 end
John@3 439 function bsk:PopulateListRandom(index)
John@3 440 -- difference (raid+reserve)^list, then random shuffle that, then add
John@3 441 bsk:PopulateRaidList()
John@3 442 local list = bsk.lists[index]
John@3 443
John@3 444 local t = {}
John@3 445 --for i = 1,#list do
John@8 446 -- if not (raidList(list[i]) or reserveList(list[i])) then
John@3 447 -- tinsert(t,)
John@3 448 -- end
John@3 449 --end
John@3 450 end
John@1 451 --}}}
John@1 452
John@1 453 -- "Soft" actions- ie things that cause nonpermanent state {{{
John@1 454
John@1 455 -- reserves
John@1 456 function bsk:AddReserve(name)
John@8 457 reserveList[name]=true
John@1 458 -- TODO: communicate to others. don't store this in any way.
John@1 459 end
John@1 460
John@1 461 function bsk:RemoveReserve(name)
John@8 462 reserveList[name]=false
John@1 463 -- TODO: communicate to others. don't store this in any way.
John@1 464 end
John@1 465
John@1 466
John@1 467 --function bsk:GetActiveList()
John@1 468 -- return bsk.lists[1] -- todo!
John@1 469 --end
John@1 470
John@1 471 --}}}
John@0 472
John@0 473 -- The following code is from Xinhuan (wowace forum member)
John@0 474 -- Pre-create the unitID strings we will use
John@0 475 local pID = {}
John@0 476 local rID = {}
John@0 477 for i = 1, 4 do
John@0 478 pID[i] = format("party%d", i)
John@0 479 end
John@0 480 for i = 1, 40 do
John@0 481 rID[i] = format("raid%d", i)
John@0 482 end
John@0 483 function bsk:PopulateRaidList()
John@0 484 local inParty = GetNumPartyMembers()
John@0 485 local inRaid = GetNumRaidMembers()
John@0 486
John@8 487 wipe(raidList)
John@0 488 if inRaid > 0 then
John@0 489 for i = 1, inRaid do
John@8 490 raidList[UnitName(rID[i])]=true
John@0 491 end
John@0 492 elseif inParty > 0 then
John@0 493 for i = 1, inParty do
John@8 494 raidList[UnitName(pID[i])]=true
John@0 495 end
John@0 496 -- Now add yourself as the last party member
John@8 497 raidList[UnitName("player")]=true
John@0 498 else
John@0 499 -- You're alone
John@8 500 raidList[UnitName("player")]=true
John@0 501 end
John@0 502 end
John@0 503
John@0 504 -- undo rules!
John@0 505 -- only the most recent event can be undone
John@0 506 -- ^^^ on a given list?
John@0 507 -- algorithm is easy, given "Suicide A B C"
John@0 508 -- just find A,B,C in the list and replace in order from the s message
John@0 509 -- while undo is allowed *per-list*, certain events in the stream will
John@0 510 -- prevent proper undo, such as add/delete player or add/delete list
John@0 511
John@0 512
John@1 513 function bsk:GetSuicideList(name,list)
John@1 514 --self:Print("Calculating changeset for "..name.." from list -")
John@1 515 --self:PrintTable(list)
John@1 516 local t = {}
John@1 517 local ret = {}
John@1 518 local pushing = false
John@1 519 for i = 1, #list do
John@1 520 if list[i] == name then
John@1 521 pushing = true
John@1 522 end
John@8 523 if pushing and (raidList[list[i]] or reserveList[list[i]]) then
John@1 524 tinsert(ret,list[i])
John@1 525 end
John@1 526 end
John@1 527 return ret
John@0 528 end
John@0 529
John@5 530 -- returns true if the events in the list are in time order
John@5 531 function bsk:CheckListCausality()
John@5 532 local t = nil
John@5 533 for i,v in ipairs(bsk.db.profile.changes) do
John@5 534 if t ~= nil then
John@5 535 if v.time <= t then
John@5 536 return false
John@5 537 end
John@5 538 end
John@5 539 t = v.time
John@5 540 end
John@5 541 return true
John@5 542 end
John@0 543
John@0 544 -- Support functions
John@0 545
John@0 546 function bsk:GetListIndex(name)
John@0 547 for i,v in pairs(bsk.lists) do
John@0 548 if v.name == name then
John@0 549 return i
John@0 550 end
John@0 551 end
John@0 552 assert(false)
John@0 553 end
John@1 554
John@3 555 local shuffleArray = function(array)
John@3 556 local arrayCount = #array
John@3 557 for i = arrayCount, 2, -1 do
John@3 558 local j = math.random(1, i)
John@3 559 array[i], array[j] = array[j], array[i]
John@3 560 end
John@3 561 return array
John@3 562 end
John@3 563