5 * @author Hengband Team
9 #include "core/asking-player.h"
10 #include "core/player-redraw-types.h"
11 #include "core/stuff-handler.h"
12 #include "core/turn-compensator.h"
13 #include "core/visuals-reseter.h"
14 #include "dungeon/dungeon.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-class.h"
20 #include "player/player-personality.h"
21 #include "player/player-race.h"
22 #include "realm/realm-names-table.h"
23 #include "system/angband-version.h"
24 #include "system/floor-type-definition.h"
25 #include "system/system-variables.h"
26 #include "term/gameterm.h"
27 #include "term/screen-processor.h"
28 #include "util/angband-files.h"
29 #include "view/display-messages.h"
30 #include "world/world.h"
37 #include <netinet/in.h>
38 #include <sys/socket.h>
40 #include <sys/types.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));
86 p->max_size = BUFSIZE;
87 p->data = malloc(BUFSIZE);
98 * @param b 解放するバッファの参照ポインタ
100 static void buf_delete(BUF *b)
107 * @brief 転送用バッファにデータを追加する
108 * @param buf 追加先バッファの参照ポインタ
113 static int buf_append(BUF *buf, concptr data, size_t size)
115 while (buf->size + size > buf->max_size) {
117 if ((tmp = malloc(buf->max_size * 2)) == NULL)
120 memcpy(tmp, buf->data, buf->max_size);
127 memcpy(buf->data + buf->size, data, size);
134 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
135 * @param buf 追加先バッファの参照ポインタ
136 * @param fmt 文字列フォーマット
139 static int buf_sprintf(BUF *buf, concptr fmt, ...)
146 #if defined(HAVE_VSNPRINTF)
147 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
149 ret = vsprintf(tmpbuf, fmt, ap);
156 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
161 * @brief HTTPによるダンプ内容伝送
164 * @param buf 伝送内容バッファ
167 static bool http_post(int sd, concptr url, BUF *buf)
170 char response_buf[1024] = "";
171 concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
174 buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
175 buf_sprintf(output, "User-Agent: Hengband %d.%d.%d.%d\r\n", FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH, FAKE_VER_EXTRA);
177 buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
178 buf_sprintf(output, "Content-Encoding: binary\r\n");
181 buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
184 buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
187 buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
189 buf_sprintf(output, "\r\n");
190 buf_append(output, buf->data, buf->size);
192 soc_write(sd, output->data, output->size);
194 soc_read(sd, response_buf, sizeof(response_buf));
196 return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
200 * @brief キャラクタダンプを作って BUFに保存
201 * @param creature_ptr プレーヤーへの参照ポインタ
202 * @param dumpbuf 伝送内容バッファ
205 static errr make_dump(player_type *creature_ptr, BUF *dumpbuf, void (*update_playtime)(void), display_player_pf display_player)
209 GAME_TEXT file_name[1024];
211 /* Open a new file */
212 fff = angband_fopen_temp(file_name, 1024);
215 msg_format("一時ファイル %s を作成できませんでした。", file_name);
217 msg_format("Failed to create temporary file %s.", file_name);
223 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
224 make_character_dump(creature_ptr, fff, update_playtime, display_player);
228 fff = angband_fopen(file_name, "r");
230 while (fgets(buf, 1024, fff)) {
231 (void)buf_sprintf(dumpbuf, "%s", buf);
241 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
242 * @return 作成したスクリーンダンプの参照ポインタ
244 concptr make_screen_dump(player_type *creature_ptr, void (*process_autopick_file_command)(char *))
246 static concptr html_head[] = {
247 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
251 static concptr html_foot[] = {
253 "</body>\n</html>\n",
258 term_get_size(&wid, &hgt);
262 screen_buf = buf_new();
263 if (screen_buf == NULL)
266 bool old_use_graphics = use_graphics;
267 if (old_use_graphics) {
268 /* Clear -more- prompt first */
271 use_graphics = FALSE;
272 reset_visuals(creature_ptr, process_autopick_file_command);
274 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
275 handle_stuff(creature_ptr);
278 for (int i = 0; html_head[i]; i++)
279 buf_sprintf(screen_buf, html_head[i]);
281 /* Dump the screen */
282 for (int y = 0; y < hgt; y++) {
285 buf_sprintf(screen_buf, "\n");
288 TERM_COLOR a = 0, old_a = 0;
290 for (int x = 0; x < wid - 1; x++) {
293 /* Get the attr/char */
294 (void)(term_what(x, y, &a, &c));
317 c = (a == 0x09) ? '%' : '#';
323 if ((y == 0 && x == 0) || a != old_a) {
324 rv = angband_color_table[a][1];
325 gv = angband_color_table[a][2];
326 bv = angband_color_table[a][3];
327 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">", ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
332 buf_sprintf(screen_buf, "%s", cc);
334 buf_sprintf(screen_buf, "%c", c);
338 buf_sprintf(screen_buf, "</font>");
340 for (int i = 0; html_foot[i]; i++)
341 buf_sprintf(screen_buf, html_foot[i]);
343 /* Screen dump size is too big ? */
345 if (screen_buf->size + 1 > SCREEN_BUF_MAX_SIZE) {
348 /* Terminate string */
349 buf_append(screen_buf, "", 1);
351 ret = string_make(screen_buf->data);
355 buf_delete(screen_buf);
357 if (!old_use_graphics)
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);
369 * todo メッセージは言語選択の関数マクロで何とかならんか?
370 * @brief スコア転送処理のメインルーチン
371 * @param creature_ptr プレーヤーへの参照ポインタ
372 * @return 正常終了の時0、異常があったら1
374 errr report_score(player_type *creature_ptr, void (*update_playtime)(void), display_player_pf display_player)
378 WORD wVersionRequested = (WORD)((1) | (1 << 8));
384 char seikakutmp[128];
388 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
390 sprintf(seikakutmp, "%s ", ap_ptr->title);
393 buf_sprintf(score, "name: %s\n", creature_ptr->name);
394 buf_sprintf(score, "version: %s\n", title);
395 buf_sprintf(score, "score: %d\n", calc_score(creature_ptr));
396 buf_sprintf(score, "level: %d\n", creature_ptr->lev);
397 buf_sprintf(score, "depth: %d\n", creature_ptr->current_floor_ptr->dun_level);
398 buf_sprintf(score, "maxlv: %d\n", creature_ptr->max_plv);
399 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
400 buf_sprintf(score, "au: %d\n", creature_ptr->au);
401 buf_sprintf(score, "turns: %d\n", turn_real(creature_ptr, current_world_ptr->game_turn));
402 buf_sprintf(score, "sex: %d\n", creature_ptr->psex);
403 buf_sprintf(score, "race: %s\n", rp_ptr->title);
404 buf_sprintf(score, "class: %s\n", cp_ptr->title);
405 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
406 buf_sprintf(score, "realm1: %s\n", realm_names[creature_ptr->realm1]);
407 buf_sprintf(score, "realm2: %s\n", realm_names[creature_ptr->realm2]);
408 buf_sprintf(score, "killer: %s\n", creature_ptr->died_from);
409 buf_sprintf(score, "-----charcter dump-----\n");
411 make_dump(creature_ptr, score, update_playtime, display_player);
414 buf_sprintf(score, "-----screen shot-----\n");
415 buf_append(score, screen_dump, strlen(screen_dump));
419 if (WSAStartup(wVersionRequested, &wsaData)) {
420 msg_print("Report: WSAStartup failed.");
436 prt("connecting...", 0, 0);
441 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
443 /* Connect to the score server */
444 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
448 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
450 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
456 if (!get_check_strict(creature_ptr, "もう一度接続を試みますか? ", CHECK_NO_HISTORY))
458 if (!get_check_strict(creature_ptr, "Try again? ", CHECK_NO_HISTORY))
471 prt("スコア送信中...", 0, 0);
473 prt("Sending the score...", 0, 0);
477 if (!http_post(sd, SCORE_PATH, score)) {
478 disconnect_server(sd);
480 sprintf(buff, "スコア・サーバへの送信に失敗しました。");
482 sprintf(buff, "Failed to send to the score server.");
488 if (!get_check_strict(creature_ptr, "もう一度接続を試みますか? ", CHECK_NO_HISTORY))
490 if (!get_check_strict(creature_ptr, "Try again? ", CHECK_NO_HISTORY))
502 disconnect_server(sd);
512 #endif /* WORLD_SCORE */