· 8 years ago · Jan 16, 2018, 08:52 PM
1include <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 results.FetchString(1, gS_MapNameP, 128);
106 int changed = results.FetchInt(2);
107
108 if (StrEqual(ip, gS_ServerIP))
109 {
110 if (changed == 1)
111 {
112 LastTimeForcedMapChange = true;
113 }
114 }
115
116 if (StrEqual(map, gS_MapName) && !StrEqual(ip, gS_ServerIP))
117 {
118 ForcedMapChange = true;
119 }
120 }
121
122 delete results;
123
124 return Plugin_Handled;
125}
126
127public Action ReplaceMapInfo(Handle timer)
128{
129 char[] sQuery = new char[512];
130 FormatEx(sQuery, 512, "REPLACE INTO %scurrentmap (server_ip, current_map, last_time_changed) VALUES ('%s', '%s', %i);", gS_MySQLPrefix, gS_ServerIP , gS_MapName , ForcedMapChange)
131
132 SQL_Query(gH_SQL, sQuery);
133
134 return Plugin_Handled;
135}
136
137public Action ChangeMap(Handle timer)
138{
139 if(ForcedMapChange)
140 {
141 char map[128];
142 GetNextMap(map, 128);
143 ForceChangeLevel(map, "Played on Another Server");
144 }
145
146 return Plugin_Handled;
147}
148
149public Action PrintChangedMapInfo(Handle timer)
150{
151 if(LastTimeForcedMapChange)
152 {
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 PrintToChatAll("===MAP CHANGED AUTOMATICALY, BECOUSE IT'S ALREADY PLAYED ON THE OTHER SERVER===");
156 LastTimeForcedMapChange = false;
157 }
158
159 return Plugin_Handled;
160}
161
162// --------------------
163// DATABASE CONNECTIONS
164// --------------------
165
166public void Shavit_OnDatabaseLoaded()
167{
168 Database db;
169 gH_SQL = db;
170}
171
172void SQL_SetPrefix()
173{
174 char[] sFile = new char[PLATFORM_MAX_PATH];
175 BuildPath(Path_SM, sFile, PLATFORM_MAX_PATH, "configs/shavit-prefix.txt");
176
177 File fFile = OpenFile(sFile, "r");
178
179 if(fFile == null)
180 {
181 SetFailState("Cannot open \"configs/shavit-prefix.txt\". Make sure this file exists and that the server has read permissions to it.");
182 }
183
184 char[] sLine = new char[PLATFORM_MAX_PATH*2];
185
186 while(fFile.ReadLine(sLine, PLATFORM_MAX_PATH*2))
187 {
188 TrimString(sLine);
189 strcopy(gS_MySQLPrefix, 32, sLine);
190
191 break;
192 }
193
194 delete fFile;
195}
196
197Action SetSQLInfo()
198{
199 if(gH_SQL == null)
200 {
201 Shavit_GetDB(gH_SQL);
202
203 CreateTimer(0.5, CheckForSQLInfo);
204 }
205
206 else
207 {
208 SQL_DBConnect();
209
210 return Plugin_Stop;
211 }
212
213 return Plugin_Continue;
214}
215
216void SQL_DBConnect()
217{
218 if(gH_SQL != null)
219 {
220 char[] sDriver = new char[8];
221 gH_SQL.Driver.GetIdentifier(sDriver, 8);
222 }
223}
224
225public Action CheckForSQLInfo(Handle Timer)
226{
227 return SetSQLInfo();
228}
229
230public Action Command_Test(int client, int args)
231{
232 PrintToChatAll("%s to nasza mapka a przeciwnika to %s", gS_MapName, gS_MapNameP);
233}