SHOW:
|
|
- or go back to the newest paste.
1 | unit Unit1; | |
2 | ||
3 | {$mode objfpc}{$H+} | |
4 | ||
5 | interface | |
6 | ||
7 | uses | |
8 | Classes, SysUtils, db, LResources, mysql50conn, mysql51conn, sqldb, FileUtil, | |
9 | Forms, Controls, Graphics, Dialogs, StdCtrls, Unit2, Process, INIFiles; | |
10 | ||
11 | type | |
12 | ||
13 | { TForm1 } | |
14 | ||
15 | TForm1 = class(TForm) | |
16 | Button1: TButton; | |
17 | ButtonAccept: TButton; | |
18 | Datasource1: TDatasource; | |
19 | Login: TEdit; | |
20 | Password: TEdit; | |
21 | conn: TMySQL51Connection; | |
22 | query: TSQLQuery; | |
23 | transaction: TSQLTransaction; | |
24 | procedure Button1Click(Sender: TObject); | |
25 | ||
26 | //procedure ButtonAcceptClick(Sender: TObject); | |
27 | private | |
28 | { private declarations } | |
29 | ||
30 | public | |
31 | { public declarations } | |
32 | end; | |
33 | ||
34 | var | |
35 | Form1: TForm1; | |
36 | balance:Integer; | |
37 | XCow: TProcess; | |
38 | s: String; | |
39 | MyLogin: Integer; | |
40 | ||
41 | implementation | |
42 | ||
43 | {$R *.lfm} | |
44 | ||
45 | { TForm1 } | |
46 | ||
47 | procedure CallCow(); | |
48 | begin | |
49 | XCow := TProcess.Create(nil); | |
50 | XCow.CommandLine := 'xcowsay -t5 --at=1600,566 ' + s; | |
51 | XCow.Execute; | |
52 | XCow.Free; | |
53 | s:=''; | |
54 | end; | |
55 | ||
56 | procedure ReadIni(); | |
57 | begin | |
58 | filename:='/etc/blocker/settings.ini'; | |
59 | IF(FileExists(filename))then | |
60 | begin | |
61 | IniF := TINIFile.Create(filename); | |
62 | srvName:=INiF.ReadString('connection','srvName',''); | |
63 | dbName:=INiF.ReadString('connection','dbName',''); | |
64 | userName:=INiF.ReadString('connection','userName',''); | |
65 | pass:=INiF.ReadString('connection','pass',''); | |
66 | adminName:=INiF.ReadString('admin','adminName',''); | |
67 | adminPass:=INiF.ReadString('admin','adminPass',''); | |
68 | End else ShowMessage('Файл настроек не найден...'); | |
69 | end; | |
70 | ||
71 | procedure StartWorkWithDB(); | |
72 | ||
73 | begin | |
74 | Form1.conn := TMySQL51Connection.Create(Nil); | |
75 | Form1.query := TSQLQuery.Create(nil); | |
76 | Form1.transaction := TSQLTransaction.Create(nil); | |
77 | Form1.conn.HostName:=srvName; | |
78 | Form1.conn.UserName:=userName; | |
79 | Form1.conn.Password:=pass; | |
80 | Form1.conn.DatabaseName:=dbName; | |
81 | Form1.conn.Connected:=true; | |
82 | Form1.conn.Transaction:=Form1.transaction; | |
83 | Form1.query.DataBase:=Form1.conn; | |
84 | end; | |
85 | ||
86 | procedure BDWorkUD(); | |
87 | begin | |
88 | if (Form1.conn.Connected=False) then Form1.conn.Connected:=True; | |
89 | if (Form1.transaction.Active=False) then Form1.transaction.Active:=True; | |
90 | Form1.query.ExecSQL; | |
91 | end; | |
92 | ||
93 | procedure BDWork(); | |
94 | begin | |
95 | Form1.conn.Open; | |
96 | Form1.query.Open; | |
97 | end; | |
98 | ||
99 | procedure EndWorkWithDB(); | |
100 | begin | |
101 | Form1.query.Close; | |
102 | Form1.conn.Close; | |
103 | Form1.query.Free; | |
104 | Form1.transaction.Free; | |
105 | Form1.conn.Free; | |
106 | end; | |
107 | ||
108 | procedure EndWorkWithDBUD(); | |
109 | ||
110 | begin | |
111 | Form1.query.Close; | |
112 | Form1.transaction.CommitRetaining; | |
113 | end; | |
114 | ||
115 | function UserExist:Boolean; | |
116 | var | |
117 | b: Boolean; | |
118 | begin | |
119 | b := False; | |
120 | try | |
121 | MyLogin := StrToInt(AnsiToUtf8(Form1.Login.Text)); | |
122 | Except | |
123 | On E : EConvertError do | |
124 | begin | |
125 | s:='Вы что-то ввели неверно...'; | |
126 | CallCow(); | |
127 | Result := b; | |
128 | end; | |
129 | end; | |
130 | StartWorkWithDB(); | |
131 | Form1.query.SQL.Text:='select case when exists (select id, pass from users where (id = :id) and pass like :password) then 1 else 0 end AS result;'; | |
132 | Form1.query.ParamByName('id').AsInteger:= MyLogin; | |
133 | Form1.query.ParamByName('password').AsString:=Form1.Password.Text; | |
134 | BDWork(); | |
135 | b := Form1.query.FieldByName('result').AsBoolean; | |
136 | EndWorkWithDB(); | |
137 | Result := b; | |
138 | end; | |
139 | ||
140 | procedure TimeBalance(); | |
141 | begin | |
142 | StartWorkWithDB(); | |
143 | Form1.query.SQL.Text:='select time_balance from time_used where user_id = :id;'; | |
144 | Form1.query.ParamByName('id').AsInteger:=MyLogin; | |
145 | BDWorkUD(); | |
146 | balance := Form1.query.FieldByName('balance').AsInteger; | |
147 | EndWorkWithDB(); | |
148 | end; | |
149 | ||
150 | procedure BalanceMinus(); | |
151 | begin | |
152 | balance:=balance - 1; | |
153 | StartWorkWithDB(); | |
154 | Form1.query.SQL.Text:='UPDATE time_used SET time_balance = :balance WHERE user_id = :id;'; | |
155 | Form1.query.ParamByName('id').AsInteger:=MyLogin; | |
156 | Form1.query.ParamByName('balance').AsInteger:=balance; | |
157 | BDWorkUD(); | |
158 | Form1.query.ExecSQL; | |
159 | EndWorkWithDBUD(); | |
160 | end; | |
161 | ||
162 | procedure UpdateUserBalance(); | |
163 | begin | |
164 | StartWorkWithDB(); | |
165 | Form1.query.SQL.Text:='UPDATE time_used SET time_balance = (SELECT norm FROM restrictions WHERE id = (SELECT age FROM user_age WHERE user_id = :id))WHERE user_id = :id;'; | |
166 | Form1.query.ParamByName('id').AsInteger:=MyLogin; | |
167 | BDWorkUD(); | |
168 | Form1.query.ExecSQL; | |
169 | EndWorkWithDBUD(); | |
170 | end; | |
171 | ||
172 | function IsBurthday:Boolean; | |
173 | var | |
174 | birthday: Boolean; | |
175 | begin | |
176 | birthday:=False; | |
177 | StartWorkWithDB(); | |
178 | Form1.query.SQL.Text:='select (select date_format((current_date),''%m''))-(select date_format((select birthday from users where id = :id),''%m''))+(select date_format((current_date),''%d''))-(select date_format((select birthday from users where id = :id),''%d'')) as bday;'; | |
179 | Form1.query.ParamByName('id').AsInteger:=MyLogin; | |
180 | BDWork(); | |
181 | birthday := Form1.query.FieldByName('bday').AsBoolean; | |
182 | EndWorkWithDB(); | |
183 | Result := birthday; | |
184 | end; | |
185 | ||
186 | procedure AgeUp(); | |
187 | begin | |
188 | StartWorkWithDB(); | |
189 | - | Form1.query.SQL.Text:='UPDATE user_age SET age = ((select age where user_id = :id)+1) where user_id = :id;'; |
189 | + | Form1.query.SQL.Text:='UPDATE user_age SET age = ((select age where user_id = @id)+1) where user_id = :id;'; |
190 | Form1.query.ParamByName('id').AsInteger:=MyLogin; | |
191 | BDWorkUD(); | |
192 | Form1.query.ExecSQL; | |
193 | EndWorkWithDBUD(); | |
194 | end; | |
195 | ||
196 | function IsBanned:Boolean; | |
197 | var | |
198 | tmpIntBan: Integer; | |
199 | begin | |
200 | StartWorkWithDB(); | |
201 | Form1.query.SQL.Text:='select ban from users where id = :id;'; | |
202 | Form1.query.ParamByName('id').AsInteger:=MyLogin; | |
203 | BDWork(); | |
204 | tmpIntBan := Form1.query.FieldByName('ban').AsInteger; | |
205 | EndWorkWithDB(); | |
206 | if (tmpIntBan = 1) then Result := True | |
207 | else Result := False; | |
208 | end; | |
209 | ||
210 | function LastLoginNotToday:boolean; | |
211 | var | |
212 | tmpInt: Integer; | |
213 | begin | |
214 | StartWorkWithDB(); | |
215 | Form1.query.SQL.Text:='select (select date_format((current_date),"%m"))-(select date_format((select time_start from time_used where user_id = :id),"%m"))+(select date_format((current_date),"%d"))-(select date_format((select time_start from time_used where user_id = :id),"%d")) as yn;'; | |
216 | Form1.query.ParamByName('id').AsInteger:=MyLogin; | |
217 | BDWork(); | |
218 | tmpInt:= Form1.query.FieldByName('yn').AsInteger; | |
219 | EndWorkWithDB(); | |
220 | if (tmpInt > 0 ) then Result := True | |
221 | else | |
222 | Result := False; | |
223 | end; | |
224 | ||
225 | procedure iLogin(); | |
226 | begin | |
227 | balance:=0; | |
228 | if (UserExist = True) | |
229 | then begin | |
230 | if (IsBanned = False) | |
231 | then begin | |
232 | s:='Привет!'; | |
233 | CallCow(); | |
234 | if (LastLoginNotToday = True) | |
235 | then | |
236 | begin | |
237 | //TODO Пофиксить багу с неизменением нормы, если человек | |
238 | //не пришёл в свой день рождения | |
239 | if (IsBurthday = True) | |
240 | then | |
241 | begin | |
242 | AgeUp(); | |
243 | s:='С Днём Рождения!'; | |
244 | CallCow(); | |
245 | end; | |
246 | UpdateUserBalance(); | |
247 | end; | |
248 | end | |
249 | //TODO Здесь нужно будет вызывать xcowsay и говорить, что забанен | |
250 | else | |
251 | s:='Простите, но мне сказали, что Вы нарушали правила!'; | |
252 | CallCow(); | |
253 | end | |
254 | //TODO Здесь нужно сказать, что нет такого пользователя | |
255 | else | |
256 | s:='Я не могу найти такого пользователя. Наверное, Вы ошиблись!'; | |
257 | CallCow(); | |
258 | ||
259 | end; | |
260 | ||
261 | ||
262 | procedure TForm1.Button1Click(Sender: TObject); | |
263 | var | |
264 | a, b : string; | |
265 | begin | |
266 | ReadIni(); | |
267 | a := Form1.Login.Text; | |
268 | b := Form1.Password.Text; | |
269 | if (((a) = (adminName)) and ((b) = (adminPass))) then | |
270 | begin | |
271 | s := 'Привет, Админ! Оставляю комп тебе и отключаюсь ;-)'; | |
272 | CallCow(); | |
273 | Form1.Close; | |
274 | end | |
275 | else | |
276 | iLogin(); | |
277 | end; | |
278 | ||
279 | end. |