Databases, accounts, and you. Part 1: basic tutorial.

Looking for Resources for your mod/project? Want to share some of your creations so others can use them? Post them here!
User avatar
Catastrophe
Topic author
ZanStuff Reviewer
Offline
Posts: 2258
Joined: Sat Jun 02, 2012 2:44 am

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#21 by Catastrophe » Tue Mar 28, 2017 9:03 am

marco75 wrote:


None of the hastebin.com links work for me, have they expired?

Can you edit post to show code with

Code: Select all

code
tag instead?

How do i export the database to a file if I'm running the Zandronum client?


Hey sorry about the very, very late reply. I didn't realize that hastebin auto-deletes pastes and unfortunately the code is lost. I will be making an updated tutorial soon on databases again so keep an eye on that.

Also to your last question, you need to be the host of a server to export to a database file. Just type in databasefile "namehere" in the server console and the file will be automatically generated.
Youtube Channel
Projects: Super Demon | Idiotic LMS | ZCC | Cata WeaponZ

Currently hooked on Overwatch and PUBattlegrounds.

User avatar
Catastrophe
Topic author
ZanStuff Reviewer
Offline
Posts: 2258
Joined: Sat Jun 02, 2012 2:44 am

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#22 by Catastrophe » Sun May 07, 2017 8:51 pm

Tutorial updated; the scripts are back up again.
Youtube Channel
Projects: Super Demon | Idiotic LMS | ZCC | Cata WeaponZ

Currently hooked on Overwatch and PUBattlegrounds.

User avatar
Ivan
Addicted to Zandronum
Offline
Posts: 1836
Joined: Mon Jun 04, 2012 5:38 pm

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#23 by Ivan » Wed Jun 28, 2017 3:13 pm

So when I try to use the database methods I get some problems. I create a local server with the authhostname set as auth.zandronum.com:166666. While saving the data, everything works fine but when loading it's all 0.

Relevant parts of code: (Pastebin: Click)
Spoiler (Open)

Code: Select all

Script 1001 (void) NET {
    // if not hardcore don't bother trying to load
    if(!GetCVar("dnd_hardcore"))
        Terminate;
   
    // not logged in we don't care
    if(!PlayerIsLoggedIn(PlayerNumber()) || !HardcoreSet)
        Terminate;
    LoadPlayerData(PlayerNumber());
}
 
void LoadPlayerData(int pnum) {
    // assumes all checks have been performed before reaching this function
    int tid = P_TIDSTART + pnum, i = 0;
    str pacc = GetPlayerAccountName(pnum);
    print(s:pacc);
    // prevents weapon tips from going crazy
    GiveActorInventory(tid, "ParsingData", 1);
   
    // read all things recorded one by one and give inventories
   
    // reset weapons
    ClearInventory();
    for(i = 0; i < MAXWEPS; ++i)
        TakeInventory(Weapons[i][WEPINFO_NAME], 1);
   
    // read weapons
    int temp = GetDBEntry(DND_DB_WEAPONINT_1, pacc);
    print(d:temp);
    for(i = SHOP_WEAPON_BEGIN; i < 32; ++i)
        if(IsSet(temp, i))
            GiveInventory(ShopItemNames[i][SHOPNAME_ITEM], 1);
   
    temp = GetDBEntry(DND_DB_WEAPONINT_2, pacc);
    for(i = SHOP_WEAPON_BEGIN + 32; i < SHOP_LASTWEP_INDEX; ++i)
        if(IsSet(temp, i))
            GiveInventory(ShopItemNames[i][SHOPNAME_ITEM], 1);
 
.......
.......
}
 
void SavePlayerData(int pnum) {
    int temp, tid = pnum + P_TIDSTART, i, bit;
    str pacc = GetPlayerAccountName(PlayerNumber());
    // save weapons
    for(i = 0; i < 32; ++i)
        if(CheckActorInventory(tid, Weapons[i][WEPINFO_NAME]))
            temp = SetBit(temp, i);
    // send temp over
    SetDBEntry(DND_DB_WEAPONINT_1, pacc, temp);
    Log(d:GetDBentry(DND_DB_WEAPONINT_1, pacc));
   
    temp = 0;
    for(i = 32; i < MAXWEPS; ++i)
        if(CheckActorInventory(tid, Weapons[i][WEPINFO_NAME]))
            temp = SetBit(temp, i);
    // send temp over
    SetDBEntry(DND_DB_WEAPONINT_2, pacc, temp);
.....
.....
}
 
