<commit_before><?php
namespace bmtmgr;

class Discipline extends \bmtmgr\Model {
	public $id;
	public $tournament_id;
	public $name;
	public $dtype;
	public $ages;
	public $leagues;
	public $note;
	public $capacity;

	protected function __construct($row, $_is_new) {
		$this->id = $row['id'];
		$this->tournament_id = $row['tournament_id'];
		$this->name = $row['name'];
		$this->dtype = $row['dtype'];
		$this->ages = $row['ages'];
		$this->leagues = $row['leagues'];
		$this->note = $row['note'];
		$this->capacity = $row['capacity'];

		$this->_is_new = $_is_new;
	}

	public function player_gender() {
		return $this->male_player() ? 'm': 'f';
	}

	public function male_player() {
		return in_array($this->dtype, ['MS', 'MD', 'MX']);
	}

	public function partner_gender() {
		switch ($this->dtype) {
		case 'MS':
		case 'WS':
			return null;
		case 'MD':
			return 'm';
		case 'WD':
		case 'MX':
			return 'f';
		default:
			\assert(false);
		}
	}

	public function male_partner() {
		return in_array($this->dtype, ['MD']);
	}

	public function with_partner() {
		return in_array($this->dtype, ['MD', 'WD', 'MX']);
	}

	public function entry_player_count() {
		return $this->with_partner() ? 2 : 1;
	}

	public static function create($tournament, $name, $dtype) {
		return new static([
			'id' => null,
			'tournament_id' => $tournament->id,
			'name' => $name,
			'dtype' => $dtype,
			'ages' => null,
			'leagues' => null,
			'capacity' => null,
			'note' => null,
		], true);
	}

	public function get_tournament() {
		return Tournament::by_id($this->tournament_id);
	}

	public function get_entries($add_sql='', $creation_callback=null) {
		return Entry::get_all('WHERE entry.discipline_id=? ' . $add_sql, [$this->id], [], '', $creation_callback);
	}

	public function get_entry_rows() {
		$players = Player::get_all(
			' WHERE entry.discipline_id = ? AND (entry.player_id = player.id OR entry.partner_id = player.id)',
			[$this->id],
			['entry']
		);
		$player_dict = [];
		foreach ($players as $p) {
			$player_dict[$p->id] = $p;
		}

		$clubs = User::get_all(
			' WHERE entry.discipline_id = ? AND (entry.player_club_id = user.id OR entry.partner_club_id = user.id)',
			[$this->id],
			['entry']
		);
		$club_dict = [];
		foreach ($clubs as $c) {
			$club_dict[$c->id] = $c;
		}

		$res = static::get_entries(' ORDER by entry.id', function($row) use ($player_dict, $club_dict) {
			return [
				'id' => $row['id'],
				'player' => $player_dict[$row['player_id']],
				'player_club' => $club_dict[$row['player_club_id']],
				'player_club_is_special' => $row['player_club_id'] != $player_dict[$row['player_id']]->club_id,
				'partner' => ($row['partner_id'] ? $player_dict[$row['partner_id']] : null),
				'partner_club' => ($row['partner_club_id'] ? $club_dict[$row['partner_club_id']] : null),
				'partner_club_is_special' => (($row['partner_id'] && $row['partner_club_id']) ? ($row['partner_club_id'] != $player_dict[$row['partner_id']]->club_id) : null),
				'same_club' => ($row['partner_club_id'] ? $row['player_club_id'] == $row['partner_club_id'] : null),
				'email' => $row['email'],
				'created_time_str' => \date('Y-m-d H:i', \intval($row['created_time'])),
			];
		});
		for ($i = 0;$i < \count($res);$i++) {
			$res[$i]['numstr'] = \strval($i + 1);
		}
		return $res;
	}

