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
mtuyooper
Beginner


Joined: 24 Jul 2009
Posts: 14

PostPosted: Tue Apr 19, 2011 6:54 pm   

Trigger : Time between Occurences
 
Hello folks,

I'm trying to setup a trigger that will record an occurrence and display the elapsed time when that event happens again. I currently have a trigger showing me the current time when it happens and I do a visual scan for the previous iteration. I use the variable table at the moment to record the count of the occurences, but am thinking I'll need to use the database for this type.

Trigger text is :

* You think your %1 skill has improved. *

Which triggers :

ooc %1+ %time(hh:mm:ss)
#ad %1 1

Ideally it would state the elapsed time since the last improvement.

The time frame between iterations will range from seconds to up to 30 minutes.

Could anyone help point me in the right direction?

Thanks!
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Tue Apr 19, 2011 9:14 pm   
 
You'll probably want to use the %secs variable rather than the %time() function. %secs returns the milliseconds since Windows booted, so it always increases. And actually CMUD uses the fine-resolution Windows timer so %secs is valid at the millisecond level for doing precise timing.
Reply with quote
mtuyooper
Beginner


Joined: 24 Jul 2009
Posts: 14

PostPosted: Tue Apr 19, 2011 10:50 pm   
 
Ok!

I've modified it to first move the old %secs key value, and that is where I'm having an issue.

Here it is so far...

#add %1.OldTime @%1.Time
#addk %1 Count 1
#addk %1 Time %secs

After the trigger activates it stores my count, and the time properly. I just can't figure how to get the Time value into the OldTime value.

After that I'll do the math, but thats' simple enough.

Thanks!

C
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Tue Apr 19, 2011 11:07 pm   
 
It depends, though, if you're wanting it to continue to count, even if your computer reboots for any reason. For that, lua's os.time feature is probably your best bet. I have two functions I use:

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <func name="parsetime" copy="yes">
    <value>#LOCAL timeresult
#IF $timeend {$timeresult=@luatimediff( %item( %db( $timeend, timeend), 1), %item( %db( $timeend, timeend), 2), %item( %db( $timeend, timeend), 3), %item( %db( $timeend, timeend), 4), %item( %db( $timeend, timeend), 5), %item( %db( $timeend, timeend), 6), %item( %db( $timestart, timestart), 1), %item( %db( $timestart, timestart), 2), %item( %db( $timestart, timestart), 3), %item( %db( $timestart, timestart), 4), %item( %db( $timestart, timestart), 5), %item( %db( $timestart, timestart), 6))} {$timeresult=@luatimediff( %int( %time( "yyyy")), %int( %time( "mm")), %int( %time( "dd")), %int( %time( "hh")), %int( %time( "nn")), %int( %time( "ss")), %item( %db( $timestart, timestart), 1), %item( %db( $timestart, timestart), 2), %item( %db( $timestart, timestart), 3), %item( %db( $timestart, timestart), 4), %item( %db( $timestart, timestart), 5), %item( %db( $timestart, timestart), 6))}
$timeresult=%subregex( $timeresult, "^0y ")
$timeresult=%subregex( $timeresult, "0(?:mo|d) ")
#RETURN $timeresult</value>
    <arglist>$timestart, $timeend</arglist>
  </func>
  <func name="luatimediff" language="Lua" copy="yes">
    <value><![CDATA[local teyear,temonth,teday,tehour,temin,tesec,tsyear,tsmonth,tsday,tshour,tsmin,tssec=zs.param(1),zs.param(2),zs.param(3),zs.param(4),zs.param(5),zs.param(6),zs.param(7),zs.param(8),zs.param(9),zs.param(10),zs.param(11),zs.param(12)
local timeDiff = function(t2,t1)
   local d1,d2,carry,diff = os.date('*t',t1),os.date('*t',t2),false,{}
   local colMax = {60,60,24,os.date('*t',os.time{year=d1.year,month=d1.month+1,day=0}).day,12}
   d2.hour = d2.hour - (d2.isdst and 1 or 0) + (d1.isdst and 1 or 0) -- handle dst
   for i,v in ipairs({'sec','min','hour','day','month','year'}) do
      diff[v] = d2[v] - d1[v] + (carry and -1 or 0)
      carry = diff[v] < 0
      if carry then diff[v] = diff[v] + colMax[i] end
   end
   return diff
end

local td=timeDiff(os.time{year=teyear,month=temonth,day=teday,hour=tehour,min=temin,sec=tesec},os.time{year=tsyear,month=tsmonth,day=tsday,hour=tshour,min=tsmin,sec=tssec})
return td.year .. "y " .. td.month .. "mo " .. td.day .. "d " .. string.format("%02.f",td.hour) .. ":" .. string.format("%02.f",td.min) .. ":" .. string.format("%02.f",td.sec)]]></value>
    <notes>local td=timeDiff(os.time{year=teyear,month=temonth,day=teday,hour=tehour,min=temin,sec=tesec},os.time{year=tsyear,month=tsmonth,day=tsday,hour=tshour,min=tsmin,sec=tssec})
