This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnosogvts.pas
211 lines (189 loc) · 5.07 KB
/
nosogvts.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
unit nosogvts;
{$mode ObjFPC}{$H+}
INTERFACE
uses
Classes, SysUtils, nosodebug, nosocrypto;
type
TGVT = packed record
number : string[2];
owner : string[32];
Hash : string[64];
control : integer;
end;
Function CreateGVTsFile():boolean;
Function GetGVTsAsStream(out LStream:TMemoryStream):int64;
Function SaveStreamAsGVTs(Const LStream:TMemoryStream):Boolean;
Procedure GetGVTsFileData();
Procedure SaveGVTs();
Function ChangeGVTOwner(Lnumber:integer;OldOwner,NewOWner:String): integer;
Function GetGVTIndex(Index:Integer):TGVT;
Function GetGVTLength:integer;
Function CountAvailableGVTs():Integer;
Function GetGVTPrice(available:integer;ToSell:boolean = false):int64;
CONST
GVTBaseValue = 70000000000;
var
GVTsFilename : string= 'NOSODATA'+DirectorySeparator+'gvts.psk';
FileGVTs : file of TGVT;
ArrGVTs : array of TGVT;
CSGVTsArray : TRTLCriticalSection;
CSGVTsFile : TRTLCriticalSection;
MyGVTsHash : string = '';
IMPLEMENTATION
// Creates a GVTs file. If it already exists, rewrite a new empty one.
Function CreateGVTsFile():boolean;
var
MyStream : TMemoryStream;
Begin
Result := True;
MyStream := TMemoryStream.Create;
EnterCriticalSection(CSGVTsFile);
TRY
MYStream.SaveToFile(GVTsFilename);
EXCEPT ON E:EXCEPTION DO
begin
Result := false;
ToDeepDeb('NosoGVTs,CreateGVTsFile,'+E.Message);
end;
END;
LeaveCriticalSection(CSGVTsFile);
MyStream.Free;
End;
// Loads the GVTs file into a stream
Function GetGVTsAsStream(out LStream:TMemoryStream):int64;
Begin
Result := 0;
EnterCriticalSection(CSGVTsFile);
TRY
LStream.LoadFromFile(GVTsFilename);
result:= LStream.Size;
LStream.Position:=0;
EXCEPT ON E:Exception do
begin
ToDeepDeb('NosoGVTs,GetGVTsAsStream,'+E.Message);
end;
END{Try};
LeaveCriticalSection(CSGVTsFile);
End;
// Save a stream as the GVT file
Function SaveStreamAsGVTs(Const LStream:TMemoryStream):Boolean;
Begin
result := false;
EnterCriticalSection(CSGVTsFile);
TRY
LStream.SaveToFile(GVTsFilename);
Result := true;
EXCEPT ON E:Exception do
begin
ToDeepDeb('NosoGVTs,SaveStreamAsGVTs,'+E.Message);
end;
END{Try};
LeaveCriticalSection(CSGVTsFile);
MyGVTsHash := HashMD5File(GVTsFilename);
End;
Procedure GetGVTsFileData();
var
counter : integer;
Begin
EnterCriticalSection(CSGVTsFile);
EnterCriticalSection(CSGVTsArray);
Assignfile(FileGVTs, GVTsFilename);
TRY
reset(FileGVTs);
Setlength(ArrGVTs,filesize(FileGVTs));
For counter := 0 to filesize(FileGVTs)-1 do
begin
seek(FileGVTs,counter);
read(FileGVTs,ArrGVTs[counter]);
end;
Closefile(FileGVTs);
EXCEPT ON E:Exception do
begin
ToDeepDeb('NosoGVTs,GetGVTsFileData,'+E.Message);
end;
END;
LeaveCriticalSection(CSGVTsArray);
LeaveCriticalSection(CSGVTsFile);
MyGVTsHash := HashMD5File(GVTsFilename);
End;
Procedure SaveGVTs();
var
counter : integer;
Begin
EnterCriticalSection(CSGVTsFile);
EnterCriticalSection(CSGVTsArray);
TRY
rewrite(FileGVTs);
For counter := 0 to length(ArrGVTs)-1 do
begin
seek(FileGVTs,counter);
write(FileGVTs,ArrGVTs[counter]);
end;
Closefile(FileGVTs);
EXCEPT ON E:Exception do
ToDeepDeb('NosoGVTs,SaveGVTs,'+E.Message);
END;
LeaveCriticalSection(CSGVTsArray);
LeaveCriticalSection(CSGVTsFile);
MyGVTsHash := HashMD5File(GVTsFilename);
End;
Function GetGVTIndex(Index:Integer):TGVT;
Begin
result := Default(TGVT);
if index > GetGVTLength-1 then exit;
EnterCriticalSection(CSGVTsArray);
Result := ArrGVTs[index];
LeaveCriticalSection(CSGVTsArray);
End;
Function GetGVTLength:integer;
Begin
EnterCriticalSection(CSGVTsArray);
Result := length(ArrGVTs);
LeaveCriticalSection(CSGVTsArray);
End;
Function ChangeGVTOwner(Lnumber:integer;OldOwner,NewOWner:String): integer;
var
LData : TGVT;
Begin
result := 0;
if LNumber > 99 then result := 1;
LData := GetGVTIndex(Lnumber);
if LData.owner <> OldOwner then result := 2;
if not IsValidHashAddress(NewOWner) then result := 3;
if result = 0 then
begin
EnterCriticalSection(CSGVTsArray);
ArrGVTs[Lnumber].owner := NewOWner;
LeaveCriticalSection(CSGVTsArray);
end;
End;
Function CountAvailableGVTs():Integer;
var
counter : integer;
Begin
Result := 0;
EnterCriticalSection(CSGVTsArray);
for counter := 0 to length(ArrGVTs)-1 do
if ArrGVTs[counter].owner = 'NpryectdevepmentfundsGE' then Inc(Result);
LeaveCriticalSection(CSGVTsArray);
End;
Function GetGVTPrice(available:integer;ToSell:boolean = false):int64;
var
counter : integer;
Begin
result := GVTBaseValue;
available:= 40-available;
for counter := 1 to available do
result := (result *110) div 100;
if result < GVTBaseValue then result := GVTBaseValue;
if ToSell then Result := (result *85) div 100;
End;
INITIALIZATION
InitCriticalSection(CSGVTsArray);
InitCriticalSection(CSGVTsFile);
SetLength(ArrGVTs,0);
Assignfile(FileGVTs,GVTsFilename);
FINALIZATION
DoneCriticalSection(CSGVTsArray);
DoneCriticalSection(CSGVTsFile);
END. // End unit