Example #1
0
 /**
  * @param $savedrace_id
  * @param $yourTime
  *
  * @return array
  *
  * ['Racers'] => array of the 5 racers
  *      [0-4] => ['USSAPoints] => Racer's USSA points,
  *               ['FISPoints'] => Racer's FIS points,
  *               ['USSANote'] => USSA note,
  *               ['FISNote'] => FIS note,
  *               ['LastName'] => Racer's last name,
  *               ['FirstName'] => Racer's first name
  *
  * ['USSAPenalty'] => USSA penalty
  * ['FISPenalty'] => FIS penalty
  * ['RacePoints'] => Race Points
  * ['USSAFinalPoints'] => Final USSA Points
  * ['FISFinalPoints'] => Final FIS Points
  * ['RaceName'] => Race Name
  * ['Gender'] => Gender
  * ['USSA_List'] => USSA list used to score
  * ['FIS_List'] => FIS list used to score
  */
 public function scoreSavedRace($savedrace_id, $yourTime)
 {
     $race = savedrace::where('savedrace_id', '=', $savedrace_id)->first();
     /*
      * Set Variables
      */
     $output = [];
     if ($race->format == 1) {
         $fFactor = 800;
     } elseif ($race->format == 2) {
         $fFactor = 1400;
     } elseif ($race->format == 3) {
         $fFactor = 1200;
     }
     /*
      * Convert time to seconds
      */
     $usersTime = time_to_sec($yourTime);
     $winnersTime = time_to_sec($race->winners_time);
     $points = $race->getPoints()->get();
     $i = 0;
     foreach ($points as $point) {
         $output['Racers'][$i] = ['USSAPoints' => $point->USSA_points, 'FISPoints' => $point->FIS_points, 'USSANote' => $point->USSA_note, 'FISNote' => $point->FIS_note, 'LastName' => $point->savedracer()->first()->last, 'FirstName' => $point->savedracer()->first()->first];
         $racersUSSAPoints[$i] = $point->USSA_points;
         $racersFISPoints[$i] = $point->FIS_points;
         $i++;
     }
     /*
      * Calculate USSA penalty
      * Sum of three lowest points divided by 3.75
      */
     $sortResult = sort($racersUSSAPoints);
     $USSAtopThree = array_slice($racersUSSAPoints, 0, 3, true);
     $USSAPenalty = array_sum($USSAtopThree) / 3.75;
     $USSAPenalty = round($USSAPenalty, 2, PHP_ROUND_HALF_UP);
     $output['USSAPenalty'] = $USSAPenalty;
     /*
      * Calculate FIS penalty
      * Sum of three lowest points divided by 3.75
      */
     $sortResult = sort($racersFISPoints);
     $FIStopThree = array_slice($racersFISPoints, 0, 3, true);
     $FISPenalty = array_sum($FIStopThree) / 3.75;
     $FISPenalty = round($FISPenalty, 2, PHP_ROUND_HALF_UP);
     $output['FISPenalty'] = $FISPenalty;
     /*
      * Calculate race points
      *
      * Page 20
      * http://www.fis-ski.com/mm/Document/documentlibrary/Cross-Country/04/26/74/FISpointsrules2015-2016_inclattachments_English.pdf
      *
      */
     $racePoints = $usersTime * $fFactor / $winnersTime - $fFactor;
     $racePoints = round($racePoints, 2, PHP_ROUND_HALF_UP);
     $output['RacePoints'] = $racePoints;
     /*
      * Calculate final USSA Points
      */
     $USSAFinalPoints = $USSAPenalty + $racePoints;
     $output['USSAFinalPoints'] = $USSAFinalPoints;
     /*
      * Calculate final FIS Points
      */
     $FISFinalPoints = $FISPenalty + $racePoints;
     $output['FISFinalPoints'] = $FISFinalPoints;
     /*
      * Basic Race Data
      */
     $output['RaceName'] = $race->race_name;
     $output['Gender'] = $race->gender;
     $output['USSA_List'] = $race->USSA_list;
     $output['FIS_List'] = $race->FIS_list;
     /*
      * Update number of clicks
      */
     $clicks = $race->clicks;
     $clicks++;
     $race->clicks = $clicks;
     $race->save();
     return $output;
 }
 public function loadMoreRaces()
 {
     $offset = Request::input('offset');
     $orderBy = Request::input('orderBy');
     $moreRaces = $recentRaces = savedrace::where('status', '=', 'approved')->orderBy($orderBy, 'DESC')->offset($offset)->take(12)->get();
     foreach ($moreRaces as $key => $moreRace) {
         $moreRaces[$key]->location = $moreRace->savedLocation()->first()->location_name;
     }
     return json_encode($moreRaces);
 }
