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