5 * @author Bakabakaband 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"
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
52 concptr screen_dump = NULL;
55 * internet resource value
57 #define HTTP_PROXY "" /*!< デフォルトのプロキシURL / Default proxy url */
58 #define HTTP_PROXY_PORT 0 /*!< デフォルトのプロキシポート / Default proxy port */
59 #define HTTP_TIMEOUT 20 /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
60 #define SCORE_SERVER "Bakabakaband.osdn.jp" /*!< デフォルトのスコアサーバURL / Default score server url */
61 #define SCORE_PORT 80 /*!< デフォルトのスコアサーバポート / Default score server port */
64 #define SCORE_PATH "http://Bakabakaband.osdn.jp/score/register_score.php" /*!< スコア開示URL */
66 #define SCORE_PATH "http://moon.kmc.gr.jp/Bakabakaband/hengscore-en/score.cgi" /*!< スコア開示URL */
70 * simple buffer library
78 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
82 * @return 確保したバッファの参照ポインタ
84 static BUF* buf_new(void)
87 p = malloc(sizeof(BUF));
91 p->max_size = BUFSIZE;
92 p->data = malloc(BUFSIZE);
105 * @param b 解放するバッファの参照ポインタ
107 static void buf_delete(BUF *b)
115 * @brief 転送用バッファにデータを追加する
116 * @param buf 追加先バッファの参照ポインタ
121 static int buf_append(BUF *buf, concptr data, size_t size)
123 while (buf->size + size > buf->max_size)
126 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
128 memcpy(tmp, buf->data, buf->max_size);
135 memcpy(buf->data + buf->size, data, size);
143 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
144 * @param buf 追加先バッファの参照ポインタ
145 * @param fmt 文字列フォーマット
148 static int buf_sprintf(BUF *buf, concptr fmt, ...)
155 #if defined(HAVE_VSNPRINTF)
156 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
158 ret = vsprintf(tmpbuf, fmt, ap);
162 if (ret < 0) return -1;
164 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
170 * @brief HTTPによるダンプ内容伝送
173 * @param buf 伝送内容バッファ
176 static bool http_post(int sd, concptr url, BUF *buf)
179 char response_buf[1024] = "";
180 concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
183 buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
184 buf_sprintf(output, "User-Agent: Bakabakaband %d.%d.%d\r\n",
185 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
187 buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
188 buf_sprintf(output, "Content-Encoding: binary\r\n");
191 buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
194 buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
197 buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
199 buf_sprintf(output, "\r\n");
200 buf_append(output, buf->data, buf->size);
202 soc_write(sd, output->data, output->size);
204 soc_read(sd, response_buf, sizeof(response_buf));
206 return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
211 * @brief キャラクタダンプを作って BUFに保存
212 * @param creature_ptr プレーヤーへの参照ポインタ
213 * @param dumpbuf 伝送内容バッファ
216 static errr make_dump(player_type *creature_ptr, BUF* dumpbuf, void(*update_playtime)(void), display_player_pf display_player)
220 GAME_TEXT file_name[1024];
222 /* Open a new file */
223 fff = angband_fopen_temp(file_name, 1024);
227 msg_format("一時ファイル %s を作成できませんでした。", file_name);
229 msg_format("Failed to create temporary file %s.", file_name);
235 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
236 make_character_dump(creature_ptr, fff, update_playtime, display_player);
240 fff = angband_fopen(file_name, "r");
242 while (fgets(buf, 1024, fff))
244 (void)buf_sprintf(dumpbuf, "%s", buf);
255 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
256 * @return 作成したスクリーンダンプの参照ポインタ
258 concptr make_screen_dump(player_type *creature_ptr, void(*process_autopick_file_command)(char*))
260 static concptr html_head[] = {
261 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
265 static concptr html_foot[] = {
267 "</body>\n</html>\n",
272 term_get_size(&wid, &hgt);
276 screen_buf = buf_new();
277 if (screen_buf == NULL) return (NULL);
279 bool old_use_graphics = use_graphics;
280 if (old_use_graphics)
282 /* Clear -more- prompt first */
285 use_graphics = FALSE;
286 reset_visuals(creature_ptr, process_autopick_file_command);
288 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
289 handle_stuff(creature_ptr);
292 for (int i = 0; html_head[i]; i++)
293 buf_sprintf(screen_buf, html_head[i]);
295 /* Dump the screen */
296 for (int y = 0; y < hgt; y++)
300 buf_sprintf(screen_buf, "\n");
303 TERM_COLOR a = 0, old_a = 0;
305 for (int x = 0; x < wid - 1; x++)
309 /* Get the attr/char */
310 (void)(term_what(x, y, &a, &c));
314 case '&': cc = "&"; break;
315 case '<': cc = "<"; break;
316 case '>': cc = ">"; break;
317 case '"': cc = """; break;
318 case '\'': cc = "'"; break;
320 case 0x1f: c = '.'; break;
321 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
326 if ((y == 0 && x == 0) || a != old_a) {
327 rv = angband_color_table[a][1];
328 gv = angband_color_table[a][2];
329 bv = angband_color_table[a][3];
330 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">",
331 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
336 buf_sprintf(screen_buf, "%s", cc);
338 buf_sprintf(screen_buf, "%c", c);
342 buf_sprintf(screen_buf, "</font>");
344 for (int i = 0; html_foot[i]; i++)
345 buf_sprintf(screen_buf, html_foot[i]);
347 /* Screen dump size is too big ? */
349 if (screen_buf->size + 1 > SCREEN_BUF_MAX_SIZE)
355 /* Terminate string */
356 buf_append(screen_buf, "", 1);
358 ret = string_make(screen_buf->data);
362 buf_delete(screen_buf);
364 if (!old_use_graphics) return ret;
367 reset_visuals(creature_ptr, process_autopick_file_command);
369 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
370 handle_stuff(creature_ptr);
376 * todo メッセージは言語選択の関数マクロで何とかならんか?
377 * @brief スコア転送処理のメインルーチン
378 * @param creature_ptr プレーヤーへの参照ポインタ
379 * @return 正常終了の時0、異常があったら1
381 errr report_score(player_type *creature_ptr, void(*update_playtime)(void), display_player_pf display_player)
385 WORD wVersionRequested = (WORD)((1) | (1 << 8));
391 char seikakutmp[128];
393 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
395 sprintf(seikakutmp, "%s ", ap_ptr->title);
398 buf_sprintf(score, "name: %s\n", creature_ptr->name);
400 buf_sprintf(score, "version: 馬鹿馬鹿蛮怒 %d.%d.%d\n",
401 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
403 buf_sprintf(score, "version: Bakabakaband %d.%d.%d\n",
404 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
406 buf_sprintf(score, "score: %d\n", calc_score(creature_ptr));
407 buf_sprintf(score, "level: %d\n", creature_ptr->lev);
408 buf_sprintf(score, "depth: %d\n", creature_ptr->current_floor_ptr->dun_level);
409 buf_sprintf(score, "maxlv: %d\n", creature_ptr->max_plv);
410 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
411 buf_sprintf(score, "au: %d\n", creature_ptr->au);
412 buf_sprintf(score, "turns: %d\n", turn_real(creature_ptr, current_world_ptr->game_turn));
413 buf_sprintf(score, "sex: %d\n", creature_ptr->psex);
414 buf_sprintf(score, "race: %s\n", rp_ptr->title);
415 buf_sprintf(score, "class: %s\n", cp_ptr->title);
416 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
417 buf_sprintf(score, "realm1: %s\n", realm_names[creature_ptr->realm1]);
418 buf_sprintf(score, "realm2: %s\n", realm_names[creature_ptr->realm2]);
419 buf_sprintf(score, "killer: %s\n", creature_ptr->died_from);
420 buf_sprintf(score, "-----charcter dump-----\n");
422 make_dump(creature_ptr, score, update_playtime, display_player);
426 buf_sprintf(score, "-----screen shot-----\n");
427 buf_append(score, screen_dump, strlen(screen_dump));
431 if (WSAStartup(wVersionRequested, &wsaData))
433 msg_print("Report: WSAStartup failed.");
450 prt("connecting...", 0, 0);
455 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
457 /* Connect to the score server */
458 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
463 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
465 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
471 if (!get_check_strict(creature_ptr, "もう一度接続を試みますか? ", CHECK_NO_HISTORY))
473 if (!get_check_strict(creature_ptr, "Try again? ", CHECK_NO_HISTORY))
486 prt("スコア送信中...", 0, 0);
488 prt("Sending the score...", 0, 0);
492 if (!http_post(sd, SCORE_PATH, score)) {
493 disconnect_server(sd);
495 sprintf(buff, "スコア・サーバへの送信に失敗しました。");
497 sprintf(buff, "Failed to send to the score server.");
503 if (!get_check_strict(creature_ptr, "もう一度接続を試みますか? ", CHECK_NO_HISTORY))
505 if (!get_check_strict(creature_ptr, "Try again? ", CHECK_NO_HISTORY))
517 disconnect_server(sd);
527 #endif /* WORLD_SCORE */