Example #3
0
 /**
  * @param $input
  * @param $FISArray
  * @param $USSAArray
  *
  * @return string
  *
  * Saves race, points, and racer when called. Returns string with status of save
  */
 public function SaveRace($input, $FISArray, $USSAArray)
 {
     /*
      * Check to see if race already exists in the database. If it does set the saveStatus
      */
     //Set Variables for the options
     $season = Option::where('option_name', '=', 'season')->first()->value;
     $USSA_list = Option::where('option_name', '=', 'USSA_list')->first()->value;
     $FIS_list = Option::where('option_name', '=', 'FIS_list')->first()->value;
     //Save location to database
     $saveLocation = new racelocation();
     $latitude = round($input['hidden-lat'], 3);
     $longitude = round($input['hidden-long'], 3);
     //Check for location already existing
     $checkForLocation = $saveLocation->where('latitude', '=', $latitude)->where('longitude', '=', $longitude)->first();
     if ($checkForLocation == null) {
         $saveLocation->location_name = $input['race-location'];
         $saveLocation->latitude = $latitude;
         $saveLocation->longitude = $longitude;
         $saveLocation->save();
         $locationID = $saveLocation->location_id;
     } else {
         $locationID = $checkForLocation->location_id;
     }
     //Save race to datbase
     $saveRace = new savedrace();
     $saveRace->status = 'not approved';
     $saveRace->race_name = $input['race-name'];
     $saveRace->gender = $input['gender'];
     $saveRace->clicks = 0;
     $saveRace->USSA_list = $season . " " . $USSA_list;
     $saveRace->FIS_list = $season . " " . $FIS_list;
     $saveRace->distance = $input['distance'];
     $saveRace->format = $input['format'];
     $saveRace->winners_time = $input['winners-time'];
     $saveRace->technique = $input['technique'];
     $saveRace->distance_km = $input['distance-km'];
     $saveRace->savedLocation()->associate($locationID);
     $saveRace->save();
     //Hold race that was just saved in a variable
     $savedRace = savedrace::where('savedrace_id', '=', $saveRace->savedrace_id)->first();
     //Loop through racers and save them to the database
     $savedRacers = [];
     $iter = 1;
     foreach ($FISArray['Racers'] as $racer) {
         //Check to see if the racer already exists in our database
         $checkForSavedRacer = savedracer::where('last', 'LIKE', '%' . trim($racer['LastName']) . '%')->where('first', 'LIKE', '%' . trim($racer['FirstName']) . '%')->first();
         if ($checkForSavedRacer != NULL) {
             $savedRacers[$iter] = $checkForSavedRacer;
         } else {
             $saveRacer = new savedracer();
             $saveRacer->last = $racer['LastName'];
             $saveRacer->first = $racer['FirstName'];
             $saveRacer->save();
             $savedRacers[$iter] = savedracer::where('savedracer_id', '=', $saveRacer->id)->first();
         }
         $iter++;
     }
     //Loop through and save all the points from the race
     for ($i = 1; $i < 6; $i++) {
         $savePoints = new savedpoint();
         $savePoints->place = $i;
         $savePoints->USSA_points = $USSAArray['Racers'][$i - 1]['Points'];
         $savePoints->FIS_points = $FISArray['Racers'][$i - 1]['Points'];
         if (array_key_exists('Note', $USSAArray['Racers'][$i - 1])) {
             $savePoints->USSA_note = $USSAArray['Racers'][$i - 1]['Note'];
         } else {
             $savePoints->USSA_note = null;
         }
         if (array_key_exists('Note', $FISArray['Racers'][$i - 1])) {
             $savePoints->FIS_note = $FISArray['Racers'][$i - 1]['Note'];
         } else {
             $savePoints->FIS_note = null;
         }
         //Associate race and racer with the points
         $savePoints->savedrace()->associate($savedRace);
         $savePoints->savedracer()->associate($savedRacers[$i]);
         $savePoints->save();
     }
     $saveStatus = 'Race Successfully Saved, Waiting To Be Approved By Admin';
     mail('*****@*****.**', 'Race Waiting to be approved', $input['race-name']);
     return $saveStatus;
 }