return td.year .. "y " .. td.month .. "mo " .. td.day .. "d " .. td.hour .. "h 0" .. td.min .. "m 0" .. td.sec

--------------------
return tsyear .. " " .. teyear .. " " .. tsmonth .. " " .. temonth .. " " .. tsday .. " " .. teday .. " " .. tshour .. " " .. tehour .. " " .. tsmin .. " " .. temin .. " " .. tssec .. " " .. tesec</notes>
  </func>
</cmud>


It probably could look a lot better, but it works for its purpose.

To make it work, you'd create a database variable with the following two keys: timestart and timeend.

The trigger would store the time start as %time("yyyy|mm|dd|hh|nn|ss"), and the same for the time end. Then, all you have to do is:

occ %1+ @parsetime(@timedbvariable)

Hope that helps! If you have any other questions, please feel free to ask me. :)
Reply with quote
mtuyooper
Beginner


Joined: 24 Jul 2009
Posts: 14

PostPosted: Tue Apr 19, 2011 11:17 pm   
 
Wow!

How do I go about entering in the function as noted above? I try to copy and paste it into the Cmud prompt but return errors. I've added a variable time, with the keys timeend and timestart but it returns the error :invalid local variable timeend.

I'm sorry but I'm rather lost on how to proceed...
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Tue Apr 19, 2011 11:27 pm   
 
Open up settings, right click on your main window (or a folder that you want to store your functions in) and click paste.

Whenever someone gives xml output like the above, it will always follow the procedure I just gave you.
Reply with quote
mtuyooper
Beginner


Joined: 24 Jul 2009
Posts: 14

PostPosted: Tue Apr 19, 2011 11:45 pm   
 
OK, I've got the function entered in, now I'm having a hard time with the trigger code.

Quote:
The trigger would store the time start as %time("yyyy|mm|dd|hh|nn|ss"), and the same for the time end. Then, all you have to do is:

ooc %1+ @parsetime(@timedbvariable)


Should I do an #addkey %1.timestart for the first occurence?

There isn't a defined start and end point but rather occurrence A, and a second repeat of that same event a certain amount of time later. So I'm not exactly sure how to define the time start as above, or the timeend.

I'm sorry if I'm being a pain here...
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Tue Apr 19, 2011 11:57 pm   
 
Code:
#TRIGGER {Occurrence here} {#IF (%1.timestart) {#ADDKEY %1 timeend %time("yyyy|mm|dd|hh|nn|ss");occ %1+ @parsetime(@timedbvariable);#ADDKEY %1 timestart %time("yyyy|mm|dd|hh|nn|ss")} {#ADDKEY %1 timestart %time("yyyy|mm|dd|hh|nn|ss")}}


That should be what you're looking for, if I understand your question correctly.
Reply with quote
mtuyooper
Beginner


Joined: 24 Jul 2009
Posts: 14

PostPosted: Wed Apr 20, 2011 12:12 am   
 
