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