forked from mabako/mta-paradise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shop.lua
353 lines (312 loc) · 11.2 KB
/
shop.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
--[[
Copyright (c) 2010 MTA: Paradise
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
local addCommandHandler_ = addCommandHandler
addCommandHandler = function( commandName, fn, restricted, caseSensitive )
-- add the default command handlers
if type( commandName ) ~= "table" then
commandName = { commandName }
end
for key, value in ipairs( commandName ) do
if key == 1 then
addCommandHandler_( value, fn, restricted, caseSensitive )
else
addCommandHandler_( value,
function( player, ... )
-- check if he has permissions to execute the command, default is not restricted (aka if the command is restricted - will default to no permission; otherwise okay)
if hasObjectPermissionTo( player, "command." .. commandName[ 1 ], not restricted ) then
fn( player, ... )
end
end
)
end
end
end
--
local p = { }
local shops = { }
local dimensions = { }
local function createShopPed( shopID )
local shop = shops[ shopID ]
if shop then
local ped = createPed( shop.skin ~= 0 and shop.skin or shop_configurations[ shop.configuration ].skin, shop.x, shop.y, shop.z, shop.rotation )
if ped then
shops[ ped ] = shopID
shop.ped = ped
setPedRotation( ped, shop.rotation )
setElementInterior( ped, shop.interior )
setElementDimension( ped, shop.dimension )
setPedFrozen( ped, true )
return true
end
end
outputDebugString( "Failed to create Shop " .. tostring( shopID ) )
return false
end
addEventHandler( "onPedWasted", resourceRoot,
function( )
local shopID = shops[ source ]
if shopID then
shops[ source ] = nil
destroyElement( source )
createShopPed( shopID )
end
end
)
local function loadShop( shopID, x, y, z, rotation, interior, dimension, configuration, skin )
shops[ shopID ] = { shopID = shopID, x = x, y = y, z = z, rotation = rotation, interior = interior, dimension = dimension, configuration = configuration, skin = skin }
if not createShopPed( shopID ) then
outputDebugString( "shop creation failed: shop " .. tostring( shopID ) )
end
end
addEventHandler( "onResourceStart", resourceRoot,
function( )
-- check for our tables to exist
if not exports.sql:create_table( 'shops',
{
{ name = 'shopID', type = 'int(10) unsigned', auto_increment = true, primary_key = true },
{ name = 'x', type = 'float' },
{ name = 'y', type = 'float' },
{ name = 'z', type = 'float' },
{ name = 'rotation', type = 'float' },
{ name = 'interior', type = 'tinyint(3) unsigned' },
{ name = 'dimension', type = 'int(10) unsigned' },
{ name = 'configuration', type = 'varchar(45)' },
{ name = 'skin', type = 'int(10) unsigned', default = 0 },
} ) then cancelEvent( ) return end
if not exports.sql:create_table( 'shopitems',
{
{ name = 'shopItemID', type = 'int(10) unsigned', auto_increment = true, primary_key = true },
{ name = 'shopID', type = 'int(10) unsigned' },
{ name = 'item', type = 'int(10) unsigned' },
{ name = 'value', type = 'text' },
{ name = 'name', type = 'text', null = true },
{ name = 'description', type = 'text', null = true },
{ name = 'price', type = 'int(10) unsigned' },
} ) then cancelEvent( ) return end
--
local result = exports.sql:query_assoc( "SELECT * FROM shops ORDER BY shopID ASC" )
if result then
for key, data in ipairs( result ) do
loadShop( data.shopID, data.x, data.y, data.z, data.rotation, data.interior, data.dimension, data.configuration, data.skin )
dimensions[ data.dimension ] = true
if exports['job-carrier'] then
exports['job-carrier']:addDropOff( data.dimension )
end
end
end
--
local result = exports.sql:query_assoc( "SELECT * FROM shopitems ORDER BY shopItemID ASC" )
if result then
for key, data in ipairs( result ) do
local shop = shops[ data.shopID ]
if shop then
if not shop.items then
shop.items = { }
end
table.insert( shop.items, { itemID = data.item, itemValue = data.value, name = data.name and data.name ~= "" and data.name, description = data.description and data.description ~= "" and data.description, price = data.price } )
end
end
end
end
)
addCommandHandler( "createshop",
function( player, commandName, config )
if config then
if shop_configurations[ config ] then
local x, y, z = getElementPosition( player )
local rotation = getPedRotation( player )
local interior = getElementInterior( player )
local dimension = getElementDimension( player )
local shopID = exports.sql:query_insertid( "INSERT INTO shops (x, y, z, rotation, interior, dimension, configuration) VALUES (" .. table.concat( { x, y, z, rotation, interior, dimension, '"%s"' }, ", " ) .. ")", config )
if shopID then
loadShop( shopID, x, y, z, rotation, interior, dimension, config, 0 )
outputChatBox( "Created new shop with ID " .. shopID .. ", type is " .. config .. ".", player, 0, 255, 0 )
exports['job-carrier']:addDropOff( dimension )
else
outputChatBox( "Shop creation failed (SQL-Error).", player, 255, 0, 0 )
end
else
outputChatBox( "There is no configuration named '" .. config .. "'.", player, 255, 0, 0 )
end
else
outputChatBox( "Syntax: /" .. commandName .. " [type]", player, 255, 255, 255 )
end
end,
true
)
local function deleteShop( shopID )
local shop = shops[ shopID ]
if shop then
-- close gui using this shop
for player, data in pairs( p ) do
if data.shopID == shopID then
triggerClientEvent( player, "shops:clear", shop.ped or resourceRoot )
p[ player ].shopID = nil
end
end
-- remove from shops list
if shop.ped then
destroyElement( shop.ped )
shops[ shop.ped ] = nil
end
-- unset
shops[ shopID ] = nil
-- check if we still have any shops in this dimension
local stillHasAShop = false
for key, value in pairs( shops ) do
if value.dimension == shop.dimension then
stillHasAShop = true
break
end
end
if not stillHasAShop and exports['job-carrier'] then
dimensions[ shop.dimension ] = nil
exports['job-carrier']:removeDropOff( shop.dimension )
end
end
end
addCommandHandler( { "deleteshop", "delshop" },
function( player, commandName, shopID )
shopID = tonumber( shopID )
if shopID then
local shop = shops[ shopID ]
if shop then
if exports.sql:query_free( "DELETE FROM shops WHERE shopID = " .. shopID ) and exports.sql:query_free( "DELETE FROM shopitems WHERE shopID = " .. shopID ) then
outputChatBox( "You deleted shop " .. shopID .. ".", player, 0, 255, 153 )
deleteShop( shopID )
else
outputChatBox( "MySQL-Query failed.", player, 255, 0, 0 )
end
else
outputChatBox( "Shop not found.", player, 255, 0, 0 )
end
else
outputChatBox( "Syntax: /" .. commandName .. " [id]", player, 255, 255, 255 )
end
end,
true
)
addCommandHandler( "nearbyshops",
function( player, commandName )
if hasObjectPermissionTo( player, "command.createshop", false ) or hasObjectPermissionTo( player, "command.deleteshop", false ) then
local x, y, z = getElementPosition( player )
local dimension = getElementDimension( player )
local interior = getElementInterior( player )
outputChatBox( "Nearby Shops:", player, 255, 255, 0 )
for key, value in pairs( shops ) do
if isElement( key ) and getElementDimension( key ) == dimension and getElementInterior( key ) == interior then
local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( key ) )
if distance < 20 then
outputChatBox( " Shop " .. value .. " - Type: " .. tostring( shops[ value ].configuration ) .. ".", player, 255, 255, 0 )
end
end
end
end
end
)
-- client interaction
addEventHandler( "onElementClicked", resourceRoot,
function( button, state, player )
if button == "left" and state == "up" then
local shopID = shops[ source ]
if shopID then
local shop = shops[ shopID ]
if shop then
local x, y, z = getElementPosition( player )
if getDistanceBetweenPoints3D( x, y, z, getElementPosition( source ) ) < 5 and getElementDimension( player ) == getElementDimension( source ) then
if not p[ player ] then
p[ player ] = { synched = { } }
end
p[ player ].shopID = shopID
if shop.items then -- custom items
-- these are manually synched if not sent yet
if not p[ player ].synched[ shopID ] then
triggerClientEvent( player, "shops:sync", source, shopID, shop.items )
p[ player ].synched[ shopID ] = true
end
triggerClientEvent( player, "shops:open", source, shopID )
elseif shop_configurations[ shop.configuration ] then
triggerClientEvent( player, "shops:open", source, shop.configuration )
end
end
end
end
end
end
)
addEventHandler( "onCharacterLogout", root,
function( )
if p[ source ] then
p[ source ].shopID = nil
end
end
)
addEventHandler( "onPlayerQuit", root,
function( )
p[ source ] = nil
end
)
addEvent( "shops:close", true )
addEventHandler( "shop:close", root,
function( )
if source == client then
p[ source ].shopID = nil
end
end
)
addEvent( "shops:buy", true )
addEventHandler( "shops:buy", root,
function( key )
if source == client and type( key ) == "number" then
-- check if the player is even meant to shop, if so only the index is transferred so we need to know where
if p[ source ] then
local shop = shops[ p[ source ].shopID ]
if shop then
-- check if it's a valid item
local item = shop.items and shop.items[ key ] or shop_configurations[ shop.configuration ][ key ]
if item then
if exports.players:takeMoney( source, item.price ) then
local value = item.itemID == 7 and exports.items:createPhone( ) or item.itemValue
if exports.items:give( source, item.itemID, value, item.name ) then
outputChatBox( "You've bought a " .. ( item.name or exports.items:getName( item.itemID ) ) .. " for $" .. item.price .. ".", source, 0, 255, 0 )
if item.itemID == 7 then
outputChatBox( "Your phone number is #" .. value .. ".", source, 0, 255, 0 )
end
end
else
outputChatBox( "You can't afford to buy a " .. ( item.name or exports.items:getName( item.itemID ) ) .. ".", source, 0, 255, 0 )
end
end
end
end
end
end
)
--
function clearDimension( dimension )
if dimension then
for key, value in pairs( shops ) do
if type( value ) == "table" then
if value.dimension == dimension then
if exports.sql:query_free( "DELETE FROM shops WHERE shopID = " .. value.shopID ) and exports.sql:query_free( "DELETE FROM shopitems WHERE shopID = " .. value.shopID ) then
deleteShop( value.shopID )
end
end
end
end
end
end
function getAllDimensions( )
return dimensions
end