	protected function find_conflicting($player_id) {
		$vals = [
			':dtype' => $this->dtype,
			':tournament_id' => $this->tournament_id,
			':player_id' => $player_id,
		];<commit_msg>show capacity<commit_after><?php
namespace bmtmgr;

class Discipline extends \bmtmgr\Model {
	public $id;
	public $tournament_id;
	public $name;
	public $dtype;
	public $ages;
	public $leagues;
	public $note;
	public $capacity;

	protected function __construct($row, $_is_new) {
		$this->id = $row['id'];
		$this->tournament_id = $row['tournament_id'];
		$this->name = $row['name'];
		$this->dtype = $row['dtype'];
		$this->ages = $row['ages'];
		$this->leagues = $row['leagues'];
		$this->note = $row['note'];
		$this->capacity = $row['capacity'];

		$this->_is_new = $_is_new;
	}

	public function player_gender() {
		return $this->male_player() ? 'm': 'f';
	}

	public function male_player() {
		return in_array($this->dtype, ['MS', 'MD', 'MX']);
	}

	public function partner_gender() {
		switch ($this->dtype) {
		case 'MS':
		case 'WS':
			return null;
		case 'MD':
			return 'm';
		case 'WD':
		case 'MX':
			return 'f';
		default:
			\assert(false);
		}
	}

	public function male_partner() {
		return in_array($this->dtype, ['MD']);
	}

	public function with_partner() {
		return in_array($this->dtype, ['MD', 'WD', 'MX']);
	}

	public function entry_player_count() {
		return $this->with_partner() ? 2 : 1;
	}

	public static function create($tournament, $name, $dtype) {
		return new static([
			'id' => null,
			'tournament_id' => $tournament->id,
			'name' => $name,
			'dtype' => $dtype,
			'ages' => null,
			'leagues' => null,
			'capacity' => null,
			'note' => null,
		], true);
	}

	public function get_tournament() {
		return Tournament::by_id($this->tournament_id);
	}

	public function get_entries($add_sql='', $creation_callback=null) {
		return Entry::get_all('WHERE entry.discipline_id=? ' . $add_sql, [$this->id], [], '', $creation_callback);
	}

	public function get_entry_rows() {
		$players = Player::get_all(
			' WHERE entry.discipline_id = ? AND (entry.player_id = player.id OR entry.partner_id = player.id)',
			[$this->id],
			['entry']
		);
		$player_dict = [];
		foreach ($players as $p) {
			$player_dict[$p->id] = $p;
		}

		$clubs = User::get_all(
			' WHERE entry.discipline_id = ? AND (entry.player_club_id = user.id OR entry.partner_club_id = user.id)',
			[$this->id],
			['entry']
		);
		$club_dict = [];
		foreach ($clubs as $c) {
			$club_dict[$c->id] = $c;
		}

		$res = static::get_entries(' ORDER by entry.id', function($row) use ($player_dict, $club_dict) {
			return [
				'id' => $row['id'],
				'player' => $player_dict[$row['player_id']],
				'player_club' => $club_dict[$row['player_club_id']],
				'player_club_is_special' => $row['player_club_id'] != $player_dict[$row['player_id']]->club_id,
				'partner' => ($row['partner_id'] ? $player_dict[$row['partner_id']] : null),
				'partner_club' => ($row['partner_club_id'] ? $club_dict[$row['partner_club_id']] : null),
				'partner_club_is_special' => (($row['partner_id'] && $row['partner_club_id']) ? ($row['partner_club_id'] != $player_dict[$row['partner_id']]->club_id) : null),
				'same_club' => ($row['partner_club_id'] ? $row['player_club_id'] == $row['partner_club_id'] : null),
				'email' => $row['email'],
				'created_time_str' => \date('Y-m-d H:i', \intval($row['created_time'])),
			];
		});
		for ($i = 0;$i < \count($res);$i++) {
			$res[$i]['numstr'] = (
				(($this->capacity === NULL) || ($i < $this->capacity)) ?
				\strval($i + 1) :
				\sprintf('%d+%d', $this->capacity, $i - $this->capacity + 1)
			);
		}
		return $res;
	}

	protected function find_conflicting($player_id) {
		$vals = [
			':dtype' => $this->dtype,
			':tournament_id' => $this->tournament_id,
			':player_id' => $player_id,
		];
{'fraction_non_alphanumeric': 0.21678616617460986, 'fraction_numerical': 0.0012652889076339097, 'mean_word_length': 4.219369038884813, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 101, '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': '197064', 'n_tokens_mistral': 3049, 'n_tokens_neox': 2733, 'n_words': 508}