· 9 years ago · Oct 04, 2016, 02:48 AM
1fcd.dataStrings = {}
2fcd.dataStrings[ 'dir' ] = 'tupacscripts/freshcardealer_revamped/'
3fcd.dataStrings[ 'dealerSpawns' ] = fcd.dataStrings[ 'dir' ] .. 'dealer_spawns_' .. game.GetMap() .. '.txt'
4fcd.dataStrings[ 'vehicles' ] = fcd.dataStrings[ 'dir' ] .. 'fcd_vehicles.txt'
5fcd.dataStrings[ 'ownedTbl' ] = 'fcd_playerData'
6
7function fcd.databaseInit()
8 local ac = MySQLite.isMySQL() and "AUTO_INCREMENT" or "AUTOINCREMENT"
9
10 MySQLite.query( 'create table if not exists ' .. fcd.dataStrings[ 'ownedTbl' ] .. ' ( id integer not null primary key ' .. ac .. ', uniqueID varchar( 255 ) not null, vehicles text not null )')
11
12 if not file.IsDir( 'tupacscripts', 'DATA' ) then
13 file.CreateDir( 'tupacscripts' )
14 end
15
16 if not file.IsDir( fcd.dataStrings[ 'dir' ], 'DATA' ) then
17 file.CreateDir( fcd.dataStrings[ 'dir' ] )
18 end
19
20 if not file.Exists( fcd.dataStrings[ 'dealerSpawns' ], 'DATA' ) then
21 file.Write( fcd.dataStrings[ 'dealerSpawns' ] )
22 end
23
24 if not file.Exists( fcd.dataStrings[ 'vehicles' ], 'DATA' ) then
25 file.Write( fcd.dataStrings[ 'vehicles' ] )
26 end
27
28 local newTbl = {}
29
30 if sql.TableExists( 'fcd_ownedtbl' ) then
31 local res = sql.Query( 'SELECT * FROM fcd_ownedtbl' )
32
33 if istable( res ) then
34 for k, v in pairs( res ) do
35 newTbl[ v.plyid ] = newTbl[ v.plyid ] or {}
36 table.insert( newTbl[ v.plyid ], v.vehid )
37 end
38
39 sql.Query( 'delete from fcd_ownedtbl' )
40 end
41
42 for id, veh in pairs( newTbl ) do
43 local old = util.TableToJSON( newTbl[ id ] )
44 local data = {}
45
46 local q = MySQLite.query( "select * from fcd_playerData where uniqueID = " .. id, function( ret )
47 data = ret
48 end )
49
50 if data then
51 MySQLite.query( "UPDATE " .. fcd.dataStrings[ 'ownedTbl' ] .. " SET vehicles = '" .. old .. "' WHERE uniqueID = " .. id )
52 else
53 MySQLite.query( "insert into ' .. fcd.dataStrings[ 'ownedTbl' ] .. ' (`id`, `vehicles`) values( '" .. id .. "', '" .. old .. "' )" )
54 end
55 end
56 end
57
58 fcd.loadRegisteredVehicles()
59 fcd.notifyServer( 'Initialized database!' )
60end
61
62hook.Add( 'Initialize', 'fcd.databaseInit', fcd.databaseInit )
63
64function fcd.dataRegVehicle( class, data )
65 if not class then return end
66 if not data then return end
67
68 if fcd.registeredVehicles[ class ] then
69 fcd.dataFormatVehicle( class, data )
70 end
71
72 fcd.saveRegisteredVehicles()
73end
74
75function fcd.dataFormatVehicle( class, data )
76 local info = list.Get( 'Vehicles' )[ class ]
77 if not info then return end
78
79 fcd.dataVehicles[ class ] = {}
80 fcd.dataVehicles[ class ].price = data.price
81
82 if data.rankRestrict then
83 fcd.dataVehicles[ class ].rankRestrict = data.rankRestrict
84 end
85
86 if data.jobRestrict then
87 fcd.dataVehicles[ class ].jobRestrict = data.jobRestrict
88 end
89
90 if data.failMsg then
91 fcd.dataVehicles[ class ].failMsg = data.failMsg
92 end
93
94 if data.category then
95 fcd.dataVehicles[ class ].category = data.category
96 end
97
98 if data.specificDealer then
99 fcd.dataVehicles[ class ].specificDealer = data.specificDealer
100 end
101
102 if data.isVip then
103 fcd.dataVehicles[ class ].isVip = true
104 else
105 fcd.dataVehicles[ class ].isVip = false
106 end
107
108 local info = file.Read( info.KeyValues.vehiclescript, true )
109 if not info then return end
110
111 info = util.KeyValuesToTable( info )
112
113 if info.engine then
114 fcd.dataVehicles[ class ].maxSpeed = math.Round( info.engine.maxspeed )
115 fcd.dataVehicles[ class ].maxRPM = math.Round( info.engine.maxrpm )
116 fcd.dataVehicles[ class ].horsePower = math.Round( info.engine.horsepower )
117 fcd.dataVehicles[ class ].axel = math.Round( info.engine.axleratio )
118 end
119end
120
121function fcd.saveRegisteredVehicles()
122 file.Write( fcd.dataStrings[ 'vehicles' ], util.TableToJSON( fcd.dataVehicles ) )
123end
124
125function fcd.loadRegisteredVehicles()
126 local data = file.Read( fcd.dataStrings[ 'vehicles' ], 'DATA')
127 data = util.JSONToTable( data )
128
129 if istable( data ) then
130 for i, v in pairs( data ) do
131
132 fcd.registeredVehicles[ v.price ] = {}
133 --[[
134 fcd.registeredVehicles[ i ].price = v.price
135
136 if v.price == 0 then
137 fcd.registeredVehicles[ i ].free = true
138 end
139
140 if v.rankRestrict then
141 fcd.registeredVehicles[ i ].rankRestrict = function( ply )
142 return table.HasValue( v.rankRestrict, ply:GetUserGroup() )
143 end
144 end
145
146 if v.jobRestrict then
147 fcd.registeredVehicles[ i ].jobRestrict = function( ply )
148 return table.HasValue( v.jobRestrict, team.GetName( ply:Team() ) )
149 end
150 end
151
152 if v.failMsg then
153 fcd.registeredVehicles[ i ].failMsg = v.failMsg
154 end
155
156 ]]
157
158 fcd.dataFormatVehicle( str, v )
159 end
160 end
161end