stoneharry

Untitled

Jan 25th, 2012
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.31 KB | None | 0 0
  1.  
  2. local OBJECT_END = 0x0006
  3. local UNIT_FIELD_FLAGS = OBJECT_END + 0x0035 -- Size: 1, Type: INT, Flags: PUBLIC
  4.  
  5. local Race_System = {
  6.     RACE_STATUS = "Looking For Racers",
  7.     RACERS = {},
  8.     WON = {},
  9.     LOST = {},
  10.     PAGE = {},
  11.     SHOW = {},
  12.     CURRENT = {},
  13.     started = false,
  14.     RACING = {}
  15. }
  16.  
  17. function PageCounter(page)
  18.     if page == 0 then
  19.         return 1
  20.     else
  21.         return (page*10)+1
  22.     end
  23. end
  24.  
  25. function DisplayRacesWon(id, name) -- integrate with DB support
  26.     local query = CharDBQuery("SELECT `won` FROM `racestats` WHERE `name` = '"..name.."';")
  27.     if query ~= nil then
  28.         return query:GetColumn(0):GetString()
  29.     else
  30.         return 0
  31.     end
  32.     --if Race_System.WON[Race_System.RACERS[id]] ~= nil then
  33.     --  return Race_System.WON[Race_System.RACERS[id]];
  34.     --[[else
  35.         return 0;
  36.     end]]
  37. end
  38.  
  39. function DisplayRacesLost(id, name) -- integrate with DB support
  40.     local query = CharDBQuery("SELECT `lost` FROM `racestats` WHERE `name` = '"..name.."';")
  41.     if query ~= nil then
  42.         return query:GetColumn(0):GetString()
  43.     else
  44.         return 0
  45.     end
  46.     --if Race_System.LOST[Race_System.RACERS[id]] ~= nil then
  47.     --  return Race_System.LOST[Race_System.RACERS[id]];
  48.     --[[else
  49.         return 0;
  50.     end]]
  51. end
  52.  
  53. function ReturnPageLength(PAGE_COUNTER, TABLE_LENGTH)
  54.     if (TABLE_LENGTH - PAGE_COUNTER) > 10 then
  55.         return PAGE_COUNTER+10;
  56.     else
  57.         return TABLE_LENGTH;
  58.     end
  59. end
  60.  
  61. function Test_Item_Trigger(item, event, player)
  62.     Test_Item(item, player)
  63. end
  64.  
  65. function Test_Item(item, player, intid, code)
  66.     if Race_System.PAGE[player:GetName()] == nil then
  67.         Race_System.PAGE[player:GetName()] = 0
  68.     end
  69.    
  70.     item:GossipCreateMenu(50, player, 0)
  71.     --[[if intid ~= nil then
  72.         player:SendBroadcastMessage(intid)
  73.     end]]
  74.     --player:SendBroadcastMessage("[DEBUG] Page: "..Race_System.PAGE[player:GetName()])
  75.     item:GossipMenuAddItem(4, "|cff3060B5Current status:|r "..Race_System.RACE_STATUS, 1, 0)
  76.     if Race_System.RACE_STATUS == "Looking For Racers" then
  77.         item:GossipMenuAddItem(9, "Sign me up for this race!", 2, 0)
  78.     end
  79.     if #Race_System.RACERS ~= 0 then
  80.         for i = PageCounter(Race_System.PAGE[player:GetName()]), ReturnPageLength(PageCounter(Race_System.PAGE[player:GetName()])-1, #Race_System.RACERS) do
  81.             item:GossipMenuAddItem(4, "   > Racer "..i..": "..Race_System.RACERS[i]:GetName(), 100+i, 0)
  82.             if (intid == i+100) and (Race_System.SHOW[intid] == true) then
  83.                 item:GossipMenuAddItem(4, "|cff095F0B     > Races won: |r"..DisplayRacesWon(i, player:GetName()), 100+i, 0)
  84.                 item:GossipMenuAddItem(4, "|cFFFF0000     > Races lost: |r"..DisplayRacesLost(i, player:GetName()), 100+i, 0)
  85.             end
  86.             if tostring(i/10):find('%.') == nil and #Race_System.RACERS > i then
  87.                 item:GossipMenuAddItem(7, "Next page.", 4, 0)
  88.             end
  89.             if Race_System.PAGE[player:GetName()] ~= 0 and (i == ReturnPageLength(PageCounter(Race_System.PAGE[player:GetName()])-1, #Race_System.RACERS)) then
  90.                 item:GossipMenuAddItem(7, "Previous page.", 5, 0)
  91.             end
  92.         end
  93.     end
  94.     item:GossipMenuAddItem(0, "Nevermind.", 99, 0)
  95.     item:GossipSendMenu(player)
  96. end
  97.  
  98. function Test_Item_Select(item, event, player, id, intid, code)
  99.     if (intid == 1) then
  100.         Test_Item(item, player)
  101.     end
  102.    
  103.     if (intid == 2) then -- commented only during debug, so we can sign up more than once
  104.         local PLAYER_MATCH = 0 -- using a local variable instead of return otherwise we can't send the menu again, thus
  105.         for k,v in pairs(Race_System.RACERS) do -- we would be stuck with a "broken" menu
  106.             if v:GetName() == player:GetName() then
  107.                 PLAYER_MATCH = 1
  108.                 break;
  109.             end
  110.         end
  111.         if PLAYER_MATCH == 0 then
  112.                 table.insert(Race_System.RACERS, player)
  113.         else
  114.             player:SendBroadcastMessage("You've already been signed up!")
  115.         end
  116.         Test_Item(item, player)
  117.     end
  118.    
  119.     if (intid == 4) then
  120.         Race_System.PAGE[player:GetName()] = Race_System.PAGE[player:GetName()] + 1
  121.         Test_Item(item, player)
  122.     end
  123.    
  124.     if (intid == 5) then
  125.         Race_System.PAGE[player:GetName()] = Race_System.PAGE[player:GetName()] - 1
  126.         Test_Item(item, player)
  127.     end
  128.    
  129.     if (intid == 99) then
  130.         player:GossipComplete()
  131.     end
  132.    
  133.     for i = 101, (#Race_System.RACERS+100) do
  134.         if (intid == i) then
  135.             if Race_System.SHOW[intid] == true then
  136.                 Race_System.SHOW[intid] = false
  137.             else
  138.                 Race_System.SHOW[intid] = true
  139.             end
  140.             Test_Item(item, player, intid)
  141.         end
  142.     end
  143. end
  144.  
  145. RegisterItemGossipEvent(57060, 1, "Test_Item_Trigger")
  146. RegisterItemGossipEvent(57060, 2, "Test_Item_Select")
  147.  
  148. function CheckIfRaceCanStart()
  149.     if Race_System.RACE_STATUS == "Looking For Racers" then
  150.         if #Race_System.RACERS > 1 then
  151.             local i = 0
  152.             for k,plr in pairs(Race_System.RACERS) do
  153.                 if plr == nil then
  154.                     table.remove(Race_System.RACERS, plr)
  155.                 else
  156.                     i = i + 1
  157.                 end
  158.             end
  159.             if i > 1 then
  160.                 Race_System.RACE_STATUS = "Race in progress..."
  161.                 for k,plr in pairs(Race_System.RACERS) do
  162.                     if k == 1 then
  163.                         plr:Teleport(1, -6204, -3911, -60.32)
  164.                     elseif k == 2 then
  165.                         plr:Teleport(1, -6204, -3907, -60.32)
  166.                     elseif k == 3 then
  167.                         plr:Teleport(1, -6204, -3903,  -60.32)
  168.                     elseif k == 4 then
  169.                         plr:Teleport(1, -6202, -3898,  -60.32)
  170.                     elseif k == 5 then
  171.                         plr:Teleport(1, -6204, -3894,  -60.32)
  172.                     elseif k > 5 then
  173.                         break
  174.                     end
  175.                     plr:Dismount()
  176.                     plr:SetPlayerLock(true)
  177.                     plr:Root()
  178.                     table.insert(Race_System.CURRENT, plr)
  179.                 end
  180.                 -- spawning a vehicle in a timed event = crash
  181.             end
  182.         end
  183.     end
  184. end
  185.  
  186. RegisterTimedEvent("CheckIfRaceCanStart", 10000, 0)
  187.  
  188. function Race_Creature_Handler_Spawn(pUnit, Event)
  189.     pUnit:RegisterEvent("Check_PlayersIfRacing", 1000, 0)
  190. end
  191.  
  192. function Check_PlayersIfRacing(pUnit)
  193.     for _,plr in pairs(pUnit:GetInRangePlayers()) do
  194.         if plr ~= nil then
  195.             if Race_System.RACE_STATUS == "Race in progress..." then
  196.                 if not plr:IsOnVehicle() then
  197.                     local name = plr:GetName()
  198.                     local istrue = false
  199.                     local position = 0
  200.                     for k,two in pairs(Race_System.CURRENT) do
  201.                         if two:GetName() == name then
  202.                             istrue = true
  203.                             position = k
  204.                             break
  205.                         end
  206.                     end
  207.                     if istrue then
  208.                         local query = CharDBQuery("SELECT `vehicle` FROM `racestats` WHERE `name` = '"..name.."';")
  209.                         if query == nil then
  210.                             CharDBQuery("INSERT INTO `racestats` VALUES ('"..name.."', 0, 0, 33062);")
  211.                             query = "33062"
  212.                         else
  213.                             query = query:GetColumn(0):GetString()
  214.                         end
  215.                         if tonumber(query) == 33062 then
  216.                             plr:SpawnCreature(33775, plr:GetX(), plr:GetY(), plr:GetZ(), plr:GetO(), 35, 0)
  217.                         end
  218.                         plr:SpawnAndEnterVehicle(tonumber(query), 1000)
  219.                         table.remove(Race_System.CURRENT, position)
  220.                         table.insert(Race_System.RACING, plr)
  221.                     end
  222.                 elseif plr:GetVehicleBase():GetEntry() == 33062 and plr:HasEmptyVehicleSeat() then
  223.                     local unit = plr:GetCreatureNearestCoords(plr:GetX(), plr:GetY(), plr:GetZ(), 33775)
  224.                     if unit ~= nil and not unit:IsOnVehicle() then
  225.                         if plr:IsOnVehicle() then
  226.                             unit:SetUInt32Value(UNIT_FIELD_FLAGS, 10) -- look like a player
  227.                             unit:EnterVehicle(plr:GetVehicleBase(), 1000)
  228.                         end
  229.                     end
  230.                 end
  231.                 if not Race_System.started then
  232.                     if plr:IsOnVehicle() then
  233.                         plr:GetVehicleBase():Root()
  234.                     end
  235.                 else
  236.                     plr:Unroot()
  237.                     plr:SetPlayerLock(false)
  238.                     if plr:IsOnVehicle() then
  239.                         plr:GetVehicleBase():Unroot()
  240.                     end
  241.                 end
  242.             end
  243.         end
  244.     end
  245. end
  246.  
  247. RegisterUnitEvent(33340, 18, "Race_Creature_Handler_Spawn")
Advertisement
Add Comment
Please, Sign In to add comment