forked from VE3NEA/MorseRunner
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathArrlFd.pas
624 lines (514 loc) · 16.3 KB
/
ArrlFd.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
unit ARRLFD;
{$ifdef FPC}
{$MODE Delphi}
{$endif}
interface
uses
Generics.Defaults, Generics.Collections, Contest, Station, DxStn, Log;
type
TFdCallRec = class
private
function GetTxCnt : integer;
public
Call: string; // call sign
StnClass: string; // station classification (e.g. 3A)
Section: string; // ARRL/RAC section (e.g. OR)
UserText: string; // club name
function GetString: string; // returns 3A OR [club name]
class function compareCall(const left, right: TFdCallRec) : integer; static;
property TxCnt : integer read GetTxCnt;
end;
TPendingCall = record
TxCnt: UInt16; // transmitter count (sorting in descending order)
Index: UInt16; // index into original slst
constructor Create(txcnt : Integer; inx : Integer);
class function comparePendingCall(const left, right : TPendingCall) : integer; static;
end;
{ Holds stations for a given club name, sort by TxCnt }
TPendingClubCalls = class(TList<TPendingCall>)
private
Sorted: Boolean;
public
constructor Create(PendingCallComparer : IComparer<TPendingCall>);
destructor Destroy; override;
procedure AddCall(I: Integer; const rec: TFdCallRec);
function PickPendingCallIdx : Integer;
end;
TPendingStations = class(TObjectDictionary<String, TPendingClubCalls>)
PendingCallComparer: IComparer<TPendingCall>;
constructor Create;
destructor Destroy; override;
procedure AddPendingCall(const ClubName : String; const rec: TFdCallRec; I: Integer);
end;
TArrlFieldDay = class(TContest)
private
FdCallList: TObjectList<TFdCallRec>;
Comparer: IComparer<TFdCallRec>;
public
constructor Create;
destructor Destroy; override;
function LoadCallHistory(const AUserCallsign : string) : boolean; override;
function PickStation(): integer; override;
procedure DropStation(id : integer); override;
function GetCall(id : integer): string; override; // returns station callsign
procedure GetExchange(id : integer; out station : TDxStation); override;
function getExch1(id:integer): string; // returns station info (e.g. 3A)
function getExch2(id:integer): string; // returns section info (e.g. OR)
function getClass(id:integer): string; // returns station class (e.g. 3A)
function getSection(id:integer): string; // returns section (e.g. OR)
function getUserText(id:integer): string; // returns optional club name
function FindCallRec(out fdrec: TFdCallRec; const ACall: string): Boolean;
procedure SendMsg(const AStn: TStation; const AMsg: TStationMessage); override;
function GetStationInfo(const ACallsign: string) : string; override;
function ExtractMultiplier(Qso: PQso) : string; override;
//{$define DISTRIBUTION_REPORT}
{$ifdef DISTRIBUTION_REPORT}
function GetDistributionReport : string;
{$endif}
end;
implementation
uses
SysUtils, Classes,
Ini,
System.Generics.Collections,
{$ifdef DISTRIBUTION_REPORT}
Dialogs, // for ShowMessage
Vcl.Clipbrd, // for TClipBoard
{$endif}
DXCC;
var
IntegerComparer: IComparer<Integer>;
{ TPendingCall }
constructor TPendingCall.Create(txcnt : Integer; inx : Integer);
begin
Self.TxCnt := UInt16(txcnt);
Self.Index := UInt16(inx);
end;
class function TPendingCall.comparePendingCall(const left, right : TPendingCall) : integer;
begin
Result := IntegerComparer.Compare(right.TxCnt, left.TxCnt);
end;
{ TPendingClubCalls }
constructor TPendingClubCalls.Create(PendingCallComparer : IComparer<TPendingCall>);
begin
inherited Create(PendingCallComparer);
Sorted := False;
end;
destructor TPendingClubCalls.Destroy;
begin
end;
procedure TPendingClubCalls.AddCall(I: Integer; const rec: TFdCallRec);
begin
Self.Add(TPendingCall.Create(rec.TxCnt, I));
Sorted := False;
end;
function TPendingClubCalls.PickPendingCallIdx : Integer;
const
BeginIdx : Integer = 0;
var
EndIdx, Idx : Integer;
Item : TPendingCall;
begin
// sort PendingCall record by decreasing TxCnt
if not Sorted then
begin
Sort;
Sorted := True;
end;
if First.TxCnt = Last.TxCnt then
EndIdx := Count
else begin
Item.TxCnt := First.TxCnt;
EndIdx := LastIndexOf(Item)+1;
end;
if (EndIdx - BeginIdx) > 1 then
Idx := BeginIdx + Random(EndIdx - BeginIdx)
else
Idx := BeginIdx;
Result := Items[Idx].Index;
end;
{ TPendingStations }
constructor TPendingStations.Create;
begin
inherited Create([doOwnsValues]);
PendingCallComparer := TComparer<TPendingCall>.Construct(TPendingCall.comparePendingCall);
end;
destructor TPendingStations.Destroy;
begin
inherited Destroy;
end;
procedure TPendingStations.AddPendingCall(
const ClubName : String;
const rec: TFdCallRec;
I: Integer);
var
pending: TPendingClubCalls;
begin
if not TryGetValue(ClubName, pending) then begin
pending := TPendingClubCalls.Create(Self.PendingCallComparer);
Add(ClubName, pending);
end;
pending.AddCall(I, rec);
pending := nil;
end;
{ TArrlFieldDay }
function TArrlFieldDay.LoadCallHistory(const AUserCallsign : string) : boolean;
const
DelimitChar: char = ',';
var
slst, tl: TStringList;
i: integer;
S : String;
ClubNames : TDictionary<String, TFdCallRec>;
PendingStations: TPendingStations;
PendingCalls : TPendingClubCalls;
Pair : TPair<String, TPendingClubCalls>;
HomeStn, PortableStn : Boolean;
rec: TFdCallRec;
existing: TFdCallRec;
begin
// reload call history if empty
Result := FdCallList.Count <> 0;
if Result then
Exit;
slst:= TStringList.Create;
tl:= TStringList.Create;
ClubNames := TDictionary<String, TFdCallRec>.Create;
PendingStations := TPendingStations.Create;
tl.Delimiter := DelimitChar;
tl.StrictDelimiter := True;
rec := nil;
existing := nil;
try
FdCallList.Clear;
slst.LoadFromFile(ParamStr(1) + 'FDGOTA.TXT');
// Pass 1 - find and process all club stations (class A, C or F).
// - deffer all home/portable stations w/ a club name to Pass 2.
for i:= 0 to slst.Count-1 do
begin
if (slst.Strings[i].StartsWith('!!Order!!')) then continue;
if (slst.Strings[i].StartsWith('#')) then continue;
tl.DelimitedText := slst.Strings[i];
if (tl.Count > 2) then
begin
if rec = nil then
rec := TFdCallRec.Create;
rec.Call := UpperCase(tl.Strings[0]);
rec.StnClass := UpperCase(tl.Strings[1]);
rec.Section := UpperCase(tl.Strings[2]);
rec.UserText := '';
if (tl.Count >= 4) then rec.UserText := Trim(tl.Strings[3]);
if rec.Call='' then continue;
if rec.StnClass='' then continue;
if rec.Section='' then continue;
// In 2020, Covid resulted in FD rule changes allowing home stations
// to be used and contribute to the operator's club score. This
// resulted in an above average number of home stations since these
// stations were connected to the clubs score.
//
// Skip home (D/E) and portable (B) stations with a club name by
// assuming they are associated with a club.
S := rec.StnClass.Substring(rec.StnClass.Length-1);
HomeStn := S.Equals('D') or S.Equals('E');
PortableStn := S.Equals('B');
if HomeStn or PortableStn then
begin
if rec.UserText.IsEmpty then
begin
if Random < 0.25 then
begin
// home/portable stations w/o club name can be added
FdCallList.Add(rec);
rec := nil;
end;
continue
end
else
begin
// retain this call and club name to see if it is part of a club.
PendingStations.AddPendingCall(rec.UserText, rec, I);
end;
continue;
end
else
begin
// this is a club station (A, C, or F)
if not rec.UserText.IsEmpty then
begin
// have we seen this club name before?
if ClubNames.TryGetValue(rec.UserText, existing) then
begin
// keep the station with the higher transmitter count
if rec.TxCnt > existing.TxCnt then
begin
FdCallList.Delete(FdCallList.IndexOf(existing));
existing := nil;
FdCallList.Add(rec);
ClubNames.Items[rec.UserText] := rec;
rec := nil;
end;
continue;
end;
ClubNames.Add(rec.UserText, rec);
end;
FdCallList.Add(rec);
rec := nil;
end;
end;
end;
// Pass 2 - add any portable/home (B, D, E) stations not associated
// with a named club station from pass 1 above.
for Pair in PendingStations do
begin
// does this group of home/portable stations have an associated club name?
if ClubNames.TryGetValue(Pair.Key, existing) then
continue;
// this group of pending stations is not associated with a club.
// pick a call from this group.
i := Pair.Value.PickPendingCallIdx;
tl.DelimitedText := slst.Strings[i];
if (tl.Count > 2) then
begin
if rec = nil then
rec := TFdCallRec.Create;
rec.Call := UpperCase(tl.Strings[0]);
rec.StnClass := UpperCase(tl.Strings[1]);
rec.Section := UpperCase(tl.Strings[2]);
rec.UserText := '';
if (tl.Count >= 4) then rec.UserText := Trim(tl.Strings[3]);
if rec.Call='' then continue;
if rec.StnClass='' then continue;
if rec.Section='' then continue;
assert(Pair.Key.Equals(rec.UserText));
// keep all portable stations and 25% of the home stations
if rec.StnClass.EndsWith('B') or (Random < 0.25) then
begin
FdCallList.Add(rec);
ClubNames.Add(rec.UserText, rec);
rec := nil;
end;
end;
end; // end for pair in PendingStations
FdCallList.Sort;
{$ifdef DISTRIBUTION_REPORT}
S := GetDistributionReport;
ShowMessage(S);
ClipBoard.AsText := S;
{$endif}
Result := True;
finally
slst.Free;
tl.Free;
ClubNames.Free;
PendingStations.Free;
if rec <> nil then rec.Free;
end;
end;
constructor TArrlFieldDay.Create;
begin
inherited Create;
Comparer := TComparer<TFdCallRec>.Construct(TFdCallRec.compareCall);
FdCallList:= TObjectList<TFdCallRec>.Create(Comparer);
end;
destructor TArrlFieldDay.Destroy;
begin
FreeAndNil(FdCallList);
inherited;
end;
function TArrlFieldDay.PickStation(): integer;
begin
result := random(FdCallList.Count);
end;
procedure TArrlFieldDay.DropStation(id : integer);
begin
assert(id < FdCallList.Count);
FdCallList.Delete(id);
end;
function TArrlFieldDay.FindCallRec(out fdrec: TFdCallRec; const ACall: string): Boolean;
var
rec: TFdCallRec;
{$ifdef FPC}
index: int64;
{$else}
index: integer;
{$endif}
begin
rec := TFdCallRec.Create();
rec.Call := ACall;
fdrec:= nil;
try
if FdCallList.BinarySearch(rec, index, Comparer) then
fdrec:= FdCallList.Items[index];
finally
rec.Free;
end;
Result:= fdrec <> nil;
end;
{
Overrides TContest.SendMsg() to send contest-specific messages.
Adding a contest: TContest.SendMsg(AMsg): send contest-specfic messages
}
procedure TArrlFieldDay.SendMsg(const AStn: TStation; const AMsg: TStationMessage);
begin
case AMsg of
msgCQ: SendText(AStn, 'CQ FD <my>');
msgNrQm:
case Random(5) of
0,1: SendText(AStn, 'NR?');
2: SendText(AStn, 'SECT?');
3: SendText(AStn, 'CLASS?');
4: SendText(AStn, 'CL?');
end;
msgLongCQ: SendText(AStn, 'CQ CQ FD <my> <my> FD'); // QrmStation only
else
inherited SendMsg(AStn, AMsg);
end;
end;
// return status bar information string from field day call history file.
// for DX stations, their Entity and Continent is also included.
// this string is used in MainForm.sbar.Caption (status bar).
// Format: '<call> - <user text from fdCallHistoryFile> [- Entity/Continent]'
function TArrlFieldDay.GetStationInfo(const ACallsign: string) : string;
var
fdrec : TFdCallRec;
dxrec : TDXCCRec;
userText : string;
dxEntity : string;
begin
fdrec := nil;
dxrec := nil;
userText := '';
dxEntity := '';
result:= '';
if FindCallRec(fdrec, ACallsign) then
begin
userText:= fdrec.UserText;
// if caller is DX station, include its Continent/Entity
if (fdrec.Section = 'DX') and
gDXCCList.FindRec(dxrec, ACallsign) then
dxEntity:= dxRec.Continent + '/' + dxRec.Entity;
end;
if (userText <> '') or (dxEntity <> '') then
begin
result:= ACallsign;
if userText <> '' then
result:= result + ' - ' + userText;
if dxEntity <> '' then
result:= result + ' - ' + dxEntity;
end;
end;
{
Field Day doesn't have an expliclit multiplier; return a non-empty string.
Also sets contest-specific Qso.Points for this QSO.
}
function TArrlFieldDay.ExtractMultiplier(Qso: PQso) : string;
begin
Qso^.Points := 2;
Result:= '1';
end;
// returns station callsign
function TArrlFieldDay.GetCall(id : integer): string;
begin
result := FdCallList.Items[id].Call;
end;
procedure TArrlFieldDay.GetExchange(id : integer; out station : TDxStation);
begin
station.Exch1 := getExch1(id);
station.Exch2 := getExch2(id);
station.UserText := getUserText(id);
end;
function TArrlFieldDay.getExch1(id:integer): string; // returns station info (e.g. 3A)
begin
result := FdCallList.Items[id].StnClass;
end;
function TArrlFieldDay.getExch2(id:integer): string; // returns section info (e.g. OR)
begin
result := FdCallList.Items[id].Section;
end;
function TArrlFieldDay.getClass(id:integer): string; // returns section (e.g. OR)
begin
result := FdCallList.Items[id].StnClass;
end;
function TArrlFieldDay.getSection(id:integer): string; // returns section (e.g. OR)
begin
result := FdCallList.Items[id].Section;
end;
function TArrlFieldDay.getUserText(id:integer): string; // returns optional club name
begin
result := FdCallList.Items[id].UserText;
end;
{$ifdef DISTRIBUTION_REPORT}
function TArrlFieldDay.GetDistributionReport : string;
const
CallsPerDot : Integer = 20;
var
dist : TDictionary<String, Integer>;
summary : TList<String>;
Value : Integer;
Comparison: TComparison<String>;
begin
dist := TDictionary<String, Integer>.Create;
summary := TList<String>.Create;
try
// count calls by classification
for var rec : TFdCallRec in FdCallList do
begin
if not dist.TryGetValue(rec.StnClass, Value) then
dist.Add(rec.StnClass, 0);
dist[rec.StnClass] := Value + 1;
end;
// format summary lines
for var item : TPair<String, Integer> in dist do
begin
var Len : integer := (item.Value div CallsPerDot);
var Str : String := StringOfChar('*', Len);
if Ini.IsNum(item.Key.Substring(0,2)) then
summary.Add(Format('%3s: %4d %s',
[item.Key, item.Value, Str]))
else
summary.Add(Format('0%2s: %4d %s',
[item.Key, item.Value, Str]));
end;
// sort summary by station class, then by transmitter count
Comparison :=
function(const Left, Right: String): Integer
begin
Result := CompareText(Left.Substring(2,1), Right.Substring(2,1));
if Result = 0 then
Result := CompareText(Left.Substring(0,2), Right.Substring(0,2));
end;
summary.Sort(TComparer<String>.Construct(Comparison));
// format summary report
Result := Format(
'ARRL FD - Call History Distribution by Classification (%d calls/dot)' +
sLineBreak, [CallsPerDot]);
for var S : String in summary do
if S.StartsWith('0') then
Result := Result + S.Replace('0', ' ', []) + sLineBreak
else
Result := Result + S + sLineBreak;
finally
dist.Free;
summary.Free;
end;
end;
{$endif}
class function TFdCallRec.compareCall(const left, right: TFdCallRec) : integer;
begin
Result := CompareStr(left.Call, right.Call);
end;
function TFdCallRec.GetString: string; // returns 3A OR [club name]
begin
Result := Format(' - %s %s %s', [StnClass, Section, UserText]);
end;
function TFdCallRec.GetTxCnt : integer;
var
L : integer;
begin
for L := 1 to length(Self.StnClass) do
if Pos(copy(StnClass,L,1),'0123456789') = 0 then break;
Result := StnClass.Substring(0, L-1).ToInteger;
end;
initialization
IntegerComparer := TComparer<Integer>.Default;
end.