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