OSDN Git Service

f69f9944fc5c6bd314a1f3a3a0c1da827544d32d
[hengband/web.git] / score / score_ranking.php
1 <?php
2 //ini_set('display_errors', 'On');
3
4 ini_set('log_errors', 'On');
5 ini_set('error_log', 'errors/'.pathinfo(__FILE__, PATHINFO_FILENAME).'.log');
6
7 require_once "db_common.inc";
8 require_once "dump_file.inc";
9 require_once "web_template.inc";
10
11 ini_set('zlib.output_compression', 'On');
12
13
14 /**
15  * ページ情報を計算する
16  *
17  * @param integer $total_data_count 全データ件数
18  * @param integer $start_num GETパラメータで渡された開始データ番号
19  * @param integer $data_count_per_page 1ページあたりのデータ件数
20  *
21  * @return array 計算したページ情報を保持する連想配列
22  */
23 function calc_page_info($total_data_count, $start_num, $data_count_per_page)
24 {
25     $current_page = intval($start_num / $data_count_per_page);
26     $last_page = intval(($total_data_count - 1) / $data_count_per_page);
27     $navi_page_range_start = ($current_page - 5) > 0 ? $current_page - 5: 0;
28     $navi_page_range_count = max(0, min(9, $last_page - $navi_page_range_start));
29     $navi_page_list = range($navi_page_range_start, $navi_page_range_start + $navi_page_range_count);
30
31     $pageinfo['current'] = $current_page;
32     $pageinfo['last'] = $last_page;
33     $pageinfo['navi_list'] = $navi_page_list;
34
35     $pageinfo['total_data_count'] = $total_data_count;
36     $pageinfo['data_count_per_page'] = $data_count_per_page;
37
38     return $pageinfo;
39 }
40
41
42 /**
43  * ページナビゲーションテーブルを出力する
44  *
45  * @param resource $fp 出力先リソースへのハンドル
46  * @param array $pageinfo calc_page_info()関数で取得したページ情報を保持する連想配列
47  */
48 function print_navi_page_table($fp, $pageinfo)
49 {
50     if (count($pageinfo['navi_list']) <= 1) return;
51
52     $href_base = filter_input(INPUT_SERVER, 'SCRIPT_NAME')."?"
53                .preg_replace('/(&?start=\w+)/', '', filter_input(INPUT_SERVER, 'QUERY_STRING'));
54     if (strpos($href_base, "?") === FALSE) {
55         $href_base .= "?";
56     }
57
58     fwrite($fp, "<table align='center'>\n"
59            ."<tr>\n");
60
61     if ($pageinfo['current'] > 0) {
62         $href = $href_base . "&start=". ($pageinfo['current'] - 1) * $pageinfo['data_count_per_page'];
63         fwrite($fp, "<td><a href={$href}>&lt; 前へ</a></td>\n");
64     }
65
66     foreach ($pageinfo['navi_list'] as $page) {
67         $page_num = $page + 1;
68         $href = $href_base . "&start=". $page * $pageinfo['data_count_per_page'];
69         if ($page === $pageinfo['current']) {
70             fwrite($fp, "<td>$page_num</td>\n");
71         } else {
72             fwrite($fp, "<td><a href={$href}>$page_num</a></td>\n");
73         }
74     }
75
76     if ($pageinfo['current'] < $pageinfo['last']) {
77         $href = $href_base . "&start=". ($pageinfo['current'] + 1) * $pageinfo['data_count_per_page'];
78         fwrite($fp, "<td><a href={$href}>次へ &gt;</a></td>\n");
79     }
80
81     fwrite($fp, "</tr>\n"
82            ."</table>\n");
83 }
84
85
86 /**
87  * スコアランキングテーブルを出力する
88  *
89  * @param resource $fp 出力先リソースへのハンドル
90  * @param array $scores スコア
91  * @param integer $rank_start 順位の開始番号(0オリジン)
92  */
93 function print_score_table($fp, $scores, $rank_start)
94 {
95     fwrite($fp, <<<EOM
96 <table class="score">
97 <thead>
98 <tr>
99 <th>順位</th>
100 <th>スコア</th>
101 <th>日付</th>
102 <th>名前</th>
103 <th>種族</th>
104 <th>職業</th>
105 <th>性別</th>
106 <th>死因</th>
107 </tr>
108 </thead>
109
110 EOM
111     );
112
113     fwrite($fp, "<tbody>\n");
114     foreach($scores as $idx => $score) {
115         $rank = $rank_start + $idx + 1;
116         $date = substr($score['date'], 0, 10); // 日時から日付部分を取り出す
117         $sex_str = $score['sex'] ? "男" : "女";
118         $depth = !$score['winner'] ? $score['depth']."階, " : "";
119         $realms = isset($score['realms_name']) ? "(".$score['realms_name'].")" : "";
120         $dumpfile = new DumpFile($score['score_id']);
121
122         if ($dumpfile->exists('dumps', 'txt')) {
123             $name = "<a href=\"show_dump.php?score_id={$score['score_id']}\">{$score['personality_name']}{$score['name']}</a>";
124         } else {
125             $name = "{$score['personality_name']}{$score['name']}";
126         }
127         fwrite($fp, <<<EOM
128 <tr>
129 <td>$rank</td>
130 <td class="number">{$score['score']}</td>
131 <td><nobr>$date</nobr></td>
132 <td>$name</td>
133 <td>{$score['race_name']}</td>
134 <td>{$score['class_name']}$realms</td>
135 <td>$sex_str</td>
136
137 EOM
138         );
139         if ($dumpfile->exists('screens', 'html')) {
140             fwrite($fp, "<td><a href=\"show_screen.php?score_id={$score['score_id']}\">{$score['death_reason']}</a>");
141         } else {
142             fwrite($fp, "<td>{$score['death_reason']}");
143         }
144         fwrite($fp, "<br>({$depth}{$score['version']})</td>\n".
145                "</tr>\n");
146     }
147     fwrite($fp, "</tbody>\n");
148     fwrite($fp, "</table>\n");
149 }
150
151 $db = new ScoreDB();
152
153 $start_num = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT) ?: 0;
154 $search_result = $db->search_score($start_num, 50);
155
156 $pageinfo = calc_page_info($search_result['total_data_count'], $start_num, 50);
157
158
159 $wt = new WebTemplate();
160 $wt->set_title("変愚蛮怒 スコアランキング");
161 $wt->add_head_contents('<link rel="stylesheet" type="text/css" href="css/score-table.css">');
162 $fp = $wt->main_contents_fp();
163 fprintf($fp, "<h2>変愚蛮怒 歴代スコア (%s)</h2>\n", $db->get_sort_mode_name());
164 fprintf($fp, <<<EOM
165 <div align="right">
166 <small>
167 件数 %d 件 (%.2f 秒)
168 </small>
169 </div>
170
171 EOM
172         ,$search_result['total_data_count'], $search_result['elapsed_time']
173 );
174
175 print_navi_page_table($fp, $pageinfo);
176 print_score_table($fp, $search_result['scores'], $pageinfo['current'] * $pageinfo['data_count_per_page']);
177 print_navi_page_table($fp, $pageinfo);
178
179 $wt->print_page();