Is this a logic problem?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
grovecat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 99
Joined: Thu Aug 04, 2011 10:32 am
Location: Albany, Western Australia

Is this a logic problem?

Post by grovecat » Wed Jul 04, 2012 10:15 am

I've been tearing my hair out over this so decided I have to ask for help with it.

Basically, the app will be used in my volunteer work as a radio operator with the sea rescue. It accepts inputs of multiple vessels, with their names and estimated return times (ETRs). So far the app works exactly as expected for the first vessel input. That is, when a vessel's ETR equals system time, an answer dialog allow a response of "Out" or "Returned". If Returned, "Back" is reported on the table and no more action for that vessel. If it is still Out, ETR is incremented by 10 minutes and the ETR shows in orange. And so on to the next phase as shown in the script.

However, when I input a second vessel, the thing goes haywire in that when the time is triggered, the ETR field in the table goes orange at the same time as the dialog appears, before I have responded. Even more puzzling, I can ignore the dialog and the sequence continues, changing to red after 10 minutes and then to "ALERT" without any dialog inputs stating what the vessel status actually is. I wonder if it is a logic error, or I am asking LiveCode to do something that it can't. Anyway, here is the script and I would be grateful for some help with it. I apologise if it is too long to be posting here.

Thanks
Don

Code: Select all

local B, V, x, y, tResult

/* Vessels use 24 hour clock, so 12 hour time set to false in card script
Vessel estimated return (ETR) in V[x][1] and name in V[x][2]
Vessel status (Out or Returned) in B[x] */

on mouseUp
   if x >= 1 then
      add 1 to x
   else
      put 1 into x
   end if
   put field "Hour" into V[x][1]
   put ":" after V[x][1]
   put field "Min" after V[x][1] -- format nn:nn
   put field "Name" into V[x][2]
   put "Out" into B[x] -- we log vessel as having left
   put V[x][2] into line x of field "Vessel" --Names and times in table
   put V[x][1] into line x of field "ETR"
   put "Mariner " into field "Name" -- default prefix for our area
   put empty into field "Hour"
   put empty into field "Min"
   set forecolor of line x of field "ETR" to black
   focus on field "Name"
   put " " after character 7 of field "Name"
   send checkTime to me in 1 sec
end mouseUp

on checkTime
   repeat   with y = 1 to x
      If V[y][1] = system time and B[y] is not "Back" then
         answer V[y][2] with "Out" and "Returned"
         put it into tResult
         switch B[y]
            case "Out"
               if tResult is "Returned" then
                  backIn
               else
                  put "Call1" into B[y]
                  set forecolor of line y of field "ETR" to orange
                  addTen 
               end if
               break
            case "Call1"
               if tResult is "Returned" then
                  backIn
               else
                  set forecolor of line y of field "ETR" to red
                  addTen 
               end if
               break
            case "Call2"
               if tResult is "Returned" then
                  backIn
               else
                  put "ALERT" into line y of field "ETR"
                  Beep 10
                  answer "Call Coordinator" with OK
                  Put "Back" into B[y]
               end if
               break
         end switch
      end if
   end repeat
   send checkTime to me in 1 sec
end checkTime

on addTen
   put V[y][1] into tTime
   put char 1 of tTime into tHour
   put char 2 of tTime after tHour
   put char 4 of tTime into tMin
   put char 5 of tTime after tMin
   Add 10 to tMin
   if tMin > 60
   then
      add 1 to tHour
      subtract 60 from tMin
      if tMin < 10 then put 0 before tMin
   end if
   put tHour into tTime
   put ":" after tTime
   put tMin after tTime
   put tTime into V[y][1]
end addTen

on backIn
   put "Back" into B[y]
   set forecolor of line y of field "ETR" to black
   put "Back" into line y of field "ETR"
end backIn

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm
Location: Seattle, USA

Re: Is this a logic problem?

Post by townsend » Wed Jul 04, 2012 3:28 pm

Logic? To start-- your variables: local B, V, x, y
should be renamed to something more descriptive.
That would make understanding the logic a LOT easier.

In the script editor, Ctl-F bring us a menu at the bottom on the screen.
Look for the More button. There you'll be able to search and replace
all occurrences of each cryptic variable name with a descriptive name.

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

Re: Is this a logic problem?

Post by dunbarx » Wed Jul 04, 2012 11:49 pm

I have not delved into your script, but it looks like you are more than capable of working this out. LC can easily do this sort of thing; its capabilities are not an issue at all.

Townsend is correct about the variable naming.

You are very wordy. Is this for readability? If not, compacting will make the logic more accessible. To recreate your card requires the creation of all the several fields and loading them as needed. I will try it later, unless someone else fixes the problem first.

The problem is certainly not in the logic. It is in the details.

Craig Newman

NoN'
Posts: 82
Joined: Thu Jul 03, 2008 9:56 pm
Location: Paris
Contact:

Re: Is this a logic problem?

Post by NoN' » Thu Jul 05, 2012 1:00 am

Hi Don,

It's seems that, once you enter the first vessel and click on the button,you never quit the loop of the "send checkTime to me in 1 sec".

Maybe you should try to quit the loop before enter a new vessel :

on checktime
[...]
if the mouseloc is not within the rectangle of me then
send checkTime to me in 1 sec
else
exit checkTime
end if
end checkTime

Regards,

Renaud

grovecat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 99
Joined: Thu Aug 04, 2011 10:32 am
Location: Albany, Western Australia

Re: Is this a logic problem?

Post by grovecat » Thu Jul 05, 2012 6:58 am

Thanks guys

Point taken about variable names, and the code is probably wordy because this is only my second app and I still need it to be very readable.

Renaud, your solution did the trick so many thanks for that.

Cheers
Don

NoN'
Posts: 82
Joined: Thu Jul 03, 2008 9:56 pm
Location: Paris
Contact:

Re: Is this a logic problem?

Post by NoN' » Fri Jul 06, 2012 12:11 am

Don,

Your goal is generous so :

I'm happy for you, and it has been a pleasure to help.

Cheers,

renaud

LittleGreyMan
Posts: 49
Joined: Sat Jun 16, 2012 7:57 pm
Location: City of God Lug

Re: Is this a logic problem?

Post by LittleGreyMan » Fri Jul 06, 2012 8:56 am

grovecat wrote:Point taken about variable names
Some variables are judiciously named, as tResult, so you probably already have some knowledge about good practices for variables.

If you want to learn more, have a look at this excellent page on Richard's site:

http://www.fourthworld.com/embassy/arti ... style.html
Best regards,

Didier

Post Reply

Return to “Talking LiveCode”