/assignments.lua

https://github.com/Shadowed/PaladinBuffer · Lua · 193 lines · 139 code · 35 blank · 19 comment · 52 complexity · bd367f606b8c3c34696a408daca337c0 MD5 · raw file

  1. if( not PaladinBuffer ) then return end
  2. local Assign = PaladinBuffer:NewModule("Assign", "AceEvent-3.0", "AceComm-3.0")
  3. local blessings, priorities, currentSort, assignments, blacklist
  4. local singleToGreater = {["might"] = "gmight", ["kings"] = "gkings", ["wisdom"] = "gwisdom", ["sanct"] = "gsanct"}
  5. local pointsInfo = {}
  6. -- Create our tables for doing smart assignments
  7. function Assign:CreateTables()
  8. if( blessings ) then
  9. return
  10. end
  11. -- People we've already used
  12. blacklist = {}
  13. -- Blessings
  14. blessings = {}
  15. for spellToken, type in pairs(PaladinBuffer.blessingTypes) do
  16. if( type == "greater" ) then
  17. blessings[spellToken] = {}
  18. end
  19. end
  20. self.blessings = blessings
  21. -- Blessing priorities for people
  22. priorities = {
  23. ["ROGUE"] = {"gkings", "gmight", "gsanct"},
  24. ["WARRIOR"] = {"gkings", "gmight", "gsanct"},
  25. ["DEATHKNIGHT"] = {"gkings", "gmight", "gsanct"},
  26. ["PRIEST"] = {"gkings", "gwisdom", "gsanct"},
  27. ["WARLOCK"] = {"gkings", "gwisdom", "gsanct"},
  28. ["MAGE"] = {"gkings", "gwisdom", "gsanct"},
  29. ["HUNTER"] = {"gkings", "gmight", "gwisdom", "gsanct"},
  30. ["PALADIN"] = {"gkings", "gwisdom", "gmight", "gsanct"},
  31. ["DRUID"] = {"gkings", "gwisdom", "gmight", "gsanct"},
  32. ["SHAMAN"] = {"gkings", "gwisdom", "gmight", "gsanct"},
  33. }
  34. -- Set assignments for classes
  35. assignments = {}
  36. for classToken in pairs(priorities) do
  37. assignments[classToken] = {}
  38. end
  39. self.assignments = assignments
  40. end
  41. -- Sort the tables so the people with the highest rank of blessings come first
  42. local function sortOrder(a, b)
  43. if( not a ) then
  44. return false
  45. elseif( not b ) then
  46. return true
  47. end
  48. return pointsInfo[a] < pointsInfo[b]
  49. end
  50. function Assign:SetHighestBlessers(classToken)
  51. -- Reset our list
  52. for _, list in pairs(blessings) do for i=#(list), 1, -1 do table.remove(list, i) end end
  53. -- Load the list of players by blessing into a table
  54. for name, data in pairs(PaladinBuffer.db.profile.blessings) do
  55. for spellToken, rank in pairs(data) do
  56. if( rank ~= "none" and PaladinBuffer.blessingTypes[spellToken] == "greater" ) then
  57. table.insert(blessings[spellToken], name)
  58. end
  59. end
  60. end
  61. -- Now go through that list
  62. for spellToken, list in pairs(blessings) do
  63. currentSort = spellToken
  64. -- What this does is find the person who is least likely to conflict with someone else, and ultimately give the highest blessing assignment
  65. for k in pairs(pointsInfo) do pointsInfo[k] = nil end
  66. for name, data in pairs(PaladinBuffer.db.profile.blessings) do
  67. pointsInfo[name] = pointsInfo[name] or 0
  68. for token, rank in pairs(data) do
  69. if( token == spellToken ) then
  70. pointsInfo[name] = pointsInfo[name] - (rank * 1000)
  71. else
  72. -- Find the blessing priority
  73. local priority = 10
  74. for pID, pToken in pairs(priorities[classToken]) do
  75. if( pToken == token ) then
  76. priority = pID * 10
  77. break
  78. end
  79. end
  80. -- Add it up
  81. pointsInfo[name] = pointsInfo[name] + (rank * (100 - priority))
  82. end
  83. end
  84. end
  85. -- Sort it with our least likely conflicter
  86. table.sort(list, sortOrder)
  87. end
  88. end
  89. function Assign:CalculateBlessings()
  90. self:CreateTables()
  91. -- Reset assignments
  92. for _, list in pairs(assignments) do for k in pairs(list) do list[k] = nil end end
  93. -- Loop through and do all of our fancy assigning
  94. for classToken, classPriorities in pairs(priorities) do
  95. for k in pairs(blacklist) do blacklist[k] = nil end
  96. self:SetHighestBlessers(classToken)
  97. -- Loop through the priorities in order
  98. for _, spellToken in pairs(classPriorities) do
  99. -- Now find out who can do the highest rank of this, that isn't black listed
  100. for _, playerName in pairs(blessings[spellToken]) do
  101. if( not blacklist[playerName] ) then
  102. blacklist[playerName] = true
  103. assignments[classToken][playerName] = spellToken
  104. break
  105. end
  106. end
  107. end
  108. end
  109. -- Reset all previous assignments
  110. PaladinBuffer:ClearAllAssignments()
  111. -- Now assign the new ones
  112. for classToken, list in pairs(assignments) do
  113. for playerName, spellToken in pairs(list) do
  114. PaladinBuffer:AssignBlessing(playerName, spellToken, classToken)
  115. end
  116. end
  117. end
  118. function Assign:TotalSingleAssigns(spellToken, playerName)
  119. if( not PaladinBuffer.db.profile.assignments[playerName] ) then
  120. return 0
  121. end
  122. local total = 0
  123. for assignment, token in pairs(PaladinBuffer.db.profile.assignments[playerName]) do
  124. if( token == spellToken ) then
  125. total = total + 1
  126. end
  127. end
  128. return total
  129. end
  130. function Assign:FindSingleBlesser(spellToken)
  131. local highestName, highestRank
  132. for name, data in pairs(PaladinBuffer.db.profile.blessings) do
  133. if( data[spellToken] and ( not highestRank or highestRank < data[spellToken] ) ) then
  134. highestName = name
  135. highestRank = data[spellToken]
  136. end
  137. end
  138. return highestName
  139. end
  140. function Assign:IsBlessingAvailable(spellToken, playerName)
  141. if( playerName and PaladinBuffer.db.profile.blessings[playerName] ) then
  142. return PaladinBuffer.db.profile.blessings[playerName][spellToken] and true or false
  143. end
  144. for _, data in pairs(PaladinBuffer.db.profile.blessings) do
  145. if( data[spellToken] ) then
  146. return true
  147. end
  148. end
  149. return false
  150. end
  151. function Assign:IsGreaterAssigned(class, spellToken)
  152. for _, data in pairs(PaladinBuffer.db.profile.assignments) do
  153. if( data[class] == singleToGreater[spellToken] ) then
  154. return true
  155. end
  156. end
  157. return false
  158. end