OSDN Git Service

[feature]新着スコア登録時に新着順スコア一覧のATOM形式フィードを生成
authorHabu <habu@users.sourceforge.jp>
Sun, 18 Mar 2018 09:04:33 +0000 (18:04 +0900)
committerHabu <habu@users.sourceforge.jp>
Sun, 18 Mar 2018 09:04:33 +0000 (18:04 +0900)
dump_file.inc
feed_maker.inc [new file with mode: 0644]
register_score.php

index ccbf1bc..e0a85f8 100644 (file)
@@ -76,6 +76,39 @@ class DumpFile
     }
 
 
+    /**
+     * キャラクタダンプの死ぬ直前のメッセージもしくは勝利メッセージを取得する
+     *
+     * @return array 死ぬ直前のメッセージもしくは勝利メッセージを1行1要素にした文字列の配列
+     */
+    public function get_last_message()
+    {
+        $zp = gzopen($this->get_filename('dumps', 'txt'), 'r');
+        if ($zp === FALSE) return [];
+
+        $in_message = FALSE;
+        $result = [];
+        while (!gzeof($zp)) {
+            $line = gzgets($zp);
+
+            if (preg_match('/^\s*\[(.*)\]\s*$/u', $line, $matches)) {
+                if ($matches[1] == '*勝利*メッセージ' ||
+                    $matches[1] == '死ぬ直前のメッセージ') {
+                    $in_message = TRUE;
+                }
+                else if ($in_message) {
+                    break;
+                }
+            }
+
+            if ($in_message) {
+                $result[] = rtrim($line, "\n");
+            }
+        }
+
+        return $result;
+    }
+
     private static function browser_accept_encodings()
     {
         $accept_encoding = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_ENCODING');
diff --git a/feed_maker.inc b/feed_maker.inc
new file mode 100644 (file)
index 0000000..7f029d5
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+require_once 'dump_file.inc';
+require_once 'FeedWriter/vendor/autoload.php';
+
+use \FeedWriter\ATOM;
+
+class FeedMaker
+{
+    public function __construct($db)
+    {
+        $this->db = $db;
+    }
+
+    /**
+     * ATOM形式の新着スコア一覧のフィードを生成する
+     *
+     * @param $feed_path フィードを書き込むファイルのパス
+     */
+    public function make_atom_feed($feed_path)
+    {
+        $base_url = "http://". filter_input(INPUT_SERVER, 'SERVER_NAME') . dirname(filter_input(INPUT_SERVER, 'SCRIPT_NAME'));
+
+        $feed = new ATOM();
+
+        $feed->setTitle('変愚蛮怒 スコアランキング (新着順)');
+        $feed->setDescription('変愚蛮怒新着スコア');
+        $feed->setLink("{$base_url}/score_ranking?mode=newcome");
+        $feed->setDate(new DateTime());
+        $feed->setImage("http://".filter_input(INPUT_SERVER, 'SERVER_NAME')."/tama.gif");
+        $feed->setSelfLink("{$base_url}/$feed_path");
+
+        $this->db->set_sort_mode('newcome');
+        $search_result = $this->db->search_score(0, 30); // 最新30件を取得
+
+        foreach ($search_result['scores'] as $s) {
+            $item = $feed->createNewItem();
+
+            $dump_url = "${base_url}/show_dump.php?score_id={$s['score_id']}";
+            $item->setTitle("{$s['personality_name']}{$s['name']} Score:{$s['score']} {$s['race_name']} {$s['class_name']} {$s['death_reason']} @{$s['depth']}F");
+            $item->setLink($dump_url);
+            $item->setDate($s['date']);
+
+            $dump_file = new DumpFile($s['score_id']);
+            $contents = implode(
+                '',
+                array_map(function ($v) {
+                    return htmlentities($v).'<br>';
+                }, $dump_file->get_last_message())
+            );
+            $item->setContent($contents);
+
+            $feed->addItem($item);
+        }
+
+        return file_put_contents($feed_path, $feed->generateFeed());
+    }
+}
index d8a7143..cf4bf79 100644 (file)
@@ -6,6 +6,7 @@ ini_set('error_log', 'errors/'.pathinfo(__FILE__, PATHINFO_FILENAME).'.log');
 
 require_once "db_common.inc";
 require_once "dump_file.inc";
+require_once "feed_maker.inc";
 
 // 登録が成功しない場合、HTTPレスポンスコード 400 Bad Request を返す
 http_response_code(400);
@@ -177,3 +178,6 @@ $dumpfile->save('screens', 'html', $split_contents[2]);
 
 // 登録成功、HTTPレスポンスコード 200 OK を返す
 http_response_code(200);
+
+$feed_maker = new FeedMaker($db);
+$feed_maker->make_atom_feed("feed/newcome-atom.xml");