5 * @author Hengband Team
8 #define _GNU_SOURCE /*!< 未使用*/
16 #include "player-status.h"
17 #include "player-class.h"
18 #include "player-race.h"
19 #include "player-personality.h"
24 #include "view-mainwindow.h"
35 #elif defined(MACINTOSH)
36 #include <OpenTransport.h>
37 #include <OpenTptInternet.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
49 concptr screen_dump = NULL;
52 * internet resource value
54 #define HTTP_PROXY "" /*!< デフォルトのプロキシURL / Default proxy url */
55 #define HTTP_PROXY_PORT 0 /*!< デフォルトのプロキシポート / Default proxy port */
56 #define HTTP_TIMEOUT 20 /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
57 #define SCORE_SERVER "hengband.osdn.jp" /*!< デフォルトのスコアサーバURL / Default score server url */
58 #define SCORE_PORT 80 /*!< デフォルトのスコアサーバポート / Default score server port */
61 #define SCORE_PATH "http://hengband.osdn.jp/score/register_score.php" /*!< スコア開示URL */
63 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore-en/score.cgi" /*!< スコア開示URL */
68 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/scoretest/score.cgi" /*!< スコア開示URL */
72 * simple buffer library
80 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
84 * @return 確保したバッファの参照ポインタ
86 static BUF* buf_new(void)
90 if ((p = malloc(sizeof(BUF))) == NULL)
94 p->max_size = BUFSIZE;
95 if ((p->data = malloc(BUFSIZE)) == NULL)
105 * @param b 解放するバッファの参照ポインタ
107 static void buf_delete(BUF *b)
114 * @brief 転送用バッファにデータを追加する
115 * @param buf 追加先バッファの参照ポインタ
120 static int buf_append(BUF *buf, concptr data, size_t size)
122 while (buf->size + size > buf->max_size)
125 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
127 memcpy(tmp, buf->data, buf->max_size);
134 memcpy(buf->data + buf->size, data, size);
141 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
142 * @param buf 追加先バッファの参照ポインタ
143 * @param fmt 文字列フォーマット
146 static int buf_sprintf(BUF *buf, concptr fmt, ...)
153 #if defined(HAVE_VSNPRINTF)
154 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
156 ret = vsprintf(tmpbuf, fmt, ap);
160 if (ret < 0) return -1;
162 #if ('\r' == 0x0a && '\n' == 0x0d)
165 * Originally '\r'= CR (= 0x0d) and '\n'= LF (= 0x0a)
166 * But for MPW (Macintosh Programers Workbench), these
167 * are reversed so that '\r'=LF and '\n'=CR unless the
168 * -noMapCR option is not defined.
170 * We need to swap back these here since the score
171 * dump text should be written using LF as the end of
175 for (ptr = tmpbuf; *ptr; ptr++)
177 if (0x0d == *ptr) *ptr = 0x0a;
182 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
188 static int buf_read(BUF *buf, int fd)
196 tmp = calloc( BUFSIZE , sizeof(char) );
199 while ((len = read(fd, tmp, BUFSIZE)) > 0)
200 buf_append(buf, tmp, len);
207 static int buf_write(BUF *buf, int fd)
209 write(fd, buf->data, buf->size);
214 static int buf_search(BUF *buf, concptr str)
218 ret = my_strstr(buf->data, str);
222 return ret - buf->data;
225 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
229 if (pos1 < 0) return NULL;
233 if (sz <= 0) sz = buf->size - pos1;
235 buf_append(ret, buf->data + pos1, sz);
242 * @brief HTTPによるダンプ内容伝送
245 * @param buf 伝送内容バッファ
248 static bool http_post(int sd, concptr url, BUF *buf)
251 char response_buf[1024] = "";
252 concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
255 buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
256 buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
257 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
259 buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
260 buf_sprintf(output, "Content-Encoding: binary\r\n");
263 buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
266 buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
269 buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
271 buf_sprintf(output, "\r\n");
272 buf_append(output, buf->data, buf->size);
274 soc_write(sd, output->data, output->size);
276 soc_read(sd, response_buf, sizeof(response_buf));
278 return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
282 * @brief キャラクタダンプを作って BUFに保存
283 * @param creature_ptr プレーヤーへの参照ポインタ
284 * @param dumpbuf 伝送内容バッファ
287 static errr make_dump(player_type *creature_ptr, BUF* dumpbuf)
291 GAME_TEXT file_name[1024];
293 /* Open a new file */
294 fff = my_fopen_temp(file_name, 1024);
298 msg_format("一時ファイル %s を作成できませんでした。", file_name);
300 msg_format("Failed to create temporary file %s.", file_name);
306 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
307 (void)make_character_dump(creature_ptr, fff);
311 fff = my_fopen(file_name, "r");
313 while (fgets(buf, 1024, fff))
315 (void)buf_sprintf(dumpbuf, "%s", buf);
325 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
326 * @return 作成したスクリーンダンプの参照ポインタ
328 concptr make_screen_dump(player_type *creature_ptr)
334 TERM_COLOR a = 0, old_a = 0;
337 static concptr html_head[] = {
338 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
342 static concptr html_foot[] = {
344 "</body>\n</html>\n",
348 bool old_use_graphics = use_graphics;
352 Term_get_size(&wid, &hgt);
355 screen_buf = buf_new();
356 if (screen_buf == NULL) return (NULL);
358 if (old_use_graphics)
360 /* Clear -more- prompt first */
363 use_graphics = FALSE;
366 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
370 for (i = 0; html_head[i]; i++)
371 buf_sprintf(screen_buf, html_head[i]);
373 /* Dump the screen */
374 for (y = 0; y < hgt; y++)
378 buf_sprintf(screen_buf, "\n");
381 for (x = 0; x < wid - 1; x++)
385 /* Get the attr/char */
386 (void)(Term_what(x, y, &a, &c));
390 case '&': cc = "&"; break;
391 case '<': cc = "<"; break;
392 case '>': cc = ">"; break;
393 case '"': cc = """; break;
394 case '\'': cc = "'"; break;
396 case 0x1f: c = '.'; break;
397 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
402 if ((y == 0 && x == 0) || a != old_a) {
403 rv = angband_color_table[a][1];
404 gv = angband_color_table[a][2];
405 bv = angband_color_table[a][3];
406 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">",
407 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
411 buf_sprintf(screen_buf, "%s", cc);
413 buf_sprintf(screen_buf, "%c", c);
416 buf_sprintf(screen_buf, "</font>");
418 for (i = 0; html_foot[i]; i++)
419 buf_sprintf(screen_buf, html_foot[i]);
421 /* Screen dump size is too big ? */
422 if (screen_buf->size + 1> SCREEN_BUF_MAX_SIZE)
428 /* Terminate string */
429 buf_append(screen_buf, "", 1);
431 ret = string_make(screen_buf->data);
435 buf_delete(screen_buf);
437 if (old_use_graphics)
442 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
450 * @brief スコア転送処理のメインルーチン
451 * @param creature_ptr プレーヤーへの参照ポインタ
454 errr report_score(player_type *creature_ptr)
464 WORD wVersionRequested =(WORD) (( 1) | ( 1 << 8));
469 char seikakutmp[128];
474 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
476 sprintf(seikakutmp, "%s ", ap_ptr->title);
479 buf_sprintf(score, "name: %s\n", creature_ptr->name);
481 buf_sprintf(score, "version: 変愚蛮怒 %d.%d.%d\n",
482 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
484 buf_sprintf(score, "version: Hengband %d.%d.%d\n",
485 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
487 buf_sprintf(score, "score: %d\n", calc_score(creature_ptr));
488 buf_sprintf(score, "level: %d\n", creature_ptr->lev);
489 buf_sprintf(score, "depth: %d\n", creature_ptr->current_floor_ptr->dun_level);
490 buf_sprintf(score, "maxlv: %d\n", creature_ptr->max_plv);
491 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
492 buf_sprintf(score, "au: %d\n", creature_ptr->au);
493 buf_sprintf(score, "turns: %d\n", turn_real(current_world_ptr->game_turn));
494 buf_sprintf(score, "sex: %d\n", creature_ptr->psex);
495 buf_sprintf(score, "race: %s\n", rp_ptr->title);
496 buf_sprintf(score, "class: %s\n", cp_ptr->title);
497 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
498 buf_sprintf(score, "realm1: %s\n", realm_names[creature_ptr->realm1]);
499 buf_sprintf(score, "realm2: %s\n", realm_names[creature_ptr->realm2]);
500 buf_sprintf(score, "killer: %s\n", creature_ptr->died_from);
501 buf_sprintf(score, "-----charcter dump-----\n");
503 make_dump(creature_ptr, score);
507 buf_sprintf(score, "-----screen shot-----\n");
508 buf_append(score, screen_dump, strlen(screen_dump));
512 if (WSAStartup(wVersionRequested, &wsaData))
514 msg_print("Report: WSAStartup failed.");
520 #if TARGET_API_MAC_CARBON
521 err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
523 err = InitOpenTransport();
527 msg_print("Report: OpenTransport failed.");
540 prt("connecting...", 0, 0);
545 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
547 /* Connect to the score server */
548 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
553 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
555 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
561 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
563 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
574 prt("スコア送信中...", 0, 0);
576 prt("Sending the score...", 0, 0);
580 if (!http_post(sd, SCORE_PATH, score)) {
581 disconnect_server(sd);
583 sprintf(buff, "スコア・サーバへの送信に失敗しました。");
585 sprintf(buff, "Failed to send to the score server.");
591 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
593 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
603 disconnect_server(sd);
613 #if TARGET_API_MAC_CARBON
614 CloseOpenTransportInContext(NULL);
616 CloseOpenTransport();
623 #endif /* WORLD_SCORE */