5 * @author Hengband Team
9 #include "core/asking-player.h"
10 #include "core/stuff-handler.h"
11 #include "core/turn-compensator.h"
12 #include "core/visuals-reseter.h"
13 #include "dungeon/dungeon.h"
14 #include "floor/floor.h"
15 #include "game-option/special-options.h"
16 #include "io-dump/character-dump.h"
18 #include "io/input-key-acceptor.h"
19 #include "player/player-personality.h"
20 #include "system/angband-version.h"
21 #include "term/gameterm.h"
22 #include "util/angband-files.h"
23 #include "view/display-messages.h"
24 #include "world/world.h"
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
46 concptr screen_dump = NULL;
49 * internet resource value
51 #define HTTP_PROXY "" /*!< デフォルトのプロキシURL / Default proxy url */
52 #define HTTP_PROXY_PORT 0 /*!< デフォルトのプロキシポート / Default proxy port */
53 #define HTTP_TIMEOUT 20 /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
54 #define SCORE_SERVER "hengband.osdn.jp" /*!< デフォルトのスコアサーバURL / Default score server url */
55 #define SCORE_PORT 80 /*!< デフォルトのスコアサーバポート / Default score server port */
58 #define SCORE_PATH "http://hengband.osdn.jp/score/register_score.php" /*!< スコア開示URL */
60 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore-en/score.cgi" /*!< スコア開示URL */
64 * simple buffer library
72 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
76 * @return 確保したバッファの参照ポインタ
78 static BUF* buf_new(void)
81 p = malloc(sizeof(BUF));
85 p->max_size = BUFSIZE;
86 p->data = malloc(BUFSIZE);
99 * @param b 解放するバッファの参照ポインタ
101 static void buf_delete(BUF *b)
109 * @brief 転送用バッファにデータを追加する
110 * @param buf 追加先バッファの参照ポインタ
115 static int buf_append(BUF *buf, concptr data, size_t size)
117 while (buf->size + size > buf->max_size)
120 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
122 memcpy(tmp, buf->data, buf->max_size);
129 memcpy(buf->data + buf->size, data, size);
137 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
138 * @param buf 追加先バッファの参照ポインタ
139 * @param fmt 文字列フォーマット
142 static int buf_sprintf(BUF *buf, concptr fmt, ...)
149 #if defined(HAVE_VSNPRINTF)
150 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
152 ret = vsprintf(tmpbuf, fmt, ap);
156 if (ret < 0) return -1;
158 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
164 * @brief HTTPによるダンプ内容伝送
167 * @param buf 伝送内容バッファ
170 static bool http_post(int sd, concptr url, BUF *buf)
173 char response_buf[1024] = "";
174 concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
177 buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
178 buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
179 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
181 buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
182 buf_sprintf(output, "Content-Encoding: binary\r\n");
185 buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
188 buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
191 buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
193 buf_sprintf(output, "\r\n");
194 buf_append(output, buf->data, buf->size);
196 soc_write(sd, output->data, output->size);
198 soc_read(sd, response_buf, sizeof(response_buf));
200 return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
205 * @brief キャラクタダンプを作って BUFに保存
206 * @param creature_ptr プレーヤーへの参照ポインタ
207 * @param dumpbuf 伝送内容バッファ
210 static errr make_dump(player_type *creature_ptr, BUF* dumpbuf, void(*update_playtime)(void), display_player_pf display_player, map_name_pf map_name)
214 GAME_TEXT file_name[1024];
216 /* Open a new file */
217 fff = angband_fopen_temp(file_name, 1024);
221 msg_format("一時ファイル %s を作成できませんでした。", file_name);
223 msg_format("Failed to create temporary file %s.", file_name);
229 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
230 make_character_dump(creature_ptr, fff, update_playtime, display_player, map_name);
234 fff = angband_fopen(file_name, "r");
236 while (fgets(buf, 1024, fff))
238 (void)buf_sprintf(dumpbuf, "%s", buf);
249 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
250 * @return 作成したスクリーンダンプの参照ポインタ
252 concptr make_screen_dump(player_type *creature_ptr, void(*process_autopick_file_command)(char*))
254 static concptr html_head[] = {
255 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
259 static concptr html_foot[] = {
261 "</body>\n</html>\n",
266 Term_get_size(&wid, &hgt);
270 screen_buf = buf_new();
271 if (screen_buf == NULL) return (NULL);
273 bool old_use_graphics = use_graphics;
274 if (old_use_graphics)
276 /* Clear -more- prompt first */
279 use_graphics = FALSE;
280 reset_visuals(creature_ptr, process_autopick_file_command);
282 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
283 handle_stuff(creature_ptr);
286 for (int i = 0; html_head[i]; i++)
287 buf_sprintf(screen_buf, html_head[i]);
289 /* Dump the screen */
290 for (int y = 0; y < hgt; y++)
294 buf_sprintf(screen_buf, "\n");
297 TERM_COLOR a = 0, old_a = 0;
299 for (int x = 0; x < wid - 1; x++)
303 /* Get the attr/char */
304 (void)(Term_what(x, y, &a, &c));
308 case '&': cc = "&"; break;
309 case '<': cc = "<"; break;
310 case '>': cc = ">"; break;
311 case '"': cc = """; break;
312 case '\'': cc = "'"; break;
314 case 0x1f: c = '.'; break;
315 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
320 if ((y == 0 && x == 0) || a != old_a) {
321 rv = angband_color_table[a][1];
322 gv = angband_color_table[a][2];
323 bv = angband_color_table[a][3];
324 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">",
325 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
330 buf_sprintf(screen_buf, "%s", cc);
332 buf_sprintf(screen_buf, "%c", c);
336 buf_sprintf(screen_buf, "</font>");
338 for (int i = 0; html_foot[i]; i++)
339 buf_sprintf(screen_buf, html_foot[i]);
341 /* Screen dump size is too big ? */
343 if (screen_buf->size + 1 > SCREEN_BUF_MAX_SIZE)
349 /* Terminate string */
350 buf_append(screen_buf, "", 1);
352 ret = string_make(screen_buf->data);
356 buf_delete(screen_buf);
358 if (!old_use_graphics) return ret;
361 reset_visuals(creature_ptr, process_autopick_file_command);
363 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
364 handle_stuff(creature_ptr);
370 * todo メッセージは言語選択の関数マクロで何とかならんか?
371 * @brief スコア転送処理のメインルーチン
372 * @param creature_ptr プレーヤーへの参照ポインタ
373 * @return 正常終了の時0、異常があったら1
375 errr report_score(player_type *creature_ptr, void(*update_playtime)(void), display_player_pf display_player, map_name_pf map_name)
379 WORD wVersionRequested = (WORD)((1) | (1 << 8));
385 char seikakutmp[128];
387 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
389 sprintf(seikakutmp, "%s ", ap_ptr->title);
392 buf_sprintf(score, "name: %s\n", creature_ptr->name);
394 buf_sprintf(score, "version: 変愚蛮怒 %d.%d.%d\n",
395 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
397 buf_sprintf(score, "version: Hengband %d.%d.%d\n",
398 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
400 buf_sprintf(score, "score: %d\n", calc_score(creature_ptr));
401 buf_sprintf(score, "level: %d\n", creature_ptr->lev);
402 buf_sprintf(score, "depth: %d\n", creature_ptr->current_floor_ptr->dun_level);
403 buf_sprintf(score, "maxlv: %d\n", creature_ptr->max_plv);
404 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
405 buf_sprintf(score, "au: %d\n", creature_ptr->au);
406 buf_sprintf(score, "turns: %d\n", turn_real(creature_ptr, current_world_ptr->game_turn));
407 buf_sprintf(score, "sex: %d\n", creature_ptr->psex);
408 buf_sprintf(score, "race: %s\n", rp_ptr->title);
409 buf_sprintf(score, "class: %s\n", cp_ptr->title);
410 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
411 buf_sprintf(score, "realm1: %s\n", realm_names[creature_ptr->realm1]);
412 buf_sprintf(score, "realm2: %s\n", realm_names[creature_ptr->realm2]);
413 buf_sprintf(score, "killer: %s\n", creature_ptr->died_from);
414 buf_sprintf(score, "-----charcter dump-----\n");
416 make_dump(creature_ptr, score, update_playtime, display_player, map_name);
420 buf_sprintf(score, "-----screen shot-----\n");
421 buf_append(score, screen_dump, strlen(screen_dump));
425 if (WSAStartup(wVersionRequested, &wsaData))
427 msg_print("Report: WSAStartup failed.");
444 prt("connecting...", 0, 0);
449 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
451 /* Connect to the score server */
452 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
457 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
459 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
465 if (!get_check_strict(creature_ptr, "もう一度接続を試みますか? ", CHECK_NO_HISTORY))
467 if (!get_check_strict(creature_ptr, "Try again? ", CHECK_NO_HISTORY))
480 prt("スコア送信中...", 0, 0);
482 prt("Sending the score...", 0, 0);
486 if (!http_post(sd, SCORE_PATH, score)) {
487 disconnect_server(sd);
489 sprintf(buff, "スコア・サーバへの送信に失敗しました。");
491 sprintf(buff, "Failed to send to the score server.");
497 if (!get_check_strict(creature_ptr, "もう一度接続を試みますか? ", CHECK_NO_HISTORY))
499 if (!get_check_strict(creature_ptr, "Try again? ", CHECK_NO_HISTORY))
511 disconnect_server(sd);
521 #endif /* WORLD_SCORE */