Skip to content

Commit

Permalink
Merge pull request #16 from ArKaNeMaN/fix/set-before-user-loaded
Browse files Browse the repository at this point in the history
Исправлена ошибка при попытке сохранить значение до загрузки значений игрока
  • Loading branch information
ufame authored Jan 17, 2024
2 parents 00fce40 + 48f28ac commit b6ef2d0
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions scripting/player_prefs.sma
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum _: IntertKey {
value[256]
};

new g_iPlayerDatabaseId[MAX_PLAYERS + 1];
new g_iPlayerDatabaseId[MAX_PLAYERS + 1] = {0, ...};
new Trie: g_tPlayerPreferences[MAX_PLAYERS + 1];

new Trie: g_tKeys;
Expand Down Expand Up @@ -81,10 +81,7 @@ public bool: native_is_loaded(iPlugin, iArgs) {

new iPlayer = get_param(arg_player_id);

if (!is_user_connected(iPlayer))
return false;

return bool: g_iPlayerDatabaseId[iPlayer];
return IsUserLoaded(iPlayer);
}

public bool: native_get_preference(iPlugin, iArgs) {
Expand Down Expand Up @@ -126,6 +123,11 @@ public bool: native_set_preference(iPlugin, iArgs) {
if (!is_user_connected(iPlayer))
return false;

if (!IsUserLoaded(iPlayer)) {
log_error(0, "Attempt to set preference for not loaded player (%d).", iPlayer);
return false;
}

new szKey[32], szValue[256], szDefaultValue[256];

get_string(arg_key, szKey, charsmax(szKey));
Expand Down Expand Up @@ -167,6 +169,13 @@ public bool: native_set_key_default_value(iPlugin, iArgs) {
return bool: TrieSetString(g_tKeys, szKey, szDefaultValue);
}

bool:IsUserLoaded(const iPlayer) {
if (!is_user_connected(iPlayer))
return false;

return g_iPlayerDatabaseId[iPlayer] > 0;
}

stock LoadPreferences(iPlayer) {
if (g_hSqlTuple == Empty_Handle) {
ExecuteForward(g_iForwards[Forward_PlayerLoaded], _, iPlayer);
Expand Down Expand Up @@ -349,6 +358,9 @@ public ThreadQuery_Handler(iFailState, Handle: hQuery, szError[], iError, szData
if (iUserid != get_user_userid(iPlayer))
return;

if (g_iPlayerDatabaseId[iPlayer] <= 0)
return;

new iKeyId = szData[3] == 0 ? SQL_GetInsertId(hQuery) : szData[3];

formatex(g_szQuery, charsmax(g_szQuery),
Expand Down

0 comments on commit b6ef2d0

Please sign in to comment.