Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace eval wallet {
- # Sets the public command trigger used in channel to access bank account.
- # +------+
- variable trigger !wallet
- # +------+
- #
- variable intrate .001
- # INTEREST TIMER INTERVAL
- # Number of minutes between timer intervals.
- # +--+
- variable timerint 180
- # +--+
- #
- # LOG INTEREST PAYOUTS
- # Uses putlog to log interest amounts paid each interval.
- # 1=on 0=off
- # +-+
- variable logtransfers 1
- # +-+
- #
- # BACKUP LOCATION/FILE
- # Relative pathname starting from eggdrop dir. The location/filename of
- # the backup file used to save bank information.
- # +----------------------+
- variable backupfile "scripts/walletdb.tcl"
- # +----------------------+
- #
- ################################################################################
- # Experts only below.
- ################################################################################
- }
- bind pub - [set ::wallet::trigger] ::wallet::pub_handler
- bind dcc n wallet ::wallet::dcc_admin
- setudef flag wallet
- namespace eval wallet {
- variable walletb
- variable ver "0.0.1"
- if {[file exist [set ::wallet::backupfile]]} {
- source [set ::wallet::backupfile]
- }
- if {![info exists ::wallet::timer_running]} {
- # start new timer.
- timer [set ::wallet::logtransfers] [list ::wallet::interest_timer]
- set ::wallet::timer_running 1
- }
- proc deposit {amount payee} {
- # deposit funds.
- set ::wallet::walletb($payee) [expr [set ::wallet::walletb($payee)] + $amount]
- ::wallet::backupdb
- return 1
- }
- proc withdraw {amount debtor} {
- # withraw bank funds.
- if {$amount <= [set ::wallet::walletb($debtor)]} {
- # amount is less than account balance.
- set ::wallet::walletb($debtor) [expr [set ::wallet::walletb($debtor)] - $amount]
- ::wallet::backupdb
- return 1
- }
- }
- proc interest_timer {args} {
- # call self at timed intervals. do backup
- foreach {name value} [array get ::wallet::walletb] {
- set interest [expr {int($value * [set ::wallet::intrate])}]
- set ::wallet::walletb($name) [expr $value + $interest]
- if {[set ::wallet::logtransfers]} {
- # log interest payment.
- putlog "Paid $name $interest in interest."
- }
- }
- ::wallet::backupdb
- timer [set ::wallet::logtransfers] [list ::wallet::interest_timer]
- return 1
- }
- proc backupdb {args} {
- # backup walletdb to file.
- variable ::wallet::walletb
- set fs [open [set ::wallet::backupfile] w+]
- puts $fs "variable ::wallet::walletb"
- puts $fs "array set walletb [list [array get walletb]]"
- close $fs;
- }
- proc account {args} {
- # add/remove/edit accounts.
- set textarr [split $args]
- set action [string tolower [lindex $textarr 0]]
- set name [string tolower [lindex $textarr 1]]
- set amount [lindex $textarr 2]
- switch $action {
- "add" {
- set ::wallet::walletb($name) 100
- ::wallet::backupdb
- }
- "remove" {
- unset ::wallet::walletb($name)
- ::wallet::backupdb
- }
- "edit" {
- set ::wallet::walletb($name) $amount
- ::wallet::backupdb
- }
- }
- }
- proc pub_handler {nick userhost handle channel text} {
- set capnick [string totitle $nick]
- if {[channel get $channel wallet]} {
- set textarr [split $text]
- set first [string tolower [lindex $textarr 0]]
- set nick [string tolower $nick]
- switch $first {
- "" {
- putserv "PRIVMSG $channel :\0034usage:\00312 [set ::wallet::trigger] cash|pay|gamble ?args?"
- }
- "cash" {
- if {[info exists ::wallet::walletb($nick)]} {
- putserv "PRIVMSG $channel :\00312$capnick, your wallet has:\0039\$[set ::wallet::walletb($nick)]"
- } else {
- putserv "PRIVMSG $channel :\00312$capnick, you don't even have an account."
- }
- }
- "gamble" {
- set amt [lindex $textarr 1]
- if {[string is integer $amt]} {
- if {$amt == ""} {
- putserv "PRIVMSG $channel :\0034usage:\00312 !gamble <amount>"
- } elseif {[info exists ::wallet::walletb($nick)]} {
- set coh [set ::wallet::walletb($nick)]
- if { $amt > $coh } {
- putserv "PRIVMSG $channel :\00312$capnick, you don't even have enough cash:\0039\$$coh"
- } else {
- if {[expr int(rand() * 2 + 1)] == 2} {
- set winnings [expr $amt + $amt]
- if {[::wallet::deposit $winnings $nick] == 1} {
- putserv "PRIVMSG $channel :\00312Here..."
- }
- putserv "PRIVMSG $channel :\00312$capnick, you just won:\0039\$$winnings"
- } else {
- putserv "PRIVMSG $channel :\00312$capnick, you lost:\0039\$$amt"
- if {[::wallet::withdraw $amt $nick] == 1} { putserv "PRIVMSG $channel :\00312Thanks $capnick!" }
- }
- }
- } else {
- putserv "PRIVMSG $channel :\00312 Wow, $capnick, you don't even have a wallet..."
- }
- }
- }
- "pay" {
- set payee [lindex $textarr 1]
- set lowpayee [string tolower $payee]
- set amount [lindex $textarr 2]
- set capnick [string totitle $nick]
- set pnick [string totitle $payee]
- if {$payee == ""} {
- putserv "PRIVMSG $channel :\00312usage: !wallet pay <payee> \0039<amount>"
- } elseif {[info exists ::wallet::walletb($nick)]} {
- if {[info exists ::wallet::walletb($lowpayee)]} {
- if {[string is integer $amount]} {
- if {[::wallet::withdraw $amount $nick] == 1} {
- if {[::wallet::deposit $amount $lowpayee] == 1} {
- putserv "PRIVMSG $channel :\00312Paid $pnick \0039\$$amount"
- } else { putserv "PRIVMSG $channel :\00312Can't do it right now..." }
- } else { putserv "PRIVMSG $channel :\00312Not enough cash!" }
- } else { putserv "PRIVMSG $channel :\00312cannot use '$amount' as an amount." }
- } else { putserv "PRIVMSG $channel :\00312$pnick does not even have a wallet." }
- } else { putserv "PRIVMSG $channel :\00312$capnick, you don't even have a wallet."}
- }
- }
- }
- }
- proc dcc_admin {handle idx text} {
- set textarr [split $text]
- set text [string tolower [lindex $textarr 0]]
- set name [string tolower [lindex $textarr 1]]
- switch $text {
- "" {
- putdcc $idx "\0034usage:\00312 .wallet ?add|remove|check|list|edit? ?args?"
- }
- "list" {
- foreach {name value} [array get ::wallet::walletb] {
- putdcc $idx "$name has $value coins."
- }
- }
- "add" {
- if {$name == ""} {
- putdcc $idx "\0034usage:\00312 .wallet add <name>"
- } elseif {![info exists ::wallet::walletb($name)]} {
- ::wallet::account add $name
- putdcc $idx "Wallet created."
- } else {
- putdcc $idx "Wallet already exists. Nothing to create."
- }
- }
- "check" {
- if {$name == ""} {
- putdcc $idx "\0034usage:\00312 .wallet check <name>"
- } elseif {[info exists ::wallet::walletb($name)]} {
- putdcc $idx "balance check: $name has [set ::wallet::walletb($name)] coins."
- } else {
- putdcc $idx "$name doesn't have an account to check."
- }
- }
- "remove" {
- if {$name == ""} {
- putdcc $idx "\0034usage:\00312 .wallet remove <name>"
- } elseif {[info exists ::wallet::walletb($name)]} {
- ::wallet::account remove $name
- putdcc $idx "account removed."
- } else {
- putdcc $idx "account doesn't exist. nothing to remove."
- }
- }
- "edit" {
- set amount [lindex $textarr 2]
- if {$name == ""} {
- putdcc $idx "\0034usage:\00312 .wallet edit <name> <amount>"
- } elseif {[info exists ::wallet::walletb($name)]} {
- if {[string is integer $amount]} {
- ::wallet::account edit $name $amount
- putdcc $idx "account edited."
- } else {
- putdcc $idx "cannot use '$amount' for an amount."
- }
- } else {
- putdcc $idx "account doesn't exist."
- }
- }
- }
- }
- }
- putlog "wallet [set ::wallet::ver] Loaded"
Add Comment
Please, Sign In to add comment