Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- include "casino.php";
- $game = new Game();
- $game->playGames();
- class Game
- {
- private $filename = '';
- private $inputFilename = '';
- private $bets = array();
- private $casino = NULL;
- public $balance = 0;
- /**
- * Game constructor.
- * @param string $in
- * @param string $out
- */
- public function __construct($in = 'in.txt', $out = 'out.txt')
- {
- date_default_timezone_set('UTC');
- $this->inputFilename = $in;
- $this->filename = $out;
- $this->casino = new Casino();
- $this->readData();
- }
- // Считывание входных данных
- public function readData()
- {
- $handle = @fopen(__DIR__ . DIRECTORY_SEPARATOR . $this->inputFilename, "r");
- if ($handle){
- $row = 0;
- if( ($buffer = fgets($handle, 4096)) !== false ){
- // Считываем количество ставок игрока
- $betCount = intval(trim($buffer));
- }else{
- echo "Error: Empty file ".$this->inputFilename."<br>";
- }
- // Считываем все ставки игрока
- for ($i=0; $i < $betCount; $i++) {
- if( !($buffer = fgets($handle, 4096)) !== false ){
- echo "Error: Error read bets in ".$this->inputFilename."<br>";
- die();
- }
- $buffer = trim($buffer);
- $arTmp = explode(" ", $buffer);
- if( count($arTmp) < 3 ){
- echo "Error: error read line ".$this->inputFilename."<br>";
- }else{
- array_push($this->bets, (object) array(
- "game_id" => $arTmp[0],
- "sum" => $arTmp[1],
- "team" => $arTmp[2],
- ));
- }
- }
- if( ($buffer = fgets($handle, 4096)) !== false ){
- // Считываем количество игр
- $gameCount = intval(trim($buffer));
- }else{
- echo "Error: Game count does not exist in file ".$this->inputFilename."<br>";
- }
- // Считываем все игры
- for ($i=0; $i < $gameCount; $i++) {
- if( !($buffer = fgets($handle, 4096)) !== false ){
- echo "Error: Error read bets in ".$this->inputFilename."<br>";
- die();
- }
- $buffer = trim($buffer);
- $arTmp = explode(" ", $buffer);
- if( count($arTmp) < 5 ){
- echo "Error: error read line ".$this->inputFilename."<br>";
- }else{
- // Добавляем игры в объект казино
- $this->casino->addGame($arTmp[0], $arTmp[1], $arTmp[2], $arTmp[3], $arTmp[4]);
- }
- }
- if (!feof($handle)){
- echo "Error: unexpected fgets() fail<br>";
- }
- fclose($handle);
- }
- }
- // Воспроизвести все игры
- public function playGames(){
- foreach ($this->bets as $bet) {
- // Играем в игру казино по ID. Если игра по id не найдена, то метод вернет false
- if( ($result = $this->casino->play($bet->game_id, $bet->sum, $bet->team) ) !== false ){
- $this->balance += $result;
- }else{
- echo "Error: Game ".$bet->game_id." does not exist<br>";
- }
- }
- $this->writeData();
- }
- // Записываем результат
- private function writeData()
- {
- $handle = @fopen(__DIR__ . DIRECTORY_SEPARATOR . $this->filename, "w");
- if ($handle)
- {
- @fwrite($handle, $this->balance);
- fclose($handle);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement