stoneharry

Untitled

Jun 11th, 2015
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.87 KB | None | 0 0
  1. local CHOICE_TABLE = {
  2.     -- TYPES:
  3.     --      1: Spell
  4.     --      2: Item
  5.     -- [level] = { { type, ID, chance }, ... }
  6.     [2] = {
  7.         {1, 90008, 100},
  8.         {2, 23368, 100},
  9.         {2, 26029, 100}
  10.     },
  11.     [3] = {
  12.         {2, 50055, 100},
  13.         {2, 23368, 100},
  14.         {2, 26029, 100}
  15.     },
  16.     [4] = {
  17.         {1, 90010, 100},
  18.         {2, 23368, 100},
  19.         {2, 26029, 100}
  20.     },
  21.     [5] = {
  22.         {1, 90000, 100},
  23.         {2, 50055, 100},
  24.         {2, 23368, 100}
  25.     },
  26.     [6] = {
  27.         {1, 90011, 100},
  28.         {1, 90009, 100},
  29.         {2, 23368, 33},
  30.         {2, 50055, 33},
  31.         {2, 26029, 33}
  32.     }
  33. }
  34.  
  35. local function PLAYER_EVENT_ON_LEVEL_CHANGE(event, player, oldLevel)
  36.     local t = CHOICE_TABLE[oldLevel + 1]
  37.     if not t then
  38.         return
  39.     end
  40.     local results = {}
  41.     local position = 1
  42.     for i=1,3 do
  43.         local inserted = false
  44.         while not inserted do
  45.             local row = t[position]
  46.             if not row then
  47.                 position = 1
  48.             else
  49.                 local valid = true
  50.                 local _type = row[1]
  51.                 local id = row[2]
  52.                 local chance = row[3]
  53.                 for _,v in pairs(results) do
  54.                     if v[1] == _type and v[2] == id then
  55.                         valid = false
  56.                         break
  57.                     end
  58.                     if v[1] == 1 and player:HasSpell(v[2]) then
  59.                         valid = false
  60.                         break
  61.                     end
  62.                 end
  63.                 if valid and math.random(1, 100) <= chance then
  64.                     table.insert(results, {_type, id})
  65.                     inserted = true
  66.                 end
  67.             end
  68.         end
  69.     end
  70.    
  71.     local query = "INSERT INTO `spellselectionsystem` (guid, `level`, type1, option1, type2, option2, type3, option3) VALUES "
  72.         .. "('" .. player:GetGUIDLow() .. "', '" .. (oldLevel + 1) .. "'"
  73.     local message = "LEVELUP-" .. string.format("%02d", oldLevel + 1)
  74.     for i=1,3 do
  75.         query = query .. ", '" .. results[i][1] .. "'"
  76.         message = message .. "-" ..  string.format("%01d", results[i][1])
  77.         query = query .. ", '" .. results[i][2] .. "'"
  78.         message = message .. "-" ..  string.format("%06d", results[i][2])
  79.     end
  80.    
  81.     CharDBQuery(query .. ")")
  82.    
  83.     sendAddonMessage(player, message, 1)
  84. end
  85.  
  86. RegisterPlayerEvent(13, PLAYER_EVENT_ON_LEVEL_CHANGE)
  87.  
  88. -----------
  89. ----------- receiving
  90. -----------
  91. local _DEBUG = false
  92.  
  93. -- Prototypes
  94. local functionLookup
  95.  
  96. -- Handle addon messages
  97. local function onReceiveAddonMsg(event, plr, Type, prefix, msg, receiver)
  98.     if receiver ~= plr then return end
  99.     if not msg or not prefix or not plr then return end
  100.     if _DEBUG then print("[GOT] " .. prefix .. " | " .. msg) end
  101.     local func = functionLookup[prefix]
  102.     if func then
  103.         func(plr, msg)
  104.     end
  105. end
  106.  
  107. RegisterServerEvent(30, onReceiveAddonMsg)
  108.  
  109. function SelectLevelReward(plr, msg)
  110.     local option = tonumber(msg)
  111.     if not option or option < 1 or option > 3 then
  112.         return
  113.     end
  114.     local level = plr:GetLevel()
  115.     local result = CharDBQuery("SELECT * FROM `spellselectionsystem` WHERE `guid` = '"..plr:GetGUIDLow().."' AND `level` = '"..level.."'")
  116.     if result then
  117.         local t = result:GetRow()
  118.         if t["selected"] then
  119.             return
  120.         end
  121.         local optionVal = t["option"..tostring(option)]
  122.         local optionType = t["type"..tostring(option)]
  123.         if (optionType == 1) then
  124.             plr:LearnSpell(optionVal)
  125.         elseif (optionType == 2) then
  126.             plr:AddItem(optionVal, 1)
  127.         end
  128.         CharDBQuery("UPDATE `spellselectionsystem` SET `selectedType` = '"..tostring(optionType).."', `selected` = '"..tostring(optionVal).."' WHERE `guid` = '"..plr:GetGUIDLow().."' AND `level` = '"..level.."'")
  129.     end
  130. end
  131.  
  132. -- function lookup
  133. functionLookup = {
  134.     ["SELECT"] = SelectLevelReward
  135. }
  136.  
  137. --- on delete character
  138.  
  139. local function PLAYER_EVENT_ON_CHARACTER_DELETE(event, guid)
  140.     if guid then
  141.         CharDBQuery("DELETE FROM `spellselectionsystem` WHERE `guid` = '"..guid.."'")
  142.     end
  143. end
  144.  
  145. RegisterPlayerEvent(2, PLAYER_EVENT_ON_CHARACTER_DELETE)
  146.  
  147. ---- on first login
  148.  
  149. local function PLAYER_EVENT_ON_FIRST_LOGIN(event, plr)
  150.     if plr then
  151.         for _,t in pairs(CHOICE_TABLE) do
  152.             for _,r in pairs(t) do
  153.                 if r[1] == 2 then
  154.                     plr:ForceItemDownPlayersThroat(r[2])
  155.                 end
  156.             end
  157.         end
  158.     end
  159. end
  160.  
  161. RegisterPlayerEvent(30, PLAYER_EVENT_ON_FIRST_LOGIN)
Advertisement
Add Comment
Please, Sign In to add comment