Page 2 of 2

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

Posted: Tue Mar 28, 2017 9:03 am
by Catastrophe
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.

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

Posted: Sun May 07, 2017 8:51 pm
by Catastrophe
Tutorial updated; the scripts are back up again.

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

Posted: Wed Jun 28, 2017 3:13 pm
by Ivan
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?

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

Posted: Wed Jun 28, 2017 5:54 pm
by Catastrophe
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.

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

Posted: Wed Jun 28, 2017 6:00 pm
by Ivan
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?

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

Posted: Wed Jun 28, 2017 6:08 pm
by Catastrophe
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.

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

Posted: Wed Jun 28, 2017 7:09 pm
by Ivan
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...

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

Posted: Wed Jun 28, 2017 11:31 pm
by Ru5tK1ng
Yeah...I remember when Ivan said SQL was baby easy. Time to re-evaluate your statement.

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

Posted: Wed Jun 28, 2017 11:55 pm
by Catastrophe
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.

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

Posted: Thu Jun 29, 2017 9:18 am
by Ivan
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 :(

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

Posted: Tue Sep 26, 2017 11:15 am
by Eagle

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

Posted: Thu Oct 19, 2017 9:26 pm
by Inkubus
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.