PDA

View Full Version : Trying to send a message to a specific lnet channel



SashaFierce
08-24-2015, 07:00 PM
Here is what I'm trying to do:

I want to time my arena run, and send the final result to a specific lnet channel "DuskRuin"




TOP:
match "START", "Introducing our new challenger"
match "FINISH", "is triumphant, defeating all those that opposed"
matchwait

START:
#respond Time.now
CharSettings['duration'] = Time.now
CharSettings['timequit'] ||= Time.now
timerested = Time.now - CharSettings['timequit']
Thread.new{loop {CharSettings['timequit'] = Time.now; pause 1}}
goto "TOP"

FINISH:
#respond Time.now
duration = Time.now - CharSettings['duration']
hours = (duration / 3600).to_i
minutes = (duration / 60 - hours * 60).to_i
seconds = (duration - 60 * minutes - 3600 * hours).to_i
respond "[ Arena Ended. #{hours}h:#{'%02d' %minutes}m:#{'%02d' %seconds}s. ]"

result='[Arena Ended - #{hours}h:#{'%02d' %minutes}m:#{'%02d' %seconds}s. ]'
send_to_script 'lnet', 'chat '+result'

goto "TOP"


The problem is the
'%02d'

The result I get is:


--- Lich: error: ArenaTimer:7: syntax error, unexpected tIDENTIFIER, expecting end-of-input
...Arena Ended - #{hours}h:#{'%02d' %minutes}m:#{'%02d' %second...
... ^
C:/Users/MEDIA/Documents/lich/lich.rbw:2517:in `eval'
C:/Users/MEDIA/Documents/lich/lich.rbw:2517:in `block (3 levels) in <class:Script>'
--- Lich: ArenaTimer has exited.




#LNet.send_message(attr={'type'=>'channel'}, DUSKRUIN, ["Arena Ended - #{hours}h:#{'%02d' %minutes}m:#{'%02d' %seconds}s."])


This works, if I remove the ' ' around %02d but then the time portion breaks.

stormtov
08-24-2015, 09:33 PM
Well this is still messy but works:





def start()
respond Time.now
CharSettings['duration'] = Time.now
CharSettings['timequit'] ||= Time.now
timerested = Time.now - CharSettings['timequit']
respond "here"
Thread.new{loop {CharSettings['timequit'] = Time.now; pause 1}}
end

def stop()
respond Time.now
duration = Time.now - CharSettings['duration']
hours = (duration / 3600).to_i
minutes = (duration / 60 - hours * 60).to_i
seconds = (duration - 60 * minutes - 3600 * hours).to_i
responseText = "[ Arena Ended. #{hours}h:#{minutes}m:#{seconds}s. ]"
respond responseText
#result='[Arena Ended - (#{hours}h:#{'%02d' %minutes}m:#{'%02d' %seconds}s.)]'
#send_to_script 'lnet', 'chat '+result'
#attr={'type'=>'channel', 'channel'=>'code'} DUSKRUIN
LNet.send_message(attr={'type'=>'channel', 'channel'=>'DUSKRUIN'}, responseText)
#CharSettings['duration'].delete
#goto "TOP"
end

loop {
line = get
if line =~ /Introducing our new challenger/
start()
elsif line =~ /is triumphant, defeating all those that opposed/
stop()
end
}

stormtov
08-24-2015, 09:45 PM
I forgot to add, the script needs to be trusted to use the lnet send message. Adding this to the start of the script will prompt the user


if $SAFE > 0
respond "Script needs to be trusted to work"
respond ";trust #{script.name.downcase}"
exit
end

You don't have to exit the code if you don't want. You could still have it work as a local timer if untrusted and only chat if trusted by wrapping the lnet.send_message like:



if $SAFE == 0
LNet.send_message(attr={'type'=>'channel', 'channel'=>'DUSKRUIN'}, responseText)
end