prefix = $prefix;
$this->round = $round;
$this->board = $board;
if (file_exists('translations.json')) {
static::$translations = json_decode(file_get_contents('translations.json'), TRUE);
}
$this->deals_by_tables = array();
if(!file_exists($this->get_filename())) {
throw new Exception('file not found: ' . $this->get_filename());
}
}
static function __($string) {
if (isset(static::$translations[$string])) {
return static::$translations[$string];
}
return $string;
}
function get_filename() {
return '..' . DIRECTORY_SEPARATOR . $this->prefix . $this->round . 'b-' . $this->board . '.html';
}
function set_deal($table, $deal) {
$this->deals_by_tables[$table] = $deal;
}
function output() {
$content = file_get_contents($this->get_filename());
$dom = str_get_html($content);
$header_td1 = $dom->find('/html/body/table/tr/td[class="bdcc12"]', 0);
$header_tr = $header_td1->parent;
$tr = @$header_tr->next_sibling();
while($tr) {
$td = $tr->find('td/a', 0);
if ($td) {
$table = trim($td->innertext);
$table = str_replace(' ', '', $table);
$table = (int)$table;
if($table && array_key_exists($table, $this->deals_by_tables)) {
$nextTr = $tr->next_sibling();
$contract1 = trim(str_replace(' ', '', $tr->find('td[class="bdc"]', 0)->innertext));
$score1 = trim(str_replace(' ', '', $tr->find('td', 6)->innertext));
$contract2 = trim(str_replace(' ', '', $tr->next_sibling()->find('td[class="bdc"]', 0)->innertext));
$score2 = trim(str_replace(' ', '', $nextTr->find('td', 5)->innertext));
$deal = $this->deals_by_tables[$table];
$insert = "" . static::__("Stół") . " $table" . " – " . static::__("Rozdanie") . " {$deal->deal_num}
";
// if is played on both tables of a match
// note that the contract field for arbitral scores starts with 'A' (e.g. 'ARB' or 'AAA')
if(($score1 !== '' || strpos($contract1, 'A') === 0)
&& ($score2 !== '' || strpos($contract2, 'A') === 0)) {
$insert .= $deal->html();
} else {
$insert .= '
...
'; } $tr->outertext = '
'.$suits[0].'
'.$suits[1].'
'.$suits[2].'
'.$suits[3];
return $str;
}
function format_ability($ability_num) {
$ability = $this->ability[$ability_num];
$ab = array($ability[0], $ability[2], $ability[3], $ability[4], $ability[5], $ability[6]);
foreach($ab as $k=>$v) {
switch($v) {
case 'A': $ab[$k] = '10'; break;
case 'B': $ab[$k] = '11'; break;
case 'C': $ab[$k] = '12'; break;
case 'D': $ab[$k] = '13'; break;
}
}
return "
$4 $5, $6', $minimax);
return $minimax;
}
}
define('TIMESTAMP_FILE', '.tdd-timestamps.cache');
define('RECORDS_FILE', '.tdd-records.cache');
function load_deals_for_tables($prefix, $round, $board_in_teamy) {
$db = unserialize(file_get_contents(RECORDS_FILE));
if (isset($db[$prefix])) {
if (isset($db[$prefix][$round])) {
if (isset($db[$prefix][$round][$board_in_teamy])) {
return $db[$prefix][$round][$board_in_teamy];
}
}
}
return array();
}
function get_record_files($directory = '.') {
return glob($directory . DIRECTORY_SEPARATOR . '*.pbn');
}
function get_files_timestamps($files = array()) {
return array_combine(
$files,
array_map('filemtime', $files)
);
}
function compile_record_database($files, $dbFile) {
$db = array();
foreach ($files as $filename) {
$filename = basename($filename);
$fileParts = array();
if (preg_match('/^(.*)-r(\d+)-t(\d+)-b(\d+)\.pbn$/', $filename, $fileParts)) {
$prefix = $fileParts[1];
if (!isset($db[$prefix])) {
$db[$prefix] = array();
}
$round = (int)($fileParts[2]);
if (!isset($db[$prefix][$round])) {
$db[$prefix][$round] = array();
}
$table = (int)($fileParts[3]);
$firstBoard = (int)($fileParts[4]);
$chunks = preg_split('/(\[Board "(\d+)"\])/', file_get_contents($filename), -1, PREG_SPLIT_DELIM_CAPTURE);
$boardHeader = '';
$boardNumber = 1;
$firstBoardNumber = -1;
foreach ($chunks as $chunk) {
$chunk = trim($chunk);
if (strpos($chunk, '% PBN') > -1) {
continue;
}
if (strpos($chunk, '[Board ') === 0) {
$boardHeader = $chunk;
continue;
}
if (strpos($chunk, '[') === 0) {
try {
$deal = new Deal($boardHeader . $chunk, $boardNumber);
$boardNumberJFR = $boardNumber + $firstBoard - $firstBoardNumber;
if (!isset($db[$prefix][$round][$boardNumberJFR])) {
$db[$prefix][$round][$boardNumberJFR] = array();
}
$db[$prefix][$round][$boardNumberJFR][$table] = $deal;
} catch (NoSuchDealNumber $e) {
// ignore if the deal does not exist in the file
}
} else {
$boardNumber = (int)($chunk);
if ($firstBoardNumber < 0) {
$firstBoardNumber = $boardNumber;
}
}
}
}
}
file_put_contents(RECORDS_FILE, serialize($db));
}
function refresh_board_database() {
$recordFiles = get_record_files();
$savedTimestamps = file_exists(TIMESTAMP_FILE) ? json_decode(file_get_contents('.tdd-timestamps.cache'), TRUE) : array();
$timestamps = get_files_timestamps($recordFiles);
if (array_diff_assoc($savedTimestamps, $timestamps) || array_diff_assoc($timestamps, $savedTimestamps)) {
compile_record_database($recordFiles, RECORDS_FILE);
file_put_contents(TIMESTAMP_FILE, json_encode($timestamps));
}
}