Skip to content

Commit

Permalink
Fix: Server backend patch (Establish integer value auto convert from …
Browse files Browse the repository at this point in the history
…String)

Fix: Web client
Fix: Client code
  • Loading branch information
badcast committed Feb 5, 2024
1 parent 2f78e89 commit 52246ad
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 93 deletions.
10 changes: 5 additions & 5 deletions client-frontend/dragon-tea/include/tea_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ struct tea_net_stats

struct tea_server_urls
{
char url[255];
char url_auth[255];
char url_reg[255];
char url_msg_handler[255];
char url_info[255];
char url_base[64];
char url_auth[64];
char url_reg[64];
char url_msg_handler[64];
char url_info[64];
};

// Server features
Expand Down
16 changes: 10 additions & 6 deletions client-frontend/dragon-tea/src/net/tea_net_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ int tea_switch_server(int newServerID)
// init new server ID
tea_read_urls(&cur_server.urls);
tea_fetch_server();

return 0;
}

Expand Down Expand Up @@ -558,6 +559,9 @@ int tea_fetch_server()
else
result = 0;

if(!result)
tea_log("Server fetch status is failed");

return result;
}

Expand All @@ -571,17 +575,17 @@ void tea_read_urls(struct tea_server_urls *wrData)
return;
}

int len = MIN(strlen(server), sizeof(wrData->url));
strncpy(wrData->url, server, len);
int len = MIN(strlen(server), sizeof(wrData->url_base));
strncpy(wrData->url_base, server, len);

if(len >= (sizeof(wrData->url)-1))
if(len >= (sizeof(wrData->url_base) - 1))
{
tea_log("Warn: Server string length biggested, maximum length 255");
}
else if(wrData->url[len - 1] != '/')
else if(wrData->url_base[len - 1] != '/')
{
strncat(wrData->url, "/", MIN(len-1,1));
tea_log("Server is no containы \"/\", inserted as default");
strncat(wrData->url_base, "/", MIN(len - 1, 1));
tea_log("Server is no contains \"/\", inserted as default");
}

snprintf(wrData->url_auth, sizeof(wrData->url_auth), "%sapi/auth.php", server);
Expand Down
2 changes: 1 addition & 1 deletion client-frontend/dragon-tea/src/tea.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void ui_error_fail(const char *str)

