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"
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 */
64 * simple buffer library
72 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
76 * @return 確保したバッファの参照ポインタ
78 static BUF* buf_new(void)
81 p = malloc(sizeof(BUF));
85 p->max_size = BUFSIZE;
86 p->data = malloc(BUFSIZE);
99 * @param b 解放するバッファの参照ポインタ
101 static void buf_delete(BUF *b)
109 * @brief 転送用バッファにデータを追加する
110 * @param buf 追加先バッファの参照ポインタ
115 static int buf_append(BUF *buf, concptr data, size_t size)
117 while (buf->size + size > buf->max_size)
120 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
122 memcpy(tmp, buf->data, buf->max_size);
129 memcpy(buf->data + buf->size, data, size);
137 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
138 * @param buf 追加先バッファの参照ポインタ
139 * @param fmt 文字列フォーマット
142 static int buf_sprintf(BUF *buf, concptr fmt, ...)
149 #if defined(HAVE_VSNPRINTF)
150 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
152 ret = vsprintf(tmpbuf, fmt, ap);
156 if (ret < 0) return -1;
158 #if ('\r' == 0x0a && '\n' == 0x0d)
161 * Originally '\r'= CR (= 0x0d) and '\n'= LF (= 0x0a)
162 * But for MPW (Macintosh Programers Workbench), these
163 * are reversed so that '\r'=LF and '\n'=CR unless the
164 * -noMapCR option is not defined.
166 * We need to swap back these here since the score
167 * dump text should be written using LF as the end of
171 for (ptr = tmpbuf; *ptr; ptr++)
173 if (0x0d == *ptr) *ptr = 0x0a;
178 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
185 * @brief HTTPによるダンプ内容伝送
188 * @param buf 伝送内容バッファ
191 static bool http_post(int sd, concptr url, BUF *buf)
194 char response_buf[1024] = "";
195 concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
198 buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
199 buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
200 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
202 buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
203 buf_sprintf(output, "Content-Encoding: binary\r\n");
206 buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
209 buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
212 buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
214 buf_sprintf(output, "\r\n");
215 buf_append(output, buf->data, buf->size);
217 soc_write(sd, output->data, output->size);
219 soc_read(sd, response_buf, sizeof(response_buf));
221 return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
226 * @brief キャラクタダンプを作って BUFに保存
227 * @param creature_ptr プレーヤーへの参照ポインタ
228 * @param dumpbuf 伝送内容バッファ
231 static errr make_dump(player_type *creature_ptr, BUF* dumpbuf)
235 GAME_TEXT file_name[1024];
237 /* Open a new file */
238 fff = my_fopen_temp(file_name, 1024);
242 msg_format("一時ファイル %s を作成できませんでした。", file_name);
244 msg_format("Failed to create temporary file %s.", file_name);
250 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
251 (void)make_character_dump(creature_ptr, fff);
255 fff = my_fopen(file_name, "r");
257 while (fgets(buf, 1024, fff))
259 (void)buf_sprintf(dumpbuf, "%s", buf);
270 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
271 * @return 作成したスクリーンダンプの参照ポインタ
273 concptr make_screen_dump(player_type *creature_ptr)
275 static concptr html_head[] = {
276 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
280 static concptr html_foot[] = {
282 "</body>\n</html>\n",
287 Term_get_size(&wid, &hgt);
291 screen_buf = buf_new();
292 if (screen_buf == NULL) return (NULL);
294 bool old_use_graphics = use_graphics;
295 if (old_use_graphics)
297 /* Clear -more- prompt first */
300 use_graphics = FALSE;
301 reset_visuals(creature_ptr);
303 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
304 handle_stuff(creature_ptr);
307 for (int i = 0; html_head[i]; i++)
308 buf_sprintf(screen_buf, html_head[i]);
310 /* Dump the screen */
311 for (int y = 0; y < hgt; y++)
315 buf_sprintf(screen_buf, "\n");
318 TERM_COLOR a = 0, old_a = 0;
320 for (int x = 0; x < wid - 1; x++)
324 /* Get the attr/char */
325 (void)(Term_what(x, y, &a, &c));
329 case '&': cc = "&"; break;
330 case '<': cc = "<"; break;
331 case '>': cc = ">"; break;
332 case '"': cc = """; break;
333 case '\'': cc = "'"; break;
335 case 0x1f: c = '.'; break;
336 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
341 if ((y == 0 && x == 0) || a != old_a) {
342 rv = angband_color_table[a][1];
343 gv = angband_color_table[a][2];
344 bv = angband_color_table[a][3];
345 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">",
346 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
351 buf_sprintf(screen_buf, "%s", cc);
353 buf_sprintf(screen_buf, "%c", c);
357 buf_sprintf(screen_buf, "</font>");
359 for (int i = 0; html_foot[i]; i++)
360 buf_sprintf(screen_buf, html_foot[i]);
362 /* Screen dump size is too big ? */
364 if (screen_buf->size + 1 > SCREEN_BUF_MAX_SIZE)
370 /* Terminate string */
371 buf_append(screen_buf, "", 1);
373 ret = string_make(screen_buf->data);
377 buf_delete(screen_buf);
379 if (!old_use_graphics) return ret;
382 reset_visuals(creature_ptr);
384 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
385 handle_stuff(creature_ptr);
391 * todo メッセージは言語選択の関数マクロで何とかならんか?
392 * @brief スコア転送処理のメインルーチン
393 * @param creature_ptr プレーヤーへの参照ポインタ
396 errr report_score(player_type *creature_ptr)
401 WORD wVersionRequested = (WORD)((1) | (1 << 8));
407 char seikakutmp[128];
409 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
411 sprintf(seikakutmp, "%s ", ap_ptr->title);
414 buf_sprintf(score, "name: %s\n", creature_ptr->name);
416 buf_sprintf(score, "version: 変愚蛮怒 %d.%d.%d\n",
417 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
419 buf_sprintf(score, "version: Hengband %d.%d.%d\n",
420 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH);
422 buf_sprintf(score, "score: %d\n", calc_score(creature_ptr));
423 buf_sprintf(score, "level: %d\n", creature_ptr->lev);
424 buf_sprintf(score, "depth: %d\n", creature_ptr->current_floor_ptr->dun_level);
425 buf_sprintf(score, "maxlv: %d\n", creature_ptr->max_plv);
426 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
427 buf_sprintf(score, "au: %d\n", creature_ptr->au);
428 buf_sprintf(score, "turns: %d\n", turn_real(creature_ptr, current_world_ptr->game_turn));
429 buf_sprintf(score, "sex: %d\n", creature_ptr->psex);
430 buf_sprintf(score, "race: %s\n", rp_ptr->title);
431 buf_sprintf(score, "class: %s\n", cp_ptr->title);
432 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
433 buf_sprintf(score, "realm1: %s\n", realm_names[creature_ptr->realm1]);
434 buf_sprintf(score, "realm2: %s\n", realm_names[creature_ptr->realm2]);
435 buf_sprintf(score, "killer: %s\n", creature_ptr->died_from);
436 buf_sprintf(score, "-----charcter dump-----\n");
438 make_dump(creature_ptr, score);
442 buf_sprintf(score, "-----screen shot-----\n");
443 buf_append(score, screen_dump, strlen(screen_dump));
447 if (WSAStartup(wVersionRequested, &wsaData))
449 msg_print("Report: WSAStartup failed.");
463 prt("connecting...", 0, 0);
468 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
470 /* Connect to the score server */
471 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
476 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
478 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
484 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
486 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
497 prt("スコア送信中...", 0, 0);
499 prt("Sending the score...", 0, 0);
503 if (!http_post(sd, SCORE_PATH, score)) {
504 disconnect_server(sd);
506 sprintf(buff, "スコア・サーバへの送信に失敗しました。");
508 sprintf(buff, "Failed to send to the score server.");
514 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
516 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
526 disconnect_server(sd);
538 #endif /* WORLD_SCORE */