5 * @author Hengband Team
13 #include "player-personality.h"
14 #include "character-dump.h"
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
39 concptr screen_dump = NULL;
42 * internet resource value
44 #define HTTP_PROXY "" /*!< デフォルトのプロキシURL / Default proxy url */
45 #define HTTP_PROXY_PORT 0 /*!< デフォルトのプロキシポート / Default proxy port */
46 #define HTTP_TIMEOUT 20 /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
47 #define SCORE_SERVER "hengband.osdn.jp" /*!< デフォルトのスコアサーバURL / Default score server url */
48 #define SCORE_PORT 80 /*!< デフォルトのスコアサーバポート / Default score server port */
51 #define SCORE_PATH "http://hengband.osdn.jp/score/register_score.php" /*!< スコア開示URL */
53 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore-en/score.cgi" /*!< スコア開示URL */
57 * simple buffer library
65 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
69 * @return 確保したバッファの参照ポインタ
71 static BUF* buf_new(void)
74 p = malloc(sizeof(BUF));
78 p->max_size = BUFSIZE;
79 p->data = malloc(BUFSIZE);
92 * @param b 解放するバッファの参照ポインタ
94 static void buf_delete(BUF *b)
102 * @brief 転送用バッファにデータを追加する
103 * @param buf 追加先バッファの参照ポインタ
108 static int buf_append(BUF *buf, concptr data, size_t size)
110 while (buf->size + size > buf->max_size)
113 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
115 memcpy(tmp, buf->data, buf->max_size);
122 memcpy(buf->data + buf->size, data, size);
130 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
131 * @param buf 追加先バッファの参照ポインタ
132 * @param fmt 文字列フォーマット
135 static int buf_sprintf(BUF *buf, concptr fmt, ...)
142 #if defined(HAVE_VSNPRINTF)
143 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
145 ret = vsprintf(tmpbuf, fmt, ap);
149 if (ret < 0) return -1;
151 #if ('\r' == 0x0a && '\n' == 0x0d)
154 * Originally '\r'= CR (= 0x0d) and '\n'= LF (= 0x0a)
155 * But for MPW (Macintosh Programers Workbench), these
156 * are reversed so that '\r'=LF and '\n'=CR unless the
157 * -noMapCR option is not defined.
159 * We need to swap back these here since the score
160 * dump text should be written using LF as the end of
164 for (ptr = tmpbuf; *ptr; ptr++)
166 if (0x0d == *ptr) *ptr = 0x0a;
171 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
178 * @brief HTTPによるダンプ内容伝送
181 * @param buf 伝送内容バッファ
184 static bool http_post(int sd, concptr url, BUF *buf)
187 char response_buf[1024] = "";
188 concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
191 buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
192 buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
193 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
195 buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
196 buf_sprintf(output, "Content-Encoding: binary\r\n");
199 buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
202 buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
205 buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
207 buf_sprintf(output, "\r\n");
208 buf_append(output, buf->data, buf->size);
210 soc_write(sd, output->data, output->size);
212 soc_read(sd, response_buf, sizeof(response_buf));
214 return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
219 * @brief キャラクタダンプを作って BUFに保存
220 * @param creature_ptr プレーヤーへの参照ポインタ
221 * @param dumpbuf 伝送内容バッファ
224 static errr make_dump(player_type *creature_ptr, BUF* dumpbuf, void(*update_playtime)(void), void(*display_player)(player_type*, int))
228 GAME_TEXT file_name[1024];
230 /* Open a new file */
231 fff = my_fopen_temp(file_name, 1024);
235 msg_format("一時ファイル %s を作成できませんでした。", file_name);
237 msg_format("Failed to create temporary file %s.", file_name);
243 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
244 (void)make_character_dump(creature_ptr, fff, update_playtime, display_player);
248 fff = my_fopen(file_name, "r");
250 while (fgets(buf, 1024, fff))
252 (void)buf_sprintf(dumpbuf, "%s", buf);
263 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
264 * @return 作成したスクリーンダンプの参照ポインタ
266 concptr make_screen_dump(player_type *creature_ptr)
268 static concptr html_head[] = {
269 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
273 static concptr html_foot[] = {
275 "</body>\n</html>\n",
280 Term_get_size(&wid, &hgt);
284 screen_buf = buf_new();
285 if (screen_buf == NULL) return (NULL);
287 bool old_use_graphics = use_graphics;
288 if (old_use_graphics)
290 /* Clear -more- prompt first */
293 use_graphics = FALSE;
294 reset_visuals(creature_ptr);
296 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
297 handle_stuff(creature_ptr);
300 for (int i = 0; html_head[i]; i++)
301 buf_sprintf(screen_buf, html_head[i]);
303 /* Dump the screen */
304 for (int y = 0; y < hgt; y++)
308 buf_sprintf(screen_buf, "\n");
311 TERM_COLOR a = 0, old_a = 0;
313 for (int x = 0; x < wid - 1; x++)
317 /* Get the attr/char */
318 (void)(Term_what(x, y, &a, &c));
322 case '&': cc = "&"; break;
323 case '<': cc = "<"; break;
324 case '>': cc = ">"; break;
325 case '"': cc = """; break;
326 case '\'': cc = "'"; break;
328 case 0x1f: c = '.'; break;
329 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
334 if ((y == 0 && x == 0) || a != old_a) {
335 rv = angband_color_table[a][1];
336 gv = angband_color_table[a][2];
337 bv = angband_color_table[a][3];
338 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">",
339 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
344 buf_sprintf(screen_buf, "%s", cc);
346 buf_sprintf(screen_buf, "%c", c);
350 buf_sprintf(screen_buf, "</font>");
352 for (int i = 0; html_foot[i]; i++)
353 buf_sprintf(screen_buf, html_foot[i]);
355 /* Screen dump size is too big ? */
357 if (screen_buf->size + 1 > SCREEN_BUF_MAX_SIZE)
363 /* Terminate string */
364 buf_append(screen_buf, "", 1);
366 ret = string_make(screen_buf->data);
370 buf_delete(screen_buf);
372 if (!old_use_graphics) return ret;
375 reset_visuals(creature_ptr);
377 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
378 handle_stuff(creature_ptr);
384 * todo メッセージは言語選択の関数マクロで何とかならんか?
385 * @brief スコア転送処理のメインルーチン
386 * @param creature_ptr プレーヤーへの参照ポインタ
387 * @return 正常終了の時0、異常があったら1
389 errr report_score(player_type *creature_ptr, void(*update_playtime)(void), void(*display_player)(player_type*, int))
393 WORD wVersionRequested = (WORD)((1) | (1 << 8));
399 char seikakutmp[128];
401 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
403 sprintf(seikakutmp, "%s ", ap_ptr->title);
406 buf_sprintf(score, "name: %s\n", creature_ptr->name);
408 buf_sprintf(score, "version: 変愚蛮怒 %d.%d.%d\n",
409 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
411 buf_sprintf(score, "version: Hengband %d.%d.%d\n",
412 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
414 buf_sprintf(score, "score: %d\n", calc_score(creature_ptr));
415 buf_sprintf(score, "level: %d\n", creature_ptr->lev);
416 buf_sprintf(score, "depth: %d\n", creature_ptr->current_floor_ptr->dun_level);
417 buf_sprintf(score, "maxlv: %d\n", creature_ptr->max_plv);
418 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
419 buf_sprintf(score, "au: %d\n", creature_ptr->au);
420 buf_sprintf(score, "turns: %d\n", turn_real(creature_ptr, current_world_ptr->game_turn));
421 buf_sprintf(score, "sex: %d\n", creature_ptr->psex);
422 buf_sprintf(score, "race: %s\n", rp_ptr->title);
423 buf_sprintf(score, "class: %s\n", cp_ptr->title);
424 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
425 buf_sprintf(score, "realm1: %s\n", realm_names[creature_ptr->realm1]);
426 buf_sprintf(score, "realm2: %s\n", realm_names[creature_ptr->realm2]);
427 buf_sprintf(score, "killer: %s\n", creature_ptr->died_from);
428 buf_sprintf(score, "-----charcter dump-----\n");
430 make_dump(creature_ptr, score, update_playtime, display_player);
434 buf_sprintf(score, "-----screen shot-----\n");
435 buf_append(score, screen_dump, strlen(screen_dump));
439 if (WSAStartup(wVersionRequested, &wsaData))
441 msg_print("Report: WSAStartup failed.");
458 prt("connecting...", 0, 0);
463 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
465 /* Connect to the score server */
466 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
471 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
473 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
479 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
481 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
494 prt("スコア送信中...", 0, 0);
496 prt("Sending the score...", 0, 0);
500 if (!http_post(sd, SCORE_PATH, score)) {
501 disconnect_server(sd);
503 sprintf(buff, "スコア・サーバへの送信に失敗しました。");
505 sprintf(buff, "Failed to send to the score server.");
511 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
513 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
525 disconnect_server(sd);
535 #endif /* WORLD_SCORE */