Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.01 KB | None | 0 0
  1. <?
  2.  
  3. include "casino.php";
  4.  
  5. $game = new Game();
  6.  
  7. $game->playGames();
  8.  
  9. class Game
  10. {
  11.     private $filename = '';
  12.     private $inputFilename = '';
  13.     private $bets = array();
  14.     private $casino = NULL;
  15.     public $balance = 0;
  16.  
  17.     /**
  18.      * Game constructor.
  19.      * @param string $in
  20.      * @param string $out
  21.      */
  22.     public function __construct($in = 'in.txt', $out = 'out.txt')
  23.     {
  24.         date_default_timezone_set('UTC');
  25.  
  26.         $this->inputFilename = $in;
  27.         $this->filename = $out;
  28.  
  29.         $this->casino = new Casino();
  30.  
  31.         $this->readData();
  32.     }
  33.  
  34.     // Считывание входных данных
  35.     public function readData()
  36.     {
  37.         $handle = @fopen(__DIR__ . DIRECTORY_SEPARATOR . $this->inputFilename, "r");
  38.  
  39.         if ($handle){
  40.             $row = 0;
  41.  
  42.             if( ($buffer = fgets($handle, 4096)) !== false ){
  43.                 // Считываем количество ставок игрока
  44.                 $betCount = intval(trim($buffer));
  45.             }else{
  46.                 echo "Error: Empty file ".$this->inputFilename."<br>";
  47.             }
  48.  
  49.             // Считываем все ставки игрока
  50.             for ($i=0; $i < $betCount; $i++) {
  51.                 if( !($buffer = fgets($handle, 4096)) !== false ){
  52.                     echo "Error: Error read bets in ".$this->inputFilename."<br>";      
  53.                     die();
  54.                 }
  55.                 $buffer = trim($buffer);
  56.  
  57.                 $arTmp = explode(" ", $buffer);
  58.                 if( count($arTmp) < 3 ){
  59.                     echo "Error: error read line ".$this->inputFilename."<br>";      
  60.                 }else{
  61.                     array_push($this->bets, (object) array(
  62.                         "game_id" => $arTmp[0],
  63.                         "sum" => $arTmp[1],
  64.                         "team" => $arTmp[2],
  65.                     ));
  66.                 }
  67.             }
  68.  
  69.             if( ($buffer = fgets($handle, 4096)) !== false ){
  70.                 // Считываем количество игр
  71.                 $gameCount = intval(trim($buffer));
  72.             }else{
  73.                 echo "Error: Game count does not exist in file ".$this->inputFilename."<br>";
  74.             }
  75.  
  76.             // Считываем все игры
  77.             for ($i=0; $i < $gameCount; $i++) {
  78.                 if( !($buffer = fgets($handle, 4096)) !== false ){
  79.                     echo "Error: Error read bets in ".$this->inputFilename."<br>";      
  80.                     die();
  81.                 }
  82.                 $buffer = trim($buffer);
  83.  
  84.                 $arTmp = explode(" ", $buffer);
  85.                 if( count($arTmp) < 5 ){
  86.                     echo "Error: error read line ".$this->inputFilename."<br>";      
  87.                 }else{
  88.                     // Добавляем игры в объект казино
  89.                     $this->casino->addGame($arTmp[0], $arTmp[1], $arTmp[2], $arTmp[3], $arTmp[4]);
  90.                 }
  91.             }
  92.  
  93.             if (!feof($handle)){
  94.                 echo "Error: unexpected fgets() fail<br>";
  95.             }
  96.             fclose($handle);
  97.         }
  98.     }
  99.  
  100.     // Воспроизвести все игры
  101.     public function playGames(){
  102.         foreach ($this->bets as $bet) {
  103.             // Играем в игру казино по ID. Если игра по id не найдена, то метод вернет false
  104.             if( ($result = $this->casino->play($bet->game_id, $bet->sum, $bet->team) ) !== false ){
  105.                 $this->balance += $result;
  106.             }else{
  107.                 echo "Error: Game ".$bet->game_id." does not exist<br>";
  108.             }
  109.         }
  110.  
  111.         $this->writeData();
  112.     }
  113.  
  114.     // Записываем результат
  115.     private function writeData()
  116.     {
  117.         $handle = @fopen(__DIR__ . DIRECTORY_SEPARATOR . $this->filename, "w");
  118.         if ($handle)
  119.         {
  120.             @fwrite($handle, $this->balance);
  121.             fclose($handle);
  122.         }
  123.     }
  124. }
  125.  
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement