Ejemplo n.º 1
0
 public function loadGamesFromFile($gamesFile, $timestamp = null)
 {
     if (!$timestamp) {
         $timestamp = time();
     }
     $this->matches = array();
     $gamesRaw = file_get_contents($this->gameFolder . $gamesFile);
     if ($gamesRaw) {
         $gamesJson = json_decode($gamesRaw, true);
         foreach ($gamesJson as $gameJson) {
             if ($timestamp > $gameJson['timestamp']) {
                 $result = $gameJson['result'];
                 $players = array_keys($result);
                 $scores = array_values($result);
                 if (count($players) == 2 && !$this->ignoreSingles) {
                     // 1vs1
                     $player1 = $this->getPlayerByName($players[0]);
                     $player2 = $this->getPlayerByName($players[1]);
                     $match = new FoosMatch($player1, $scores[0], $player2, $scores[1]);
                     $match->setTimestamp($gameJson['timestamp']);
                     $this->addMatch($match);
                 } elseif (count($players) == 4 && !$this->ignoreDoubles) {
                     // 2vs2
                     if ($scores[0] != $scores[1] || $scores[2] != $scores[3]) {
                         trigger_error('Inconsisten team scores found: ' . var_export($gameJson, true));
                     } else {
                         $player1 = $this->getPlayerByName($players[0]);
                         $player2 = $this->getPlayerByName($players[1]);
                         $team1 = new FoosTeam($player1, $player2);
                         $score1 = $scores[0];
                         $player3 = $this->getPlayerByName($players[2]);
                         $player4 = $this->getPlayerByName($players[3]);
                         $team2 = new FoosTeam($player3, $player4);
                         $score2 = $scores[2];
                         $match = new FoosMatch($team1, $score1, $team2, $score2);
                         $match->setTimestamp($gameJson['timestamp']);
                         $this->addMatch($match);
                     }
                 }
             }
         }
         // Daily backup
         $todaysBackupName = 'backup-' . $this->getDayTextForTimestamp(time()) . ".json";
         if (!file_exists($this->gameFolder . $todaysBackupName)) {
             // Create backup
             $this->saveToFile($todaysBackupName);
         }
         return true;
     } else {
         return false;
     }
 }