Permalink
Browse files

Implement tie breaks in overall rankings (part 2)

  • Loading branch information...
andreas-gruenbacher committed Oct 29, 2017
1 parent 0366274 commit 9ba0617588ee5d313c8d821b305a77f8590f2b40
Showing with 87 additions and 5 deletions.
  1. +22 −1 backend/index.js
  2. +50 −4 htdocs/admin/serie/controller.js
  3. +13 −0 htdocs/admin/serie/view.html
  4. +2 −0 lib/Jahreswertung.pm
View
@@ -452,6 +452,15 @@ async function get_serie(connection, serie_id) {
event[row.number] = row.new_number;
});
serie.tie_break = {};
(await connection.queryAsync(`
SELECT number, tie_break
FROM series_tie_break
WHERE serie = ?`, [serie_id])
).forEach((row) => {
serie.tie_break[row.number] = row.tie_break;
});
return serie;
}
@@ -1552,6 +1561,17 @@ async function __update_serie(connection, serie_id, old_serie, new_serie) {
});
});
await zipHashAsync(
old_serie.tie_break, new_serie.tie_break,
async function(a, b, number) {
await update(connection, 'series_tie_break',
{serie: serie_id, number: number},
['tie_break'],
a, b,
(x) => (x != null ? {tie_break: x} : null))
&& (changed = true);
});
return changed;
}
@@ -1565,7 +1585,8 @@ async function update_serie(connection, serie_id, old_serie, new_serie) {
events: true,
classes: true,
new_numbers: true,
version: true
version: true,
tie_break: true
};
var nonkeys = Object.keys(new_serie || {}).filter(
@@ -109,9 +109,9 @@ var serieController = [
function normalize_new_numbers(serie) {
angular.forEach(serie.new_numbers, function(new_numbers) {
if (!new_numbers.length ||
new_numbers[new_numbers.length - 1].number !== null)
new_numbers.push({ number: null, new_number: null });
else {
new_numbers[new_numbers.length - 1].number !== null) {
new_numbers.push({ number: null, new_number: null });
} else {
for (var n = 0; n < new_numbers.length - 1; n++)
if (new_numbers[n].number == null) {
new_numbers.splice(n, 1);
@@ -121,12 +121,33 @@ var serieController = [
});
}
function normalize_tie_break(serie) {
var tie_break = serie.tie_break;
if (!tie_break.length ||
tie_break[tie_break.length - 1].number !== null) {
tie_break.push({ number: null, tie_break: null });
} else {
for (var n = 0; n < tie_break.length - 1; n++)
if (tie_break[n].number == null) {
tie_break.splice(n, 1);
n--;
}
}
}
$scope.$watch('serie.new_numbers', function() {
try {
normalize_new_numbers($scope.serie);
} catch (_) {}
}, true);
$scope.$watch('serie.tie_break', function() {
try {
normalize_tie_break($scope.serie);
} catch (_) {}
}, true);
function new_numbers_from_api(new_numbers) {
Object.keys(new_numbers).forEach(function(id) {
new_numbers[id] = Object.keys(new_numbers[id])
@@ -150,6 +171,27 @@ var serieController = [
});
}
function tie_break_from_api(serie) {
var tie_break = [];
Object.keys(serie.tie_break).sort(cmp_null)
.forEach(function(number) {
tie_break.push({
number: +number,
tie_break: serie.tie_break[number]
});
});
serie.tie_break = tie_break;
}
function tie_break_to_api(serie) {
serie.tie_break = serie.tie_break.reduce(
function(hash, tie_break) {
if (tie_break.number)
hash[tie_break.number] = tie_break.tie_break;
return hash;
}, {});
}
function assign_serie(serie) {
if (serie === undefined)
serie = $scope.old_serie;
@@ -161,13 +203,16 @@ var serieController = [
classes: [],
events: [],
new_numbers: {},
ranking: 1
ranking: 1,
tie_break: {}
};
}
new_numbers_from_api(serie.new_numbers);
tie_break_from_api(serie);
normalize_classes(serie);
normalize_events(serie);
normalize_new_numbers(serie);
normalize_tie_break(serie);
}
$scope.is_new = serie.serie === undefined;
$scope.serie = serie;
@@ -189,6 +234,7 @@ var serieController = [
if (classes.length && classes[classes.length - 1]['class'] === null)
classes.pop();
new_numbers_to_api(serie.new_numbers);
tie_break_to_api(serie);
$scope.busy = true;
var request;
if (serie.serie)
@@ -88,6 +88,19 @@ <h1>{{serie.name}}</h1>
</fieldset><br>
</span>
</fieldset><br>
<fieldset style="display:inline">
<legend>Reihung bei Punktegleichstand</legend>
<table>
<tr>
<th>Startnummer</th>
<th>Reihung</th>
</tr>
<tr ng-repeat="tie_break in serie.tie_break">
<td style="text-align:center"><input type="text" numeric size="3" maxlength="5" min="1" max="99999" ng-model="tie_break.number" style="text-align:center"></td>
<td style="text-align:center"><input type="text" numeric size="1" maxlength="2" min="1" max="99" ng-model="tie_break.tie_break" style="text-align:center"></td>
</tr>
</table>
</fieldset><br>
<p>
<button type="button" ng-disabled="!(modified() && form.$valid) || busy" ng-click="save()">Speichern</button>
<button type="button" ng-disabled="!modified() || busy" ng-click="discard()" tabindex="-1">Verwerfen</button>
View
@@ -57,6 +57,8 @@ sub jahreswertung_cmp($$$;$) {
return $bb->{gesamtpunkte} <=> $aa->{gesamtpunkte}
if $aa->{gesamtpunkte} != $bb->{gesamtpunkte};
# Eine explizite Reihung von Fahrern bei Punktegleichstand überschreibt
# den Vergleich der Platzierungen, usw.:
if (%$tie_break) {
my $tie_aa = $tie_break->{$aa->{startnummer}};
my $tie_bb = $tie_break->{$bb->{startnummer}};

0 comments on commit 9ba0617

Please sign in to comment.