// Handle all database stuff now
Script 894 OPEN {
    while(1) {
        if(HardcoreSet && MapChanged) {
            BeginDBTransaction();
            for(int i = 0; i < MAXPLAYERS; ++i) {
                if(PlayerInGame(i) && PlayerIsLoggedIn(i)) {
                    SavePlayerData(i);
                    Log(s:"Saving player ", d:i, s:"'s data.");
                }
            }
            EndDBTransaction();
        }
        Delay(35);
    }
}


I read correct values in the save function, which is usually a big number from GetDBEntry. But on the load function this is 0. Why is that?

Additionally, how do I view the database state? What must I do in this sqlite browser to view it?
=== RAGNAROK DM ON ... uh... dead forever? ===
=== ALWAYS BET ON ... uh... dead forever? ===
=== Who wanta sum wang? ===
=== Death and Decay - A new Monster/Weapon replacer ===

User avatar
Catastrophe
Topic author
ZanStuff Reviewer
Offline
Posts: 2258
Joined: Sat Jun 02, 2012 2:44 am

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#24 by Catastrophe » Wed Jun 28, 2017 5:54 pm

Ivan wrote:So when I try to use the database methods I get some problems. I create a local server with the authhostname set as auth.zandronum.com:166666. While saving the data, everything works fine but when loading it's all 0.

I read correct values in the save function, which is usually a big number from GetDBEntry. But on the load function this is 0. Why is that?

Additionally, how do I view the database state? What must I do in this sqlite browser to view it?


I see in one script you have

str pacc = GetPlayerAccountName(PlayerNumber());

Whereas in another script you have

str pacc = GetPlayerAccountName(pnum);

You may have an activator issue somewhere.

For the sqlite browser simply open the databasefile with it and all the data should appear under the "browse data" tab as shown in the image.
Youtube Channel
Projects: Super Demon | Idiotic LMS | ZCC | Cata WeaponZ

Currently hooked on Overwatch and PUBattlegrounds.

User avatar
Ivan
Addicted to Zandronum
Offline
Posts: 1836
Joined: Mon Jun 04, 2012 5:38 pm

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#25 by Ivan » Wed Jun 28, 2017 6:00 pm

Catastrophe wrote:
Ivan wrote:So when I try to use the database methods I get some problems. I create a local server with the authhostname set as auth.zandronum.com:166666. While saving the data, everything works fine but when loading it's all 0.

I read correct values in the save function, which is usually a big number from GetDBEntry. But on the load function this is 0. Why is that?

Additionally, how do I view the database state? What must I do in this sqlite browser to view it?


I see in one script you have

str pacc = GetPlayerAccountName(PlayerNumber());

Whereas in another script you have

str pacc = GetPlayerAccountName(pnum);

You may have an activator issue somewhere.

For the sqlite browser simply open the databasefile with it and all the data should appear under the "browse data" tab as shown in the image.


I think I also forgot to setup the database file or something, I can't find it. How do I set that up? Also, I plan to use this for automated servers (TSPG), what precautions should I take?
=== RAGNAROK DM ON ... uh... dead forever? ===
=== ALWAYS BET ON ... uh... dead forever? ===
=== Who wanta sum wang? ===
=== Death and Decay - A new Monster/Weapon replacer ===

User avatar
Catastrophe
Topic author
ZanStuff Reviewer
Offline
Posts: 2258
Joined: Sat Jun 02, 2012 2:44 am

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#26 by Catastrophe » Wed Jun 28, 2017 6:08 pm

Ivan wrote:I think I also forgot to setup the database file or something, I can't find it. How do I set that up? Also, I plan to use this for automated servers (TSPG), what precautions should I take?


