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.10 by elmex, Mon Dec 24 16:41:55 2007 UTC vs.
Revision 1.16 by elmex, Thu Aug 20 18:27:21 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines