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"
33 #elif defined(MACINTOSH)
34 #include <OpenTransport.h>
35 #include <OpenTptInternet.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
47 concptr screen_dump = NULL;
50 * internet resource value
52 #define HTTP_PROXY "" /*!< デフォルトのプロキシURL / Default proxy url */
53 #define HTTP_PROXY_PORT 0 /*!< デフォルトのプロキシポート / Default proxy port */
54 #define HTTP_TIMEOUT 20 /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
55 #define SCORE_SERVER "hengband.osdn.jp" /*!< デフォルトのスコアサーバURL / Default score server url */
56 #define SCORE_PORT 80 /*!< デフォルトのスコアサーバポート / Default score server port */
59 #define SCORE_PATH "http://hengband.osdn.jp/score/register_score.php" /*!< スコア開示URL */
61 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore-en/score.cgi" /*!< スコア開示URL */
66 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/scoretest/score.cgi" /*!< スコア開示URL */
70 * simple buffer library
78 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
82 * @return 確保したバッファの参照ポインタ
84 static BUF* buf_new(void)
88 if ((p = malloc(sizeof(BUF))) == NULL)
92 p->max_size = BUFSIZE;
93 if ((p->data = malloc(BUFSIZE)) == NULL)
103 * @param b 解放するバッファの参照ポインタ
105 static void buf_delete(BUF *b)
112 * @brief 転送用バッファにデータを追加する
113 * @param buf 追加先バッファの参照ポインタ
118 static int buf_append(BUF *buf, concptr data, size_t size)
120 while (buf->size + size > buf->max_size)
123 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
125 memcpy(tmp, buf->data, buf->max_size);
132 memcpy(buf->data + buf->size, data, size);
139 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
140 * @param buf 追加先バッファの参照ポインタ
141 * @param fmt 文字列フォーマット
144 static int buf_sprintf(BUF *buf, concptr fmt, ...)
151 #if defined(HAVE_VSNPRINTF)
152 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
154 ret = vsprintf(tmpbuf, fmt, ap);
158 if (ret < 0) return -1;
160 #if ('\r' == 0x0a && '\n' == 0x0d)
163 * Originally '\r'= CR (= 0x0d) and '\n'= LF (= 0x0a)
164 * But for MPW (Macintosh Programers Workbench), these
165 * are reversed so that '\r'=LF and '\n'=CR unless the
166 * -noMapCR option is not defined.
168 * We need to swap back these here since the score
169 * dump text should be written using LF as the end of
173 for (ptr = tmpbuf; *ptr; ptr++)
175 if (0x0d == *ptr) *ptr = 0x0a;
180 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
186 static int buf_read(BUF *buf, int fd)
194 tmp = calloc( BUFSIZE , sizeof(char) );
197 while ((len = read(fd, tmp, BUFSIZE)) > 0)
198 buf_append(buf, tmp, len);
205 static int buf_write(BUF *buf, int fd)
207 write(fd, buf->data, buf->size);
212 static int buf_search(BUF *buf, concptr str)
216 ret = my_strstr(buf->data, str);
220 return ret - buf->data;
223 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
227 if (pos1 < 0) return NULL;
231 if (sz <= 0) sz = buf->size - pos1;
233 buf_append(ret, buf->data + pos1, sz);
240 * @brief HTTPによるダンプ内容伝送
243 * @param buf 伝送内容バッファ
246 static bool http_post(int sd, concptr url, BUF *buf)
249 char response_buf[1024] = "";
250 concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
253 buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
254 buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
255 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
257 buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
258 buf_sprintf(output, "Content-Encoding: binary\r\n");
261 buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
264 buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
267 buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
269 buf_sprintf(output, "\r\n");
270 buf_append(output, buf->data, buf->size);
272 soc_write(sd, output->data, output->size);
274 soc_read(sd, response_buf, sizeof(response_buf));
276 return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
280 * @brief キャラクタダンプを作って BUFに保存
281 * @param dumpbuf 伝送内容バッファ
284 static errr make_dump(BUF* dumpbuf)
288 GAME_TEXT file_name[1024];
290 /* Open a new file */
291 fff = my_fopen_temp(file_name, 1024);
295 msg_format("一時ファイル %s を作成できませんでした。", file_name);
297 msg_format("Failed to create temporary file %s.", file_name);
303 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
304 (void)make_character_dump(fff);
308 fff = my_fopen(file_name, "r");
310 while (fgets(buf, 1024, fff))
312 (void)buf_sprintf(dumpbuf, "%s", buf);
322 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
323 * @return 作成したスクリーンダンプの参照ポインタ
325 concptr make_screen_dump(void)
331 TERM_COLOR a = 0, old_a = 0;
334 static concptr html_head[] = {
335 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
339 static concptr html_foot[] = {
341 "</body>\n</html>\n",
345 bool old_use_graphics = use_graphics;
349 Term_get_size(&wid, &hgt);
352 screen_buf = buf_new();
353 if (screen_buf == NULL) return (NULL);
355 if (old_use_graphics)
357 /* Clear -more- prompt first */
360 use_graphics = FALSE;
363 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
367 for (i = 0; html_head[i]; i++)
368 buf_sprintf(screen_buf, html_head[i]);
370 /* Dump the screen */
371 for (y = 0; y < hgt; y++)
375 buf_sprintf(screen_buf, "\n");
378 for (x = 0; x < wid - 1; x++)
382 /* Get the attr/char */
383 (void)(Term_what(x, y, &a, &c));
387 case '&': cc = "&"; break;
388 case '<': cc = "<"; break;
389 case '>': cc = ">"; break;
390 case '"': cc = """; break;
391 case '\'': cc = "'"; break;
393 case 0x1f: c = '.'; break;
394 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
399 if ((y == 0 && x == 0) || a != old_a) {
400 rv = angband_color_table[a][1];
401 gv = angband_color_table[a][2];
402 bv = angband_color_table[a][3];
403 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">",
404 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
408 buf_sprintf(screen_buf, "%s", cc);
410 buf_sprintf(screen_buf, "%c", c);
413 buf_sprintf(screen_buf, "</font>");
415 for (i = 0; html_foot[i]; i++)
416 buf_sprintf(screen_buf, html_foot[i]);
418 /* Screen dump size is too big ? */
419 if (screen_buf->size + 1> SCREEN_BUF_MAX_SIZE)
425 /* Terminate string */
426 buf_append(screen_buf, "", 1);
428 ret = string_make(screen_buf->data);
432 buf_delete(screen_buf);
434 if (old_use_graphics)
439 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
447 * @brief スコア転送処理のメインルーチン
450 errr report_score(void)
460 WORD wVersionRequested =(WORD) (( 1) | ( 1 << 8));
465 char seikakutmp[128];
470 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
472 sprintf(seikakutmp, "%s ", ap_ptr->title);
475 buf_sprintf(score, "name: %s\n", p_ptr->name);
477 buf_sprintf(score, "version: 変愚蛮怒 %d.%d.%d\n",
478 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
480 buf_sprintf(score, "version: Hengband %d.%d.%d\n",
481 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
483 buf_sprintf(score, "score: %d\n", calc_score());
484 buf_sprintf(score, "level: %d\n", p_ptr->lev);
485 buf_sprintf(score, "depth: %d\n", current_floor_ptr->dun_level);
486 buf_sprintf(score, "maxlv: %d\n", p_ptr->max_plv);
487 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
488 buf_sprintf(score, "au: %d\n", p_ptr->au);
489 buf_sprintf(score, "turns: %d\n", turn_real(current_world_ptr->game_turn));
490 buf_sprintf(score, "sex: %d\n", p_ptr->psex);
491 buf_sprintf(score, "race: %s\n", rp_ptr->title);
492 buf_sprintf(score, "class: %s\n", cp_ptr->title);
493 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
494 buf_sprintf(score, "realm1: %s\n", realm_names[p_ptr->realm1]);
495 buf_sprintf(score, "realm2: %s\n", realm_names[p_ptr->realm2]);
496 buf_sprintf(score, "killer: %s\n", p_ptr->died_from);
497 buf_sprintf(score, "-----charcter dump-----\n");
503 buf_sprintf(score, "-----screen shot-----\n");
504 buf_append(score, screen_dump, strlen(screen_dump));
508 if (WSAStartup(wVersionRequested, &wsaData))
510 msg_print("Report: WSAStartup failed.");
516 #if TARGET_API_MAC_CARBON
517 err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
519 err = InitOpenTransport();
523 msg_print("Report: OpenTransport failed.");
536 prt("connecting...", 0, 0);
541 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
543 /* Connect to the score server */
544 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
549 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
551 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
557 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
559 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
570 prt("スコア送信中...", 0, 0);
572 prt("Sending the score...", 0, 0);
576 if (!http_post(sd, SCORE_PATH, score)) {
577 disconnect_server(sd);
579 sprintf(buff, "スコア・サーバへの送信に失敗しました。");
581 sprintf(buff, "Failed to send to the score server.");
587 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
589 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
599 disconnect_server(sd);
609 #if TARGET_API_MAC_CARBON
610 CloseOpenTransportInContext(NULL);
612 CloseOpenTransport();
619 #endif /* WORLD_SCORE */