Page 1 of 1

Timer algorithm

Posted: Sun Dec 31, 2023 12:24 pm
by trevix
I have a problem on mobile that I am not good enough to solve and I hope some one can give me some hints...

My standalone shows two timers, the Match timer and the Set timer.
The Match timer start at 00:00:00 at the beginning of a match and should always run till the end of it.
The Set timer start at 00:00:00 at the beginning of a match BUT gets zeroed at the end of each set and start a new counting for the next Set.
This is because at the end of a match, I need to have a Match time that is the sum of each Set time (there could be up to 5 Sets in a match).

Running a recursive command every seconds, and adding 1 sec to each counter, doesn't work because other scripts delay in a noticeable way the counters (in respect to the OS clock).
So, I am using the following (simplified version):

Code: Select all

local sMatchTimeDiff, sSetTimeDiff
On MatchStart
  put the seconds into tNow
  put tNow into tNowItems
  convert tNowItems to dateitems
   put item 1 to 3 of tNowItems,0,0,0,item 7 of tNowItems into tTime
   convert tTime to seconds
   put tNow - tTime into sMatchTimeDiff --difference in seconds between clock time and match time
   put sMatchTimeDiff into sSetTimeDiff --same difference at match start
  RunMatchTimer
end MatchStart

On RunMatchTimer
  put the seconds into tNow
  put tNow - sMatchTimeDiff into tMatchTime
  convert tMatchTime to long time
               --do the set
  put tNow - sSetTimeDiff into tSetTime
  convert tSetTime to long time
  put tMatchTime into fld "MatchTime"
  put tSetTime into fld "SetTime"
  send "RunMatchTimer" to me in 1 seconds
end RunMatchTimer
It works fine, keeping the timers accurate.

My problem is the "undo":
Because players may score a wrong point, they are able to revert to the previous score, but both timers meanwhile should keep running.
No problem when there is an undo during a Set, but if the undo happens just when a new Set has been started, like for example Match timer= 00:02:25 and Set Timer= 00:00:00 (because at the start of each Set the SetTimer gets reset), I don't know how to reset the sSetTimeDiff value so that everything works as it should.

Time is tough and this is a conceptual problem that my age prevents me from solving...
Thanks for any help.
Trevix

Re: Timer algorithm

Posted: Sun Dec 31, 2023 5:39 pm
by AndyP
No sure about the undo problem but, you should amend this line first

Code: Select all

send "RunMatchTimer" to me in 1 seconds
to

Code: Select all

send "RunMatchTimer" to me in 1 seconds with messages
otherwise your timing loop becomes blocking

Re: Timer algorithm

Posted: Mon Jan 01, 2024 3:33 am
by stam
AndyP wrote:
Sun Dec 31, 2023 5:39 pm

Code: Select all

send "RunMatchTimer" to me in 1 seconds with messages
otherwise your timing loop becomes blocking
Not sure that's right?
I thought 'send in time' was not a blocking action... and I can't see a dictionary entry for send in time with messages
On the other hand there is a wait with messages command which is non-blocking, is that what you mean?

Re: Timer algorithm

Posted: Mon Jan 01, 2024 4:34 am
by dunbarx
"Send (in time)" is never blocking. "Wait" always is, unless "with messages" is appended.

Craig

Re: Timer algorithm

Posted: Mon Jan 01, 2024 4:37 am
by dunbarx
I tested this to make sure I knew what I was talking about. Something never to be taken for granted. On a card with a button and a field, in the button script:

Code: Select all

on mouseUp
   send "XX" to me in 60
   put random(99) into fld 1
end mouseUp

on xx
   put any char of "asdfg" into fld 1
end xx
The random number appears at once, or rather right after the "send" command, and the character appears a second later.

Craig

Re: Timer algorithm

Posted: Mon Jan 01, 2024 9:32 am
by AndyP
Yep correct send in time is non- blocking, must put my glasses on next time.🥳Happy new year to all.

Re: Timer algorithm SOLVED

Posted: Tue Jan 02, 2024 3:24 pm
by trevix
I should have been thinking to how a chronometer could work.
I set the sSetTimeDiff as a difference in seconds between the MatchTime and the SetTime.
So, after I reset to 0 the SetTime, doing an "undo" I can always revert the sSetTimeDiff to the previous value (in the previous score).