We're playing RPG online (by IRC) and I need a script which can "simulate" dice. It has to do this: When user types "!result" it picks a random integer between 1-100 (1 and 100 included) and then it prints out the result of the action (as text) based on the picked number.
I think I managed to write the main part of this script. It looks like this:
{
set number [rand 100]
set number [expr $string + 1]
if {$number > 0} then {set result "Critical failure"}
if {$number > 5} then {set result Very bad"}
if {$number > 15} then {set result "Bad"}
if {$number > 35} then {set result "Average"}
if {$number > 65} then {set result "Good"}
if {$number > 85} then {set result "Very good"}
if ($number > 95} then {set result "Critical success"}
puthelp "PRIVMSG $chan :Result... $result"
}
I think it's OK (is it?). I know I should have used "elseif", but Eggbot didn't recognize the command. Kinda strange...
Anyway, I know very little about Tcl. The script above does not work. It is missing proc-line and bind-line. Could you please tell me how they shall look like?
bind pub - !result pubresult
proc pubresult { n u h c t } {
set number [expr {[rand 100]+1}]
if {$number < 5} {
set result "Critical failure"
} elseif {$number < 15} {
set result "Very bad"
} elseif {$number < 35} {
set result "Bad"
} elseif {$number < 65} {
set result "Average"
} elseif {$number < 85} {
set result "Good"
} elseif {$number < 95} {
set result "Very Good"
} else {
set result "Critical success"
}
puthelp "PRIVMSG $c :Result... $result ($number)"
}
proc myproc {n u h c t} {
if {....} { #do stuff }
elseif {....} { #do stuff }
elseif {....} { #do stuff }
elseif {....} { #do stuff }
else { #do stuff }
}
{....} being the condition to satisfy. Remember "else" will have no condition to satisfy as it will be your last choice, saying your last resort, if every condition before it does not satisfy and run, then else will do the job.
Or you can do a similar thing with alot of "if's" and it will still work as well. But note this one does not have an "else". So if the conditions are satisfied they will run, otherwise they wont. Generally this type of structure is used if more than one condition is true; for multiple conditions.
proc myproc {n u h c t} {
if {....} { #do stuff }
if {....} { #do stuff }
if {....} { #do stuff }
if {....} { #do stuff }
if {....} { #do stuff }
}
this will trigger all the elseif will end on the first condition it meets
for example lets say the number is 5 then $number is smaller then 35 but also smaller then 65 !!!!!
same with bigger if number is 60 then it is bigger then 30 and bigger then 10 .. so thats why elseif it will enter the first condition it meets and not go on any further unless you use return on the end of the if statement ..