From: Holger Schemel Date: Mon, 16 Aug 2021 21:39:56 +0000 (+0200) Subject: added renaming player also on score server X-Git-Tag: 4.3.0.0~94 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=0321cbef506f76d246fda98882aa8ad21a48be27;p=rocksndiamonds.git added renaming player also on score server --- diff --git a/src/libgame/system.h b/src/libgame/system.h index 4f17a86c..013b729e 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -111,6 +111,7 @@ #define API_SERVER_METHOD "POST" #define API_SERVER_URI_ADD "/api/scores/add" #define API_SERVER_URI_GET "/api/scores/get" +#define API_SERVER_URI_RENAME "/api/players/rename" // values for touch control #define TOUCH_CONTROL_OFF "off" diff --git a/src/screens.c b/src/screens.c index 4dcaef34..5c78347a 100644 --- a/src/screens.c +++ b/src/screens.c @@ -4035,6 +4035,99 @@ void HandleInfoScreen(int mx, int my, int dx, int dy, int button) } +// ============================================================================ +// change name functions +// ============================================================================ + +static void RenamePlayerOnServerExt(struct HttpRequest *request, + struct HttpResponse *response, + char *player_name_raw, + char *player_uuid_raw) +{ + request->hostname = setup.api_server_hostname; + request->port = API_SERVER_PORT; + request->method = API_SERVER_METHOD; + request->uri = API_SERVER_URI_RENAME; + + char *player_name = getEscapedJSON(player_name_raw); + char *player_uuid = getEscapedJSON(player_uuid_raw); + + snprintf(request->body, MAX_HTTP_BODY_SIZE, + "{\n" + "%s" + " \"name\": \"%s\",\n" + " \"uuid\": \"%s\"\n" + "}\n", + getPasswordJSON(setup.api_server_password), + player_name, + player_uuid); + + checked_free(player_name); + checked_free(player_uuid); + + ConvertHttpRequestBodyToServerEncoding(request); + + if (!DoHttpRequest(request, response)) + { + Error("HTTP request failed: %s", GetHttpError()); + + return; + } + + if (!HTTP_SUCCESS(response->status_code)) + { + Error("server failed to handle request: %d %s", + response->status_code, + response->status_text); + + return; + } +} + +static void RenamePlayerOnServer(char *player_name, char *player_uuid) +{ + struct HttpRequest *request = checked_calloc(sizeof(struct HttpRequest)); + struct HttpResponse *response = checked_calloc(sizeof(struct HttpResponse)); + + RenamePlayerOnServerExt(request, response, player_name, player_uuid); + + checked_free(request); + checked_free(response); +} + +struct RenamePlayerOnServerThreadData +{ + char *player_name; + char *player_uuid; +}; + +static int RenamePlayerOnServerThread(void *data_raw) +{ + struct RenamePlayerOnServerThreadData *data = data_raw; + + RenamePlayerOnServer(data->player_name, data->player_uuid); + + checked_free(data->player_name); + checked_free(data->player_uuid); + checked_free(data); + + return 0; +} + +static void RenamePlayerOnServerAsThread(void) +{ + struct RenamePlayerOnServerThreadData *data = + checked_malloc(sizeof(struct RenamePlayerOnServerThreadData)); + + data->player_name = getStringCopy(setup.player_name); + data->player_uuid = getStringCopy(setup.player_uuid); + + ExecuteAsThread(RenamePlayerOnServerThread, + "RenamePlayerOnServer", data, + "rename player on server"); +} + + // ============================================================================ // type name functions // ============================================================================ @@ -4172,6 +4265,9 @@ static void setTypeNameValues(char *name, struct TextPosInfo *pos, // save setup of edited user SaveSetup(); + // change name of edited user on score server + RenamePlayerOnServerAsThread(); + if (game_status == GAME_MODE_PSEUDO_TYPENAMES || reset_setup) { if (reset_setup)