· 5 years ago · Mar 29, 2020, 10:36 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 DisableInteriorEnterExits();
89
90 return 1;
91}
92
93public OnGameModeExit()
94{
95 mysql_close(SQL);
96 return 1;
97}
98
99public OnPlayerSpawn(playerid)
100{
101 SetPlayerPos(playerid, 1258.7352, -2036.7100, 59.4561); //POS - LS
102 return 1;
103}
104
105GetHouseID()
106{
107 for(new i = 0; i < MAX_DOMY; i++)
108 {
109 if(!DomInfo[i][dIDUsed])
110 return i;
111 }
112 return MAX_DOMY;
113}
114
115forward OnCreateHouse(playerid, interior, cena, Float:HX, Float:HY, Float:HZ, Float:HA);
116public OnCreateHouse(playerid, interior, cena, Float:HX, Float:HY, Float:HZ, Float:HA)
117{
118 new hid = GetHouseID();
119
120 DomInfo[hid][dKupiony] = false;
121 DomInfo[hid][dUID] = hid;
122 DomInfo[hid][dCena] = cena;
123 DomInfo[hid][dPosX] = HX;
124 DomInfo[hid][dPosY] = HY;
125 DomInfo[hid][dPosZ] = HZ;
126 DomInfo[hid][dPosA] = HA;
127 DomInfo[hid][dInterior] = interior;
128 DomInfo[hid][dIDUsed] = true;
129
130 format(str, sizeof(str), "UID: %d\nWłaściciel: Brak\nCena: %d$\nAby kupić dom, wpisz /kupdom", hid, DomInfo[hid][dCena]);
131 DomInfo[hid][dPickup] = CreateDynamicPickup(19522, 1, DomInfo[hid][dPosX], DomInfo[hid][dPosY], DomInfo[hid][dPosZ]);
132 DomInfo[hid][dLabel] = CreateDynamic3DTextLabel(str, -1, DomInfo[hid][dPosX], DomInfo[hid][dPosY], DomInfo[hid][dPosZ], 40);
133 DomInfo[hid][dMapIcon] = CreateDynamicMapIcon(DomInfo[hid][dPosX], DomInfo[hid][dPosY], DomInfo[hid][dPosZ], 31, -1);
134 return 1;
135}
136
137forward OnInteriorsLoaded(z);
138public OnInteriorsLoaded(z)
139{
140 new rows = cache_num_rows();
141
142 for(new i = 0; i <= rows; i++)
143 {
144 cache_get_value_float(i, "PosX", IntInfo[i][IntPosX]);
145 cache_get_value_float(i, "PosY", IntInfo[i][IntPosY]);
146 cache_get_value_float(i, "PosZ", IntInfo[i][IntPosZ]);
147 cache_get_value_float(i, "PosA", IntInfo[i][IntPosA]);
148 cache_get_value_int(i, "UID", IntInfo[i][IntUID]);
149 cache_get_value_int(i, "InteriorID", IntInfo[i][Int]);
150 cache_get_value_int(i, "Cena", IntInfo[i][IntCena]);
151 cache_get_value(i, "Nazwa", IntInfo[i][IntNazwa], 24);
152
153 IntInfo[i][IntPickup] = CreateDynamicPickup(19523, 1, IntInfo[i][IntPosX], IntInfo[i][IntPosY], IntInfo[i][IntPosZ]);
154 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);
155 }
156 printf("Interiorów: %d", rows);
157 return 1;
158}
159
160forward OnHousesLoaded();
161public OnHousesLoaded()
162{
163 new rows = cache_num_rows();
164
165 for(new i = 0; i <= rows; i++)
166 {
167 cache_get_value_float(i, "PosX", DomInfo[i][dPosX]);
168 cache_get_value_float(i, "PosY", DomInfo[i][dPosY]);
169 cache_get_value_float(i, "PosZ", DomInfo[i][dPosZ]);
170 cache_get_value_float(i, "PosA", DomInfo[i][dPosA]);
171 cache_get_value_int(i, "UID", DomInfo[i][dUID]);
172 cache_get_value_int(i, "Kupiony", DomInfo[i][dKupiony]);
173 cache_get_value_int(i, "Cena", DomInfo[i][dCena]);
174 cache_get_value_int(i, "Interior", DomInfo[i][dInterior]);
175 cache_get_value(i, "Wlasciciel", DomInfo[i][dWlasciciel], MAX_PLAYER_NAME);
176
177 DomInfo[i][dIDUsed] = true;
178
179 if(DomInfo[i][dKupiony])
180 {
181 format(str, sizeof(str), "UID: %d\nWłaściciel: %s\nAby wejść do domu, wpisz /wejdzdom", i, DomInfo[i][dWlasciciel]);
182 DomInfo[i][dPickup] = CreateDynamicPickup(1273, 1, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ]);
183 DomInfo[i][dLabel] = CreateDynamic3DTextLabel(str, -1, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ], 40);
184 DomInfo[i][dMapIcon] = CreateDynamicMapIcon(DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ], 32, -1);
185 }
186 else
187 {
188 format(str, sizeof(str), "UID: %d\nWłaściciel: Brak\nCena: %d$\nAby kupić dom, wpisz /kupdom", i, DomInfo[i][dCena]);
189 DomInfo[i][dPickup] = CreateDynamicPickup(19522, 1, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ]);
190 DomInfo[i][dLabel] = CreateDynamic3DTextLabel(str, -1, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ], 40);
191 DomInfo[i][dMapIcon] = CreateDynamicMapIcon(DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ], 31, -1);
192 }
193 }
194 printf("Domów: %d", rows);
195 return 1;
196}
197
198CMD:stworzdom(playerid, params[])
199{
200 if(IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, -1, "Nie możesz być w pojeździe!");
201
202 new int, cena;
203
204 if(sscanf(params, "dd", int, cena)) return SendClientMessage(playerid, -1, "/stworzdom int cena");
205
206 new Float:X, Float:Y, Float:Z, Float:A;
207
208 GetPlayerPos(playerid, X, Y, Z);
209 GetPlayerFacingAngle(playerid, A);
210
211 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);
212 mysql_tquery(SQL, query, "OnCreateHouse", "dddffff", playerid, int, cena, X, Y, Z, A);
213
214 SendClientMessage(playerid, -1, "Dom został pomyślnie stworzony!");
215
216 return 1;
217}
218
219CMD:wejdzdom(playerid)
220{
221 new hID = PlayerEnterPointHouse(playerid);
222 PlayerInsideHouse(playerid, hID);
223 return 1;
224}
225
226CMD:wyjdzdom(playerid)
227{
228 new hID = PlayerExitPointHouse(playerid);
229 PlayerOutsideHouse(playerid, hID);
230 return 1;
231}
232
233PlayerEnterPointHouse(playerid)
234{
235 for(new i = 0; i < MAX_DOMY; i++)
236 {
237 if(IsPlayerInRangeOfPoint(playerid, 1.0, DomInfo[i][dPosX], DomInfo[i][dPosY], DomInfo[i][dPosZ]))
238 return i;
239 }
240 return MAX_DOMY;
241}
242
243PlayerExitPointHouse(playerid)
244{
245 for(new i = 0; i < MAX_DOMY; i++)
246 {
247 if(IsPlayerInRangeOfPoint(playerid, 1.0, IntInfo[i][IntPosX], IntInfo[i][IntPosY], IntInfo[i][IntPosZ]))
248 return i;
249 }
250 return MAX_DOMY;
251}
252
253forward PlayerInsideHouse(playerid, HouseID);
254public PlayerInsideHouse(playerid, HouseID)
255{
256 if(HouseID == MAX_DOMY) return SendClientMessage(playerid, -1, "Musisz być przy drzwiach domu.");
257
258 new houseinterior = DomInfo[HouseID][dInterior];
259 //new houseinterior = HouseID;
260
261 SetPlayerPos(playerid, IntInfo[houseinterior][IntPosX], IntInfo[houseinterior][IntPosY], IntInfo[houseinterior][IntPosZ]);
262 SetPlayerFacingAngle(playerid, IntInfo[houseinterior][IntPosA]);
263
264 SetPlayerInterior(playerid, IntInfo[houseinterior][Int]);
265
266 SetPlayerVirtualWorld(playerid, 50 + HouseID);
267
268 new dmsg[64];
269 format(dmsg, sizeof(dmsg), "Wszedłeś do domu o ID %d.", HouseID);
270 SendClientMessage(playerid, -1, dmsg);
271
272 return 1;
273}
274
275forward PlayerOutsideHouse(playerid, HouseID);
276public PlayerOutsideHouse(playerid, HouseID)
277{
278 if(HouseID == MAX_DOMY) return SendClientMessage(playerid, -1, "Musisz być przy wyjściu z domu.");
279
280 SetPlayerPos(playerid, DomInfo[HouseID][dPosX], DomInfo[HouseID][dPosY], DomInfo[HouseID][dPosZ]);
281 SetPlayerFacingAngle(playerid, DomInfo[HouseID][dPosA]);
282
283 SetPlayerInterior(playerid, 0);
284 SetPlayerVirtualWorld(playerid, 0);
285
286 new dmsg[64];
287 format(dmsg, sizeof(dmsg), "Wyszedłeś z domu o ID: %d.", HouseID);
288 SendClientMessage(playerid, -1, dmsg);
289
290 return 1;
291}
292
293CMD:reset(playerid)
294{
295 SetPlayerInterior(playerid, 0);
296 SetPlayerVirtualWorld(playerid, 0);
297 SetPlayerHealth(playerid, 0);
298 return 1;
299}
300
301stock SetupInteriorTable()
302{
303 mysql_tquery(SQL, "CREATE TABLE IF NOT EXISTS `interiory` ( \
304 `UID` INT(11) NOT NULL AUTO_INCREMENT, \
305 `PosX` FLOAT NOT NULL, \
306 `PosY` FLOAT NOT NULL, \
307 `PosZ` FLOAT NOT NULL, \
308 `PosA` FLOAT NOT NULL, \
309 `InteriorID` INT(11) NOT NULL DEFAULT '0', \
310 `Nazwa` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_polish_ci', \
311 `Cena` INT(11) NOT NULL DEFAULT '0', \
312 PRIMARY KEY (`UID`))");
313
314 return 1;
315}
316
317stock SetupHouseTable()
318{
319 mysql_tquery(SQL, "CREATE TABLE IF NOT EXISTS `domy` ( \
320 `UID` INT(11) NOT NULL AUTO_INCREMENT, \
321 `PosX` FLOAT NOT NULL, \
322 `PosY` FLOAT NOT NULL, \
323 `PosZ` FLOAT NOT NULL, \
324 `PosA` FLOAT NOT NULL, \
325 `Kupiony` TINYINT(4) NOT NULL DEFAULT '0', \
326 `Wlasciciel` TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_polish_ci', \
327 `Cena` INT(11) NOT NULL DEFAULT '0', \
328 `Interior` INT(11) NOT NULL DEFAULT '0', \
329 PRIMARY KEY (`UID`))");
330
331 return 1;
332}
333
334stock GetNick(playerid)
335{
336 new PlayerNick[MAX_PLAYER_NAME];
337 GetPlayerName(playerid, PlayerNick, sizeof(PlayerNick));
338 return PlayerNick;
339}