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