-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathairgain_verify.pl
More file actions
executable file
·258 lines (219 loc) · 5.92 KB
/
Copy pathairgain_verify.pl
File metadata and controls
executable file
·258 lines (219 loc) · 5.92 KB
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
#!/usr/bin/perl
#
# Airgain
#
# Name: validate_airgain
#
# Put waypoints into km(?) buckets that represent distance from centre
#
# In loop for each trackpoint:
# * Calculate our dist from centre
# * check all points in bucket to see if we 'tagged' them
# * add to our list (only once)
#
# Score based upon waypoint name
# Store it ...
#
# IDEA: could use a 2d tree instead
#
# Geoff Wong 2011
#
require DBD::mysql;
use Math::Trig;
use Data::Dumper;
use POSIX qw(ceil floor);
use TrackLib qw(:all);
sub validate_airgain
{
my ($task, $flight, $reg) = @_;
my $wpts = $reg->{'waypoints'};
my $centre = $reg->{'centre'};
my $coords = $flight->{'coords'};
my %accum;
my @bucket;
my $sub;
my $sc;
my $dist;
my $score;
my $utcmod;
my $coord;
my $wcount;
# Check for UTC crossover
if (defined($task))
{
$utcmod = 0;
$coord = $coords->[0];
if ($coord->{'time'} > $task->{'sfinish'})
{
$utcmod = 86400;
}
}
for $coord (@$wpts)
{
my $buc;
$dist = floor(distance($centre, $coord)/100);
#print $coord->{'name'}, " dist to centre: $dist\n";
if (!defined($bucket[$dist]))
{
$bucket[$dist] = [];
}
$buc = $bucket[$dist];
push @$buc, $coord;
}
#print "@bucket=", Dumper(\@bucket);
# Initialise some working variables
# Go through the coordinates and verify the track against the task
for $coord (@$coords)
{
# Check task start probably a good idea ...
# Don't start scoring until after start time
# and we're within centre radius
# Check the task isn't finished ..
if (defined($task))
{
$coord->{'time'} = $coord->{'time'} - $utcmod;
if ($coord->{'time'} > $task->{'sfinish'})
{
print "Coordinate after task finish: ",
$coord->{'time'}, " ", $task->{'sfinish'}, ".\n";
last;
}
}
$dist = floor(distance($centre, $coord)/100);
$sub = $bucket[$dist];
if (defined($sub))
{
#print $dist, " ", Dumper($sub);
for $sc (@$sub)
{
#print Dumper($sc);
$dist = distance($coord, $sc);
#print "Dist coord to sc=$dist\n";
if ($dist < 400)
{
print "Inside : ", $sc->{'name'}, "\n";
$accum{$sc->{'name'}} = $sc;
}
}
}
}
# Can't start later than start close time
if (defined($task))
{
if (($task->{'sstartclose'} > $task->{'sstart'}) and ($startss > $task->{'sstartclose'}))
{
$startss = $task->{'sstartclose'};
}
# Sanity
if ($startss > $finish)
{
$startss = $finish;
}
}
#
# Now compute our score
#
$score = 0.0;
for my $k (keys %accum)
{
$wcount++;
$val = 0 + substr($accum{$k}->{'name'},3,3);
$score = $score + $val;
}
if ($score < 0)
{
printf "Somehow the distance score ($score) is < 0\n";
$score = 0;
}
$result{'start'} = 0;
$result{'goal'} = 0;
$result{'startSS'} = 0;
$result{'endSS'} = 0;
$result{'distance'} = $flight->{'traLength'};
$result{'penalty'} = 0;
$result{'comment'} = '';
$result{'coeff'} = 0;
$result{'score'} = $score;
$result{'waypoints_made'} = $wcount;
return \%result;
}
sub store_airgain
{
my ($track,$res) = @_;
my ($tasPk,$traPk,$dist,$score,$turnpoints,$comment);
$tasPk = $track->{'tasPk'};
$traPk = $track->{'traPk'};
$dist = $res->{'distance'};
$score = $res->{'score'};
$turnpoints = $res->{'waypoints_made'};
$penalty = $res->{'penalty'};
$comment = $res->{'comment'};
$dbh->do("delete from tblTaskResult where traPk=? and tasPk=?", undef, $traPk, $tasPk);
#print("insert into tblTaskResult (tasPk,traPk,tarDistance,tarSpeed,tarStart,tarGoal,tarSS,tarES,tarTurnpoints,tarLeadingCoeff,tarPenalty,tarComment) values ($tasPk,$traPk,$dist, 0, 0, 0, 0, 0,$turnpoints,$score,$penalty,'$comment')\n");
$sth = $dbh->prepare("insert into tblTaskResult (tasPk,traPk,tarDistance,tarSpeed,tarStart,tarGoal,tarSS,tarES,tarTurnpoints,tarLeadingCoeff,tarPenalty,tarComment) values ($tasPk,$traPk,$dist, 0, 0, 0, 0, 0,$turnpoints,$score,$penalty,'$comment')");
$sth->execute();
}
#
# Main program here ..
#
my $flight;
my $wpts;
my $task;
my $tasPk = 0;
my $comPk = 0;
my $regPk;
my $info;
my $duration;
if (scalar @ARGV < 1)
{
print "airgain_verify.pl traPk comPk [tasPk]\n";
exit 1;
}
$dbh = db_connect();
# Read the flight
$flight = read_track($ARGV[0]);
#print Dumper($flight);
$comPk = 0+$ARGV[1];
if ($comPk < 1)
{
print "airgain_verify.pl traPk comPk [tasPk]\n";
exit 1;
}
#print Dumper($wpts);
# Get the waypoints for specific task
if (0+$ARGV[2] > 0)
{
$tasPk = 0 + $ARGV[2];
$flight->{'tasPk'} = $tasPk;
$task = read_task($tasPk);
#print Dumper($task);
# Verify and find waypoints / distance / speed / etc
if ($task->{'type'} ne 'airgain')
{
print "Task $tasPk not an airgain type task\n";
print "airgain_verify.pl traPk comPk [tasPk]\n";
exit 1;
}
$wpts = read_region($task->{'region'});
$info = validate_airgain($task,$flight,$wpts);
}
else
{
$sth = $dbh->prepare("select C.* tblCompetition where C.comPk=$comPk");
$sth->execute();
if ($ref = $sth->fetchrow_hashref())
{
$regPk = 0 + $ref->{'regPk'};
}
else
{
print "Unknown competition $comPk\n";
print "airgain_verify.pl traPk comPk [tasPk]\n";
}
print "Free airgain validate\n";
$wpts = read_region($regPk);
$info = validate_airgain(undef,$flight,$wpts);
}
#print Dumper($info);
# Store results in DB
store_airgain($flight,$info);