Excellent!

The trigger is entered and upon actuating creates the proper key with a value of "2011|04|19|20|09|35".

However it outputs :

Code:

ooc split defense+ #LOCAL timeresult #IF $timeend {$timeresult=@luatimediff( %item( %db( $timeend, timeend), 1), %item( %db( $timeend, timeend), 2), %item( %db( $timeend, timeend), 3), %item( %db( $timeend, timeend), 4), %item( %db( $timeend, timeend), 5), %item( %db( $timeend, timeend), 6), %item( %db( $timestart, timestart), 1), %item( %db( $timestart, timestart), 2), %item( %db( $timestart, timestart), 3), %item( %db( $timestart, timestart), 4), %item( %db( $timestart, timestart), 5), %item( %db( $timestart, timestart), 6))} {$timeresult=@luatimediff( %int( %time( "yyyy")), %int( %time( "mm")), %int( %time( "dd")), %int( %time( "hh")), %int( %time( "nn")), %int( %time( "ss")), %item( %db( $timestart, timestart), 1), %item( %db( $timestart, timestart), 2), %item( %db( $timestart, timestart), 3), %item( %db( $timestart, timestart), 4), %item( %db( $timestart, timestart), 5), %item( %db( $timestart, timestart), 6))} $timeresult=%subregex( $timeresult, "^0y ") $timeresult=%subregex( $timeresult, "0(?:mo|d) ") #RETURN $timeresult

You say (OOC), 'split defense+ #LOCAL timeresult'


It sure is trying to work! Any thoughts?[/code]
Reply with quote
mtuyooper
Beginner


Joined: 24 Jul 2009
Posts: 14

PostPosted: Wed Apr 20, 2011 12:47 am   
 
I think I've overstepped myself. I don't understand what is happening with your example Charneus, maybe you could help me with my simpler one :)

So far I've got this :


Code:
<pattern>~* You think your%s(*)%sskill has improved. ~*</pattern>
  <value>#addkey %1 OldTime @%1.Time
#addkey %1 Time %secs
</value>
</trigger>


When the following string appears :

* You think your parry skill has improved. *

I get the proper value in the variable, a rather large number. However in my OldTime value I get : @parry.Time

By my logic when I trigger it I will move the value in Time, to OldTime, and after that action record the new time in the Time variable.

Am I not referencing the location of the time properly in :
Code:
#addkey %1 OldTime @%1.Time


Thank you for your patience and help Charneus!
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Wed Apr 20, 2011 12:56 am   
 
Use {} around the variable name you want CMud to expand and evaluate:

@{%1.time}

Note that there are situations where even this doesn't work, so you may need to use the %db() function instead (curly braces may be unnecessary here):

%db(@{%1},time)
_________________
EDIT: I didn't like my old signature
Reply with quote
mtuyooper
Beginner


Joined: 24 Jul 2009
Posts: 14

PostPosted: Wed Apr 20, 2011 1:20 am   
 
edit : added multi char support

Ah hah! Excellent!

Here is what I came up with...

Code:

<trigger priority="70" id="7">
  <pattern>~* You think your%s(*)%sskill has improved. ~*</pattern>
  <value>#addkey %1 OldTime @{%1}.Time
#addkey %1 Time %secs
#math Difference ((@{%1}.Time - @{%1}.OldTime) / 1000)
#ad %1count 1
#show ---> @Difference Seconds since last %1 improvement <---
#show ---> @{%1count} total improvements <---
show skills %1
ooc %1 %time(hh:nn:ss) +


</value>
</trigger>



Which returns :


---> 8 Seconds since last parry improvement <---
---> 23 total improvements <---
show skills parry
ooc parry 21:15:13 +
> parry: A Virtuoso.
> You say (OOC), 'parry 21:15:13 +'


Most excellent! Thank you for the help folks! I can see an issue like Charneus mentioned over long times, but hopefully the gap won't be all that long. I'll also have to cleanup the seconds...

For anyone curious, it's for Dartmud, another imp counter.
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