· 5 years ago · Mar 29, 2020, 02:34 PM
1#include "a_samp"
2#include "inc/a_mysql"
3#include "inc/streamer"
4#include "inc/Pawn.CMD"
5#include "inc/sscanf2"
6
7main(){}
8
9#define MYSQL_HOST "localhost"
10#define MYSQL_USER "root"
11#define MYSQL_PASSWORD ""
12#define MYSQL_DATABASE "mojabaza"
13
14new MySQL:SQL;
15
16new query[325];
17
18new str[258];
19
20#define MAX_DOMY 100
21
22enum DomData
23{
24 dUID,
25 Float:dPosX,
26 Float:dPosY,
27 Float:dPosZ,
28 Float:dPosA,
29 bool:dKupiony,
30 dWlasciciel[MAX_PLAYER_NAME],
31 Text3D:dLabel,
32 dMapIcon,
33 dPickup,
34 dCena,
35 dInterior,
36 bool:dKlucz,
37 bool:dIDUsed
38};
39new DomInfo[MAX_DOMY][DomData];
40
41#define MAX_INT 10
42
43enum IntData
44{
45 IntUID,
46 Float:IntPosX,
47 Float:IntPosY,
48 Float:IntPosZ,
49 Float:IntPosA,
50 Int,
51 IntCena,
52 IntNazwa[24],
53 IntPickup,
54 Text3D:IntLabel
55};
56new IntInfo[MAX_INT][IntData];
57
58#define DIALOG_DOM1 10000
59
60public OnGameModeInit()
61{
62 new MySQLOpt:option_id = mysql_init_options();
63
64 mysql_set_option(option_id, AUTO_RECONNECT, true);
65
66 SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id);
67
68 if(SQL == MYSQL_INVALID_HANDLE || mysql_errno(SQL) != 0)
69 {
70 print("[MYSQL - BŁĄD] Nie można było połączyć się z bazą danych! Wyłączam serwer...");
71 SendRconCommand("exit");
72 return 1;
73 }
74 else
75 {
76 print("[MYSQL] Połączenie z bazą danych została nawiązana!");
77 }
78
79 SetupHouseTable();
80 SetupInteriorTable();
81
82 mysql_format(SQL, query, sizeof(query), "SELECT * FROM domy LIMIT %d", MAX_DOMY);
83 mysql_tquery(SQL, query, "OnHousesLoaded");
84
85 mysql_format(SQL, query, sizeof(query), "SELECT * FROM interiory LIMIT %d", MAX_INT);
86 mysql_tquery(SQL, query, "OnInteriorsLoaded");
87
88 return 1;
89}
90
91public OnGameModeExit()
92{
93 mysql_close(SQL);
94 return 1;
95}
96
97public OnPlayerSpawn(playerid)
98{
99 SetPlayerPos(playerid, 1258.7352, -2036.7100, 59.4561); //POS - LS
100 return 1;
101}
102
103GetHouseID()
104{
105 for(new i = 0; i < MAX_DOMY; i++)
106 {
107 if(!DomInfo[i][dIDUsed])
108 return i;
109 }
110 return MAX_DOMY;
111}
112
113forward OnCreateHouse(playerid, interior, cena, Float:HX, Float:HY, Float:HZ, Float:HA);
114public OnCreateHouse(playerid, interior, cena, Float:HX, Float:HY, Float:HZ, Float:HA)
115{
116 new hid = GetHouseID();
117
118 DomInfo[hid][dKupiony] = false;
119 DomInfo[hid][dUID] = hid;
120 DomInfo[hid][dCena] = cena;
121 DomInfo[hid][dPosX] = HX;
122 DomInfo[hid][dPosY] = HY;
123 DomInfo[hid][dPosZ] = HZ;
124 DomInfo[hid][dPosA] = HA;
125 DomInfo[hid][dInterior] = interior;
126 DomInfo[hid][dIDUsed] = true;
127
128 format(str, sizeof(str), "UID: %d\nWłaściciel: Brak\nCena: %d$\nAby kupić dom, wpisz /kupdom", hid, DomInfo[hid][dWlasciciel], DomInfo[hid][dCena]);
129 DomInfo[hid][dPickup] = CreateDynamicPickup(19522, 1, DomInfo[hid][dPosX], DomInfo[hid][dPosY], DomInfo[hid][dPosZ]);
130 DomInfo[hid][dLabel] = CreateDynamic3DTextLabel(str, -1, DomInfo[hid][dPosX], DomInfo[hid][dPosY], DomInfo[hid][dPosZ], 40);
131 DomInfo[hid][dMapIcon] = CreateDynamicMapIcon(DomInfo[hid][dPosX], DomInfo[hid][dPosY], DomInfo[hid][dPosZ], 31, -1);
132 return 1;
133}
134
135forward OnInteriorsLoaded();
136public OnInteriorsLoaded()
137{
138 new rows = cache_num_rows();
139
140 for(new i = 0; i < rows; i++)
141 {
142 cache_get_value_float(i, "PosX", IntInfo[i][IntPosX]);
143 cache_get_value_float(i, "PosY", IntInfo[i][IntPosY]);
144 cache_get_value_float(i, "PosZ", IntInfo[i][IntPosZ]);
145 cache_get_value_float(i, "PosA", IntInfo[i][IntPosA]);
146 cache_get_value_int(i, "UID", IntInfo[i][IntUID]);
147 cache_get_value_int(i, "InteriorID", IntInfo[i][Int]);
148 cache_get_value_int(i, "Cena", IntInfo[i][IntCena]);
149 cache_get_value(i, "Nazwa", IntInfo[i][IntNazwa], 24);
150
151 IntInfo[i][IntPickup] = CreateDynamicPickup(19523, 1, IntInfo[i][IntPosX], IntInfo[i][IntPosY], IntInfo[i][IntPosZ]);
152 IntInfo[i][IntLabel] = CreateDynamic3DTextLabel("Aby wyjść z domu, wpisz /wyjdzdom", -1, IntInfo[i][IntPosX], IntInfo[i][IntPosY], IntInfo[i][IntPosZ], 40, INVALID_PLAYER_ID, INVALID_VEHICLE_ID);
153 }
154 printf("Interiorów: %d", rows);
155 return 1;
156}
157
158forward OnHousesLoaded();
159public OnHousesLoaded()
160{
161 new rows = cache_num_rows();
162
163 for(new i = 0; i < rows; i++)
164 {
165 cache_get_value_float(i, "PosX", DomInfo[i][dPosX]);
166 cache_get_value_float(i, "PosY", DomInfo[i][dPosY]);
167 cache_get_value_float(i, "PosZ", DomInfo[i][dPosZ]);
168 cache_get_value_float(i, "PosA", DomInfo[i][dPosA]);
169 cache_get_value_int(i, "UID", DomInfo[i][dUID]);
170 cache_get_value_int(i, "Kupiony", DomInfo[i][dKupiony]);
171 cache_get_value_int(i, "Cena", DomInfo[i][dCena]);
172 cache_get_value_int(i, "Interior", DomInfo[i][dInterior]);
173 cache_get_value(i, "Wlasciciel", DomInfo[i][dWlasciciel], MAX_PLAYER_NAME);
174
175 DomInfo[i][dIDUsed] = true;
176
177 if(DomInfo[i][dKupiony])
178 {
179 format(str, sizeof(str), "UID: %d\nWłaściciel: %s\nAby wejść do domu, wpisz /wejdzdom", i, DomInfo[i][dWlasciciel]);
180 DomInfo[i][dPickup] = CreateDynamicPickup(1273, 1, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ]);
181 DomInfo[i][dLabel] = CreateDynamic3DTextLabel(str, -1, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ], 40);
182 DomInfo[i][dMapIcon] = CreateDynamicMapIcon(DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ], 32, -1);
183 }
184 else
185 {
186 format(str, sizeof(str), "UID: %d\nWłaściciel: Brak\nCena: %d$\nAby kupić dom, wpisz /kupdom", i, DomInfo[i][dWlasciciel], DomInfo[i][dCena]);
187 DomInfo[i][dPickup] = CreateDynamicPickup(19522, 1, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ]);
188 DomInfo[i][dLabel] = CreateDynamic3DTextLabel(str, -1, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ], 40);
189 DomInfo[i][dMapIcon] = CreateDynamicMapIcon(DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ], 31, -1);
190 }
191 }
192 printf("Domów: %d", rows);
193 return 1;
194}
195
196CMD:stworzdom(playerid, params[])
197{
198 if(IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, -1, "Nie możesz być w pojeździe!");
199
200 new int, cena;
201
202 if(sscanf(params, "dd", int, cena)) return SendClientMessage(playerid, -1, "/stworzdom int cena");
203
204 new Float:X, Float:Y, Float:Z, Float:A;
205
206 GetPlayerPos(playerid, X, Y, Z);
207 GetPlayerFacingAngle(playerid, A);
208
209 mysql_format(SQL, query, sizeof(query), "INSERT INTO `domy` (`Interior`, `Cena`, `PosX`, `PosY`, `PosZ`, `PosA`) VALUES (%d, %d, %f, %f, %f, %f)", int, cena, X, Y, Z, A);
210 mysql_tquery(SQL, query, "OnCreateHouse", "dddffff", playerid, int, cena, X, Y, Z, A);
211
212 SendClientMessage(playerid, -1, "Dom został pomyślnie stworzony!");
213
214 return 1;
215}
216
217CMD:wejdzdom(playerid)
218{
219 new hID = PlayerEnterPointHouse(playerid);
220 PlayerInsideHouse(playerid, hID);
221 return 1;
222}
223
224CMD:wyjdzdom(playerid)
225{
226 new hID = PlayerExitPointHouse(playerid);
227 PlayerOutsideHouse(playerid, hID);
228 return 1;
229}
230
231PlayerEnterPointHouse(playerid)
232{
233 for(new i = 0; i < MAX_DOMY; i++)
234 {
235 if(IsPlayerInRangeOfPoint(playerid, 1.0, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ]))
236 return i;
237 }
238 return MAX_DOMY;
239}
240
241PlayerExitPointHouse(playerid)
242{
243 for(new i = 0; i < MAX_DOMY; i++)
244 {
245 if(IsPlayerInRangeOfPoint(playerid, 1.0, IntInfo[i][IntPosX], IntInfo[i][IntPosY], IntInfo[i][IntPosZ]))
246 return i;
247 }
248 return MAX_DOMY;
249}
250
251forward PlayerInsideHouse(playerid, HouseID);
252public PlayerInsideHouse(playerid, HouseID)
253{
254 if(HouseID == MAX_DOMY) return SendClientMessage(playerid, -1, "Musisz być przy drzwiach domu.");
255
256 new houseinterior = DomInfo[HouseID][dInterior];
257
258 SetPlayerPos(playerid, IntInfo[houseinterior][IntPosX], IntInfo[houseinterior][IntPosY], IntInfo[houseinterior][IntPosZ]);
259 SetPlayerFacingAngle(playerid, IntInfo[houseinterior][IntPosA]);
260
261 SetPlayerInterior(playerid, IntInfo[houseinterior][Int]);
262
263 SetPlayerVirtualWorld(playerid, 50 + HouseID);
264
265 new dmsg[64];
266 format(dmsg, sizeof(dmsg), "Wszedłeś do domu o ID %d.", HouseID);
267 SendClientMessage(playerid, -1, dmsg);
268
269 return 1;
270}
271
272forward PlayerOutsideHouse(playerid, HouseID);
273public PlayerOutsideHouse(playerid, HouseID)
274{
275 if(HouseID == MAX_DOMY) return SendClientMessage(playerid, -1, "Musisz być przy wyjściu z domu.");
276
277 SetPlayerPos(playerid, DomInfo[HouseID][dPosX], DomInfo[HouseID][dPosY], DomInfo[HouseID][dPosZ]);
278 SetPlayerFacingAngle(playerid, DomInfo[HouseID][dPosA]);
279
280 SetPlayerInterior(playerid, 0);
281 SetPlayerVirtualWorld(playerid, 0);
282
283 new dmsg[64];
284 format(dmsg, sizeof(dmsg), "Wyszedłeś z domu o ID: %d.", HouseID);
285 SendClientMessage(playerid, -1, dmsg);
286
287 return 1;
288}
289
290CMD:reset(playerid)
291{
292 SetPlayerInterior(playerid, 0);
293 SetPlayerVirtualWorld(playerid, 0);
294 SetPlayerHealth(playerid, 0);
295 return 1;
296}
297
298stock SetupInteriorTable()
299{
300 mysql_tquery(SQL, "CREATE TABLE IF NOT EXISTS `interiory` ( \
301 `UID` INT(11) NOT NULL AUTO_INCREMENT, \
302 `PosX` FLOAT NOT NULL, \
303 `PosY` FLOAT NOT NULL, \
304 `PosZ` FLOAT NOT NULL, \
305 `PosA` FLOAT NOT NULL, \
306 `InteriorID` INT(11) NOT NULL DEFAULT '0', \
307 `Nazwa` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_polish_ci', \
308 `Cena` INT(11) NOT NULL DEFAULT '0', \
309 PRIMARY KEY (`UID`))");
310
311 return 1;
312}
313
314stock SetupHouseTable()
315{
316 mysql_tquery(SQL, "CREATE TABLE IF NOT EXISTS `domy` ( \
317 `UID` INT(11) NOT NULL AUTO_INCREMENT, \
318 `PosX` FLOAT NOT NULL, \
319 `PosY` FLOAT NOT NULL, \
320 `PosZ` FLOAT NOT NULL, \
321 `PosA` FLOAT NOT NULL, \
322 `Kupiony` TINYINT(4) NOT NULL DEFAULT '0', \
323 `Wlasciciel` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_polish_ci', \
324 `Cena` INT(11) NOT NULL DEFAULT '0', \
325 `Interior` INT(11) NOT NULL DEFAULT '0', \
326 PRIMARY KEY (`UID`))");
327
328 return 1;
329}
330
331stock GetNick(playerid)
332{
333 new PlayerNick[MAX_PLAYER_NAME];
334 GetPlayerName(playerid, PlayerNick, sizeof(PlayerNick));
335 return PlayerNick;
336}