ready for the race I guess... vrijdagavond, 't is weeral laat

This commit is contained in:
Joachim Nielandt 2015-09-18 23:32:19 +02:00
parent e1bbb91f7a
commit 02b5c74953
10 changed files with 274 additions and 36 deletions

View File

@ -28,6 +28,3 @@ body {
div.team-col {
padding: 5px;
}

View File

@ -69,7 +69,7 @@
<body>
<!-- the navbar on top ... -->
<nav class="navbar navbar-default navbar-fixed-top" ng-controller="NavCtrl">
<nav class="navbar navbar-default navbar-fixed-top" ng-controller="NavCtrl" ng-show="navbarVisible">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
@ -93,6 +93,9 @@
<li ng-show="getActiveRace()!=null">
<a href="#/championship/{{getActiveRace().idchampionship}}/poule/{{getActiveRace().tier}}/{{getActiveRace().poule}}/race/{{getActiveRace().idrace}}/drives">Actieve race: {{getActiveRace().idrace}}</a>
</li>
<li>
<button type="button" ng-click="navbarVisible = !navbarVisible" class="btn btn-default btn-xs navbar-btn glyphicon glyphicon-chevron-up"></button>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->

View File

@ -8,6 +8,8 @@ var mod = angular.module('bananaraceApp.controllers');
mod.controller('NavCtrl', ['$scope', '$location', 'MainService', function ($scope, $location, MainService) {
$scope.navbarVisible = true;
$scope.loggedIn = function() {
return LoginService.isLoggedIn();
};

View File

@ -133,6 +133,6 @@ angular.module('bananaraceApp.controllers').controller('OverviewCtrl', ['$scope'
}
};
//do this as init of the controller
//do this as init of the controller
$scope.refreshData();
}]);

View File

@ -1,8 +1,8 @@
var mod = angular.module('bananaraceApp.controllers');
mod.controller('PouleViewerCtrl', ['$scope', '$routeParams', 'ViewerService', '$interval',
function($scope, $routeParams, ViewerService, $interval) {
mod.controller('PouleViewerCtrl', ['$scope', '$timeout', '$location', '$cookies', '$routeParams', 'ViewerService', 'PouleService', '$interval',
function($scope, $timeout, $location, $cookies, $routeParams, ViewerService, PouleService, $interval) {
//store all the teams
$scope.teamsInPoule = [];
@ -10,14 +10,61 @@ mod.controller('PouleViewerCtrl', ['$scope', '$routeParams', 'ViewerService', '$
$scope.currentTier = $routeParams.tier;
$scope.currentPoule = $routeParams.poule;
//init cycletime
$scope.setCyclingPouleView = function(currentCyclingSetting) {
// console.log('SETTING CYCLING POULE VIEW'+currentCyclingSetting);
$cookies.put('cyclingpouleview', currentCyclingSetting);
// console.log('SETCYCLINGPOULE VIEW after set:'+$cookies.get('cyclingpouleview'));
if(currentCyclingSetting===false) {
// console.log('SETCYCLINGPOULE VIEW cancelling the timer');
$timeout.cancel($scope.cyclingTimer);
} else if(currentCyclingSetting === true){
// console.log('SETCYCLINGPOULE VIEW setting the timer! ');
setCyclingTimer($scope.nextViewerLink, $scope.cycletime);
} else {
// console.log('wtf mate');
}
};
$scope.getCyclingPouleView = function() {
if($cookies.get('cyclingpouleview')!=undefined) {
// console.log('defined in bookies, returning: '+$cookies.get('cyclingpouleview'));
return $cookies.get('cyclingpouleview')=='true';
} else {
// console.log('returning default: false');
return false;
}
};
$scope.getCyclingPouleTime = function() {
if($cookies.get('cyclingpouletime')!=undefined) {
return parseInt($cookies.get('cyclingpouletime'));
} else {
return 10;
}
};
$scope.cyclingPouleTimeChanged = function(cycletime) {
//set the time in the service
$cookies.put('cyclingpouletime',cycletime);
// console.log('poule time changed');
//reset the cycle timer if necessary
if($scope.getCyclingPouleView()) {
$timeout.cancel($scope.cyclingTimer);
setCyclingTimer($scope.nextViewerLink, cycletime);
}
};
$scope.refreshTeamsForPoule = function() {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
ViewerService.getTeamsForPoule(idchampionship, tier, poule).
success(function(data, status, headers, config) {
console.log('getTeamsForPoule successful...');
console.log(data);
// console.log('getTeamsForPoule successful...');
// console.log(data);
if (data['ok'] == true) {
$scope.teamsInPoule = data.teams;
} else {
@ -29,12 +76,165 @@ mod.controller('PouleViewerCtrl', ['$scope', '$routeParams', 'ViewerService', '$
});
};
$scope.refreshTeamsForPoule();
var timer = $interval($scope.refreshTeamsForPoule, 1000);
var setCyclingTimer = function(link, time) {
if(time == undefined) {
console.log('cant set a timer without time you fool...'+time);
return;
}
// console.log('-- SET CYCLING TIMER ('+time+') enabling cycle timer'+time+' '+(time*1000)+link);
$scope.cyclingTimer = $timeout(function() {
$interval.cancel($scope.refreshTimer);
//go to next page
// $location.url('http://google.com');
$location.url(link);
}, (time*1000));
};
$scope.setNextPouleLink = function() {
//load all poules of the current tier
PouleService.getPoules($routeParams.idchampionship).
success(function(data, status, headers, config) {
// console.log('getTeamsForPoule successful...');
console.log(data);
var tier = $routeParams.tier;
var currentPoule = $routeParams.poule;
var idchampionship = $routeParams.idchampionship;
if (data['ok'] == true) {
//get all the poules of this tier in one array
var poules = [];
for (var i = 0; i < data.poules.length; i++) {
if(data.poules[i].tier == tier)
poules.push(data.poules[i].poule);
}
//set up the next poule to be loaded
poules.sort();
//look up the currentpoule
var nextIndex = -1;
if(poules.indexOf(currentPoule) == poules.length-1) {
nextIndex = 0;
} else {
nextIndex=poules.indexOf(currentPoule)+1;
}
//set the link
$scope.nextViewerLink = "/viewer/championship/"+idchampionship+"/poule/"+tier+"/"+poules[nextIndex]+"/overview";
// /viewer/championship/16/poule/1/1/overview
//http://localhost/bananarace/#/viewer/championship/16/poule/1/1/overview
// console.log('built next viewer link:'+$scope.nextViewerLink);
if($scope.getCyclingPouleView()) {
console.log('jus fetched data, gonna enable cycle timer!');
setCyclingTimer($scope.nextViewerLink, $scope.cycletime);
}
} else {
console.log('call returned but wasnt ok: ' + data.error);
}
})
};
$scope.average = function(times) {
console.log('average got called: '+times);
var sum = 0;
for (var i = 0; i < times.length; i++) {
console.log(times[i]);
sum+=times[i];
}
sum/=times.length;
return sum;
};
$scope.getAverageOfBestOfTwo = function(races) {
// console.log('getting avg of best of two');
// console.log(races);
//collect sum laptimes
var sumlaptimes = [];
for (var i = 0; i < races.length; i++) {
if(races[i].sumlaptime!=undefined)
sumlaptimes.push(races[i].sumlaptime);
}
//sort the avglaptimes
sumlaptimes.sort();
sumlaptimes.reverse();
//cut off everything but two
var removeThisMany = 2;
sumlaptimes.splice(removeThisMany, sumlaptimes.length-removeThisMany);
// console.log(sumlaptimes);
if(sumlaptimes.length==2) {
return (parseFloat(sumlaptimes[0])+parseFloat(sumlaptimes[1]))/2;
} else {
return undefined;
}
};
$scope.bestOfTwo = function(race, races) {
//can't do anything if you don't give me the arrrrrs
if(races.length<2)
return 'unknown';
if(race == null)
return 'unknown';
if(races == null)
return 'unknown';
//collect sum laptimes
var sumlaptimes = [];
for (var i = 0; i < races.length; i++) {
sumlaptimes.push(races[i].sumlaptime);
}
//sort the avglaptimes
sumlaptimes.sort();
sumlaptimes.reverse();
//cut off everything but two
var removeThisMany = 2;
sumlaptimes.splice(removeThisMany, sumlaptimes.length-removeThisMany);
var laptimesok = sumlaptimes.indexOf(race.sumlaptime)!=-1;
//are there enough laptimes present?
var oklaptimescount = 0;
for (var i = 0; i < races.length; i++) {
if(races[i].laptimes.length>0)
oklaptimescount++;
}
var laptimesarethere = oklaptimescount>1;
if(laptimesarethere && laptimesok) {
return 'best';
}
if(!laptimesarethere) {
return 'unknown';
}
return 'notthebest';
};
//cycle settings
$scope.currentCyclingSetting = $scope.getCyclingPouleView();
$scope.cycletime = $scope.getCyclingPouleTime();
//refresh data: initial init
$scope.refreshTeamsForPoule();
//do times things: keep refreshing
//TODO this refresh shouldn't just reset the variable, it should overwrite team's times cleverly...
$scope.refreshTimer = $interval($scope.refreshTeamsForPoule, 2000);
// Cancel timer on destroying controller
$scope.$on('$destroy', function() {
$interval.cancel(timer);
$interval.cancel($scope.refreshTimer);
});
$scope.$on('$destroy', function() {
$timeout.cancel($scope.cyclingTimer);
});
$scope.setNextPouleLink();
}
]);

View File

@ -270,6 +270,7 @@ appServices.service('OverviewService', function($http) {
});
appServices.service('ViewerService', function($http) {
this.getTeamsForPoule = function(idchampionship, tier, poule) {
return $http.post('server/getteamsforpoule.php', {
tier: tier,
@ -302,7 +303,7 @@ appServices.service('MainService', function($http, $cookies) {
// console.log('idrace was null, setting activerace to null');
activerace = null;
}
console.log(activerace);
// console.log(activerace);
}).
error(function(data, status, headers, config) {
console.log('getConfig failed...');

View File

@ -15,13 +15,13 @@
</div>
<div class="col-xs-11">
<div class="row" ng-repeat="poule in tier.poules">
<div class="col-xs-1">
<div class="col-xs-2">
<!-- <span class="label label-primary">Poule {{poule.poule}}</span> -->
<!-- <a href="" type="button" class="btn btn-default btn-xs">Poule {{poule.poule}}</button> -->
<a href="#/championship/{{currentChampionship}}/poules" class="btn btn-default btn-xs" role="button">Poule {{poule.poule}}</a>
<a href="#/viewer/championship/{{currentChampionship}}/poule/{{tier.tier}}/{{poule.poule}}/overview" class="btn btn-default btn-xs glyphicon glyphicon-eye-open" role="button"></a>
<a href="#/viewer/championship/{{currentChampionship}}/poule/{{tier.tier}}/{{poule.poule}}/overview" class="btn btn-default btn-xs" role="button"><span class="glyphicon glyphicon-eye-open text-muted"> </span></a>
</div>
<div class="col-xs-11" ng-show="poule.teams.length>0">
<div class="col-xs-10" ng-show="poule.teams.length>0">
<div class="row" ng-repeat="team in poule.teams">
<div class="col-xs-2">
<a href="#/championship/{{currentChampionship}}/poule/{{tier.tier}}/{{poule.poule}}/races" class="btn btn-default btn-xs" role="button">{{team.name}}</a>
@ -61,7 +61,7 @@
</div>
</div>
</div>
<div class="col-xs-11" ng-show="poule.teams.length==0">
<div class="col-xs-10" ng-show="poule.teams.length==0">
<a href="#/championship/{{currentChampionship}}/poule/{{tier.tier}}/{{poule.poule}}/teams"
type="button" class="btn btn-warning btn-xs" title="Geen teams in deze poule, voeg er toe..."><span class="glyphicon glyphicon-import"> </span></a>
</div>

View File

@ -1,9 +1,17 @@
<div class="row">
<div class="col-xs-12">
<div class="col-xs-10">
<div class="page-header">
<h1>Tier {{currentTier}} <small>poule {{currentPoule}}</small></h1>
</div>
</div>
<div class="col-xs-2">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="currentCyclingSetting" name="cyclingcheckbox" ng-change="setCyclingPouleView(currentCyclingSetting)" ng-true-value="true" ng-false-value="false"]><small>Cycle poules</small>
</label>
<small>in</small> <input type="number" ng-model="cycletime" min="5" max="300" required="string" ng-change="cyclingPouleTimeChanged(cycletime)"> <small>s</small>
</div>
</div>
</div>
<div class="row">
@ -12,16 +20,24 @@
<h2>{{team.teamname}}</h2>
<div ng-repeat="race in team.races">
<div class="panel panel-default">
<div class="panel panel-default" ng-class="{'panel-success':bestOfTwo(race, team.races)=='best', 'panel-danger':bestOfTwo(race, team.races)=='notthebest'}">
<div class="panel-heading">
<h3 class="panel-title">Race {{race.idrace}}</h3>
<h3 class="panel-title"><i class="fa fa-flag-o"></i> Race {{$index+1}}</h3>
</div>
<div class="panel-body">
<div ng-repeat="laptime in race.laptimes">{{laptime.laptime | interval}}</div>
<div ng-show="race.laptimes.length>0" ng-repeat="laptime in race.laptimes | orderBy:'lapnr'">
<span><small>{{laptime.lapnr}}:</small> {{laptime.laptime | secondsToString}} <span ng-show="laptime.totalpenalty>0" class="label label-danger">+{{laptime.totalpenalty}}s</span></span>
</div>
<div ng-show="race.laptimes.length==0" class="text-center"><span class="glyphicon glyphicon-glass"> </span></div>
</div>
<div class="panel-footer" ng-show="race.laptimes.length>0">
<strong><small>Racetijd:</small> {{race.sumlaptime | secondsToString}}</strong>
</div>
</div>
</div>
<!-- block to show average of best of two -->
<div ng-show="getAverageOfBestOfTwo(team.races)!=undefined" class="alert alert-info" role="alert"><i class="fa fa-flag-checkered fa-lg"></i> Eindgemiddelde: <strong>{{getAverageOfBestOfTwo(team.races)|secondsToString}}</strong></div>
</div>
</div>
</div>
</div

View File

@ -1,4 +1,4 @@
<?php
$dbconn = pg_connect("host=192.168.0.150 port=5432 dbname=race user=postgres password=915015");
$dbconn = pg_connect("host=localhost port=5432 dbname=race user=raceadmin password=bananaracer");
echo pg_last_error($dbconn);
?>

View File

@ -16,13 +16,34 @@
//fetch teams
$teamfetch = pg_prepare($dbconn, "teamfetch", "select team.*, team.name teamname from team inner join team_poule using (idteam, idchampionship) where tier = $1 and poule = $2 and idchampionship = $3");
// $result['lasterror'].=pg_last_error()."\n";
$racefetch = pg_prepare($dbconn, "racefetch", "select * from race where tier = $1 and poule = $2 and idchampionship = $3 and idteam = $4");
// $result['lasterror'].=pg_last_error()."\n";
$laptimefetch = pg_prepare($dbconn, "laptimefetch", "select vm1.idrace, vm2.cleaneduptime-vm1.cleaneduptime laptime from valid_measurements vm1 inner join valid_measurements vm2 on vm1.idrace = vm2.idrace and vm1.idmeasurement != vm2.idmeasurement and vm1.cleaneduptime < vm2.cleaneduptime and vm2.cleaneduptime = (select min(cleaneduptime) from valid_measurements vm3 where vm3.cleaneduptime>vm1.cleaneduptime and vm3.idrace = vm1.idrace) where vm1.idrace = $1 order by vm1.idrace, laptime");
$racefetch = pg_prepare($dbconn, "racefetch", "select *,
(select extract(epoch from avg(laptime)) from valid_laptimes where idrace = race.idrace) avglaptime,
(select extract(epoch from sum(laptime)) from valid_laptimes where idrace = race.idrace)
+(select coalesce(sum(penaltyseconds), 0) from comment where idrace = race.idrace)
sumlaptime
from race where tier = $1 and poule = $2 and idchampionship = $3 and idteam = $4");
// $result['lasterror'].=pg_last_error()."\n";
// $laptimefetch = pg_prepare($dbconn, "laptimefetch", "select vm1.idrace, extract(epoch from vm2.cleaneduptime-vm1.cleaneduptime) laptime from valid_measurements vm1 inner join valid_measurements vm2 on vm1.idrace = vm2.idrace and vm1.idmeasurement != vm2.idmeasurement and vm1.cleaneduptime < vm2.cleaneduptime and vm2.cleaneduptime = (select min(cleaneduptime) from valid_measurements vm3 where vm3.cleaneduptime>vm1.cleaneduptime and vm3.idrace = vm1.idrace) where vm1.idrace = $1 order by vm1.idrace, laptime");
// $laptimefetch = pg_prepare($dbconn, "laptimefetch", "
// with result as (
// with sortedtimes as
// (select idrace, cleaneduptime, row_number() over (partition by idrace order by cleaneduptime) from valid_measurements order by idrace, cleaneduptime)
// select idrace, row_number lapnr, cleaneduptime, (select cleaneduptime-st1.cleaneduptime from sortedtimes where row_number = st1.row_number+1 and idrace = st1.idrace) laptime from sortedtimes st1 where row_number <= (select sum(laps) from drive where idrace = st1.idrace)
// )
// select idrace, extract(epoch from laptime) laptime, lapnr,
// (select sum(penaltyseconds) from comment where idrace = $1) penaltyseconds
// from result where laptime is not null and idrace = $1");
$laptimefetch = pg_prepare($dbconn, "laptimefetch", "with penaltiestodrive as (
select *, coalesce((select sum(laps)+1 from drive d2 where d1.idrace = d2.idrace and d2.drivenr<d1.drivenr), 1) fromdrive,
(select sum(laps) from drive d2 where d1.idrace = d2.idrace and d2.drivenr<=d1.drivenr) todrive
from drive d1 left join comment using (idrace, drivenr) order by idrace, drivenr)
select idrace, extract(epoch from laptime) laptime, lapnr, (select sum(penaltyseconds) from penaltiestodrive where penaltiestodrive.idrace = valid_laptimes.idrace and lapnr >= fromdrive and lapnr <= todrive) totalpenalty
from valid_laptimes where idrace = $1");
$result['debug2']=pg_last_error();
$teamfetch = pg_execute($dbconn, "teamfetch", array($tier, $poule, $idchampionship));
// $result['lasterror'].=pg_last_error()."\n";
//resulting teams array
$teams = array();
@ -35,29 +56,27 @@
//loop over each race
$racefetchexec = pg_execute($dbconn, "racefetch", array($tier, $poule, $idchampionship, $idteam));
// $result['lasterror'].=pg_last_error()."\n";
while($racerow = pg_fetch_assoc($racefetchexec)) {
$result['GETTING IN IT'] = true;
$race = array();
$race['idrace'] = $racerow['idrace'];
$race['laptimes'] = array();
$race['avglaptime'] = $racerow['avglaptime'];
$race['sumlaptime'] = $racerow['sumlaptime'];
//fetch the laptimes
$laptimefetch = pg_execute($dbconn, "laptimefetch", array($race['idrace']));
// $result['lasterror'].=pg_last_error()."\n";
$result['debug1']=pg_last_error();
while($laptimerow = pg_fetch_assoc($laptimefetch)) {
$laptime = array();
$laptime['laptime'] = $laptimerow['laptime'];
$laptime['lapnr'] = $laptimerow['lapnr'];
$laptime['totalpenalty'] = $laptimerow['totalpenalty'];
$race['laptimes'][] = $laptime;
}
$team['races'][] = $race;
}
$teams[] = $team;
}