This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

What is wrong with this script?

Old posts that have not been replied to for several years.
Locked
t
theo

What is wrong with this script?

Post by theo »

Hi,

i made myself a script to use for players from a online game.

The script should tell people how much gold they will have after a couple of hours by interest.

The interest is 0.01 an hour.

the script only doesn't calculate it right.

i tried to calculate 100 gold. the hours to bild interest was 5. so it should say that after 5 hours my gold would be 105 gold.

that is doing it right, but the hours that are past give interest also.

If i do a 10000 with 50 hours it says 10500 gold, while it should be 10510 gold.

I put the script in a new message.

Theo.
Last edited by theo on Sun Aug 25, 2002 2:07 pm, edited 1 time in total.
t
theo

Post by theo »

Code: Select all


# listen to all theo commands in channels 
bind pub - theo pub:theo 

# limit the chans to listen in by filling them in here ( more chans goes this way {#chan1 #chan2} 
set enabledchans {#attack} 

# procedure to check if chan where command is used is listed chan 
proc isenabledchan { channel } { 
   global enabledchans 
   # compare each chan in $enabledchans with current chan and return 1 (true) if chan is listed 
   foreach enablechan $enabledchans { 
      if { $channel == $enablechan } { 
         return 1 
      } 
   } 
   return 0 
} 

# main procedure to handle the theo commands 
proc pub:theo { nick uhost handle channel arg } { 
   # check if command should work in this chan 
   set chan [string tolower $channel]    
   if { ![isenabledchan $chan] } { 
      return 1 
   } 
   # check if valid command (calc) 
   if { [llength $arg] == 0 } { 
      putserv "PRIVMSG $channel :Unable to perform request, give command." 
   } else { 
      # get command and compare it to "calc" 
      set command [lindex $arg 0] 
      set command [string tolower $command] 
      if { [string compare $command calc] == 0 } { 
         # check if valid number of arguments 
         if { [llength $arg] < 2 } { 
            putserv "PRIVMSG $channel :Please check the help function if you don't know how to use this command." 
         # command syntax is ok, process request 
         } else { 
            set gold_start [lindex $arg 1]
            set gold_hours [lindex $arg 2]
            set gold_temp [expr $gold_start / 1.01]
            set gold_new [expr $gold_temp * $gold_hours]
            set gold_make [expr $gold_start + $gold_new]
              putserv "PRIVMSG $channel :After $gold_hours hour(s) in the bank, $gold_start gold will be $gold_make gold."
            }
          }
   }
}
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

# command syntax is ok, process request 
         } else { 
            set gold_start [lindex $arg 1] 
            set gold_hours [lindex $arg 2] 
            set gold_new [expr [expr $gold_start * 0.01] * $gold_hours] 
            set gold_make [expr $gold_start + $gold_new] 
              putserv "PRIVMSG $channel :After $gold_hours hour(s) in the bank, $gold_start gold will be $gold_make gold." 
            } 
          } 
that should do it
Elen sila lúmenn' omentielvo
t
theo

Post by theo »

thanks papilon.

You responded before i could edit my message again.

The game doesn't work with comma's.

The script you made puts :

After 50 hour(s) in the bank, 10000 gold will be 15000.0 gold

while it should be 16420.

The hours that past also give interest

could you please help me again?

Theo
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

# command syntax is ok, process request 
         } else { 
            set gold_start [lindex $arg 1]
            set gold_hours [lindex $arg 2] 
	    set check 1
	    set gold_make "$gold_start"
	    while {$check != [expr $gold_hours + 1]} { 
	      set gold_new [expr $gold_make * 0.01]
	      set gold_make [expr $gold_make + $gold_new]
	      set check [incr check]
	    }
            putserv "PRIVMSG $channel :After $gold_hours hour(s) in the bank, $gold_start gold will be $gold_make gold." 
            } 
          }
that works.. but it is now with comma's, 10000 dollars for 50 hours will return "16446.3182184"
how to make it without comma's I'll leave to u... don't have the knowledge of how :) .... or the time to figure it out
Elen sila lúmenn' omentielvo
I
Iridium

Post by Iridium »

this is really dirty, but try [lindex [split "it" .] 0] where "it" is whatever is returning 1234.1234
t
theo

Post by theo »

i am just beginning with tcl, so i don't know where to put it exactly.

i tried a couple of places, but only thing that happens is the script stops working at all.

anybody that can put me in the right direction?

Theo
I
Iridium

Post by Iridium »

where does it say 16446.3182184?
t
theo

Post by theo »

that would be at putserv "PRIVMSG $channel :After $gold_hours hour(s) in the bank, $gold_start gold will be $gold_make gold."

the $gold_make would be the place where it says that
I
Iridium

Sorry about the while to reply..

Post by Iridium »

Code: Select all

putserv "PRIVMSG $channel :After $gold_hours hour(s) in the bank, $gold_start gold will be [expr int($gold_make)] gold."
hth..

warning.. this will unfortunately not round up correctly..

e.g.
1.4 -> 1
1.5 -> 1
1.6 -> 1
2.5 -> 1

You get the idea.
t
theo

Post by theo »

ok.

this removes the comma.

now one more thing.

if i do the command theo calc 100 24 it says that it will be 125 gold.

this isn't right.

it should be 124.

anybody knows how to fix that?
t
theo

Post by theo »

ok.

The script is doing exactly what it should do now.

Thanks to Papillon and Iridium for their help.

Theo
I
Iridium

if you want to round up correctly..

Post by Iridium »

Use this proc:

Code: Select all

proc integer {integer} {
  if {[expr $integer - int($integer)] < 0.5} {
    return [expr int($integer)]
  } else {
    return [expr int($integer) + 1]
  }
}
Put it in your code like so:

Code: Select all

putserv "PRIVMSG $channel :After $gold_hours hour(s) in the bank, $gold_start gold will be [integer $gold_make] gold."
The rounding should be correct now.
D
Darude

Post by Darude »

Might want to use the round() function instead.
Locked