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.

Bar.tcl 1.0

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Bar.tcl 1.0

Post by ComputerTech »

Hey all so i made a bar script and a friend helped me with some things.

Only thing i dont personally like about this script is the amount of procs.

I dont like every drink having a separate proc, any advise on making this script better or shorter is much appreciated :D

Code: Select all

    set barcmd "!"

    namespace eval ::beer {
     global barcmd
 
    variable beer
     set pt "0"

    bind pub - "${barcmd}beer" ::beer::do_the_beer
    bind pub - "${barcmd}pay" ::beer::do_the_pay
    bind pub - "${barcmd}menu" ::beer::do_the_menu
    bind pub - "${barcmd}wine" ::beer::do_the_wine
    bind pub - "${barcmd}water" ::beer::do_the_water
    bind pub - "${barcmd}coke" ::beer::do_the_coke
    bind pub - "${barcmd}coke" ::beer::do_the_orange

    proc do_the_beer {nick uhost handle chan text} {
    	variable beer 
        global pt
    	set guy [lindex [split $text] 0]
    	set loaded [array names beer -exact $nick]
        if {$pt == 50} {
        putserv "privmsg $chan : $nick Your Tab is full With the amount of $pt Please pay before ordering more drink"     
         return                
     }
       	incr pt +5
        putserv "PRIVMSG $chan :\001ACTION Fills up the glass with Beer\001"
        putserv "PRIVMSG $chan :\001ACTION Gives the Beer to $nick\001"
        putserv "privmsg $chan : Enjoy your Beer $nick, Your Total Bill Amount Is $pt" 
    	array unset beer $nick
    }

     proc do_the_wine {nick uhost handle chan text} {
    	variable beer 
        global pt
    	set guy [lindex [split $text] 0]
    	set loaded [array names beer -exact $nick]
        if {$pt == 50} {
        putserv "privmsg $chan : $nick Your Tab is full With the amount of $pt Please pay before ordering more drink"     
         return                
     }
       	incr pt +25
        putserv "PRIVMSG $chan :\001ACTION Fills up the glass with Wine\001"
        putserv "PRIVMSG $chan :\001ACTION Gives the Wine to $nick\001"
        putserv "privmsg $chan : Enjoy your Wine $nick, Your Total Bill Amount Is $pt" 
    	array unset beer $nick
    }

     proc do_the_water {nick uhost handle chan text} {
    	variable beer 
        global pt
    	set guy [lindex [split $text] 0]
    	set loaded [array names beer -exact $nick]
        if {$pt == 50} {
        putserv "privmsg $chan : $nick Your Tab is full With the amount of $pt Please pay before ordering more drink"     
         return                
     }
       	incr pt +2
        putserv "PRIVMSG $chan :\001ACTION Fills up the glass with Water\001"
        putserv "PRIVMSG $chan :\001ACTION Gives the Water to $nick\001"
        putserv "privmsg $chan : Enjoy your Water $nick, Your Total Bill Amount Is $pt" 
    	array unset beer $nick
    }

     proc do_the_orange {nick uhost handle chan text} {
    	variable beer 
        global pt
    	set guy [lindex [split $text] 0]
    	set loaded [array names beer -exact $nick]
        if {$pt == 50} {
        putserv "privmsg $chan : $nick Your Tab is full With the amount of $pt Please pay before ordering more drink"     
         return                
     }
       	incr pt +3
        putserv "PRIVMSG $chan :\001ACTION Fills up the glass with Orange juice\001"
        putserv "PRIVMSG $chan :\001ACTION Gives the Orange Juice to $nick\001"
        putserv "privmsg $chan : Enjoy your Orange Juice $nick, Your Total Bill Amount Is $pt" 
    	array unset beer $nick
    }



     proc do_the_coke {nick uhost handle chan text} {
    	variable beer 
        global pt
    	set guy [lindex [split $text] 0]
    	set loaded [array names beer -exact $nick]
        if {$pt == 50} {
        putserv "privmsg $chan : $nick Your Tab is full With the amount of $pt Please pay before ordering more drink"     
         return                
     }
       	incr pt +4
        putserv "PRIVMSG $chan :\001ACTION Fills up the glass with Coke\001"
        putserv "PRIVMSG $chan :\001ACTION Gives the Coke to $nick\001"
        putserv "privmsg $chan : Enjoy your Coke $nick, Your Total Bill Amount Is $pt" 
    	array unset beer $nick
    }

    proc do_the_pay {nick uhost handle chan text} {
    	variable beer 
        global pt
    	set loaded [array names beer -exact $nick]
    	if {$loaded != ""} {
    		putserv "privmsg $chan :$nick Your Already Payed Your Bill"
    		return
    	}
    	array set beer "$nick 1"
        putserv "privmsg $chan :$nick Thank You For Paying Your Bill, Here's your receipt"
        putserv "notice $chan : -------------------  "
        putserv "notice $chan :|Bar receipt                    "    
        putserv "notice $chan :| $ $pt  "
        putserv "notice $chan :| $nick       " 
        putserv "notice $chan :| $chan"
        putserv "notice $chan : ------------------- "
        set pt "0"
    }
     

 proc do_the_menu {nick uhost handle chan text} {
 global barcmd 
putserv "notice $nick :Welcome $nick This is our Menu"
 putserv "notice $nick :${barcmd}beer $ 5 , ${barcmd}water  $ 2 , {barcmd}wine $ 25 , ${barcmd}coke $ 4 ${barcmd}orange $ 3"
     }
    } ;# end namespace
