+sub train_model {
+ my ($filename, $ratings, $covariances, $aux_params) = @_;
+
+ open RATINGS, "$config::base_dir/bayeswf < $filename |"
+ or die "bayeswf: $!";
+ while (<RATINGS>) {
+ chomp;
+ my @x = split;
+ if ($x[0] eq 'covariance') {
+ push @$covariances, (join("\t", @x[1..3]));
+ } elsif ($x[0] eq 'aux_param') {
+ push @$aux_params, ("nb-NO" . "\t" . $x[1] . "\t" . $x[2]);
+ } else {
+ push @$ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
+ }
+ }
+
+ close RATINGS;
+}
+
+my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
+ or die "connect: " . $DBI::errstr;
+$dbh->{AutoCommit} = 0;
+$dbh->{RaiseError} = 1;
+
+$dbh->do('SET client_min_messages TO WARNING');
+
+my $last_season = find_last_season($dbh);
+my @games = ();
+my %ids = ();
+fetch_games($dbh, $last_season, \@games, \%ids);
+my $tmpnam = output_to_file(\@games, \%ids);
+
+my @ratings = ();
+my @covariances = ();
+my @aux_params = ();
+train_model($tmpnam, \@ratings, \@covariances, \@aux_params);