ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/rent.ext
Revision: 1.8
Committed: Tue May 22 10:42:48 2007 UTC (17 years ago) by root
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.7: +1 -1 lines
Log Message:
fix longstanding inconsequential (up to now :) bug

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     use List::Util;
4    
5     my %apartment = (
6     "/scorn/apartment/apartments" => [ 1, "scorn", "skorn"],
7     "/santo_dominion/sdomino_appartment" => [ 10, "santo dominion", "domino"],
8     "/darcap/darcap/apartment" => [ 30, "darcap", "thecap"],
9     "/navar_city/apartments/apartment" => [ 250, "navar", "navar"],
10     "/azumauindo/ranbounagisatoshi/apartments/sapartment" => [ 100, "乱暴渚都市", "benjo"],
11     "/azumauindo/suno-yamatoshi/apartments/lapartment1" => [ 1000, "スノー大和島根", "sama"],
12     "/pup_land/nurnberg/apartment/main" => [ 300, "nürnberg", "sauerkraut"],
13     "/pup_land/lone_town/apartment/groundfloor" => [50000, "lone town", "looney"],
14     "/brest/apartments/brest_town_house" => [30000, "brest", "brecht"],
15     );
16    
17     # we have to special case some special cases :)
18     sub reject_entry {
19     my ($pl) = @_;
20    
21 root 1.7 my $prev_pos = $pl->ob->{_prev_pos};
22 root 1.8 $pl->ob->goto ($prev_pos ? @$prev_pos : ("/world/world_105_115", 2, 34));
23 root 1.7
24 root 1.1 cf::override;
25     }
26    
27     sub update_balance {
28     my ($pl) = @_;
29    
30     my $NOW = time;
31    
32     # 1 silver per day per level per apartment of kingdom tax
33     my $offline = (List::Util::min 30, ($NOW - $pl->{rent}{last_offline_check}) / 86400)
34     * (cf::exp_to_level $pl->ob->stats->exp)
35     * scalar keys %{ $pl->{rent}{apartment} };
36    
37     # once per hour per map rented
38     my $online = ($NOW - $pl->{rent}{last_online_check}) / 3600
39     * List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} };
40    
41     $pl->{rent}{last_offline_check} = $NOW;
42     $pl->{rent}{last_online_check} = $NOW;
43    
44     $pl->{rent}{balance} += $offline + $online;
45     }
46    
47     sub pay_balance {
48     my ($pl) = @_;
49    
50     update_balance $pl;
51    
52     return unless $pl->{rent}{balance} > 0;
53    
54     my $deduct = cf::ceil $pl->{rent}{balance};
55    
56     my $deduct_string = cf::cost_string_from_value $deduct;
57    
58     if ($deduct <= $pl->ob->{bank_balance}) {
59     cf::db_put rent => balance => $deduct + cf::db_get rent => "balance";
60     $pl->ob->{bank_balance} -= $deduct;
61     $pl->{rent}{balance} -= $deduct;
62     $pl->ob->reply (undef, "Something whispers into your ear:\n"
63     . "Sir, we deducted your apartment rent ($deduct_string) from your bank account.");
64     } else {
65     $pl->ob->reply (undef, "Something whispers into your ear:\n"
66     . "Sir, we want to deduct the apartment rent ($deduct_string), but the bank informed us that they cannot perform the transaction. "
67     . "Please even out your balance so we can deduct the fees, otherwise we will be forced to shut down your access to the apartment.");
68     }
69     }
70    
71     sub check_balance {
72     my ($pl) = @_;
73    
74     pay_balance $pl if $pl->{rent}{balance} > 0;
75    
76     $pl->{rent}{balance} <= 0
77     }
78    
79     sub find_apartment {
80     my ($pl, $name) = @_;
81    
82     my $apartment = (grep $apartment{$_}[2] eq $name, keys %apartment)[0]
83     or $pl->ob->reply (undef, "Sorry, but we do not offer model '$name' for rent.");
84    
85     $apartment
86     }
87    
88     cf::register_script_function "rent::overview" => sub {
89     my ($pl, $types) = @_;
90    
91     while (my ($k, $v) = each %apartment) {
92     my $type = exists $pl->{rent}{apartment}{$k} ? 1 : 2;
93    
94     $pl->ob->reply (undef, "model \"$v->[2]\", situated in $v->[1] ("
95     . (cf::cost_string_from_value $v->[0]) . "/hr)")
96     if $type & $types;
97     }
98     };
99    
100     cf::register_script_function "rent::status" => sub {
101     my ($pl, $types) = @_;
102    
103     if ($pl->{rent}{balance} <= 0) {
104     $pl->ob->reply (undef, "You have no debts to pay.");
105     } else {
106     $pl->ob->reply (undef, "You owe us " . (cf::cost_string_from_value $pl->{rent}{balance}) . ".");
107     }
108     };
109    
110     cf::register_script_function "rent::rent" => sub {
111     my ($pl, $apartment) = @_;
112    
113     $apartment = find_apartment $pl, $apartment
114     or return;
115    
116     update_balance $pl;
117    
118     $pl->{rent}{apartment}{$apartment} = undef;
119    
120     $pl->ob->reply (undef, "Wonderful decision, sir! "
121     . "We told the proprietor in $apartment{$apartment}[1] to expect you and let you in. "
122     . "We are sure you will be satisfied!");
123     };
124    
125     cf::register_script_function "rent::stop" => sub {
126     my ($pl, $apartment) = @_;
127    
128     $apartment = find_apartment $pl, $apartment
129     or return;
130    
131     update_balance $pl;
132    
133     delete $pl->{rent}{apartment}{$apartment};
134    
135     $pl->ob->reply (undef, "I am sorry to hear that, we will immediately stop charging you for your apartment, of course.");
136     };
137    
138 root 1.2 cf::player->attach (
139     prio => 100,
140 root 1.1 on_login => sub {
141     my ($pl) = @_;
142    
143     $pl->{rent}{last_offline_check} ||= time;
144    
145     if ($pl->{rent}{last_online_check}) {
146     $pl->{rent}{last_online_check} = time
147     - List::Util::min 3600,
148     $pl->ob->get_ob_key_value ("schmorplog_last_save") - $pl->{rent}{last_online_check};
149     } else {
150     $pl->{rent}{last_online_check} = time;
151     }
152    
153     update_balance $pl;
154 root 1.2 },
155     );
156 root 1.1
157 root 1.2 cf::map::attachment rent =>
158 root 1.1 on_enter => sub {
159     my ($map, $pl, $x, $y) = @_;
160    
161 root 1.7 my $pfx = sprintf "~%s/", $pl->ob->name;
162 root 1.1
163 root 1.7 # only do something if entering ones own apartment
164     if ($pfx eq substr $map->path, 0, length $pfx) {
165     for my $path (keys %{ $pl->{rent}{apartment} }) {
166     $path = sprintf "~%s%s", $pl->ob->name, $path;
167    
168     if ($map->path eq $path) {
169     if (check_balance $pl) {
170     $pl->ob->reply (undef, "Welcome to your apartment, sir!");
171     } else {
172     $pl->ob->reply (undef, "We are sorry, sir, you have to pay your rent first.");
173     reject_entry $pl;
174     }
175 root 1.1
176 root 1.7 return;
177 root 1.1 }
178 root 1.7 }
179 root 1.1
180 root 1.7 $pl->ob->reply (undef, "Sir, you have to rent this apartment in The Apartment Shop in Scorn first!");
181     reject_entry $pl;
182 root 1.1 }
183     },
184     ;
185    
186 root 1.6 our $RENT_TIMER = Event->timer (
187     reentrant => 0,
188     after => 60,
189     interval => 3600,
190     data => cf::WF_AUTOCANCEL,
191     cb => sub {
192     pay_balance $_ for cf::player::list;
193     },
194     );
195 root 1.1