Scoring

Please don’t ask me what the score is, I’m not even sure what the game is.

I made a design decision to store all of the scores in one long text field…

event_has_class_competitor

So, how are the scores displayed?

   /* for each section */
   for ($i = 1; $i <= $event->sections; $i++)   { ?>

        <td class="section-left"><?php
    /* for each round */
    for ($j = 1; $j <= $event->rounds; $j++) {
     echo $rider->score($i, $j); ?></td><?php
     if ($j < $event->rounds) { ?><td><?php } ?>
<?php } /* ($j) for each round */ ?>
<?php } /* ($i) for each section */ ?>

        <td class="section-left"><?php
    /* total for each lap */
    for ($j = 1; $j <= $event->rounds; $j++) {
     echo $rider->round($j); ?></td><?php
     if ($j < $event->rounds) { ?><td><?php } ?>
<?php } /* ($j) for each round */ ?>

The following is from Model_Event_Competitor.

   //
   // return the section score for a particular round
   //
   public function score($section, $round)
   {
      // sanity check
      if ($section < 1 || $section > $this->event->sections ||
          $round   < 1 || $round   > $this->event->rounds)
         return '?';

      // calculate 0-based index into score string
      $index = ((($section-1) * $this->event->rounds) + $round) - 1;      

      // if we don't have this many scores...
      if ($index >= strlen($this->scores))
         return '-';

      return $this->scores[$index];
   }

   //
   // return the total score for a particular round
   // (calculated over all sections)
   //
   public function round($round)
   {
      // sanity check
      if ($round   < 1 || $round  > $this->event->rounds)
         return '?';

      $sum = 0;
      for ($i = 0; $i < $this->event->sections; $i++)
      {
         // calculate 0-based index into score string
         $index = (($i * $this->event->rounds) + $round) - 1;      

         // if we don't have this many scores...
         if ($index < strlen($this->scores))
           $sum += intval($this->scores[$index]);
      }
      return $sum;
   }