· 8 years ago · Jan 02, 2018, 10:44 PM
1#include <sourcemod>
2#include <shavit>
3
4//MySQL settings
5Database gH_SQL = null;
6char gS_MySQLPrefix[32];
7
8
9char gS_ServerIP[32];
10char gS_MapName[128];
11bool ForcedMapChange;
12bool LastTimeForcedMapChange;
13
14
15public Plugin myinfo =
16{
17 name = "Unique Map",
18 author = "theSaint",
19 description = "Disallow to play the same map on different bhop ggeasy servers",
20 version = "1.0",
21 url = "http://ggeasy.pl"
22}
23
24public void OnPluginStart()
25{
26
27 // THIS IS TO GET SERVERIP
28 char server_port[10];
29 char server_ip[16];
30
31 Handle cvar_port = FindConVar("hostport");
32 GetConVarString(cvar_port, server_port, sizeof(server_port));
33 CloseHandle(cvar_port);
34
35 Handle cvar_ip = FindConVar("ip");
36 GetConVarString(cvar_ip, server_ip, sizeof(server_ip));
37 CloseHandle(cvar_ip);
38
39 FormatEx(gS_ServerIP,32,"%s:%s",server_ip,server_port);
40
41 // DATABASE CONNECTIONS
42 Shavit_GetDB(gH_SQL);
43 SQL_SetPrefix();
44 SetSQLInfo();
45}
46
47public void OnMapStart()
48{
49 //Getting MapName
50 GetCurrentMap(gS_MapName, 128);
51 //Secure Change of Bools
52 ForcedMapChange = false;
53 LastTimeForcedMapChange = false;
54
55 UniqueMapProcedure();
56}
57
58
59// --------------------
60// Functions
61// --------------------
62
63void UniqueMapProcedure()
64{
65 CreateTimer(0.5, CreateCurrentMapTable);
66 CreateTimer(1.0, GetDataFromCurrentMapTable);
67 CreateTimer(1.2, ReplaceMapInfo);
68 CreateTimer(5.0, ChangeMap);
69 CreateTimer(60.0, PrintChangedMapInfo);
70}
71
72public Action CreateCurrentMapTable(Handle timer)
73{
74 char[] sQuery = new char[256];
75 FormatEx(sQuery, 256, "CREATE TABLE IF NOT EXISTS `%scurrentmap` (`server_ip` VARCHAR(32), `current_map` VARCHAR(128), `last_time_changed` INT, PRIMARY KEY (`server_ip`));",gS_MySQLPrefix);
76
77 SQL_Query(gH_SQL, sQuery);
78
79 return Plugin_Handled;
80}
81
82public Action GetDataFromCurrentMapTable(Handle timer)
83{
84 char[] sQuery = new char[256];
85 FormatEx(sQuery, 256, "SELECT * FROM %scurrentmap;", gS_MySQLPrefix);
86
87 DBResultSet results = SQL_Query(gH_SQL, sQuery);
88
89 if(results == null)
90 {
91 LogError("COS SIE SPIERDOLILO W GetDataFromCurrentMapTable")
92 return Plugin_Handled;
93 }
94
95 while(results.FetchRow())
96 {
97 char ip[32];
98 char map[128];
99
100 results.FetchString(0, ip, 32);
101 results.FetchString(1, map, 128);
102 int changed = results.FetchInt(2);
103
104 if (StrEqual(ip, gS_ServerIP))
105 {
106 if (changed == 1)
107 {
108 LastTimeForcedMapChange = true;
109 }
110 }
111
112 if (StrEqual(map, gS_MapName) && !StrEqual(ip, gS_ServerIP))
113 {
114 ForcedMapChange = true;
115 }
116 }
117
118 delete results;
119
120 return Plugin_Handled;
121}
122
123public Action ReplaceMapInfo(Handle timer)
124{
125 char[] sQuery = new char[512];
126 FormatEx(sQuery, 512, "REPLACE INTO %scurrentmap (server_ip, current_map, last_time_changed) VALUES ('%s', '%s', %i);", gS_MySQLPrefix, gS_ServerIP , gS_MapName , ForcedMapChange)
127
128 SQL_Query(gH_SQL, sQuery);
129
130 return Plugin_Handled;
131}
132
133public Action ChangeMap(Handle timer)
134{
135 if(ForcedMapChange)
136 {
137 char map[128];
138 GetNextMap(map, 128);
139 ForceChangeLevel(map, "Played on Another Server");
140 }
141
142 return Plugin_Handled;
143}
144
145public Action PrintChangedMapInfo(Handle timer)
146{
147 if(LastTimeForcedMapChange)
148 {
149 PrintToChatAll("===MAP CHANGED AUTOMATICALY, BECOUSE IT'S ALREADY PLAYED ON THE OTHER SERVER===");
150 PrintToChatAll("===MAP CHANGED AUTOMATICALY, BECOUSE IT'S ALREADY PLAYED ON THE OTHER SERVER===");
151 PrintToChatAll("===MAP CHANGED AUTOMATICALY, BECOUSE IT'S ALREADY PLAYED ON THE OTHER SERVER===");
152 LastTimeForcedMapChange = false;
153 }
154
155 return Plugin_Handled;
156}
157
158// --------------------
159// DATABASE CONNECTIONS
160// --------------------
161
162public void Shavit_OnDatabaseLoaded(Database db)
163{
164 gH_SQL = db;
165}
166
167void SQL_SetPrefix()
168{
169 char[] sFile = new char[PLATFORM_MAX_PATH];
170 BuildPath(Path_SM, sFile, PLATFORM_MAX_PATH, "configs/shavit-prefix.txt");
171
172 File fFile = OpenFile(sFile, "r");
173
174 if(fFile == null)
175 {
176 SetFailState("Cannot open \"configs/shavit-prefix.txt\". Make sure this file exists and that the server has read permissions to it.");
177 }
178
179 char[] sLine = new char[PLATFORM_MAX_PATH*2];
180
181 while(fFile.ReadLine(sLine, PLATFORM_MAX_PATH*2))
182 {
183 TrimString(sLine);
184 strcopy(gS_MySQLPrefix, 32, sLine);
185
186 break;
187 }
188
189 delete fFile;
190}
191
192Action SetSQLInfo()
193{
194 if(gH_SQL == null)
195 {
196 Shavit_GetDB(gH_SQL);
197
198 CreateTimer(0.5, CheckForSQLInfo);
199 }
200
201 else
202 {
203 SQL_DBConnect();
204
205 return Plugin_Stop;
206 }
207
208 return Plugin_Continue;
209}
210
211void SQL_DBConnect()
212{
213 if(gH_SQL != null)
214 {
215 char[] sDriver = new char[8];
216 gH_SQL.Driver.GetIdentifier(sDriver, 8);
217 }
218}
219
220public Action CheckForSQLInfo(Handle Timer)
221{
222 return SetSQLInfo();
223}