Timer algorithm

Stop by to discuss use cases, requirements, information architecture, flow diagraming, unit testing and usability.

Moderators: FourthWorld, Klaus

Post Reply
trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Timer algorithm

Post by trevix » Sun Dec 31, 2023 12:24 pm

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
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Timer algorithm

Post by AndyP » Sun Dec 31, 2023 5:39 pm

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
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

stam
Posts: 2680
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Timer algorithm

Post by stam » Mon Jan 01, 2024 3:33 am

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?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9655
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Timer algorithm

Post by dunbarx » Mon Jan 01, 2024 4:34 am

"Send (in time)" is never blocking. "Wait" always is, unless "with messages" is appended.

Craig
Last edited by dunbarx on Mon Jan 01, 2024 4:41 am, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9655
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Timer algorithm

Post by dunbarx » Mon Jan 01, 2024 4:37 am

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

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Timer algorithm

Post by AndyP » Mon Jan 01, 2024 9:32 am

Yep correct send in time is non- blocking, must put my glasses on next time.🥳Happy new year to all.
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Timer algorithm SOLVED

Post by trevix » Tue Jan 02, 2024 3:24 pm

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).
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

Post Reply

Return to “Software Engineering”