ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
(Generate patch)

Comparing deliantra/server/ext/reseller.ext (file contents):
Revision 1.1 by root, Fri Dec 15 19:29:18 2006 UTC vs.
Revision 1.12 by root, Sat May 3 11:14:50 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines