ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/rent.ext
Revision: 1.31
Committed: Sat May 8 21:26:21 2010 UTC (14 years ago) by root
Branch: MAIN
CVS Tags: rel-3_1, rel-3_0, HEAD
Changes since 1.30: +1 -0 lines
Log Message:
initial wassar check-in

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