Raised This Month: $51 Target: $400
 12% 

[TF2] Store data in database


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 10-18-2013 , 03:38   [TF2] Store data in database
Reply With Quote #1

I know, I have already asked for this but I still don't understand. How can I save data for a later use ?
Exemple :
Player1 is connected.
Player1 have done 33 points and left server.
Player1 come back after 3 houre and get back is 33 points.
Player1 done 22 points more (33+22 = 55).
Player1 left server again and have a total of 55 points.
Do this each time Player1 is connected / disconnected.

NOTE: Player1 is always the same player. Should work for all player.

What should be the code for this ?

PS: If it help, I want store my data in local database.
__________________
Want to check my plugins ?

Last edited by Arkarr; 10-18-2013 at 03:38.
Arkarr is offline
floube
SourceMod Donor
Join Date: Jan 2013
Location: Austria
Old 10-18-2013 , 04:55   Re: [TF2] Store data in database
Reply With Quote #2

Out of my head ...

PHP Code:
new g_iPoints[MAXPLAYERS 1];
new 
Handle:g_hDatabase INVALID_HANDLE;

public 
OnClientPutInServer(iClient) {
    
g_iPoints[iClient] = 0;

    
LoadPlayerData(iClient);
}

public 
OnClientDisconnect(iClient) {
    
GetPlayerData(iClient);

    
g_iPoints[iClient] = 0;
}

public 
OnMapStart() {    
    
ConnectToDatabase();
}

stock ConnectToDatabase() {
    
SQL_TConnect(SQL_OnConnect"yourconfigname");
}

stock LoadPlayerData(iClient) {
    new 
String:sQuery[512], String:sSteamID[32];

    
GetClientAuthString(iClientsSteamIDsizeof(sSteamID)); 

    
Format(sQuerysizeof(sQuery), "SELECT player_points FROM player_saves WHERE steam_id = '%s'"sSteamID);

    
SQL_TQuery(g_hDatabaseSQL_OnLoadPlayerDatasQueryiClient);
}

stock GetPlayerData(iClient) { 
    new 
String:sQuery[512], String:sSteamID[32];
        
    
GetClientAuthString(iClientsSteamIDsizeof(sSteamID)); 
        
    
Format(sQuerysizeof(sQuery), "SELECT * FROM player_saves WHERE steam_id = '%s'"sSteamID);

    
SQL_TQuery(g_hDatabaseSQL_OnGetPlayerDatasQueryiClient);
}

stock SavePlayerData(iClient) {
    new 
String:sQuery[512], String:sSteamID[32];
    new 
iPoints[MAXPLAYERS 1];

    
iPoints GetClientPoints(iClient);
    
GetClientAuthString(iClientsSteamIDsizeof(sSteamID)); 

    
Format(sQuerysizeof(sQuery), "INSERT INTO player_saves (steam_id, player_points) VALUES ('%s', '%d')"sSteamIDiPoints[iClient]);
        
    
iPoints[iClient] = 0;

    
SQL_TQuery(g_hDatabaseSQL_OnDefaultCallbacksQueryiClient);
}

stock UpdatePlayerData(iClient) { 
    new 
String:sQuery[512], String:sSteamID[32];
    new 
iPoints[MAXPLAYERS 1];
        
    
iPoints GetClientPoints(iClient);
    
GetClientAuthString(iClientsSteamIDsizeof(sSteamID)); 

    
Format(sQuerysizeof(sQuery), "UPDATE player_saves SET player_points = '%d' WHERE steam_id = '%s'"iPoints[iClient], sSteamID);
        
    
iPoints[iClient] = 0;

    
SQL_TQuery(g_hDatabaseSQL_OnDefaultCallbacksQueryiClient);
}

public 
SQL_OnDefaultCallback(Handle:hOwnerHandle:hTable, const String:sError[], any:iData) {
    if (
hTable == INVALID_HANDLE) { 
        
LogError("Query failed! %s"sError);
        return 
false;
    }

    return 
true;
}

public 
SQL_OnConnect(Handle:hOwnerHandle:hDatabase, const String:sError[], any:iData) { 
    if (
hDatabase == INVALID_HANDLE) { 
        
LogError("Database failure: %s"sError);
        
SetFailState("Error: %s"sError);

        return 
false;
    } else {
        
g_hDatabase hDatabase;

        return 
true;
    }
}

public 
SQL_OnLoadPlayerData(Handle:hOwnerHandle:hTable, const String:sError[], any:iData) { 
    new 
iClient iData;

    if (
hTable == INVALID_HANDLE) { 
        
LogError("Query failed! %s"sError); 
        return 
false;
    } else if (
SQL_GetRowCount(hTable)) {
        
// Player data found

        
SQL_FetchRow(hTable);
        
g_iPoints[iClient] = SQL_FetchInt(hTable0);
        
        if (
g_iPoints[iClient] != 0) {
            
// Do something
        
}
    } 

    return 
true;
}

public 
SQL_OnGetPlayerData(Handle:hOwnerHandle:hTable, const String:sError[], any:iData) { 
    new 
iClient iData;

    if (
hTable == INVALID_HANDLE) { 
        
LogError("Query failed! %s"sError); 
        return 
false;
    } else if (
SQL_GetRowCount(hTable)) {
        
// Player does exist, update

        
UpdatePlayerData(iClient);    
    } else {
        
// Player does not exist, create new

        
SavePlayerData(iClient); 
    } 

    return 
true;
}

stock GetClientPoints(iClient) {
    if (
iClient || iClient MaxClients)
        return -
1;

    
// However you get the points ...

    
return GetClientFrags(iClient) - GetClientDeaths(iClient);

That'll cause problems if players crash (points won't be saved)

Last edited by floube; 10-18-2013 at 04:57.
floube is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 10-18-2013 , 04:59   Re: [TF2] Store data in database
Reply With Quote #3

Wow. Thanks.
__________________
Want to check my plugins ?
Arkarr is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 10-18-2013 , 05:16   Re: [TF2] Store data in database
Reply With Quote #4

If you only want to store points, i recommend you to read the Clientprefs snipped written by Kyle.
If you plan to later add more then only the points, it's better to do it like floube showed you.
__________________
Impact123 is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 10-18-2013 , 05:32   Re: [TF2] Store data in database
Reply With Quote #5

Quote:
Originally Posted by Impact123 View Post
If you only want to store points, i recommend you to read the Clientprefs snipped written by Kyle.
If you plan to later add more then only the points, it's better to do it like floube showed you.
Thanks, this looks usefull ! And yes, I planned to add more then points, like string and boolean. But it's look easy with this exemple.
__________________
Want to check my plugins ?
Arkarr is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:19.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode