Advertisement
Allarw

Hmm

Sep 19th, 2011
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.99 KB | None | 0 0
  1. local mysql = exports.mysql
  2.  
  3. local priceCache = { }
  4. local vehiclecount = { }
  5.  
  6. carshopPickup = createPickup(541.6484375, -1274.1513671875, 17.2421875, 3, 1239)
  7. exports['anticheat-system']:changeProtectedElementDataEx(carshopPickup, "shopid", 1, false)
  8.  
  9. boatshopPickup = createPickup(715.35546875, -1705.5791015625, 2.4296875, 3, 1239)
  10. exports['anticheat-system']:changeProtectedElementDataEx(boatshopPickup, "shopid", 2, false)
  11.  
  12. cheapcarshopPickup = createPickup( 2131.8115234375, -1151.3212890625, 24.060283660889, 3, 1239)
  13. exports['anticheat-system']:changeProtectedElementDataEx(cheapcarshopPickup, "shopid", 3, false)
  14.  
  15. function pickupUse(thePlayer)
  16.     if getElementData(source, "shopid") then
  17.         if getElementData(thePlayer, "license.car") == 1 then
  18.             triggerClientEvent(thePlayer, "showCarshopUI", thePlayer, getElementData(source, "shopid"))
  19.         else
  20.             outputChatBox("You need a Driving License to buy a car.", thePlayer, 255, 0, 0)
  21.         end
  22.     end
  23.     cancelEvent()
  24. end
  25. addEventHandler("onPickupHit", getResourceRootElement(), pickupUse)
  26.  
  27. local function copy( t )
  28.     if type(t) == 'table' then
  29.         local r = {}
  30.         for k, v in pairs( t ) do
  31.             r[k] = copy( v )
  32.         end
  33.         return r
  34.     else
  35.         return t
  36.     end
  37. end
  38.  
  39. local function countVehicles( )
  40.     vehiclecount = {}
  41.     for key, value in pairs( getElementsByType( "vehicle" ) ) do
  42.         if isElement( value ) then
  43.             local model = getElementModel( value )
  44.             if vehiclecount[ model ] then
  45.                 vehiclecount[ model ] = vehiclecount[ model ] + 1
  46.             else
  47.                 vehiclecount[ model ] = 1
  48.             end
  49.         end
  50.     end
  51. end
  52.  
  53.  
  54. function buildPriceCache(shopID)
  55.     priceCache[shopID] = { }
  56.    
  57.     local activeShop = copy( g_shops[shopID] )
  58.     for key, value in ipairs( activeShop ) do
  59.         if value[1] and value[2] and vehiclecount[ value[1] ] then
  60.             priceCache[shopID][ value[1] ] = value[2] + ( vehiclecount[ value[1] ] or 0 ) * 600
  61.         end
  62.     end
  63.    
  64. end
  65.  
  66. function buyCar(id, cost, col1, col2, x, y, z, rz, px, py, pz, prz, shopID)
  67.     countVehicles()
  68.     buildPriceCache(shopID)
  69.  
  70.     if not(priceCache[shopID][id]) then
  71.         exports.logs:logMessage("[CAR SHOP] " .. getPlayerIP(client).." \ ".. getPlayerName( client ) .. "  tried to buy an " .. getVehicleNameFromModel( id ) .. " at an non existing shop for $".. cost..")", 32)
  72.         return
  73.     end
  74.    
  75.     if not (priceCache[shopID][id] == cost) then
  76.         exports.logs:logMessage("[CAR SHOP] " .. getPlayerIP(client).." \ " .. getPlayerName( client ) .. "  tried to buy an " .. getVehicleNameFromModel( id ) .. " at shop ".. shopID .." for $".. cost..")", 32)
  77.         return
  78.     end
  79.    
  80.     if exports.global:hasMoney(client, cost) then
  81.         if exports.global:canPlayerBuyVehicle(client) then
  82.             outputChatBox("You bought a " .. getVehicleNameFromModel(id) .. " for " .. cost .. "$. Enjoy!", client, 255, 194, 14)
  83.            
  84.             if shopID == 1 then
  85.                 outputChatBox("You can set this vehicles spawn position by parking it and typing /park", client, 255, 194, 14)
  86.                 outputChatBox("Vehicles parked near the dealership or bus spawn point will be deleted without notice.", client, 255, 0, 0)
  87.             elseif shopID == 2 then
  88.                 outputChatBox("You can set this boats spawn position by parking it and typing /park", client, 255, 194, 14)
  89.                 outputChatBox("Boats parked near the marina will be deleted without notice.", client, 255, 0, 0)
  90.             end
  91.             outputChatBox("If you do not use /park within an hour, your car will be DELETED.", client, 255, 0, 0)
  92.             outputChatBox("Press 'K' to unlock this vehicle.", client, 255, 194, 14)
  93.             makeCar(client, id, cost, col1, col2, x, y, z, rz, px, py, pz, prz)
  94.         else
  95.             outputChatBox("You tried to buy a car, but you have too many vehicles already.", client, 255, 0, 0)
  96.         end
  97.     end
  98. end
  99. addEvent("buyCar", true)
  100. addEventHandler("buyCar", getRootElement(), buyCar)
  101.  
  102. function makeCar(thePlayer, id, cost, col1, col2, x, y, z, rz, px, py, pz, prz)
  103.     if not exports.global:takeMoney(thePlayer, cost) then
  104.         return
  105.     end
  106.    
  107.     if not exports.global:canPlayerBuyVehicle(source) then
  108.         return
  109.     end
  110.    
  111.     local rx = 0
  112.     local ry = 0
  113.        
  114.     setElementPosition(thePlayer, px, py, pz)
  115.     setPedRotation(thePlayer, prz)
  116.    
  117.     local username = getPlayerName(thePlayer)
  118.     local dbid = getElementData(thePlayer, "dbid")
  119.    
  120.     local letter1 = string.char(math.random(65,90))
  121.     local letter2 = string.char(math.random(65,90))
  122.     local plate = letter1 .. letter2 .. math.random(0, 9) .. " " .. math.random(1000, 9999)
  123.     local locked = 0
  124.        
  125.     local insertid = mysql:query_insert_free("INSERT INTO vehicles SET model='" .. mysql:escape_string(id) .. "', x='" .. mysql:escape_string(x) .. "', y='" .. mysql:escape_string(y) .. "', z='" .. mysql:escape_string(z) .. "', rotx='" .. mysql:escape_string(rx) .. "', roty='" .. mysql:escape_string(ry) .. "', rotz='" .. mysql:escape_string(rz) .. "', color1='" .. mysql:escape_string(col1) .. "', color2='" .. mysql:escape_string(col2) .. "', faction='-1', owner='" .. mysql:escape_string(dbid) .. "', plate='" .. mysql:escape_string(plate) .. "', currx='" .. mysql:escape_string(x) .. "', curry='" .. mysql:escape_string(y) .. "', currz='" .. mysql:escape_string(z) .. "', currrx='0', currry='0', currrz='" .. mysql:escape_string(rz) .. "', locked='" .. mysql:escape_string(locked) .. "'")
  126.    
  127.     if (insertid) then
  128.         local veh = call( getResourceFromName( "vehicle-system" ), "createShopVehicle", insertid, id, x, y, z, 0, 0, rz, plate )
  129.        
  130.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "fuel", 100, false)
  131.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "Impounded", 0)
  132.        
  133.         setVehicleRespawnPosition(veh, x, y, z, 0, 0, rz)
  134.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "respawnposition", {x, y, z, 0, 0, rz}, false)
  135.         setVehicleLocked(veh, false)
  136.        
  137.         setVehicleColor(veh, col1, col2, col1, col2)
  138.        
  139.         setVehicleOverrideLights(veh, 1)
  140.         setVehicleEngineState(veh, false)
  141.         setVehicleFuelTankExplodable(veh, false)
  142.        
  143.         -- make sure it's an unique key
  144.         call( getResourceFromName( "item-system" ), "deleteAll", 3, insertid )
  145.         exports.global:giveItem( thePlayer, 3, insertid )
  146.        
  147.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "fuel", 100, false)
  148.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "engine", 0, false)
  149.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "oldx", x, false)
  150.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "oldy", y, false)
  151.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "oldz", z, false)
  152.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "faction", -1)
  153.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "owner", dbid, false)
  154.         exports['anticheat-system']:changeProtectedElementDataEx(veh, "job", 0, false)
  155.        
  156.         exports['vehicle-system']:reloadVehicle(insertid)
  157.         if getVehicleType(veh) == "Boat" then
  158.             exports.global:givePlayerAchievement(thePlayer, 27)
  159.         else
  160.             exports.global:givePlayerAchievement(thePlayer, 17) -- my ride
  161.         end
  162.        
  163.         exports.logs:logMessage("[CAR SHOP] " .. getPlayerName( thePlayer ) .. " bought car #" .. insertid .. " (" .. getVehicleNameFromModel( id ) .. ")", 9)
  164.     end
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement