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!
Catastrophe
Retired Staff / Community Team Member
Posts: 2558
Joined: Sat Jun 02, 2012 2:44 am

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

#21

Post 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.

Catastrophe
Retired Staff / Community Team Member
Posts: 2558
Joined: Sat Jun 02, 2012 2:44 am

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

#22

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

Tutorial updated; the scripts are back up again.

User avatar
Ivan
Addicted to Zandronum
Posts: 2219
Joined: Mon Jun 04, 2012 5:38 pm
Location: Omnipresent

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

#23

Post 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]

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);
    }
}
[/spoiler]

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 ===

Catastrophe
Retired Staff / Community Team Member
Posts: 2558
Joined: Sat Jun 02, 2012 2:44 am

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

#24

Post 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.

User avatar
Ivan
Addicted to Zandronum
Posts: 2219
Joined: Mon Jun 04, 2012 5:38 pm
Location: Omnipresent

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

#25

Post 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 ===

Catastrophe
Retired Staff / Community Team Member
Posts: 2558
Joined: Sat Jun 02, 2012 2:44 am

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

#26

Post 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.

User avatar
Ivan
Addicted to Zandronum
Posts: 2219
Joined: Mon Jun 04, 2012 5:38 pm
Location: Omnipresent

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

#27

Post 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]Image[/spoiler]

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
Frequent Poster Miles card holder
Posts: 794
Joined: Fri Jun 01, 2012 9:04 pm

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

#28

Post 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.

Catastrophe
Retired Staff / Community Team Member
Posts: 2558
Joined: Sat Jun 02, 2012 2:44 am

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

#29

Post 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]Image[/spoiler]

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.

User avatar
Ivan
Addicted to Zandronum
Posts: 2219
Joined: Mon Jun 04, 2012 5:38 pm
Location: Omnipresent

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

#30

Post 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]Image[/spoiler]

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 ===


Inkubus
 
Posts: 28
Joined: Wed Dec 12, 2012 7:23 am

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

#32

Post by Inkubus » Thu Oct 19, 2017 9:26 pm

Okay so, along with what Eagle said above me, that URL is indeed dead.

But my question is regarding the database file. I did "databasefile test" through my server console. It said 'Opening database "test" succeeded.' Is that the message that is supposed to be printed when creating a db that does not yet exist? If so, I cannot for the life of me find the file on my computer. Where is this file being saved?

Also, when I then went back to my client (where I was connected to my server) and typed "databasefile" in the console, it printed '"databasefile" is ":memory:"' which seems to me like it didn't actually work. Or maybe I'm reading all of this wrong. :redface:


Disregard that, I suck cocks. I can see the file, but it's simply a "FILE" and not a .db or .sqlite, etc. and there is nothing when I open it with DB Browser for SQLite.

Post Reply