5 * @author Hengband Team
8 #define _GNU_SOURCE /*!< 未使用*/
15 #include "player-status.h"
16 #include "player-class.h"
17 #include "player-race.h"
18 #include "player-personality.h"
32 #elif defined(MACINTOSH)
33 #include <OpenTransport.h>
34 #include <OpenTptInternet.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 */
65 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/scoretest/score.cgi" /*!< スコア開示URL */
69 * simple buffer library
77 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
81 * @return 確保したバッファの参照ポインタ
83 static BUF* buf_new(void)
87 if ((p = malloc(sizeof(BUF))) == NULL)
91 p->max_size = BUFSIZE;
92 if ((p->data = malloc(BUFSIZE)) == NULL)
102 * @param b 解放するバッファの参照ポインタ
104 static void buf_delete(BUF *b)
111 * @brief 転送用バッファにデータを追加する
112 * @param buf 追加先バッファの参照ポインタ
117 static int buf_append(BUF *buf, concptr data, size_t size)
119 while (buf->size + size > buf->max_size)
122 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
124 memcpy(tmp, buf->data, buf->max_size);
131 memcpy(buf->data + buf->size, data, size);
138 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
139 * @param buf 追加先バッファの参照ポインタ
140 * @param fmt 文字列フォーマット
143 static int buf_sprintf(BUF *buf, concptr fmt, ...)
150 #if defined(HAVE_VSNPRINTF)
151 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
153 ret = vsprintf(tmpbuf, fmt, ap);
157 if (ret < 0) return -1;
159 #if ('\r' == 0x0a && '\n' == 0x0d)
162 * Originally '\r'= CR (= 0x0d) and '\n'= LF (= 0x0a)
163 * But for MPW (Macintosh Programers Workbench), these
164 * are reversed so that '\r'=LF and '\n'=CR unless the
165 * -noMapCR option is not defined.
167 * We need to swap back these here since the score
168 * dump text should be written using LF as the end of
172 for (ptr = tmpbuf; *ptr; ptr++)
174 if (0x0d == *ptr) *ptr = 0x0a;
179 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
185 static int buf_read(BUF *buf, int fd)
193 tmp = calloc( BUFSIZE , sizeof(char) );
196 while ((len = read(fd, tmp, BUFSIZE)) > 0)
197 buf_append(buf, tmp, len);
204 static int buf_write(BUF *buf, int fd)
206 write(fd, buf->data, buf->size);
211 static int buf_search(BUF *buf, concptr str)
215 ret = my_strstr(buf->data, str);
219 return ret - buf->data;
222 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
226 if (pos1 < 0) return NULL;
230 if (sz <= 0) sz = buf->size - pos1;
232 buf_append(ret, buf->data + pos1, sz);
239 * @brief HTTPによるダンプ内容伝送
242 * @param buf 伝送内容バッファ
245 static bool http_post(int sd, concptr url, BUF *buf)
248 char response_buf[1024] = "";
249 concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
252 buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
253 buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
254 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
256 buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
257 buf_sprintf(output, "Content-Encoding: binary\r\n");
260 buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
263 buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
266 buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
268 buf_sprintf(output, "\r\n");
269 buf_append(output, buf->data, buf->size);
271 soc_write(sd, output->data, output->size);
273 soc_read(sd, response_buf, sizeof(response_buf));
275 return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
279 * @brief キャラクタダンプを作って BUFに保存
280 * @param dumpbuf 伝送内容バッファ
283 static errr make_dump(BUF* dumpbuf)
287 GAME_TEXT file_name[1024];
289 /* Open a new file */
290 fff = my_fopen_temp(file_name, 1024);
294 msg_format("一時ファイル %s を作成できませんでした。", file_name);
296 msg_format("Failed to create temporary file %s.", file_name);
302 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
303 (void)make_character_dump(fff);
307 fff = my_fopen(file_name, "r");
309 while (fgets(buf, 1024, fff))
311 (void)buf_sprintf(dumpbuf, "%s", buf);
321 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
322 * @return 作成したスクリーンダンプの参照ポインタ
324 concptr make_screen_dump(void)
330 TERM_COLOR a = 0, old_a = 0;
333 static concptr html_head[] = {
334 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
338 static concptr html_foot[] = {
340 "</body>\n</html>\n",
344 bool old_use_graphics = use_graphics;
348 Term_get_size(&wid, &hgt);
351 screen_buf = buf_new();
352 if (screen_buf == NULL) return (NULL);
354 if (old_use_graphics)
356 /* Clear -more- prompt first */
359 use_graphics = FALSE;
362 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
366 for (i = 0; html_head[i]; i++)
367 buf_sprintf(screen_buf, html_head[i]);
369 /* Dump the screen */
370 for (y = 0; y < hgt; y++)
374 buf_sprintf(screen_buf, "\n");
377 for (x = 0; x < wid - 1; x++)
381 /* Get the attr/char */
382 (void)(Term_what(x, y, &a, &c));
386 case '&': cc = "&"; break;
387 case '<': cc = "<"; break;
388 case '>': cc = ">"; break;
389 case '"': cc = """; break;
390 case '\'': cc = "'"; break;
392 case 0x1f: c = '.'; break;
393 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
398 if ((y == 0 && x == 0) || a != old_a) {
399 rv = angband_color_table[a][1];
400 gv = angband_color_table[a][2];
401 bv = angband_color_table[a][3];
402 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">",
403 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
407 buf_sprintf(screen_buf, "%s", cc);
409 buf_sprintf(screen_buf, "%c", c);
412 buf_sprintf(screen_buf, "</font>");
414 for (i = 0; html_foot[i]; i++)
415 buf_sprintf(screen_buf, html_foot[i]);
417 /* Screen dump size is too big ? */
418 if (screen_buf->size + 1> SCREEN_BUF_MAX_SIZE)
424 /* Terminate string */
425 buf_append(screen_buf, "", 1);
427 ret = string_make(screen_buf->data);
431 buf_delete(screen_buf);
433 if (old_use_graphics)
438 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
446 * @brief スコア転送処理のメインルーチン
449 errr report_score(void)
459 WORD wVersionRequested =(WORD) (( 1) | ( 1 << 8));
464 char seikakutmp[128];
469 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
471 sprintf(seikakutmp, "%s ", ap_ptr->title);
474 buf_sprintf(score, "name: %s\n", p_ptr->name);
476 buf_sprintf(score, "version: 変愚蛮怒 %d.%d.%d\n",
477 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
479 buf_sprintf(score, "version: Hengband %d.%d.%d\n",
480 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
482 buf_sprintf(score, "score: %d\n", calc_score());
483 buf_sprintf(score, "level: %d\n", p_ptr->lev);
484 buf_sprintf(score, "depth: %d\n", current_floor_ptr->dun_level);
485 buf_sprintf(score, "maxlv: %d\n", p_ptr->max_plv);
486 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
487 buf_sprintf(score, "au: %d\n", p_ptr->au);
488 buf_sprintf(score, "turns: %d\n", turn_real(current_world_ptr->game_turn));
489 buf_sprintf(score, "sex: %d\n", p_ptr->psex);
490 buf_sprintf(score, "race: %s\n", rp_ptr->title);
491 buf_sprintf(score, "class: %s\n", cp_ptr->title);
492 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
493 buf_sprintf(score, "realm1: %s\n", realm_names[p_ptr->realm1]);
494 buf_sprintf(score, "realm2: %s\n", realm_names[p_ptr->realm2]);
495 buf_sprintf(score, "killer: %s\n", p_ptr->died_from);
496 buf_sprintf(score, "-----charcter dump-----\n");
502 buf_sprintf(score, "-----screen shot-----\n");
503 buf_append(score, screen_dump, strlen(screen_dump));
507 if (WSAStartup(wVersionRequested, &wsaData))
509 msg_print("Report: WSAStartup failed.");
515 #if TARGET_API_MAC_CARBON
516 err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
518 err = InitOpenTransport();
522 msg_print("Report: OpenTransport failed.");
535 prt("connecting...", 0, 0);
540 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
542 /* Connect to the score server */
543 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
548 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
550 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
556 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
558 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
569 prt("スコア送信中...", 0, 0);
571 prt("Sending the score...", 0, 0);
575 if (!http_post(sd, SCORE_PATH, score)) {
576 disconnect_server(sd);
578 sprintf(buff, "スコア・サーバへの送信に失敗しました。");
580 sprintf(buff, "Failed to send to the score server.");
586 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
588 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
598 disconnect_server(sd);
608 #if TARGET_API_MAC_CARBON
609 CloseOpenTransportInContext(NULL);
611 CloseOpenTransport();
618 #endif /* WORLD_SCORE */