stoneharry

Untitled

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