ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/rent.ext
Revision: 1.30
Committed: Mon Apr 26 12:09:03 2010 UTC (14 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.29: +2 -0 lines
Log Message:
added heaven and st. batholomew apartments.

File Contents

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