ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/rent.ext
Revision: 1.11
Committed: Thu Sep 13 08:35:24 2007 UTC (16 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-2_3
Changes since 1.10: +3 -1 lines
Log Message:
rent was calling blocking functions in the main coro. doh.; also, do some cleanups

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 root 1.11 cf::cede_to_tick;
51    
52 root 1.1 update_balance $pl;
53    
54     return unless $pl->{rent}{balance} > 0;
55    
56     my $deduct = cf::ceil $pl->{rent}{balance};
57    
58     my $deduct_string = cf::cost_string_from_value $deduct;
59    
60     if ($deduct <= $pl->ob->{bank_balance}) {
61     cf::db_put rent => balance => $deduct + cf::db_get rent => "balance";
62     $pl->ob->{bank_balance} -= $deduct;
63     $pl->{rent}{balance} -= $deduct;
64 root 1.10 $pl->ob->reply (undef, "Something whispers into your ear: "
65 root 1.9 . "Your highness, we deducted your apartment rent ($deduct_string) from your bank account.");
66 root 1.1 } else {
67 root 1.10 $pl->ob->reply (undef, "Something whispers into your ear: "
68 root 1.9 . "Your highness, we want to deduct the apartment rent ($deduct_string), but the bank informed us that they cannot perform the transaction. "
69 root 1.1 . "Please even out your balance so we can deduct the fees, otherwise we will be forced to shut down your access to the apartment.");
70     }
71     }
72    
73     sub check_balance {
74     my ($pl) = @_;
75    
76     pay_balance $pl if $pl->{rent}{balance} > 0;
77    
78     $pl->{rent}{balance} <= 0
79     }
80    
81     sub find_apartment {
82     my ($pl, $name) = @_;
83    
84     my $apartment = (grep $apartment{$_}[2] eq $name, keys %apartment)[0]
85     or $pl->ob->reply (undef, "Sorry, but we do not offer model '$name' for rent.");
86    
87     $apartment
88     }
89    
90     cf::register_script_function "rent::overview" => sub {
91     my ($pl, $types) = @_;
92    
93     while (my ($k, $v) = each %apartment) {
94     my $type = exists $pl->{rent}{apartment}{$k} ? 1 : 2;
95    
96     $pl->ob->reply (undef, "model \"$v->[2]\", situated in $v->[1] ("
97 root 1.10 . (cf::cost_string_from_value $v->[0]) . "/hr)\n")
98 root 1.1 if $type & $types;
99     }
100     };
101    
102     cf::register_script_function "rent::status" => sub {
103     my ($pl, $types) = @_;
104    
105     if ($pl->{rent}{balance} <= 0) {
106     $pl->ob->reply (undef, "You have no debts to pay.");
107     } else {
108     $pl->ob->reply (undef, "You owe us " . (cf::cost_string_from_value $pl->{rent}{balance}) . ".");
109     }
110     };
111    
112     cf::register_script_function "rent::rent" => sub {
113     my ($pl, $apartment) = @_;
114    
115     $apartment = find_apartment $pl, $apartment
116     or return;
117    
118     update_balance $pl;
119    
120     $pl->{rent}{apartment}{$apartment} = undef;
121    
122 root 1.9 $pl->ob->reply (undef, "Wonderful decision, your highness! "
123 root 1.1 . "We told the proprietor in $apartment{$apartment}[1] to expect you and let you in. "
124     . "We are sure you will be satisfied!");
125     };
126    
127     cf::register_script_function "rent::stop" => sub {
128     my ($pl, $apartment) = @_;
129    
130     $apartment = find_apartment $pl, $apartment
131     or return;
132    
133     update_balance $pl;
134    
135     delete $pl->{rent}{apartment}{$apartment};
136    
137     $pl->ob->reply (undef, "I am sorry to hear that, we will immediately stop charging you for your apartment, of course.");
138     };
139    
140 root 1.2 cf::player->attach (
141     prio => 100,
142 root 1.1 on_login => sub {
143     my ($pl) = @_;
144    
145     $pl->{rent}{last_offline_check} ||= time;
146    
147     if ($pl->{rent}{last_online_check}) {
148     $pl->{rent}{last_online_check} = time
149     - List::Util::min 3600,
150     $pl->ob->get_ob_key_value ("schmorplog_last_save") - $pl->{rent}{last_online_check};
151     } else {
152     $pl->{rent}{last_online_check} = time;
153     }
154    
155     update_balance $pl;
156 root 1.2 },
157     );
158 root 1.1
159 root 1.2 cf::map::attachment rent =>
160 root 1.1 on_enter => sub {
161     my ($map, $pl, $x, $y) = @_;
162    
163 root 1.7 my $pfx = sprintf "~%s/", $pl->ob->name;
164 root 1.1
165 root 1.7 # only do something if entering ones own apartment
166     if ($pfx eq substr $map->path, 0, length $pfx) {
167     for my $path (keys %{ $pl->{rent}{apartment} }) {
168     $path = sprintf "~%s%s", $pl->ob->name, $path;
169    
170     if ($map->path eq $path) {
171     if (check_balance $pl) {
172 root 1.9 $pl->ob->reply (undef, "Welcome to your apartment, your highness!");
173 root 1.7 } else {
174 root 1.9 $pl->ob->reply (undef, "We are sorry, your highness, you have to pay your rent first.");
175 root 1.7 reject_entry $pl;
176     }
177 root 1.1
178 root 1.7 return;
179 root 1.1 }
180 root 1.7 }
181 root 1.1
182 root 1.9 $pl->ob->reply (undef, "Your highness, you have to rent this apartment in The Apartment Shop in Scorn first!");
183 root 1.7 reject_entry $pl;
184 root 1.1 }
185     },
186     ;
187    
188 root 1.6 our $RENT_TIMER = Event->timer (
189     reentrant => 0,
190     after => 60,
191     interval => 3600,
192     data => cf::WF_AUTOCANCEL,
193 root 1.11 cb => Coro::unblock_sub {
194 root 1.6 pay_balance $_ for cf::player::list;
195     },
196     );
197 root 1.1