· 4 years ago · Jun 16, 2021, 06:08 AM
1#define Licenses: Licenses_
2
3#define MAX_LICENSES LICENSE_TYPE_COUNT
4#define MIN_LICENSES LICENSE_TYPE_NONE + 1
5#define MAX_LICENSES_NAME_LENGTH 23 + 1
6
7enum
8{
9 LICENSE_TYPE_NONE = -1,
10 LICENSE_TYPE_DRIVE,
11 LICENSE_TYPE_DRIVE_PRO,
12 LICENSE_TYPE_WEAPON,
13 LICENSE_TYPE_FISHING,
14 LICENSE_TYPE_HUNTING,
15 LICENSE_TYPE_MEDCARD,
16 LICENSE_TYPE_COUNT
17}
18
19enum E_LICENSES_STRUCT
20{
21 E_L_NAME[MAX_LICENSES_NAME_LENGTH],
22 E_L_PRICE
23}
24
25new g_licenses_info[MAX_LICENSES][E_LICENSES_STRUCT] =
26{
27 { "Права (Базовый уровень)", 10_000 }, // LICENSE_TYPE_DRIVE
28 { "Права (Проф. уровень)", 15_000 }, // LICENSE_TYPE_DRIVE_PRO
29 { "Оружия", 30_000 }, // LICENSE_TYPE_WEAPON
30 { "Рыбалка", 120_000 }, // LICENSE_TYPE_FISHING
31 { "Охота", 100_000 }, // LICENSE_TYPE_HUNTING
32 { "Мед. карта", 20_000 } // LICNESE_TYPE_MEDCARD
33};
34
35new g_player_licenses[MAX_PLAYERS][MAX_LICENSES];
36
37stock Licenses:Init()
38{
39 new Cache:cache_id;
40
41 cache_id = mysql_query(mysql, !"SHOW TABLES LIKE 'house_inventory'", true);
42
43 if (!cache_num_rows())
44 {
45 mysql_query(mysql, "\
46 CREATE TABLE IF NOT EXISTS `licenses` ( \
47 `id` int(11) NOT NULL AUTO_INCREMENT, \
48 `account_id` int(11) NOT NULL, \
49 `type` INT(4) NOT NULL,
50 PRIMARY KEY (`id`) \
51 ) ENGINE=InnoDB DEFAULT CHARSET=cp1251 AUTO_INCREMENT=1 ", false);
52
53 mysql_query(mysql, !"\
54 CREATE INDEX \
55 account_id_type \
56 ON \
57 licenses(account_id, type)", false);
58
59 mysql_query(mysql, !"\
60 ALTER TABLE \
61 `licenses`\
62 ADD CONSTRAINT \
63 `licenses_accounts_fk_1` \
64 FOREIGN KEY \
65 (`account_id`) \
66 REFERENCES \
67 `accounts` (`id`) \
68 ON DELETE CASCADE ON UPDATE CASCADE", false);
69 }
70
71 cache_delete(cache_id);
72}
73
74stock Licenses:OnPlayerDisconnect(playerid)
75{
76 Licenses:ResetPlayer(playerid);
77}
78
79stock Licenses:PlayerLogin(playerid)
80{
81 new
82 driving_lic = GetPlayerData(playerid, P_DRIVING_LIC),
83 weapon_lic = GetPlayerData(playerid, P_WEAPON_LIC);
84
85 if (driving_lic >= 1)
86 Licenses:GivePlayer(playerid, LICENSE_TYPE_DRIVE);
87
88 if (driving_lic >= 2)
89 Licenses:GivePlayer(playerid, LICENSE_TYPE_DRIVE_PRO);
90
91 if (weapon_lic)
92 Licenses:GivePlayer(playerid, LICENSE_TYPE_WEAPON);
93
94 if (driving_lic || weapon_lic)
95 {
96 format(FormatData_128, sizeof FormatData_128, "UPDATE accounts SET driving_lic=0, weapon_lic=0 WHERE id=%d", GetPlayerAccountID(playerid));
97 mysql_pquery(mysql, FormatData_128);
98 }
99}
100
101stock Licenses:IsHaveDrivingLic(playerid)
102{
103 return (Licenses:IsPlayerHave(playerid, LICENSE_TYPE_DRIVE) ||
104 Licenses:IsPlayerHave(playerid, LICENSE_TYPE_DRIVE_PRO));
105}
106
107stock Licenses:IsPlayerHave(playerid, type)
108{
109 if (!Licenses:IsValidType(type))
110 return 0;
111
112 return g_player_licenses[playerid][type];
113}
114
115stock Licenses:GivePlayerKit(playerid)
116{
117 for (new i = 0; i < MAX_LICENSES; ++i)
118 {
119 if (i == LICENSE_TYPE_MEDCARD)
120 continue;
121
122 Licenses:GivePlayer(playerid, i);
123 }
124}
125
126stock Licenses:GivePlayer(playerid, type)
127{
128 if (Licenses:IsPlayerHave(playerid, type) || !Licenses:IsValidType(type))
129 return 0;
130
131 g_player_licenses[playerid][type] = 1;
132
133 format(FormatData_128, sizeof FormatData_128, "INSERT INTO licenses SET account_id=%d, type=%d",
134 GetPlayerAccountID(playerid), type);
135 mysql_pquery(mysql, FormatData_128);
136
137 return 1;
138}
139
140stock Licenses:RemovePlayer(playerid, type)
141{
142 if (!Licenses:IsPlayerHave(playerid, type))
143 return 0;
144
145 g_player_licenses[playerid][type] = 0;
146
147 format(FormatData_128, sizeof FormatData_128, "DELETE FROM licenses WHERE account_id=%d AND type=%d",
148 GetPlayerAccountID(playerid), type);
149 mysql_pquery(mysql, FormatData_128);
150
151 return 1;
152}
153
154stock Licenses:SetPlayerValue(playerid, type, value)
155{
156 if (!Licenses:IsValidType(type) || !(0 <= value <= 1))
157 return 0;
158
159 g_player_licenses[playerid][type] = value;
160
161 return 1;
162}
163
164stock Licenses:ResetPlayer(playerid)
165{
166 for (new i = 0; i < MAX_LICENSES; ++i)
167 {
168 g_player_licenses[playerid][i] = 0;
169 }
170}
171
172stock Licenses:GetName(type, output_string[], const size = sizeof output_string)
173{
174 if (!Licenses:IsValidType(type))
175 return 0;
176
177 output_string[0] = EOS;
178
179 strcat(output_string, g_licenses_info[type][E_L_NAME], size);
180
181 return 1;
182}
183
184stock Licenses:GetPrice(type)
185{
186 if (!Licenses:IsValidType(type))
187 return 0;
188
189 return g_licenses_info[type][E_L_PRICE];
190}
191
192stock Licenses:IsValidType(type)
193{
194 return MIN_LICENSES <= type <= MAX_LICENSES - 1;
195}