Perl over FLHook's TCP
-
Releasing a old project i was working on for a while. Development is canceled now in favor of the Perl Script Engine Plugin
Perl access to FLHook using the TCP socket and eventmode.Supports loading and unloading of perl coded plugins on the fly.
A number of plugins have been coded including a dynamic economy system that works without client updates (functional, but never polished off), deathcost, memo system for leaving msgs to offline players, a bank and sendcash, a ‘News’ system (mostly used by the economy atm) and a few others.IMO the whole thing is pretty crude and has a few issues, but gets the job done. either way, since i’m no longer working on it (and the core runs pretty stable) i figure its best to release it before it gets lost in my archives lol
http://yspstudios.linuxniche.net/files/freelancer/FLPHook-v0-6-1.zip -
forgot to mention, the economy plugin wont work with 88flaks version of FLHook due to a change made to the output of the enumcargo command…it prints IDS names instead of hash codes
-
I was hoping the finish the interest part of your banking plugin in the download mentioned above. Logging isn’t important to me, so I deleted that part from the code I’m working with.
I added two new subroutines, called immediately after “my $account = paccount $id”.
I don’t know enough about Perl to finish this. It could take me days to read up on how to do this and I am willing to spend that time if I have to. I’m not sure if the localtime() function can output a day/year format that would be easier for the program to calculate from, or if Perl can do a days-passed calculation on its own. If you could finish this or point me in the right direction, I would appreciate it. I will post a link to the updated plugin when this is done.
sub bank_addinterest { my $account = shift; my $balance = &bank_get($account); my $rate = $Options->{bank}{interest_rate}; #read time stamp in account #calculate number of days that have passed # $balance = $balance * 2.7183 ^ ($rate * dayspassed) rounded to whole credits, or just drop the credits after the decimal &bank_set($account, $newbalance); }; sub bank_settimestamp { my $account = shift; my $t = localtime(); open MEMO,'>', $Options->{main}{accountpath}.$account."\\banktime" or return undef; print MEMO $t; close MEMO; };
-
instead of localtime(), use time() as it returns # of seconds that have passed since epoch, much easier for doing our date math
sub bank_addinterest { my $account = shift; my $balance = &bank_get($account); my $rate = $Options->{bank}{interest_rate}; #read time stamp in account - similar to our bank_get() sub my $timestamp; open MEMO, $Options->{main}{accountpath}.$account."\\banktime" or return undef; read MEMO, $timestamp, 50; close MEMO; #calculate number of days that have passed my $dayspassed = time() - $timestamp; # here $dayspassed is in seconds $dayspassed /= 86400; # divide by number of seconds per day # using int( ) will do rounding off for us my $newbalance = int( $balance * 2.7183 ^ ($rate * $dayspassed) ); &bank_set($account, $newbalance); }; sub bank_settimestamp { my $account = shift; # get time as number of seconds since epoch (jan 1 1970) my $t = time(); open MEMO,'>', $Options->{main}{accountpath}.$account."\\banktime" or return undef; print MEMO $t; close MEMO; };
-
Thanks again! I was trying several different ways to use localtime->yday() with no luck. If I had known about time() and int(), I could have done it, I think. I wasn’t sure about “*” and “^” working as math operators in Perl, but it looks good.
Here is the link to 0.6.1 from your site, in case anyone else comes across this thread and needs it to fix the death logger plugin I posted about in another forum:
http://yspstudios.linuxniche.net/files/freelancer/FLPHook-v0-6-1.zipI have attached the bank plugin version I am using. It required one minor fix for withdrawals to work properly when going beyond the min or max values set in the configuration file. “my $withdraw = &bank_withdraw($account, $amount);” had to go below the first two “if” statements or the player could lose money when attempting to go beyond the min/max values. I also removed the unused logging code.
Hmm, I guess it won’t let me upload a .pl or .txt. Looks like I have to put the entire code on here…
One more edit… the “^” is not for exponentiation, “**” is.
# FL-Perl Bank Plugin # Copyright 2008, Fenris_Wolf, http://yspstudios.linuxniche.net # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. plugin_register { name => 'bank', author => 'Fenris_Wolf', version => '1.2', date => '12/25/2008', description => 'adds banking commands', },{ help2 => sub{ send_green $_[0]->{id}, '/bank -- stores and transfers money (type command for help)'; }, bank => sub{ my $e = shift; my ($text,$from,$id)= @$e{qw{text from id}}; if (!$text) { send_green $id, '--------------- banking system help ---------------', '/bank balance -- shows how much money you got in the bank', '/bank withdraw (amount) -- withdraws money from your bank to this character', '/bank deposit (amount) -- deposits money to your bank from character', '/bank send (character) (amount) -- transfers money to another players bank account. they do not need to be online'; return undef; } my $account = paccount $id; &bank_addinterest($account); &bank_settimestamp($account); if ($text =~ /^ *balance *$/i) { my $balance = &bank_get($account); send_green $id, "current bank balance: $balance"; return undef; } my $cash = getcash $id; if ($text =~ /^ *deposit +([0-9]+) *$/i) { my $amount = $1; my $min = $Options->{bank}{deposit_min}; my $max = $Options->{bank}{deposit_max}; if ($min > 0 && $amount < $min) { send_red $id, "min deposit $min credits. aborting."; return undef; } if ($max > 0 && $amount > $max) { send_green $id, "max deposit $max credits. adjusting."; $amount = $max; } if ($amount > $cash) { # too much &bank_deposit($account, $cash); delcash $id, $cash; send_green $id, "Insuffecent funds. Depositing $cash"; } else { &bank_deposit($account, $amount); delcash $id, $amount; send_green $id, "$amount credits transfered to your bank account"; } } elsif ($text =~ /^ *withdraw +([0-9]+) *$/i) { my $amount = $1; my $min = $Options->{bank}{withdraw_min}; my $max = $Options->{bank}{withdraw_max}; if ($min > 0 && $amount < $min) { send_red $id, "min withdraw $min credits. aborting."; return undef; } if ($max > 0 && $amount > $max) { send_green $id, "max withdraw $max credits. adjusting."; $amount = $max; } my $withdraw = &bank_withdraw($account, $amount); if ($withdraw < $amount) { # not enough addcash $id, $withdraw; send_green $id, "insuffecent funds in bank. withdrawing $withdraw"; } else { addcash $id, $amount; send_green $id, "$amount withdrawn from your bank account"; } } elsif ($text =~ /^ *send +(\S+) +([0-9]+) *$/i) { my ($to, $amount) = ($1,$2); my $to_account = getaccountdirname $to; if (!$to_account) { send_red $id, "can't find a player named $to (wrong spelling?)"; return undef; } my $min = $Options->{bank}{transfer_min}; my $max = $Options->{bank}{transfer_max}; if ($min > 0 && $amount < $min) { send_red $id, "min transfer $min credits. aborting."; return undef; } if ($max > 0 && $amount > $max) { send_green $id, "max transfer $max credits. adjusting."; $amount = $max; } my $transfer = &bank_withdraw($account, $amount); &bank_deposit($to_account, $transfer); if ($amount > $transfer) { # too much send_green $id, "insuffecent funds. Tranfering $transfer credits to $to"; } else { send_green $id, "$amount credits transfered to $to\'s bank account"; } } } }, undef; sub bank_addinterest { my $account = shift; my $balance = &bank_get($account); my $rate = $Options->{bank}{interest_rate}; #read time stamp in account - similar to our bank_get() sub my $timestamp; open MEMO, $Options->{main}{accountpath}.$account."\\banktime" or return undef; read MEMO, $timestamp, 50; close MEMO; #calculate number of days that have passed my $dayspassed = time() - $timestamp; # here $dayspassed is in seconds $dayspassed /= 86400; # divide by number of seconds per day # using int( ) will do rounding off for us my $newbalance = int( $balance * (2.7183 ** ($rate * $dayspassed)) ); &bank_set($account, $newbalance); }; sub bank_settimestamp { my $account = shift; # get time as number of seconds since epoch (jan 1 1970) my $t = time(); open MEMO,'>', $Options->{main}{accountpath}.$account."\\banktime" or return undef; print MEMO $t; close MEMO; }; sub bank_get { my ($account, $return) = shift; open MEMO, $Options->{main}{accountpath}.$account."\\bank" or return 0; read MEMO, $return, 50; close MEMO; return $return; }; sub bank_set { my ($account, $balance) = (shift,shift); open MEMO,'>', $Options->{main}{accountpath}.$account."\\bank" or return 0; print MEMO $balance; close MEMO; }; sub bank_deposit { my ($account, $amount) = (shift, shift); my $balance = &bank_get($account); &bank_set($account, $balance + $amount); }; sub bank_withdraw { my ($account, $amount) = (shift, shift); my $balance = &bank_get($account); if ($balance < $amount) { &bank_set($account, '0'); return $balance; } my $max = $Options->{bank}{withdraw_max}; &bank_set($account, $balance - $amount); return $amount; }; 1;
-
It required one minor fix for withdrawals to work properly when going beyond the min or max values set in the configuration file
smacks himself in the forehead
I’ll include the changes in the download on my site… i need to repackage anyways theres a few things missing (source code for one, proper documentation for another lol)
ty for pointing out about the link too i updated it on my page but forgot to do it here -
re-uploaded with source code…re-compiling to exe might be a problem for most people though, as perl isnt exactly ment to be compiled. Theres a few extra perl modules in the src.zip too that arent actually used by FLPHook, but are part of the FLP Toolkit so figured it was best to leave them.
bank plugin changes been added (included your name in the author section too for the plugin)
proper documentation will have wait unfortunately til i get the time to write it up…have to do it from scratch i didnt even really include many comments in the source >.<