Advertisement
MrStump

Pokemon Sort

Oct 12th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1.  
  2. function onload()
  3.     masterCardTable = {
  4.         {name="Potion", xp="1", type=""},
  5.         {name="Boulder Badge", xp="4", type="Challenge"},
  6.         --etc etc etc
  7.     }
  8.    
  9.     scoreZoneTable = {
  10.         {scoreZone=getObjectFromGUID(""), color="Green"},
  11.         {scoreZone=getObjectFromGUID(""), color="Red"},
  12.         {scoreZone=getObjectFromGUID(""), color="White"},
  13.         {scoreZone=getObjectFromGUID(""), color="Blue"},
  14.         {scoreZone=getObjectFromGUID(""), color="Red"},
  15.     }
  16.     --insert button creation here
  17. end
  18.  
  19. --Gets 1 deck from each scripting zone, adds it to deckToScoreList
  20. --Along with that zone's color
  21. function onButtonPress()
  22.     deckToScoreList = {}
  23.     for i, zone in pairs(scoreZoneTable) do
  24.         local objectsInZone = zone.scoreZone.getObjects()
  25.         for j, deck in ipairs(objectsInZone) do
  26.             if deck.tag == "Deck" then
  27.                 table.insert(deckToScoreList, {deck=deck, color=zone.color})
  28.                 break
  29.             end
  30.         end
  31.     end
  32.     getDeckValues(deckToScoreList)
  33. end
  34.  
  35. --Obtains total values into a table.
  36. --Totals xp, # of challenge cards and # of evolution cards
  37. function getDeckValues(deckToScoreList)
  38.      = {}
  39.     for i, deck in ipairs(deckToScoreList) do
  40.         local cardsInDeck = deck.deck.getObjects()
  41.         for j, card in ipairs(cardsInDeck) do
  42.             table.insert(deckValueList, {xp=0, challenge=0, pokemon=0, , cards=0, color=deck.color})
  43.             for k, master in ipairs(masterCardTable) do
  44.                 if master.name == card.nickname then
  45.                     deckValueList[i].xp = deckValueList[i].xp + master.xp
  46.                     deckValueList[i].cards = deckValueList[i].cards + 1
  47.                     if master.type == "Challenge" then
  48.                         deckValueList[i].challenge = deckValueList[i].challenge + 1
  49.                     elseif master.type == "Pokemon" then
  50.                         deckValueList[i].pokemon = deckValueList[i].pokemon + 1
  51.                     end
  52.                 end
  53.             end
  54.         end
  55.     end
  56.     --At this point, we have a deckValueList that looks like:
  57.     --deckValueList = { {xp=#, challenge=#, pokemon=#, color="ColorName"}, {etc etc}, etc etc}
  58.     findWinner(deckValueList)
  59. end
  60.  
  61. function findWinner(deckValueList)
  62.     --Sort by XP
  63.     local sort_func = function( a,b ) return a.xp > b.xp end
  64.     table.sort( deckValueList, sort_func )
  65.  
  66.     if deckValueList[1].xp > deckValueList[2].xp then
  67.         --Winner is deckValueList[1].color
  68.         printToAll("Winner is: " .. deckValueList[1].color .. " with XP: " .. deckValueList[1].xp, {1,1,1})
  69.     else
  70.         --Make a new table with only tied XP players
  71.         tieList1 = {}
  72.         for i=1, #deckValueList do
  73.             if deckValueList[1] == deckValueList[i] then
  74.                 table.insert(tieList1, deckValueList[i])
  75.             end
  76.         end
  77.         --Prints tie report line
  78.         local nameString = ""
  79.         for i=1, #tieList1 do
  80.             nameString = nameString .. Player[tieList1[i].color].steam_name .. " "
  81.         end
  82.         printToAll(nameString .. "were tied for first in XP at : " .. tostring(tieList1[1].xp), {1,1,1})
  83.         --Sorts them by challenge
  84.         local sort_func = function( a,b ) return a.challenge > b.challenge end
  85.         table.sort( tieList1, sort_func )
  86.         --check for a winner
  87.         if tieList1[1].challenge > tieList1[2].challenge then
  88.             --Winner is tieList1[1].color
  89.             printToAll("Winner is: " .. tieList1[1].color .. " with XP: " .. tieList1[1].xp, {1,1,1})
  90.         else
  91.             --Make a new table with only tied Challenge players
  92.             tieList2 = {}
  93.             for i=1, #tieList1 do
  94.                 if tieList1[1] == tieList1[i] then
  95.                     table.insert(tieList2, tieList1[i])
  96.                 end
  97.             end
  98.             --Sorts them by pokemon
  99.             local sort_func = function( a,b ) return a.pokemon > b.pokemon end
  100.             table.sort( tieList2, sort_func )
  101.             --check for a winner
  102.             if tieList2[1].pokemon > tieList2[2].pokemon then
  103.                 --Winner is tieList2[1].color
  104.             else
  105.                 --Make a new table with only tied Pokemon players
  106.                 tieList3 = {}
  107.                 for i=1, #tieList2 do
  108.                     if tieList2[1] == tieList2[i] then
  109.                         table.insert(tieList3, tieList2[i])
  110.                     end
  111.                 end
  112.                 --Sorts them by pokemon
  113.                 local sort_func = function( a,b ) return a.cards > b.cards end
  114.                 table.sort( tieList2, sort_func )
  115.                 --check for a winner
  116.                 if tieList2[1].cards > tieList2[2].cards then
  117.                     --Winenr is tieList3[1].color
  118.                 else
  119.                     --4 way tie, what the eff are the odds of that?!
  120.                 end
  121.             end
  122.         end
  123.     end
  124. end
  125.  
  126. if deckValueList[1].xp > deckValueList[2].xp then
  127.     --Winner is deckValueList[1].color
  128.     local ct = stringColorToRGB(deckValueList[1].color)
  129.     printToAll("With a total of " .. deckValueList[1].xp .. " XP, " .. Player[deckValueList[1].color].steam_name .. " has proven to be the Pokémon Master this time!", ct)
  130. else
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement