Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Gregory
Beginner


Joined: 14 Nov 2013
Posts: 22
Location: Durban, South Africa

PostPosted: Mon Feb 19, 2018 7:00 pm   

Add Record With Lua
 
Hello.

I have been trying to figure this out but I am just not sure what I could be doing wrong. I am trying to add a new record to my database that keeps track of how much gold i collect within a certain time frame.

For adding the information to the database, so far I have this :

Code:
zs.cmd.dbload("reports")
print("Connected")

zs.cmd.view("GoldRecord {date|player|session_time|amount}")
zs.cmd.new("GoldRecord date=" .. tostring(os.date("%x")) .. " player=Masha session_time=15:00 amount=" .. tostring(zs.var.totalGold))

zs.cmd.dbclose("reports")
print("Row added")


After this runs though, there are no changes and nothing is added to the database.

I think there could be something wrong with the formatting in the #new command but I am also not sure If the view is being registered correctly.

Any ideas on where I could be going wrong?
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Mon Feb 19, 2018 8:02 pm   
 
Is there a reason you want to do this in Lua?
I am not the most familiar with Lua syntax.
I could likely toss out some zScript to do the same thing.
_________________
Discord: Shalimarwildcat
Reply with quote
Gregory
Beginner


Joined: 14 Nov 2013
Posts: 22
Location: Durban, South Africa

PostPosted: Mon Feb 19, 2018 8:11 pm   
 
That would be great thanks!

No particular reason, it's just that the Triggers I have written so far have been with Lua but I just can't seem to get this working correctly. I do not have much experience with ZScript but I am more than willing to learn if you could help :)
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Mon Feb 19, 2018 10:19 pm   
 
Well, what format do you like your date to show as?
The %time function offers a good bit of variability.
#HELP %time

Session_time is supposed to be the interval between records?

Code:
#ALARM "goldRecorder" -15:00 {
  $date=%time(mm/dd/yyyy)
  $time=%time(t)

  $record=%dbkey(@goldRecords, @charName)
  $today=%dbkey($record, $date)
  #ADDKEY $today $time @totalGold
  #ADDKEY $record $date $today
  #ADDKEY goldRecords @charName $record
  }


Something like that should keep track of how much gold you have by character date and time.
Enter it on the command line.
_________________
Discord: Shalimarwildcat
Reply with quote
Gregory
Beginner


Joined: 14 Nov 2013
Posts: 22
Location: Durban, South Africa

PostPosted: Tue Feb 20, 2018 7:01 am   
 
shalimar wrote:
Well, what format do you like your date to show as?


The date format you have supplied is perfect.

shalimar wrote:

Session_time is supposed to be the interval between records?


Yes. I would like to create a new database record every 15 minutes, showing how much gold has been collected in that time.

shalimar wrote:

Code:
#ALARM "goldRecorder" -15:00 {
  $date=%time(mm/dd/yyyy)
  $time=%time(t)

  $record=%dbkey(@goldRecords, @charName)
  $today=%dbkey($record, $date)
  #ADDKEY $today $time @totalGold
  #ADDKEY $record $date $today
  #ADDKEY goldRecords @charName $record
  }



I don't want to lie to you. I do not understand this part at all. I can understand that you are creating two variables to hold a value for the date and time. But it is the $record and $today that is confusing me a little. I apologize, I know I still have a lot to learn lol.

When this runs I see a new variable is created instead of adding a new record to the reports database I have created. I think this is where I am getting confused or just nor understanding correctly. Do I then need to insert the variable into the reports database? Where does the @goldRecords come from? As you can see I am still very new to this, so I apologize for not getting it exactly.

I may be over complicating things as I tend to do :P

EDIT: Wait a minute. Is the variable goldRecords itself acting as a database? I can see a row with the information. Now on the next alarm it will add a new row to that variable?
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Tue Feb 20, 2018 11:44 am   
 
Variables that start with the $ are local variables, and not stored in system memory beyond the execution of the individual setting.

@goldRecords is just a placeholder name, use the actual variable name you want to house the records.

Yes, @goldrecords is now a database variable, three layers deep.

the %dbkey function is doing the real work in $record and $today, it is accessing the current values of the database.
#HELP %dbkey
_________________
Discord: Shalimarwildcat
Reply with quote
Gregory
Beginner


Joined: 14 Nov 2013
Posts: 22
Location: Durban, South Africa

PostPosted: Tue Feb 20, 2018 12:47 pm   
 
shalimar wrote:
Variables that start with the $ are local variables, and not stored in system memory beyond the execution of the individual setting.


Ok this I understand perfectly.

shalimar wrote:
@goldRecords is just a placeholder name, use the actual variable name you want to house the records. Yes, @goldrecords is now a database variable, three layers deep.


I understand the goldRecords dbvar concept, but I will need to look at the manual I think to understand 'three layers deep'

shalimar wrote:
the %dbkey function is doing the real work in $record and $today, it is accessing the current values of the database.
#HELP %dbkey


Ok this I get now. It basically searching for a key{charname}, and then searching for any records/rows that match that key ?.

Code:
#ADDKEY $today $time  @totalGold
#ADDKEY $record $date $today
#ADDKEY goldRecords  @charName $record


This is replacing the current record with the new values?

Is a record the same thing as a normal row in a database?

EG: 1 Masha 02/20/2018 15:00 20000
2 Masha 02/30/2018 15:00 32000

It seems to be replacing the values found by the matching key? I hope I am understanding this correctly. I would like to have multiple records/rows for the same user just with different gold amounts depending on how much was collected in that session.

I really appreciate all the help. I can feel I'm so close to 'getting it' lol.
Reply with quote
Gregory
Beginner


Joined: 14 Nov 2013
Posts: 22
Location: Durban, South Africa

PostPosted: Tue Feb 20, 2018 2:22 pm   
 
I have been just been looking at threads and have seen more than a few people strongly advise against using the internal database as it is seriously out of date, and has quite a few bugs. I can understand now why you have suggested using a database variable. I just now need to understand exactly how they work. :P
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Tue Feb 20, 2018 9:03 pm   
 
Not a problem.
I assume they are the same as rows, yes.
Currently, each user in the database (1st layer), is a database unto itself (2nd layer), with a bunch of rows for the days.
The days are also a database (3rd layer), with a value of gold in 15-minute intervals.

To display the database try:
#SHOWDB @goldRecords
and then:
#SHOWDB @goldRecords.@charName
or try:
#FORALL %dbkeys(@goldRecords.@charName) {#SHOWDB @goldRecords.@charName.%i}
_________________
Discord: Shalimarwildcat
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Tue Feb 20, 2018 9:42 pm   
 
$record=%dbkey(@goldRecords, @charName)
//Retrieve 2nd layer database from first layer
$today=%dbkey($record, $date)
//retrieve 3rd layer database from 2nd
#ADDKEY $today $time @totalGold
//update 3rd layer database
#ADDKEY $record $date $today
//update 2nd layer
#ADDKEY goldRecords @charName $record
//update 1st layer
_________________
Discord: Shalimarwildcat
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Tue Feb 20, 2018 9:47 pm   
 
ah, i made a mistake.... we just want %db in the retrieval line, not %dbkey
_________________
Discord: Shalimarwildcat
Reply with quote
Gregory
Beginner


Joined: 14 Nov 2013
Posts: 22
Location: Durban, South Africa

PostPosted: Tue Feb 20, 2018 11:47 pm   
 
Thank you very much for the help. I really do appreciate it. I think I have a much better understanding after those examples. Here is my script in it's entirety.


Code:
// Local vars.
$date=%time(mm/dd/yyyy);
$time=%time(t);
$charName=%gmcp.char.base.name;
$sessionTime="15:00";

// Get record from 2nd layer.
$record=%db(@goldRecords, $charName);

// Get list from 3rd layer.
$today=%db($record, $date);

#ADDKEY $today $time @totalGold;

#ADDKEY $record $date $today;
#ADDKEY goldRecords @charName $record;

#print "+========== Session Report ==========+";
#print "+ Gold Collected............."@totalGold;
#print "+====================================+";

// Clear totalGold.
totalGold = 0;


This works perfectly. It adds the new amount to a new time each time the alarm fires producing this

Code:
02/20/2018="7:49 PM=0|7:50 PM=0|7:51 PM=0|7:52 PM=0"
(It's every minute just for testing)

Now if I am understanding this right, if I wanted to add another field I would simply insert it with #ADDKEY. Such as:

Code:
#ADDKEY $today sessiontime $sessionTime;


However, if i add this key, I do not get the desired result:

Code:
02/20/2018="7:58 PM=0|sessionTime=15:00|7:59 PM=0|8:00 PM=0|8:01 PM=0"


It only adds sessionTime once. After that only time is stored. I am not sure what I am doing wrong or if there is just one thing I am missing.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Wed Feb 21, 2018 12:12 am   
 
It only adds session time to the latest record.
The old records are already set.

Also, your $sessionTime is not currently changing value.
Technically the time of the occurrence is already being recorded in the key names.
%ctime is a predefined variable that should have the number of seconds you have been currently connected.

PS. the semicolon is the command separater character, you do not have to end your lines with it.

It is for command stacking.

wave;smell;north;go door;etc
_________________
Discord: Shalimarwildcat
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Wed Feb 21, 2018 12:19 am   
 
02/20/2018="7:58 PM=0|sessionTime=15:00|7:59 PM=0|8:00 PM=0|8:01 PM=0"
key:
02/20/2018

members:
7:58 PM=0
sessionTime=15:00
7:59 PM=0
8:00 PM=0
8:01 PM=0

Instead of:
#ADDKEY $today $time @totalGold
or
#ADDKEY $today sessiontime $sessionTime
try:
#ADDKEY $today $sessionTime @totalGold

Short of that, you are talking about adding a 4th layer.
$now=%db($today, $time)
_________________
Discord: Shalimarwildcat
Reply with quote
Gregory
Beginner


Joined: 14 Nov 2013
Posts: 22
Location: Durban, South Africa

PostPosted: Wed Feb 21, 2018 6:49 am   
 
Oh ok. So you could just keep adding new layers until you get what you need? I'm sure there is a limit. But I think I get the general idea. You are basically storing information, inside information, inside information etc.

Code:
$date=%time(mm/dd/yyyy)
$time=%time(t)
$charName=%gmcp.char.base.name
$sessionTime="15:00"

$record=%db(@goldRecords, $charName)

$today=%db($record, $date)

$now=%db($today, $time)

#ADDKEY $now amount @totalGold
#ADDKEY $now sessionTime $sessionTime

#ADDKEY $today $time $now
#ADDKEY $record $date $today

#ADDKEY goldRecords @charName $record

#print "+========== Session Report ==========+"
#print "+ Gold Collected............."@totalGold
#print "+====================================+"

// Clear totalGold.
totalGold = 0


Which gives me:

Code:
02/21/2018="3:00 AM=""amount=0|sessionTime=15:00""|3:01 AM=""amount=0|sessionTime=15:00"""


This give me the date the session occurred, along with the amount of time in the session as well as how much gold was collected. This does exactly what I need.

I cannot thank you enough for your help. Without your patience and examples I would not have achieved this easily or at all.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Wed Feb 21, 2018 7:55 am   
 
You can add a further check and nest all that recordkeeping stuff inside of:

#IF (@totalGold) {add a record}

That way it only actually updates when there is a change to record.

Oh, in the #ADDKEY, change @charName to $charName
And you still need a nonstatic $sessionTime
_________________
Discord: Shalimarwildcat
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net