ComputerTech
Online
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

Yes, it could be reduced to only one proc for all the drinks. I'll try to do something :)

Code: Select all

namespace eval ::beer {

	variable barcmd "!"
    variable beer
	array set bills {}

	array set drinks {
		beer	5
		wine	25
		water	2
		coke	3
		orange	4
	}
	
	foreach dr [array names drinks] {
		bind pubm - "*$::beer::barcmd$dr*" ::beer::do_drink
	}
	
	bind pub - "${barcmd}pay" ::beer::do_the_pay
    bind pub - "${barcmd}menu" ::beer::do_the_menu

    proc do_drink {nick uhost handle chan text} {
		set args [split $text " "]
		set key [stripcodes abcgru [lindex $args 0]]
		set key [string range $key 1 end]
		if {[lsearch [array names ::beer::drinks] $key] == -1} {
			putserv "PRIVMSG $chan :Sorry $nick, I haven't $key in the fridge"
			return 0
		}
		set cost $::beer::drinks($key)
		if {[array names ::beer::bills -exact $nick] ne ""} {
			set bill $::beer::bills($nick)
			if {$bill >= 50} {
				putserv "PRIVMSG $chan : $nick Your Tab is full With the amount of \$$bill Please pay before ordering more drink"    
				return  0           
			}
		} else {
			set ::beer::bills($nick) 0
		}
		incr ::beer::bills($nick) $cost
        putserv "PRIVMSG $chan :\001ACTION Fills up the glass with $key\001"
        putserv "PRIVMSG $chan :\001ACTION Gives the $key to $nick\001"
        putserv "PRIVMSG $chan : Enjoy your $key $nick, Your Total Bill Amount Is \$$::beer::bills($nick)"
    }


    proc do_the_pay {nick uhost handle chan text} {
       set loaded [array names ::beer::bills -exact $nick]
       if {$loaded eq ""} {
          putserv "PRIVMSG $chan :$nick Your Already Payed Your Bill"
          return
       }
        putserv "PRIVMSG $chan :$nick Thank You For Paying Your Bill, Here's your receipt"
        putserv "NOTICE $nick : -------------------  "
        putserv "NOTICE $nick :|Bar receipt                    "   
        putserv "NOTICE $nick :| \$$::beer::bills($nick)  "
        putserv "NOTICE $nick :| $nick       "
        putserv "NOTICE $nick :| $chan"
        putserv "NOTICE $nick : ------------------- "
        unset ::beer::bills($nick)
    }
     

	proc do_the_menu {nick uhost handle chan text} {
		putserv "NOTICE $nick :Welcome $nick This is our Menu"
		foreach {drink cost} [array get ::beer::drinks] {
		putserv "NOTICE $nick :$::beer::barcmd$drink : \$$cost"
		}
	}
	
	putlog "Barman 1.0.1 loaded"
}
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

Coooool, That Look's WAY better :mrgreen:

Cheers CrazyCat :D :D

Will try it rignt away 8)
ComputerTech
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

Absolute Perfection CrazyCat

Thank you so much :wink:
ComputerTech
Post Reply