'use strict';

var runId;

function getRunId() {
    runId = $.url().segment(4);
}

function noRunResults(jqXHR) {
    alert(jqXHR.responseText);
}

function transposeResults(res) {
    var t = [];
    var season, gr, ge, fishStart, fishEnd;
    for (var i in res) {
        season = res[i].season;
        gr = res[i].groupRestraint.toFixed(3);
        ge = res[i].groupEfficiency.toFixed(3);
        fishStart = res[i].fishStart;
        fishEnd = res[i].fishEnd;
        for (var j in res[i].fishers) {
            t.push({
                name: res[i].fishers[j].name,
                type: res[i].fishers[j].type,
                greed: res[i].fishers[j].type === 'bot' ?
                    res[i].fishers[j].greed.toFixed(3) : 'n/a',
                season: season,
                fishStart: fishStart,
                fishEnd: fishEnd,
                fishTaken: res[i].fishers[j].fishTaken,
                profit: res[i].fishers[j].profit,
                ir: res[i].fishers[j].individualRestraint.toFixed(3),
                gr: gr,
                ie: res[i].fishers[j].individualEfficiency.toFixed(3),
                ge: ge
            });
        }
    }

    return t;
}

function gotRunResults(r) {
    $('#time').text('Time: ' + moment(r.time).format('llll'));
    $('#participants').text('Participant IDs: ' + r.participants);

    var transposed = transposeResults(r.results);
    var table = '';
    for (var i in transposed) {
        table += '<tr><td>' + transposed[i].name + '</td><td>' +
            transposed[i].type + '</td><td>' +
            transposed[i].greed + '</td><td>' +
            transposed[i].season + '</td><td>' +
            transposed[i].fishStart + '</td><td>' +
            transposed[i].fishEnd + '</td><td>' +
            transposed[i].fishTaken + '</td><td>' +
            transposed[i].profit + '</td><td>' +
            transposed[i].ir + '</td><td>' +
            transposed[i].gr + '</td><td>' +
            transposed[i].ie + '</td><td>' +
            transposed[i].ge + '</td></tr>';
    }

    $('#run-results-table-rows').html(table);

    table = '';
    for (var i in r.log) {
        table += '<tr><td>' + r.log[i] + '</td></tr>'
    }

    $('#log-table-rows').html(table);
}

function getRunResults() {
    $.ajax({
        type: 'GET',
        url: '/runs/' + runId,
        error: noRunResults,
        success: gotRunResults
    });
}

function main() {
    getRunId();
    getRunResults();
}

$(document).ready(main);
@@ -304,16 +304,52 @@
     gr =
+ res%5Bi%5D.groupRestraint ?%0A           
  res%5Bi%5D.
@@ -365,32 +365,40 @@
 raint.toFixed(3)
+ : 'n/a'
 ;%0A        ge = r
@@ -395,16 +395,53 @@
     ge =
+ res%5Bi%5D.groupEfficiency ?%0A           
  res%5Bi%5D.
@@ -466,16 +466,24 @@
 Fixed(3)
+ : 'n/a'
 ;%0A      
@@ -756,16 +756,43 @@
 == 'bot'
+ && res%5Bi%5D.fishers%5Bj%5D.greed
  ?%0A     
@@ -1062,29 +1062,155 @@
 ofit
-,%0A                ir:
+ ?%0A                    res%5Bi%5D.fishers%5Bj%5D.profit.toFixed(2) : 'n/a',%0A                ir: res%5Bi%5D.fishers%5Bj%5D.individualRestraint ?%0A                   
  res
@@ -1246,32 +1246,40 @@
 raint.toFixed(3)
+ : 'n/a'
 ,%0A              
@@ -1307,16 +1307,77 @@
      ie:
+ res%5Bi%5D.fishers%5Bj%5D.individualEfficiency ?%0A                   
  res%5Bi%5D.
@@ -1418,16 +1418,24 @@
 Fixed(3)
+ : 'n/a'
 ,%0A      

Fix run-results to display toFixed floats when available