diff options
author | emkael <emkael@tlen.pl> | 2018-11-13 22:19:43 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2018-11-14 00:15:42 +0100 |
commit | 9e6bd1bd8eeb3aa9dd3ca43dbe7f798b88c46dd0 (patch) | |
tree | 72f2812979872c7f2373016aedb45d5c23acf3de /tdd | |
parent | 6a675104a9c61ff128fb46b0e471894c4423c1bb (diff) |
Compiling all PBN data into a dictionary and caching it
Diffstat (limited to 'tdd')
-rw-r--r-- | tdd/tdd-bootstrap.php | 106 | ||||
-rw-r--r-- | tdd/tdd-protocol.php | 2 |
2 files changed, 85 insertions, 23 deletions
diff --git a/tdd/tdd-bootstrap.php b/tdd/tdd-bootstrap.php index c01a553..6efbf7b 100644 --- a/tdd/tdd-bootstrap.php +++ b/tdd/tdd-bootstrap.php @@ -187,31 +187,91 @@ class Deal { } +define('TIMESTAMP_FILE', '.tdd-timestamps.cache'); +define('RECORDS_FILE', '.tdd-records.cache'); + function load_deals_for_tables($prefix, $round, $board_in_teamy) { - $deals_by_tables = array(); - - $prefix = preg_quote($prefix); - $filename_regex = "/^$prefix-r$round-t(\d+)-b(\d+).pbn$/"; - foreach(scandir('.') as $filename) { - if(preg_match($filename_regex, $filename, $match)) { - $file_table = $match[1]; - $file_start_board = $match[2]; - - $first_num_in_pbn = array(); - preg_match('/\[Board "(\d+)"\]/', file_get_contents($filename), $first_num_in_pbn); - $first_num_in_pbn = $first_num_in_pbn ? intval($first_num_in_pbn[1]) : 1; - - // 1 in teamy -> 1 in pbn; 24 in teamy -> 24 in pbn; 25 in teamy -> 1 in pbn - // if PBN doesn't start with Board 1, it's been adjusted - $num_in_pbn = $board_in_teamy - $file_start_board + $first_num_in_pbn; - - try { - $deal = new Deal(file_get_contents($filename), $num_in_pbn); - $deals_by_tables[$file_table] = $deal; - } catch (NoSuchDealNumber $e) { - // ignore if the deal does not exist in the file + $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 $deals_by_tables; + 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)); + } } diff --git a/tdd/tdd-protocol.php b/tdd/tdd-protocol.php index 3dcc0d8..7130599 100644 --- a/tdd/tdd-protocol.php +++ b/tdd/tdd-protocol.php @@ -6,6 +6,8 @@ $board = (int)$_GET['board']; require_once('tdd-bootstrap.php'); +refresh_board_database(); + $protocol = new Protocol($prefix, $round, $board); // security check |