In the server console (or rcon), do "Databasefile namehere". That will automatically generate a databasefile which will be named "namehere". As it stands anyone in TSPG can access your database if they manage to guess the name correctly. There is no safety net in public server clusters like TSPG. But if you can set the name to something no one can guess, then you're safe. Personally I set the databasefile to the default rcon password TSPG provides as its unique and extremely hard to guess.
Youtube Channel
Projects: Super Demon | Idiotic LMS | ZCC | Cata WeaponZ

Currently hooked on Overwatch and PUBattlegrounds.

User avatar
Ivan
Addicted to Zandronum
Offline
Posts: 1836
Joined: Mon Jun 04, 2012 5:38 pm

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#27 by Ivan » Wed Jun 28, 2017 7:09 pm

Catastrophe wrote:
Ivan wrote:I think I also forgot to setup the database file or something, I can't find it. How do I set that up? Also, I plan to use this for automated servers (TSPG), what precautions should I take?


In the server console (or rcon), do "Databasefile namehere". That will automatically generate a databasefile which will be named "namehere". As it stands anyone in TSPG can access your database if they manage to guess the name correctly. There is no safety net in public server clusters like TSPG. But if you can set the name to something no one can guess, then you're safe. Personally I set the databasefile to the default rcon password TSPG provides as its unique and extremely hard to guess.


Why do I see empty fields in the "KeyName" column for each of my rows? That's surely not supposed to happen. Here:

Spoiler (Open)
Image


It might be the reason I'm not able to read anything while loading, but it's strange because I can do the read fine while saving. I had my debug log show correct value by retrieving just after saving in SavePlayerData. The problem is loading, and the missing keynames might be the reason...
=== RAGNAROK DM ON ... uh... dead forever? ===
=== ALWAYS BET ON ... uh... dead forever? ===
=== Who wanta sum wang? ===
=== Death and Decay - A new Monster/Weapon replacer ===

User avatar
Ru5tK1ng
Forum Regular
Offline
Posts: 620
Joined: Fri Jun 01, 2012 9:04 pm

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#28 by Ru5tK1ng » Wed Jun 28, 2017 11:31 pm

Yeah...I remember when Ivan said SQL was baby easy. Time to re-evaluate your statement.

User avatar
Catastrophe
Topic author
ZanStuff Reviewer
Offline
Posts: 2258
Joined: Sat Jun 02, 2012 2:44 am

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#29 by Catastrophe » Wed Jun 28, 2017 11:55 pm

Ivan wrote:Why do I see empty fields in the "KeyName" column for each of my rows? That's surely not supposed to happen. Here:

Spoiler (Open)
Image


It might be the reason I'm not able to read anything while loading, but it's strange because I can do the read fine while saving. I had my debug log show correct value by retrieving just after saving in SavePlayerData. The problem is loading, and the missing keynames might be the reason...


That's definitely the reason. Have you double checked if there are any activator issues? Have you also tried printing out the player's account name? I'd say at this point its time to log everything.
Youtube Channel
Projects: Super Demon | Idiotic LMS | ZCC | Cata WeaponZ

Currently hooked on Overwatch and PUBattlegrounds.

User avatar
Ivan
Addicted to Zandronum
Offline
Posts: 1836
Joined: Mon Jun 04, 2012 5:38 pm

Re: Databases, accounts, and you. Part 1: basic tutorial.

Post#30 by Ivan » Thu Jun 29, 2017 9:18 am

Catastrophe wrote:
Ivan wrote:Why do I see empty fields in the "KeyName" column for each of my rows? That's surely not supposed to happen. Here:

Spoiler (Open)
Image


It might be the reason I'm not able to read anything while loading, but it's strange because I can do the read fine while saving. I had my debug log show correct value by retrieving just after saving in SavePlayerData. The problem is loading, and the missing keynames might be the reason...


That's definitely the reason. Have you double checked if there are any activator issues? Have you also tried printing out the player's account name? I'd say at this point its time to log everything.


Nevermind my previous post, I found the problem I think. It's user error :(
=== RAGNAROK DM ON ... uh... dead forever? ===
=== ALWAYS BET ON ... uh... dead forever? ===
=== Who wanta sum wang? ===
=== Death and Decay - A new Monster/Weapon replacer ===


Return to “Resources”



Who is online

Users browsing this forum: No registered users and 1 guest