int tea_server_version()
{
if(strlen(cur_server.urls.url) == 0)
if(strlen(cur_server.urls.url_base) == 0)
return -1;

return (int) (cur_server.server_version.major | cur_server.server_version.minor << 8 | cur_server.server_version.patch << 16);
Expand Down
13 changes: 8 additions & 5 deletions client-frontend/webclient/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@

<head>
<title>Dragon Tea - Web Client</title>
<script src="./tea_api.js"></script>
<script src="./tea_ui.js"></script>
<script src="./js/tea_base.js"></script>
<script src="./js/tea_api.js"></script>
<script src="./js/tea_ui.js"></script>
<link rel="stylesheet" type="text/css" href="./styles/design.css">
<link rel="stylesheet" type="text/css" href="./styles/form.css">
</head>

<body>
<body onload="tea_ui_start();">
<div id="_wforms">
<div class="form_sigin">
<p>Добро пожаловать на сервер Драконего чая!</p>
<input id="inputSignin" type="text" placeholder="User ID" value=""/>
<button style="left: 0;" onclick="document.getElementById('inputSignin').value=''">x</button>
<p></p>
<button onclick="tea_ui_signin()">Вход</button>
<span class="sticky_label2" id="signStatusBox">Не выполнен вход.</span>
<p></p>
<button onclick="tea_ui_signin(this)">Вход</button>
<div style="font-size: 12px;">
<p class="sticky_label">(!) для входа требуется только UserID которое выдано лично вам.</p>
<p>Данная Веб странница под лицензией <a target="_blank"
Expand All @@ -41,4 +44,4 @@
</div>
</body>

</html>
</html>
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
function tea_str_error(errcode) {
// Backend status reserved
const STATUS_OK = 0;
const STATUS_ID_NO_EXIST = 1;
const STATUS_INVALID_REQUEST_DATA = 2;
const STATUS_INVALID_AUTH_METHOD = 3;
const STATUS_INVALID_NICKNAME = 4;
const STATUS_INVALID_REGISTER = 5;
const STATUS_ADMIN_ACCOUNT_REACHABLE = 6;

const STATUS_PRIVATE_MESSAGE_NOT_SUPPORTED = 128;

const STATUS_INTERNAL_SERVER_ERROR = 500;
// Frontend status reserved
const STATUS_NETWORK_ERROR = 1000;

switch (errcode) {
case STATUS_OK:
Expand All @@ -30,11 +31,13 @@ function tea_str_error(errcode) {
return "private message not supported";
case STATUS_INTERNAL_SERVER_ERROR:
return "internal server error";
case STATUS_NETWORK_ERROR:
return "network error";
}
return "";
}

var net_stats =
var netStats =
{
requestVerified: 0,
requestError: 0,
Expand All @@ -57,6 +60,8 @@ var userProfile =
}
};

var lastStatus = null;

async function tea_request(requestData) {
const _netapi = {
signin: "/api/auth.php",
Expand All @@ -71,61 +76,61 @@ async function tea_request(requestData) {
let bodyStr = JSON.stringify(requestData);
console.log(bodyStr);

// Опции для запроса
// Request Options
const requestOptions = {
method: 'POST',
mode: 'cors',
body: bodyStr
};

// Выполняем запрос
let status = await fetch(url, requestOptions).then(response => response.text()) // Распарсить ответ как JSON
.then(data => {
let status = false;
// Request
let status = await fetch(url, requestOptions).then(response => response.text())
.then(responce => {
let networkStatus = false;
let errStr = null;
net_stats.requestVerified++;
netStats.requestVerified++;
while (1) {
if (data == "") {
if (responce == "") {
errStr = "Internal server error. Responce is empty.";
break;
}

// Get index of the JSON data
let fIndex = data.indexOf("{");
let fIndex = responce.indexOf("{");
if ((serverInfo.hasDamager = fIndex != 0)) {
if (fIndex === -1) {
errStr = "Server is force damager";
break;
}
// remove damaged
data = data.slice(fIndex, data.length);
responce = responce.slice(fIndex, responce.length);
}

// Convert string data to json object
data = JSON.parse(data);
responce = JSON.parse(responce);

if (data.status != 0) {
errStr = tea_str_error(data.status);
if (responce.status != 0) {
errStr = tea_str_error(responce.status);
break;
}

if (data.authorized) {
status = true;
if (responce.authorized) {
networkStatus = true;
}
break;
}

if (status) {

if (networkStatus) {
console.log("Network status: " + networkStatus);
}

return { error: errStr, ok: status }
return { error: errStr, ok: networkStatus, code: responce.status }
})
.catch(error => {
// Обработка ошибок
console.error(error);
net_stats.requestError++;
return { error: error, ok: false };
netStats.requestError++;
return { error: error, ok: false, code: STATUS_CLIENT_CATCH };
});

if (status.ok) {
Expand All @@ -134,5 +139,8 @@ async function tea_request(requestData) {
else {
console.error(status.error);
}

lastResponce = status;

return status;
}
5 changes: 5 additions & 0 deletions client-frontend/webclient/js/tea_base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

async function waitOut(ms)
{
await new Promise(resolve => setTimeout(resolve, 2000));
}
47 changes: 47 additions & 0 deletions client-frontend/webclient/js/tea_ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

const FORM =
{
FDialog: 0,
FSigin: 1
};

var UI =
{
forms: [],
activeForm: 0
};

function setSignFormStatus(message) {
let inputMsg = document.getElementById("signStatusBox");
inputMsg.innerText = message;
}

async function tea_ui_signin(self) {
let userId = document.getElementById("inputSignin").value;

setSignFormStatus("Входим...");

// Check and validate of the ID
if (/^\d+$/.test(userId) == false) {
setSignFormStatus("Инвалидное поле");
return false;
}

self.disabled = true;

let req = { user_id: userId };
let responce = await tea_request(req);

await waitOut(200);

if (responce.ok)
setSignFormStatus("Вход выполнен");
else
setSignFormStatus(responce.error);

self.disabled = false;
}

function tea_ui_start() {

}
9 changes: 8 additions & 1 deletion client-frontend/webclient/styles/design.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ a:hover {
background: rgba(226, 255, 25, 0.74);
border-radius: 10px;
padding: 3px;
}
}

.sticky_label2
{
background: rgba(0, 0, 0, 0.068);
border-radius: 5px;
padding: 3px;
}
11 changes: 5 additions & 6 deletions client-frontend/webclient/styles/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
padding: 10px;
position: absolute;
width: 400px;
height: 300px;
left: 0;
top: 0;
bottom: 0;
right: 0;
height: auto; /* автоматическая высота */
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
}
30 changes: 0 additions & 30 deletions client-frontend/webclient/tea_ui.js

This file was deleted.

1 change: 1 addition & 0 deletions server-backend/php/api/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

// Парсим JSON-строку
$data = json_decode($json_data);
$data->user_id = patch_convert_id($data->user_id);

// Проверяем, удалось ли распарсить JSON
if ($data === null || !is_int($data->user_id)) {
Expand Down
Loading

0 comments on commit 52246ad

Please sign in to comment.