-
Notifications
You must be signed in to change notification settings - Fork 5
/
UDataFolding.pas
159 lines (140 loc) · 4.54 KB
/
UDataFolding.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
unit UDataFolding;
{$MODE Delphi}
interface
uses
SysUtils,URLThread;
type
TFoldingDataThread = class(TURLThread)
private
foldLastWU, foldActClientsWeek, foldActClientsFiftyMin,
foldTeamName, foldTeamScore, foldTeamWU, foldTeamLastWU,
foldScore, foldRank, foldWU, foldUser: String;
procedure DoFoldingHTTPUpdate;
protected
procedure DoUpdate; override;
public
constructor Create;
destructor Destroy; override;
procedure ResolveVariables(var Line : string); override;
end;
implementation
uses
UConfig, UUtils, UJSON;
constructor TFoldingDataThread.Create;
begin
inherited Create(60000);
end;
destructor TFoldingDataThread.Destroy;
begin
inherited;
end;
procedure TFoldingDataThread.ResolveVariables(var Line : string);
begin
if (pos('$FOLD', line) <> 0) then
begin
fDataLock.Enter();
try
line := StringReplace(line, '$FOLDlastwu', foldLastWU, [rfReplaceAll]);
line := StringReplace(line, '$FOLDact50min', foldActClientsFiftyMin, [rfReplaceAll]);
line := StringReplace(line, '$FOLDactweek', foldActClientsWeek, [rfReplaceAll]);
line := StringReplace(line, '$FOLDteamname', foldTeamName, [rfReplaceAll]);
line := StringReplace(line, '$FOLDteamscore', foldTeamScore, [rfReplaceAll]);
line := StringReplace(line, '$FOLDteamwu', foldTeamWU, [rfReplaceAll]);
line := StringReplace(line, '$FOLDteamlastwu', foldTeamLastWU, [rfReplaceAll]);
line := StringReplace(line, '$FOLDscore', foldScore, [rfReplaceAll]);
line := StringReplace(line, '$FOLDrank', foldRank, [rfReplaceAll]);
line := StringReplace(line, '$FOLDwu', foldWU, [rfReplaceAll]);
line := StringReplace(line, '$FOLDUser', foldUser, [rfReplaceAll]);
finally
fDataLock.Leave();
end;
end;
end;
procedure TFoldingDataThread.DoFoldingHTTPUpdate;
var
sFilename: String;
JsonStats: myJSONItem;
JsonString: string;
JsonTeam: myJSONItem;
iRank: integer;
i: integer;
begin
try
JsonStats := myJSONItem.Create;
JsonTeam := myJSONItem.Create;
if TryStrToInt(config.foldUserid, i) then
begin
sFilename := getUrl('https://api2.foldingathome.org/uid/'+config.foldUserid, 30)
end
else
Raise Exception.Create('User ID must be a number');
JsonStats.LoadFromFile(sFilename);
fDataLock.Enter();
foldUser := JsonStats.Value[0].getstr; // name
foldScore := JsonStats.Value[2].getstr; // Score
foldWU := JsonStats.Value[3].getstr; // work units
iRank :=4;
if JsonStats.Has['rank'] then // if rank exists
begin
foldRank := JsonStats.Value[iRank].getstr;
iRank := iRank + 1; // for wu that could be 4 or 5 depending on rank being available
end
else
foldRank := 'No rank';
foldActClientsFiftyMin := JsonStats.Value[iRank].getstr; // Clients 50 min
foldActClientsWeek := JsonStats.Value[iRank+1].getstr; // Clients week
foldLastWU := JsonStats.Value[iRank+2].getstr; // last wu
/// Team Stuff
JsonString := JsonStats.Value[iRank+4].getJSON;
delete(JsonString, 1, 1);
JsonString := Copy(JsonString,1,length(JsonString)-1);
JsonTeam.Code := JsonString;
foldTeamName := JsonTeam.Value[1].getstr;
foldTeamScore := JsonTeam.Value[2].getstr;
foldTeamWU := JsonTeam.Value[3].getstr;
foldTeamLastWU := JsonTeam.Value[4].getstr;
fDataLock.Leave();
freeandnil(JsonTeam);
freeandnil(JsonStats);
except
on EExiting do raise;
on E: Exception do
begin
fDataLock.Enter();
try
foldLastWU := '[fold: ' + E.Message + ']';
foldActClientsWeek := '[fold: ' + E.Message + ']';
foldActClientsFiftyMin := '[fold: ' + E.Message + ']';
foldTeamName := '[fold: ' + E.Message + ']';
foldTeamScore := '[fold: ' + E.Message + ']';
foldTeamWU := '[fold: ' + E.Message + ']';
foldTeamLastWU := '[fold: ' + E.Message + ']';
foldScore := '[fold: ' + E.Message + ']';
foldRank := '[fold: ' + E.Message + ']';
foldWU := '[fold: ' + E.Message + ']';
finally
fDataLock.Leave();
end;
end;
end;
end;
procedure TFoldingDataThread.DoUpdate;
var
MyDoUpdate : boolean;
ScreenCount,LineCount : integer;
screenline : string;
begin
MyDoUpdate := false;
for ScreenCount := 1 to MaxScreens do
begin
for LineCount := 1 to config.height do
begin
screenline := config.screen[ScreenCount].line[LineCount].text;
if (pos('$FOLD', screenline) <> 0) then MyDoUpdate := true;
end;
end;
if config.foldEnabled then
if MyDoUpdate then
DoFoldingHTTPUpdate;
end;
end.