· 6 years ago · Mar 12, 2019, 10:36 PM
1-- syncLevels: 0 = No sync, 1 = Only sync with the player thats using that account, 2 = sync with everybody
2
3-- Database template
4DATABASE_TEMPLATE =
5{
6 {
7 name = "accounts",
8 uniquekey = "userid",
9 columns =
10 {
11 { name = 'username', type = 'VARCHAR(225)', sync = 0 },
12 { name = 'password', type = 'CHAR(32)', sync = 0 },
13 { name = 'email', type = 'VARCHAR(225)', sync = 0 },
14 }
15 },
16}
17
18-- On resource start create the tables
19addEventHandler ( "onResourceStart", resourceRoot,
20 function ()
21 for index, aTable in pairs ( DATABASE_TEMPLATE ) do
22 exec ( "CREATE TABLE IF NOT EXISTS `??` ( `??` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY )", aTable.name, aTable.uniquekey )
23 for index, column in ipairs ( aTable.columns ) do
24 if not ( doesColumnExist ( aTable.name, column.name ) ) then
25 exec ( "ALTER TABLE `??` ADD `??` `??` NOT NULL", aTable.name, column.name, column.type )
26 end
27 end
28 end
29 end
30)
31
32-- Check if a column exists
33function doesColumnExist ( aTable, column )
34 for k, column in ipairs ( query ( "DESCRIBE `??`", aTable ) ) do
35 if ( column.Field == column ) then
36 return true
37 end
38 end
39 return false
40end