feat(ui): implement responsive player browser experience
This commit is contained in:
@@ -187,6 +187,8 @@ typedef struct {
|
||||
uint32_t state_sequence;
|
||||
uint32_t meeting_sequence;
|
||||
maze32_game_final_result_t final_result;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint16_t self_cell_index;
|
||||
uint16_t opponent_cell_index;
|
||||
uint16_t observer_player_cell_indices[MAZE32_GAME_PLAYER_COUNT];
|
||||
|
||||
@@ -461,6 +461,8 @@ maze32_game_result_t maze32_game_project_view(const maze32_game_t* game, maze32_
|
||||
view->state_sequence = game->state_sequence;
|
||||
view->meeting_sequence = game->meeting_sequence;
|
||||
view->final_result = game->final_result;
|
||||
view->width = game->maze->config.width;
|
||||
view->height = game->maze->config.height;
|
||||
view->self_carried_key = MAZE32_GAME_NO_KEY;
|
||||
view->opponent_carried_key = MAZE32_GAME_NO_KEY;
|
||||
view->opponent_cell_index = UINT16_MAX;
|
||||
@@ -648,11 +650,12 @@ maze32_game_result_t maze32_game_serialize_view_chunk(const maze32_view_t* view,
|
||||
size_t offset = 0U;
|
||||
if (!append_text(output, output_size, &offset,
|
||||
"{\"v\":1,\"type\":\"view\",\"seq\":%lu,\"payload\":{\"role\":\"%s\","
|
||||
"\"phase\":\"%s\",\"result\":\"%s\",\"meetingSequence\":%lu,"
|
||||
"\"phase\":\"%s\",\"result\":\"%s\",\"width\":%u,\"height\":%u,"
|
||||
"\"meetingSequence\":%lu,"
|
||||
"\"chunk\":%u,\"chunks\":%u",
|
||||
(unsigned long)view->state_sequence, role_name(view->role),
|
||||
phase_name(view->phase), final_result_name(view->final_result),
|
||||
(unsigned long)view->meeting_sequence, (unsigned int)chunk_index,
|
||||
phase_name(view->phase), final_result_name(view->final_result), view->width,
|
||||
view->height, (unsigned long)view->meeting_sequence, (unsigned int)chunk_index,
|
||||
(unsigned int)total_chunks)) {
|
||||
return MAZE32_GAME_RESULT_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
@@ -7,22 +7,37 @@
|
||||
#define MAZE32_STATIC_CHUNK_SIZE 1024
|
||||
|
||||
static const char* TAG = "maze32_web";
|
||||
static const char* INDEX_PATH = "/littlefs/index.html.gz";
|
||||
|
||||
typedef struct {
|
||||
const char* path;
|
||||
const char* content_type;
|
||||
} maze32_static_asset_t;
|
||||
|
||||
static const maze32_static_asset_t INDEX_ASSET = {"/littlefs/index.html.gz",
|
||||
"text/html; charset=utf-8"};
|
||||
static const maze32_static_asset_t PLAYER_STYLE_ASSET = {"/littlefs/player.css.gz", "text/css"};
|
||||
static const maze32_static_asset_t PLAYER_SCRIPT_ASSET = {"/littlefs/player.js.gz",
|
||||
"text/javascript; charset=utf-8"};
|
||||
static const maze32_static_asset_t ICON_ASSET = {"/littlefs/icons.svg.gz", "image/svg+xml"};
|
||||
|
||||
static void preserve_static_context(void* context) {
|
||||
(void)context;
|
||||
}
|
||||
|
||||
static esp_err_t send_index(httpd_req_t* request) {
|
||||
FILE* file = fopen(INDEX_PATH, "rb");
|
||||
static esp_err_t send_static_asset(httpd_req_t* request) {
|
||||
const maze32_static_asset_t* asset = request->user_ctx;
|
||||
if (asset == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
FILE* file = fopen(asset->path, "rb");
|
||||
if (file == NULL) {
|
||||
ESP_LOGE(TAG, "Static asset is unavailable: %s", INDEX_PATH);
|
||||
ESP_LOGE(TAG, "Static asset is unavailable: %s", asset->path);
|
||||
httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR,
|
||||
"Приложение временно недоступно");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
httpd_resp_set_type(request, "text/html; charset=utf-8");
|
||||
httpd_resp_set_type(request, asset->content_type);
|
||||
httpd_resp_set_hdr(request, "Content-Encoding", "gzip");
|
||||
httpd_resp_set_hdr(request, "Cache-Control", "no-cache");
|
||||
|
||||
@@ -37,7 +52,7 @@ static esp_err_t send_index(httpd_req_t* request) {
|
||||
}
|
||||
|
||||
if (ferror(file) != 0) {
|
||||
ESP_LOGE(TAG, "Failed while reading static asset: %s", INDEX_PATH);
|
||||
ESP_LOGE(TAG, "Failed while reading static asset: %s", asset->path);
|
||||
result = ESP_FAIL;
|
||||
}
|
||||
fclose(file);
|
||||
@@ -74,7 +89,7 @@ esp_err_t maze32_web_start(httpd_handle_t* server, maze32_health_t* health,
|
||||
}
|
||||
|
||||
httpd_config_t configuration = HTTPD_DEFAULT_CONFIG();
|
||||
configuration.max_uri_handlers = 9;
|
||||
configuration.max_uri_handlers = 12;
|
||||
configuration.max_open_sockets = 9;
|
||||
configuration.lru_purge_enable = false;
|
||||
configuration.recv_wait_timeout = 5;
|
||||
@@ -91,14 +106,32 @@ esp_err_t maze32_web_start(httpd_handle_t* server, maze32_health_t* health,
|
||||
const httpd_uri_t root = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_index,
|
||||
.user_ctx = NULL,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&INDEX_ASSET,
|
||||
};
|
||||
const httpd_uri_t index = {
|
||||
.uri = "/index.html",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_index,
|
||||
.user_ctx = NULL,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&INDEX_ASSET,
|
||||
};
|
||||
const httpd_uri_t player_style = {
|
||||
.uri = "/player.css",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&PLAYER_STYLE_ASSET,
|
||||
};
|
||||
const httpd_uri_t player_script = {
|
||||
.uri = "/player.js",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&PLAYER_SCRIPT_ASSET,
|
||||
};
|
||||
const httpd_uri_t icons = {
|
||||
.uri = "/icons.svg",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&ICON_ASSET,
|
||||
};
|
||||
const httpd_uri_t health_uri = {
|
||||
.uri = "/health",
|
||||
@@ -111,6 +144,15 @@ esp_err_t maze32_web_start(httpd_handle_t* server, maze32_health_t* health,
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &index);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &player_style);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &player_script);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &icons);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &health_uri);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user