4 public function __construct($score_id) {
5 $this->score_id = $score_id;
9 public function get_filename($dir, $ext)
11 $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000);
12 return sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext);
16 public function show($dir, $ext, $content_type)
18 $filename = $this->get_filename($dir, $ext);
20 if (!file_exists($filename)) {
21 http_response_code(404);
25 $contents = file_get_contents($filename);
26 $etag = md5($contents);
28 if ($etag === filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH')) {
29 http_response_code(304);
33 $content_encoding = self::get_content_encoding();
34 header("Etag: ".$etag);
35 header("Content-Type: ".$content_type);
37 if ($content_encoding !== NULL) {
38 header("Content-Encoding: ".$content_encoding);
41 echo gzdecode($contents);
46 public function save($dir, $ext, $contents)
48 if ($contents === FALSE) return;
50 umask(2); // Group書き込み権限許可
52 $filename = $this->get_filename($dir, $ext);
53 $dirname = dirname($filename);
55 if (!file_exists($dirname)) {
56 mkdir($dirname, 02775, TRUE);
59 $zp = gzopen($filename, "w9");
61 foreach ($contents as $line) {
70 public function exists($dir, $ext)
72 $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000);
73 $filename = sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext);
75 return file_exists($filename);
80 * キャラクタダンプの死ぬ直前のメッセージもしくは勝利メッセージを取得する
82 * @return array 死ぬ直前のメッセージもしくは勝利メッセージを1行1要素にした文字列の配列
84 public function get_last_message()
86 $zp = gzopen($this->get_filename('dumps', 'txt'), 'r');
87 if ($zp === FALSE) return [];
94 if (preg_match('/^\s*\[(.*)\]\s*$/u', $line, $matches)) {
95 if ($matches[1] == '*勝利*メッセージ' ||
96 $matches[1] == '死ぬ直前のメッセージ') {
99 else if ($in_message) {
105 $result[] = rtrim($line, "\n");
112 private static function browser_accept_encodings()
114 $accept_encoding = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_ENCODING');
115 if ($accept_encoding == NULL) return [];
117 return array_map('trim', explode(",", $accept_encoding));
121 private static function get_content_encoding()
123 $supported_gzip_encodings = array_intersect(
124 self::browser_accept_encodings(),
127 return array_shift($supported_gzip_encodings);