ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
Revision: 1.8
Committed: Sun Sep 30 16:24:30 2007 UTC (16 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-2_3, rel-2_32
Changes since 1.7: +7 -7 lines
Log Message:
finally remove the old buggy plug-in cruft

File Contents

# User Rev Content
1 root 1.7 #!perl # mandatory
2 root 1.1
3     my %unit = (
4     silver => 1,
5     gold => 10,
6     platina => 50,
7     royalty => 5000,
8     );
9    
10     my %aliases = (
11     platinum => 'platina',
12     royalties => 'royalty',
13     );
14    
15 elmex 1.2 sub ob2info {
16     my ($item, $rval) = @_;
17     sprintf "[%s from %s (%d:%d%s) nrof: %d uuid: %s]",
18     $item->name, $item->get_ob_key_value ('ext_reseller_seller'),
19     $item->get_ob_key_value ('ext_reseller_orig_value'), $item->value,
20     (defined $rval ? ":$rval" : ""), $item->nrof, $item->uuid;
21     }
22    
23     sub audit_log {
24     my ($who, $action, $info) = @_;
25     warn
26     sprintf "RESELLER_AUDIT(%s) %s %s: %s\n",
27     $who->map->path, $who->name, $action, $info;
28     }
29    
30 elmex 1.5 sub find_rec;
31    
32 root 1.1 sub find_rec {
33     my ($ob, $cb) = @_;
34    
35 elmex 1.5 my @found;
36 root 1.1 for my $i ($ob->inv) {
37 elmex 1.5 push @found, $i if $cb->($i);
38     push @found, find_rec $i, $cb if $i->inv;
39     }
40 root 1.1
41 elmex 1.5 return @found;
42 root 1.1 }
43    
44     sub find_unpaid {
45     my ($ob) = @_;
46 elmex 1.5 find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) };
47 root 1.1 }
48    
49     sub find_traded {
50     my ($ob) = @_;
51 elmex 1.5 find_rec $ob, sub { $_[0]->get_ob_key_value ('ext_reseller_seller') ne '' };
52 root 1.1 }
53    
54     cf::register_script_function "reseller::list_sells" => sub {
55     my ($who, $msg, $npc) = @_;
56 elmex 1.6 my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales');
57     my $sells = $ext_re_sales && cf::from_json $ext_re_sales;
58 root 1.1 my $hissells = $sells->{$who->name};
59    
60     unless (keys %{$hissells || {}}) {
61     $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
62     return 0;
63     }
64    
65     $who->reply ($npc, "You sold:\n", cf::NDI_BROWN);
66     for (keys %$hissells) {
67     my $n = $_;
68     $n =~ s/\s*\(unpaid\)//g;
69     $who->reply ($npc, "$n for " . cf::cost_string_from_value ($hissells->{$_}), cf::NDI_BROWN);
70     }
71    
72     0
73     };
74    
75     cf::register_script_function "reseller::pay_player" => sub {
76     my ($who, $msg, $npc) = @_;
77 elmex 1.6 my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales');
78     my $sells = $ext_re_sales && cf::from_json $ext_re_sales;
79 root 1.1 my $hissells = $sells->{$who->name};
80    
81     unless (keys %{$hissells || {}}) {
82     $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
83     return 0;
84     }
85    
86     my $sum = 0;
87     $sum += $_ for values %$hissells;
88    
89     $who->pay_player ($sum);
90     $who->reply ($npc, "Here are the " . cf::cost_string_from_value ($sum) . " for your sales");
91    
92 elmex 1.2 audit_log ($who, 'collects', "$sum silver");
93    
94 root 1.1 $sells->{$who->name} = {};
95    
96 elmex 1.6 $npc->set_ob_key_value (ext_reseller_sales => cf::to_json $sells)
97     if $sells;
98 root 1.1
99     0
100     };
101    
102 root 1.3 cf::object::attachment "reseller_shopmat",
103 root 1.1 on_move_trigger => sub {
104     my ($self, $who_caused, $who) = @_;
105    
106     my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} }
107     $who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y});
108    
109     unless (@obs) {
110 elmex 1.2 warn "Couldn't find shop keeper in " . $who->map->path . "\n";
111 root 1.1 return cf::override;
112     }
113    
114 elmex 1.6 my $ext_re_sales = $obs[0]->get_ob_key_value ('ext_reseller_sales');
115     my $sells = $ext_re_sales && cf::from_json $ext_re_sales;
116 root 1.1
117     my $unpaid_items = {};
118    
119     for my $item (find_unpaid ($who)) {
120     if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) {
121 elmex 1.2 audit_log ($who, 'removes', ob2info ($item));
122 root 1.1 $item->flag (cf::FLAG_UNPAID, 0);
123     $item->remove;
124 root 1.8 $who->insert ($item);
125 root 1.1 next;
126     }
127    
128     my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP);
129    
130     warn "Object " . $item->name . " bought by " . $who->name . " on map "
131     . $who->map->path . " for $value silver has no seller set\n"
132     if $item->get_ob_key_value ('ext_reseller_seller') eq '';
133    
134     $unpaid_items->{$item} = [$value, $item];
135     }
136    
137 elmex 1.2 audit_log ($who, 'wants', (join ",", map { ob2info ($_->[1], $_->[0]) } values %$unpaid_items))
138     if %$unpaid_items;
139    
140 root 1.1 $self->apply_shop_mat ($who);
141    
142 elmex 1.2 my @seller_noted;
143    
144 root 1.1 for my $item (find_traded ($who)) {
145     next if $item->flag (cf::FLAG_UNPAID);
146     if (my $value = $unpaid_items->{$item}[0]) {
147 elmex 1.2 push @seller_noted, ob2info ($item, $value)."P";
148 root 1.1 $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value;
149 elmex 1.2 } else {
150     push @seller_noted, ob2info ($item)."T";
151 root 1.1 }
152    
153     $item->value ($item->get_ob_key_value ('ext_reseller_orig_value'));
154     $item->set_ob_key_value (ext_reseller_seller => '');
155     }
156    
157 elmex 1.2 audit_log ($who, 'removed', (join ",", @seller_noted))
158     if @seller_noted;
159    
160 elmex 1.6 $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells)
161     if $sells;
162 root 1.1
163     cf::override;
164     },
165     ;
166    
167 root 1.3 cf::object::attachment "reseller_floor",
168 root 1.1 on_drop_on => sub {
169     my ($on, $what, $who) = @_;
170     my $name = $what->custom_name;
171    
172     return if $what->flag (cf::FLAG_UNPAID);
173    
174     if ($what->type == cf::MONEY) {
175     $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN);
176 root 1.8 $who->insert ($what);
177 root 1.1 return cf::override;
178     }
179    
180     if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) {
181     $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN);
182 root 1.8 $who->insert ($what);
183 root 1.1 return cf::override;
184     }
185    
186     my $orig_value = $what->value;
187     my $value = 0;
188    
189     if ($name =~ m/\S/) {
190     unless ($name =~ m/\d+\s*\S+/) {
191     $who->message ("The shopkeeper says: Sorry, I don't recognize '$name' as currency. Please name your item like '10 royalty' or '10 platinum 2 silver'", cf::NDI_BROWN);
192 root 1.8 $who->insert ($what);
193 root 1.1 return cf::override;
194     }
195    
196     while ($name =~ s/^\s*(\d+)\s*(\S+)//) {
197     if ($aliases{lc $2} or $unit{lc $2}) {
198     $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}});
199     } else {
200 root 1.8 $who->insert ($what);
201 root 1.1 $who->message ("The shopkeeper says: I don't know the currency '$2'", cf::NDI_BROWN);
202     return cf::override;
203     }
204     }
205     } else {
206     $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1);
207     }
208    
209     if ($value < 0) {
210 root 1.8 $who->insert ($what);
211 root 1.1 $who->message ("The shopkeeper says: You can't sell something for a negative value: $value", cf::NDI_BROWN);
212     return cf::override;
213     }
214    
215     my $fee = $value / 100; # 1% selling fee
216    
217     unless ($who->pay_amount ($fee)) {
218     $who->message (
219     "The shopkeeper says: You need " . cf::cost_string_from_value ($fee) . " to pay the 1% fee for this item",
220     cf::NDI_BROWN
221     );
222 root 1.8 $who->insert ($what);
223 root 1.1 return cf::override;
224     } else {
225     $who->message (
226     "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item",
227     cf::NDI_BROWN
228     );
229     }
230    
231     $what->value ($value);
232     my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / ($what->nrof || 1);
233    
234     my $fact = 0;
235     if ($cost) {
236     $fact = $value / $cost;
237     $what->value (cf::ceil ($value * $fact));
238     }
239    
240     # warn "END VALUE: $value * $fact => " . $what->value . "\n";
241    
242     # my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / $what->nrof;
243     # warn "COSTS NOW: $cost\n";
244    
245     $who->message (
246     "The shopkeeper says: Ok, I marked "
247     . ($what->nrof || 1) . " " . $what->name . " to be sold for at least "
248     . cf::cost_string_from_value ($value)
249     . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN
250     );
251    
252     $what->set_ob_key_value (ext_reseller_seller => $who->name);
253     $what->set_ob_key_value (ext_reseller_orig_value => $orig_value);
254     # warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n";
255     $what->custom_name ($what->name . " (by " . $who->name . ")");
256     $what->flag (cf::FLAG_UNPAID, 1);
257     $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y);
258    
259 elmex 1.2 audit_log ($who, 'sells', ob2info ($what));
260    
261 root 1.1 cf::override;
262     },
263     ;