Advertisement
Guest User

Untitled

a guest
Jul 27th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. const
  2. _FOLDER_DATABASE = '';
  3. _NAME_DATABASE = 'database.txt';
  4.  
  5. var
  6. Exception, Today: string;
  7. database : array of string;
  8.  
  9. procedure OnErrorOccur(const ERROR_MSG: string);
  10. begin
  11. Exception:= ERROR_MSG;
  12. WriteLn('MySSQL: ' + Exception);
  13. end;
  14.  
  15. function IXSplit(const SOURCE: string; Delimiter: string): array of string;
  16. var
  17. i, x, d: integer;
  18. s, b: string;
  19. begin
  20. d:= length(Delimiter);
  21. i:= 1;
  22. SetArrayLength(Result, 0);
  23. while (i <= length(SOURCE)) do
  24. begin
  25. s:= Copy(SOURCE, i, d);
  26. if (s = Delimiter) then
  27. begin
  28. SetArrayLength(Result, x + 1);
  29. Result[x]:= b;
  30. Inc(i, d);
  31. Inc(x, 1);
  32. b:= '';
  33. end else
  34. begin
  35. b:= b + Copy(s, 1, 1);
  36. Inc(i, 1);
  37. end;
  38. end;
  39. if (b <> '') then
  40. begin
  41. SetArrayLength(Result, x + 1);
  42. Result[x]:= b;
  43. end;
  44. end;
  45.  
  46. function ReadFromFile(File: string): string;
  47. begin
  48. Result:= ReadFile(File);
  49. Result:= Copy(Result, 0, length(Result) - 2);
  50. end;
  51.  
  52. function DoesFileExist(Name: string): boolean;
  53. begin
  54. if (GetSystem() = 'windows') then
  55. begin
  56. if (FileExists(Name)) then
  57. begin
  58. result:= true;
  59. end;
  60. end else
  61. begin
  62. if ((FileExists(Name)) or (ReadFromFile(Name) <> '')) then
  63. begin
  64. result:= true;
  65. end;
  66. end;
  67. end;
  68.  
  69. procedure _LoadDatabase();
  70. begin
  71. if (DoesFileExist(_FOLDER_DATABASE + _NAME_DATABASE)) then
  72. begin
  73. database:= IXSplit(ReadFromFile(_FOLDER_DATABASE + _NAME_DATABASE), #13#10);
  74. end else
  75. begin
  76. WriteFile(_FOLDER_DATABASE + _NAME_DATABASE, '');
  77. end;
  78. end;
  79.  
  80. procedure _SnapDatabase();
  81. var
  82. i: integer;
  83. b: string;
  84. begin
  85. for i:= 0 to GetArrayLength(database) - 1 do
  86. begin
  87. if (b <> '') then
  88. begin
  89. b:= b + #13#10 + database[i];
  90. end else
  91. begin
  92. b:= database[i];
  93. end;
  94. end;
  95. WriteFile(_FOLDER_DATABASE + _NAME_DATABASE, b);
  96. end;
  97.  
  98. function _RowExists(RowID: integer): boolean;
  99. begin
  100. result:= ArrayHigh(database) >= RowID;
  101. end;
  102.  
  103. function _getColumnInfo(RowID, ColumnID: integer): integer;
  104. var
  105. ch, x, tabs: integer;
  106. b: string;
  107. begin
  108. tabs:= -1;
  109. b:= database[RowID];
  110. while (tabs <> ColumnID) do
  111. begin
  112. x:= StrPos(#9, b);
  113. if ((x = 0) and (tabs <> ColumnID)) then
  114. begin
  115. exit;
  116. end;
  117. Inc(tabs, 1);
  118. if (tabs = ColumnID) then
  119. begin
  120. result:= ch + 1;
  121. break;
  122. end else
  123. begin
  124. ch:= ch + x;
  125. Delete(b, 1, x);
  126. end;
  127. end;
  128. end;
  129.  
  130. function GetTypeOF(Value: variant): string;
  131. begin
  132. case VarType(Value) of
  133. 3 : result:= IntToStr(Value);
  134. 5 : result:= FloatToStr(Value);
  135. 11 : result:= iif(Value, 'true', 'false');
  136. 256: result:= Value;
  137. else result:= 'unknown Type';
  138. end;
  139. end;
  140.  
  141. procedure _CreateRow(Columns: array of variant);
  142. var
  143. i, x: integer;
  144. begin
  145. SetArrayLength(database, GetArrayLength(database) + 1);
  146. x:= GetArrayLength(database) - 1;
  147. for i:= 0 to GetArrayLength(Columns) - 1 do
  148. begin
  149. database[x]:= database[x] + GetTypeOF(Columns[i]) + #9;
  150. end;
  151. _SnapDatabase();
  152. end;
  153.  
  154. function _DeleteRow(RowID: integer): boolean;
  155. var
  156. HIndex: integer;
  157. begin
  158. if (_RowExists(RowID)) then
  159. begin
  160. HIndex:= GetArrayLength(database) - 1;
  161. if (RowID <> HIndex) then
  162. begin
  163. database[RowID]:= database[HIndex];
  164. end;
  165. SetArrayLength(database, iif(HIndex > 0, HIndex - 1, 0));
  166. _SnapDatabase();
  167. result:= true;
  168. end else
  169. begin
  170. OnErrorOccur('RowID ' + IntToStr(RowID) + ' does not exist');
  171. end;
  172. end;
  173.  
  174. function _UpdateColumn(RowID, ColumnID: integer; Increase: extended): boolean;
  175. var
  176. data, Sum: string;
  177. pos: integer;
  178. begin
  179. if (_RowExists(RowID)) then
  180. begin
  181. pos:= _getColumnInfo(RowID, ColumnID);
  182. if (pos > 0) then
  183. begin
  184. data:= GetPiece(database[RowID], #9, ColumnID);
  185. if (RegExpMatch('^-?(\d+|\d+.?\d+)$', data)) then
  186. begin
  187. Sum:= FloatToStr(StrToFloat(data) + Increase);
  188. Delete(database[RowID], pos, length(data));
  189. Insert(Sum, database[RowID], pos);
  190. result:= true;
  191. end else
  192. begin
  193. OnErrorOccur('Column "' + IntToStr(ColumnID) + '" represents no numeric value');
  194. end;
  195. end else
  196. begin
  197. OnErrorOccur('ColumnID ' + IntToStr(ColumnID) + ' does not exist');
  198. end;
  199. end else
  200. begin
  201. OnErrorOccur('RowID ' + IntToStr(RowID) + ' does not exist');
  202. end;
  203. end;
  204.  
  205. function _SetColumn(RowID, ColumnID: integer; Value: variant): boolean;
  206. var
  207. pos: integer;
  208. data: string;
  209. begin
  210. if (_RowExists(RowID)) then
  211. begin
  212. pos:= _getColumnInfo(RowID, ColumnID);
  213. if (pos > 0) then
  214. begin
  215. data:= GetPiece(database[RowID], #9, ColumnID);
  216. Delete(database[RowID], pos, length(data));
  217. Insert(GetTypeOF(Value), database[RowID], pos);
  218. result:= true;
  219. end else
  220. begin
  221. OnErrorOccur('ColumnID ' + IntToStr(ColumnID) + ' does not exist');
  222. end;
  223. end else
  224. begin
  225. OnErrorOccur('RowID ' + IntToStr(RowID) + ' does not exist');
  226. end;
  227. end;
  228.  
  229. function _AppendColumn(RowID: integer; Value: variant): boolean;
  230. begin
  231. if (_RowExists(RowID)) then
  232. begin
  233. database[RowID]:= database[RowID] + GetTypeOF(Value) + #9;
  234. result:= true;
  235. end else
  236. begin
  237. OnErrorOccur('RowID ' + IntToStr(RowID) + ' does not exist');
  238. end;
  239. end;
  240.  
  241. function FillWith(const Filler: char; Amount: integer): string;
  242. var
  243. i: integer;
  244. begin
  245. for i:= 1 to Amount do
  246. begin
  247. Result:= Result + Filler;
  248. end;
  249. end;
  250.  
  251. procedure CreateBox(ID: byte; const Headline: string; const BorderStyleX, BorderStyleY, CornerStyle: char; const Content: array of string; BorderColor: longint);
  252. var
  253. i, MaxSize, len_Headline: integer;
  254. begin
  255. len_Headline:= length(Headline);
  256. MaxSize:= len_HeadLine;
  257. for i:= 0 to ArrayHigh(Content) do
  258. begin
  259. if (length(Content[i]) > MaxSize) then
  260. begin
  261. MaxSize:= length(Content[i]);
  262. end;
  263. end;
  264. if ((MaxSize - len_Headline) MOD 2 = 1) then
  265. begin
  266. Inc(MaxSize, 1);
  267. end;
  268. WriteConsole(ID, CornerStyle + FillWith(BorderStyleX, (MaxSize - len_Headline) div 2) + Headline + FillWith(BorderStyleX, (MaxSize - len_Headline) div 2) + CornerStyle, BorderColor);
  269. for i:= 0 to GetArrayLength(Content) - 1 do
  270. begin
  271. WriteConsole(ID, BorderStyleY + Content[i] + FillWith(' ', MaxSize - length(Content[i])) + BorderStyleY, BorderColor - ((i + 1) * 25));
  272. end;
  273. WriteConsole(ID, CornerStyle + FillWith(BorderStyleX, MaxSize) + CornerStyle, BorderColor - ((i + 1) * 25));
  274. end;
  275.  
  276.  
  277. procedure ActivateServer();
  278. var
  279. i: integer;
  280. begin
  281. _LoadDatabase();
  282. if (GetArrayLength(database) - 1 < 0) then
  283. begin
  284. _CreateRow(['Today', 0]);
  285. _CreateRow(['Week', FormatDate('dddd'), 0]);
  286. _CreateRow(['Total', 0]);
  287. _LoadDatabase();
  288. end;
  289. Today:= FormatDate('dddd');
  290. end;
  291.  
  292. procedure OnDateCheck();
  293. begin
  294. if (Today <> FormatDate('dddd')) then
  295. begin
  296. _SetColumn(0, 1, 0);
  297. Today:= FormatDate('dddd');
  298. if (Today = GetPiece(database[1], #9, 1)) then
  299. begin
  300. _SetColumn(1, 2, 0);
  301. end;
  302. end;
  303. _SnapDatabase();
  304. end;
  305.  
  306. procedure AppOnIdle(Ticks: integer);
  307. var
  308. Visits: array [0..2] of string;
  309. begin
  310. if (Ticks mod (3600 * 5) = 0) then
  311. begin
  312. OnDateCheck();
  313. Visits[0]:= 'Today : ' + GetPiece(database[0], #9, 1);
  314. Visits[1]:= 'This week: ' + GetPiece(database[1], #9, 2);
  315. Visits[2]:= 'Over-all : ' + GetPiece(database[2], #9, 1);
  316. CreateBox(0, '_ Server Visits _', '_', '|', '.', Visits, $23DBDB)
  317. end;
  318. end;
  319.  
  320. procedure OnJoinGame(ID, Team: byte);
  321. begin
  322. _UpdateColumn(0, 1, 1);
  323. _UpdateColumn(1, 2, 1);
  324. _UpdateColumn(2, 1, 1);
  325. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement