Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local OBJECT_END = 0x0006
- local UNIT_FIELD_FLAGS = OBJECT_END + 0x0035 -- Size: 1, Type: INT, Flags: PUBLIC
- local Race_System = {
- RACE_STATUS = "Looking For Racers",
- RACERS = {},
- WON = {},
- LOST = {},
- PAGE = {},
- SHOW = {},
- CURRENT = {},
- started = false,
- RACING = {}
- }
- function PageCounter(page)
- if page == 0 then
- return 1
- else
- return (page*10)+1
- end
- end
- function DisplayRacesWon(id, name) -- integrate with DB support
- local query = CharDBQuery("SELECT `won` FROM `racestats` WHERE `name` = '"..name.."';")
- if query ~= nil then
- return query:GetColumn(0):GetString()
- else
- return 0
- end
- --if Race_System.WON[Race_System.RACERS[id]] ~= nil then
- -- return Race_System.WON[Race_System.RACERS[id]];
- --[[else
- return 0;
- end]]
- end
- function DisplayRacesLost(id, name) -- integrate with DB support
- local query = CharDBQuery("SELECT `lost` FROM `racestats` WHERE `name` = '"..name.."';")
- if query ~= nil then
- return query:GetColumn(0):GetString()
- else
- return 0
- end
- --if Race_System.LOST[Race_System.RACERS[id]] ~= nil then
- -- return Race_System.LOST[Race_System.RACERS[id]];
- --[[else
- return 0;
- end]]
- end
- function ReturnPageLength(PAGE_COUNTER, TABLE_LENGTH)
- if (TABLE_LENGTH - PAGE_COUNTER) > 10 then
- return PAGE_COUNTER+10;
- else
- return TABLE_LENGTH;
- end
- end
- function Test_Item_Trigger(item, event, player)
- Test_Item(item, player)
- end
- function Test_Item(item, player, intid, code)
- if Race_System.PAGE[player:GetName()] == nil then
- Race_System.PAGE[player:GetName()] = 0
- end
- item:GossipCreateMenu(50, player, 0)
- --[[if intid ~= nil then
- player:SendBroadcastMessage(intid)
- end]]
- --player:SendBroadcastMessage("[DEBUG] Page: "..Race_System.PAGE[player:GetName()])
- item:GossipMenuAddItem(4, "|cff3060B5Current status:|r "..Race_System.RACE_STATUS, 1, 0)
- if Race_System.RACE_STATUS == "Looking For Racers" then
- item:GossipMenuAddItem(9, "Sign me up for this race!", 2, 0)
- end
- if #Race_System.RACERS ~= 0 then
- for i = PageCounter(Race_System.PAGE[player:GetName()]), ReturnPageLength(PageCounter(Race_System.PAGE[player:GetName()])-1, #Race_System.RACERS) do
- item:GossipMenuAddItem(4, " > Racer "..i..": "..Race_System.RACERS[i]:GetName(), 100+i, 0)
- if (intid == i+100) and (Race_System.SHOW[intid] == true) then
- item:GossipMenuAddItem(4, "|cff095F0B > Races won: |r"..DisplayRacesWon(i, player:GetName()), 100+i, 0)
- item:GossipMenuAddItem(4, "|cFFFF0000 > Races lost: |r"..DisplayRacesLost(i, player:GetName()), 100+i, 0)
- end
- if tostring(i/10):find('%.') == nil and #Race_System.RACERS > i then
- item:GossipMenuAddItem(7, "Next page.", 4, 0)
- end
- if Race_System.PAGE[player:GetName()] ~= 0 and (i == ReturnPageLength(PageCounter(Race_System.PAGE[player:GetName()])-1, #Race_System.RACERS)) then
- item:GossipMenuAddItem(7, "Previous page.", 5, 0)
- end
- end
- end
- item:GossipMenuAddItem(0, "Nevermind.", 99, 0)
- item:GossipSendMenu(player)
- end
- function Test_Item_Select(item, event, player, id, intid, code)
- if (intid == 1) then
- Test_Item(item, player)
- end
- if (intid == 2) then -- commented only during debug, so we can sign up more than once
- local PLAYER_MATCH = 0 -- using a local variable instead of return otherwise we can't send the menu again, thus
- for k,v in pairs(Race_System.RACERS) do -- we would be stuck with a "broken" menu
- if v:GetName() == player:GetName() then
- PLAYER_MATCH = 1
- break;
- end
- end
- if PLAYER_MATCH == 0 then
- table.insert(Race_System.RACERS, player)
- else
- player:SendBroadcastMessage("You've already been signed up!")
- end
- Test_Item(item, player)
- end
- if (intid == 4) then
- Race_System.PAGE[player:GetName()] = Race_System.PAGE[player:GetName()] + 1
- Test_Item(item, player)
- end
- if (intid == 5) then
- Race_System.PAGE[player:GetName()] = Race_System.PAGE[player:GetName()] - 1
- Test_Item(item, player)
- end
- if (intid == 99) then
- player:GossipComplete()
- end
- for i = 101, (#Race_System.RACERS+100) do
- if (intid == i) then
- if Race_System.SHOW[intid] == true then
- Race_System.SHOW[intid] = false
- else
- Race_System.SHOW[intid] = true
- end
- Test_Item(item, player, intid)
- end
- end
- end
- RegisterItemGossipEvent(57060, 1, "Test_Item_Trigger")
- RegisterItemGossipEvent(57060, 2, "Test_Item_Select")
- function CheckIfRaceCanStart()
- if Race_System.RACE_STATUS == "Looking For Racers" then
- if #Race_System.RACERS > 1 then
- local i = 0
- for k,plr in pairs(Race_System.RACERS) do
- if plr == nil then
- table.remove(Race_System.RACERS, plr)
- else
- i = i + 1
- end
- end
- if i > 1 then
- Race_System.RACE_STATUS = "Race in progress..."
- for k,plr in pairs(Race_System.RACERS) do
- if k == 1 then
- plr:Teleport(1, -6204, -3911, -60.32)
- elseif k == 2 then
- plr:Teleport(1, -6204, -3907, -60.32)
- elseif k == 3 then
- plr:Teleport(1, -6204, -3903, -60.32)
- elseif k == 4 then
- plr:Teleport(1, -6202, -3898, -60.32)
- elseif k == 5 then
- plr:Teleport(1, -6204, -3894, -60.32)
- elseif k > 5 then
- break
- end
- plr:Dismount()
- plr:SetPlayerLock(true)
- plr:Root()
- table.insert(Race_System.CURRENT, plr)
- end
- -- spawning a vehicle in a timed event = crash
- end
- end
- end
- end
- RegisterTimedEvent("CheckIfRaceCanStart", 10000, 0)
- function Race_Creature_Handler_Spawn(pUnit, Event)
- pUnit:RegisterEvent("Check_PlayersIfRacing", 1000, 0)
- end
- function Check_PlayersIfRacing(pUnit)
- for _,plr in pairs(pUnit:GetInRangePlayers()) do
- if plr ~= nil then
- if Race_System.RACE_STATUS == "Race in progress..." then
- if not plr:IsOnVehicle() then
- local name = plr:GetName()
- local istrue = false
- local position = 0
- for k,two in pairs(Race_System.CURRENT) do
- if two:GetName() == name then
- istrue = true
- position = k
- break
- end
- end
- if istrue then
- local query = CharDBQuery("SELECT `vehicle` FROM `racestats` WHERE `name` = '"..name.."';")
- if query == nil then
- CharDBQuery("INSERT INTO `racestats` VALUES ('"..name.."', 0, 0, 33062);")
- query = "33062"
- else
- query = query:GetColumn(0):GetString()
- end
- if tonumber(query) == 33062 then
- plr:SpawnCreature(33775, plr:GetX(), plr:GetY(), plr:GetZ(), plr:GetO(), 35, 0)
- end
- plr:SpawnAndEnterVehicle(tonumber(query), 1000)
- table.remove(Race_System.CURRENT, position)
- table.insert(Race_System.RACING, plr)
- end
- elseif plr:GetVehicleBase():GetEntry() == 33062 and plr:HasEmptyVehicleSeat() then
- local unit = plr:GetCreatureNearestCoords(plr:GetX(), plr:GetY(), plr:GetZ(), 33775)
- if unit ~= nil and not unit:IsOnVehicle() then
- if plr:IsOnVehicle() then
- unit:SetUInt32Value(UNIT_FIELD_FLAGS, 10) -- look like a player
- unit:EnterVehicle(plr:GetVehicleBase(), 1000)
- end
- end
- end
- if not Race_System.started then
- if plr:IsOnVehicle() then
- plr:GetVehicleBase():Root()
- end
- else
- plr:Unroot()
- plr:SetPlayerLock(false)
- if plr:IsOnVehicle() then
- plr:GetVehicleBase():Unroot()
- end
- end
- end
- end
- end
- end
- RegisterUnitEvent(33340, 18, "Race_Creature_Handler_Spawn")
Advertisement
Add Comment
Please, Sign In to add comment