· 7 years ago · Dec 03, 2018, 06:26 PM
1public void SQL_InitConnection()
2{
3 SQL_ConnectToDB();
4 CreateTimer(1.0, DoTheRest);
5}
6
7public Action DoTheRest(Handle timer)
8{
9 SQL_Create_Achievements_Table();
10 SQL_Create_Players_Table();
11 SQL_Create_PlayerID_Table();
12}
13
14public void SQL_ConnectToDB()
15{
16 char sError[512];
17 //DB = SQLite_UseDatabase("AchievementsGO",sError,sizeof(sError));
18 DB = SQL_Connect("AchievementsGO",true, sError, sizeof(sError));
19
20 SQL_CheckIfConnected(sError);
21}
22
23public void SQL_CheckIfConnected(Database db, const char[] error, any data)
24{
25 if (db == null)
26 {
27 LogError("[AchievementsGO] Database failure: %s", error);
28 }
29 else
30 {
31 DB = db;
32 IsConnectionEstablished = true;
33 }
34}
35
36// @@ Creating Achievements table
37public void SQL_Create_Achievements_Table()
38{
39 if(IsConnectionEstablished)
40 {
41 char query[512];
42 FormatAchievementsQuery(query);
43 DB.Query(CheckIf_Achievements_QueryPassed, query, _, DBPrio_High);
44 }
45}
46
47public void FormatAchievementsQuery(char[] query)
48{
49 Format(query, 511, "CREATE TABLE IF NOT EXISTS `Achievements` (`ID` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE,`Name` TEXT NOT NULL,`Description` TEXT NOT NULL,`Category` TEXT,`Value` INTEGER NOT NULL); ");
50}
51public void CheckIf_Achievements_QueryPassed(Database db, DBResultSet results, const char[] error, any data)
52{
53 if (db == null)
54 {
55 LogMessage("Could not create achievements table! Error: %s", error);
56 }
57
58 tablesCreated++;
59 if (AreAllTablesCreated()) SendForwardOnRegisterAchievements();
60}
61
62// @@ Creating players table
63
64public void SQL_Create_Players_Table()
65{
66 if(IsConnectionEstablished)
67 {
68 char query[512];
69 FormatPlayersQuery(query);
70 DB.Query(CheckIf_Players_QueryPassed, query, _, DBPrio_High);
71 }
72}
73
74public void FormatPlayersQuery(char[] query)
75{
76 Format(query, 511, "CREATE TABLE IF NOT EXISTS `Players` (`PlayerID` INTEGER NOT NULL,`AchievementID` INTEGER NOT NULL,`Progress` INTEGER NOT NULL,UNIQUE(`PlayerID`,`AchievementID`)); ");
77}
78public void CheckIf_Players_QueryPassed(Database db, DBResultSet results, const char[] error, any data)
79{
80 if (db == null)
81 {
82 LogMessage("Could not create players table! Error: %s", error);
83 }
84
85 tablesCreated++;
86 if (AreAllTablesCreated()) SendForwardOnRegisterAchievements();
87}
88
89// @@ Creating PlayerID table - containing a client's unique ID-SteamID pair
90
91public void SQL_Create_PlayerID_Table()
92{
93 if(IsConnectionEstablished)
94 {
95 char query[512];
96 FormatPlayerIdTableQuery(query);
97 DB.Query(CheckIf_PlayerIdTable_QueryPassed, query, _, DBPrio_High);
98 }
99}
100
101public void FormatPlayerIdTableQuery(char[] query)
102{
103 Format(query, 511, "CREATE TABLE IF NOT EXISTS `PlayerID` (`ID` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE,`SteamID` TEXT NOT NULL UNIQUE,`Name` TEXT NOT NULL,`AccomplishedAchievements` INT NOT NULL); ");
104}
105public void CheckIf_PlayerIdTable_QueryPassed(Database db, DBResultSet results, const char[] error, any data)
106{
107 if (db == null)
108 {
109 LogMessage("Could not create PlayerID table! Error: %s", error);
110 }
111
112 tablesCreated++;
113 if (AreAllTablesCreated()) SendForwardOnRegisterAchievements();
114}
115
116public bool AreAllTablesCreated()
117{
118 return tablesCreated == 3;
119}