… | |
… | |
24 | $portal->stats->sp ($y); |
24 | $portal->stats->sp ($y); |
25 | $portal->apply ($pl->ob); |
25 | $portal->apply ($pl->ob); |
26 | $portal->free; |
26 | $portal->free; |
27 | } |
27 | } |
28 | |
28 | |
|
|
29 | # we have to special case some special cases :) |
|
|
30 | sub reject_entry { |
|
|
31 | my ($pl) = @_; |
|
|
32 | |
|
|
33 | cf::override; |
|
|
34 | |
|
|
35 | teleport $pl, "/world/world_105_115", 2, 34 |
|
|
36 | unless $pl->ob->map && $pl->ob->map->path !~ /nimbus/; |
|
|
37 | } |
|
|
38 | |
29 | sub update_balance { |
39 | sub update_balance { |
30 | my ($pl) = @_; |
40 | my ($pl) = @_; |
31 | |
41 | |
32 | my $NOW = time; |
42 | my $NOW = time; |
33 | |
43 | |
… | |
… | |
35 | my $offline = (List::Util::min 30, ($NOW - $pl->{rent}{last_offline_check}) / 86400) |
45 | my $offline = (List::Util::min 30, ($NOW - $pl->{rent}{last_offline_check}) / 86400) |
36 | * (cf::exp_to_level $pl->ob->stats->exp) |
46 | * (cf::exp_to_level $pl->ob->stats->exp) |
37 | * scalar keys %{ $pl->{rent}{apartment} }; |
47 | * scalar keys %{ $pl->{rent}{apartment} }; |
38 | |
48 | |
39 | # once per hour per map rented |
49 | # once per hour per map rented |
40 | my $online = ($NOW - $pl->{rent}{last_offline_check}) / 3600 |
50 | my $online = ($NOW - $pl->{rent}{last_online_check}) / 3600 |
41 | * List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} }; |
51 | * List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} }; |
42 | |
52 | |
43 | #TODO: add to {balance} |
53 | $pl->{rent}{last_offline_check} = $NOW; |
44 | warn "off $offline on $online\n";#d# |
54 | $pl->{rent}{last_online_check} = $NOW; |
|
|
55 | |
|
|
56 | $pl->{rent}{balance} += $offline + $online; |
45 | } |
57 | } |
46 | |
58 | |
47 | sub pay_balance { |
59 | sub pay_balance { |
48 | my ($pl) = @_; |
60 | my ($pl) = @_; |
49 | |
61 | |
50 | #TODO: rob the bank if balance > 0 |
62 | update_balance $pl; |
|
|
63 | |
|
|
64 | return unless $pl->{rent}{balance} > 0; |
|
|
65 | |
|
|
66 | my $deduct = cf::ceil $pl->{rent}{balance}; |
|
|
67 | |
|
|
68 | my $deduct_string = cf::cost_string_from_value $deduct; |
|
|
69 | |
|
|
70 | if ($deduct <= $pl->ob->{bank_balance}) { |
|
|
71 | $pl->ob->{bank_balance} -= $deduct; |
|
|
72 | $pl->{rent}{balance} -= $deduct; |
|
|
73 | $pl->ob->reply (undef, "Something whispers into your ear:\n" |
|
|
74 | . "Sir, we deducted your apartment rent ($deduct_string) from your bank account."); |
|
|
75 | } else { |
|
|
76 | $pl->ob->reply (undef, "Something whispers into your ear:\n" |
|
|
77 | . "Sir, 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 |
51 | } |
88 | } |
52 | |
89 | |
53 | sub find_apartment { |
90 | sub find_apartment { |
54 | my ($pl, $name) = @_; |
91 | my ($pl, $name) = @_; |
55 | |
92 | |
56 | #TODO: find apartment or reply with error |
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 |
57 | } |
97 | } |
58 | |
98 | |
59 | cf::register_script_function "rent::overview" => sub { |
99 | cf::register_script_function "rent::overview" => sub { |
60 | my ($pl, $types) = @_; |
100 | my ($pl, $types) = @_; |
61 | |
101 | |
… | |
… | |
79 | }; |
119 | }; |
80 | |
120 | |
81 | cf::register_script_function "rent::rent" => sub { |
121 | cf::register_script_function "rent::rent" => sub { |
82 | my ($pl, $apartment) = @_; |
122 | my ($pl, $apartment) = @_; |
83 | |
123 | |
84 | $apartment = find_apartment $apartment |
124 | $apartment = find_apartment $pl, $apartment |
85 | or return; |
125 | or return; |
86 | |
126 | |
87 | update_balance $pl; |
127 | update_balance $pl; |
88 | |
128 | |
89 | $pl->{rent}{apartment}{$apartment} = undef; |
129 | $pl->{rent}{apartment}{$apartment} = undef; |
|
|
130 | |
|
|
131 | $pl->ob->reply (undef, "Wonderful decision, sir! " |
|
|
132 | . "We told the proprietor in $apartment{$apartment}[1] to expect you and let you in. " |
|
|
133 | . "We are sure you will be satisfied!"); |
90 | }; |
134 | }; |
91 | |
135 | |
92 | cf::register_script_function "rent::stop" => sub { |
136 | cf::register_script_function "rent::stop" => sub { |
93 | my ($pl, $apartment) = @_; |
137 | my ($pl, $apartment) = @_; |
94 | |
138 | |
95 | $apartment = find_apartment $apartment |
139 | $apartment = find_apartment $pl, $apartment |
96 | or return; |
140 | or return; |
97 | |
141 | |
98 | update_balance $pl; |
142 | update_balance $pl; |
99 | |
143 | |
100 | delete $pl->{rent}{apartment}{$apartment}; |
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."); |
101 | }; |
147 | }; |
102 | |
148 | |
103 | cf::attach_to_players prio => 100, |
149 | cf::attach_to_players prio => 100, |
104 | on_login => sub { |
150 | on_login => sub { |
105 | return; |
151 | return; |
… | |
… | |
121 | update_balance $pl; |
167 | update_balance $pl; |
122 | }; |
168 | }; |
123 | |
169 | |
124 | cf::register_map_attachment rent => |
170 | cf::register_map_attachment rent => |
125 | on_enter => sub { |
171 | on_enter => sub { |
126 | return; |
|
|
127 | my ($map, $pl, $x, $y) = @_; |
172 | my ($map, $pl, $x, $y) = @_; |
128 | |
173 | |
129 | # teleport $pl, "/world/world_105_115", 2, 34; |
174 | for my $path (keys %{ $pl->{rent}{apartment} }) { |
130 | # cf::override; |
175 | $path =~ y/\//_/; |
131 | }; |
176 | $path = sprintf "%s/%s/%s/%s", |
|
|
177 | cf::localdir, cf::playerdir, $pl->ob->name, $path; |
132 | |
178 | |
|
|
179 | if ($map->path eq $path) { |
|
|
180 | if (check_balance $pl) { |
|
|
181 | $pl->ob->reply (undef, "Welcome to your apartment, sir!"); |
|
|
182 | } else { |
|
|
183 | $pl->ob->reply (undef, "We are sorry, sir, you have to pay your rent first."); |
|
|
184 | reject_entry $pl; |
|
|
185 | } |
|
|
186 | |
|
|
187 | return; |
|
|
188 | } |
|
|
189 | } |
|
|
190 | |
|
|
191 | $pl->ob->reply (undef, "Sir, you have to rent this apartment in The Apartment Shop in Scorn first!"); |
|
|
192 | reject_entry $pl; |
|
|
193 | }, |
|
|
194 | ; |
|
|
195 | |
|
|
196 | Event->timer (after => 60, interval => 3600, cb => sub { |
|
|
197 | pay_balance $_ for cf::player::list; |
|
|
198 | }); |
|
|
199 | |