<commit_before><?php

namespace App\Models;

class Game extends \Pragma\ORM\Model
{
    const PHASE_DAY     = 0;
    const PHASE_NIGHT   = 1;

    public $players;

    public function __construct()
    {
        return parent::__construct('game');
    }

    public function open($gameId)
    {
        $ret = parent::open($gameId);

        $this->players = Player::forge()->where('game_id', '=', $this->id)->get_objects();

        return $ret;
    }

    //the KeyId is the string used by a player in order to authenticate in-game (security is not a concern here)
    public static function genKeyId()
    {
        //TODO
    }

    public function addPlayer(Player $player)
    {
	    if ($player == null) {
		    return false;
		}

		if ($this->started) {
			return false;
		}

		if ($this->getPlayerByName($player->name)) {
			return false;
		}

		$player->game_id = $this->id;
		$player->save();

        $this->players[] = $player;

        return true;
    }

    public function getPlayerByRole($role)
    {
        $tmp = array();
        foreach ($this->players as $player) {
            if ($role == $player->role) {
                array_push($tmp, $player);
            }
        }
        if (count($tmp) > 1) {
            return $tmp;
        }
        if (count($tmp) == 1) {
            return $tmp[0];
        }

        return null;
    }

    public function getPlayerByName($name)
    {
        foreach ($this->players as $player) {
            if ($name == $player->name) {
                return $player;
            }
        }

        return null;
    }

    public function isAttached(Player $player)
    {
        foreach ($this->players as $attachedPlayer) {
            if ($player->id == $attachedPlayer->id) {
                return true;
            }
        }

        return false;
    }

    public function startGame()
    {
        if (count($this->players) >= 7) {
            $this->started = 1;
            $this->initGame();
            $this->save();
            return true;
        }
    return false;
    }

    public function initGame()
    {
        if (count($this->players) < 7) {
            return false;
        }

        shuffle($this->players);

        $bad_guy = array_shift($this->players);
        $bad_guy->setGenome(Player::GENOME_HOST);
        $bad_guy->mutate();

        $first_doc = array_shift($this->players);
        $first_doc->role = 'medic';<commit_msg>set $game->turn to 1 in startGame()<commit_after><?php

namespace App\Models;

class Game extends \Pragma\ORM\Model
{
    const PHASE_DAY     = 0;
    const PHASE_NIGHT   = 1;

    public $players;

    public function __construct()
    {
        return parent::__construct('game');
    }

    public function open($gameId)
    {
        $ret = parent::open($gameId);

        $this->players = Player::forge()->where('game_id', '=', $this->id)->get_objects();

        return $ret;
    }

    //the KeyId is the string used by a player in order to authenticate in-game (security is not a concern here)
    public static function genKeyId()
    {
        //TODO
    }

    public function addPlayer(Player $player)
    {
	    if ($player == null) {
		    return false;
		}

		if ($this->started) {
			return false;
		}

		if ($this->getPlayerByName($player->name)) {
			return false;
		}

		$player->game_id = $this->id;
		$player->save();

        $this->players[] = $player;

        return true;
    }

    public function getPlayerByRole($role)
    {
        $tmp = array();
        foreach ($this->players as $player) {
            if ($role == $player->role) {
                array_push($tmp, $player);
            }
        }
        if (count($tmp) > 1) {
            return $tmp;
        }
        if (count($tmp) == 1) {
            return $tmp[0];
        }

        return null;
    }

    public function getPlayerByName($name)
    {
        foreach ($this->players as $player) {
            if ($name == $player->name) {
                return $player;
            }
        }

        return null;
    }

    public function isAttached(Player $player)
    {
        foreach ($this->players as $attachedPlayer) {
            if ($player->id == $attachedPlayer->id) {
                return true;
            }
        }

        return false;
    }

    public function startGame()
    {
        if (count($this->players) >= 7) {
            $this->started = 1;
            $this->turn = 1;
            $this->initGame();
            $this->save();
            return true;
        }
    return false;
    }

    public function initGame()
    {
        if (count($this->players) < 7) {
            return false;
        }

        shuffle($this->players);

        $bad_guy = array_shift($this->players);
        $bad_guy->setGenome(Player::GENOME_HOST);
        $bad_guy->mutate();

        $first_doc = array_shift($this->players);
        $first_doc->role = 'medic';
{'fraction_non_alphanumeric': 0.1403294691885296, 'fraction_numerical': 0.0036607687614399025, 'mean_word_length': 1.68596395412343, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2181046', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1477, 'n_words': 336}