Created
October 31, 2013 12:23
-
-
Save libitte/7248805 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Perl6::Say; | |
use Data::Dumper; | |
local $Data::Dumper::Indent = 1; | |
local $Data::Dumper::Terse = 1; | |
use Digest::MD5 qw(md5_hex); | |
my $KEY = 'bra-bra'; | |
my $KEY_NUM = 10; | |
my @box_items = ( | |
{ id => 1, box_id => 1, item_id => 100, class => 0, weight => 3 }, | |
{ id => 2, box_id => 1, item_id => 110, class => 0, weight => 3 }, | |
{ id => 3, box_id => 1, item_id => 120, class => 0, weight => 3 }, | |
{ id => 4, box_id => 1, item_id => 130, class => 0, weight => 3 }, | |
{ id => 5, box_id => 1, item_id => 200, class => 1, weight => 1 }, | |
); | |
my @lottery_box_items; | |
for my $box_item (@box_items) { | |
my $multiply_num = $box_item->{weight}; | |
for (1 .. $multiply_num) { | |
push(@lottery_box_items, $box_item); | |
} | |
} | |
say Dumper \@lottery_box_items; | |
my $user_id = 10_000; | |
my $n = @lottery_box_items; | |
my $i = 0; | |
for my $box_item (@lottery_box_items) { | |
$box_item->{sort_key} = substr(md5_hex(join(":", $user_id+$i, $KEY)), 0, $KEY_NUM); | |
$i++; | |
} | |
say Dumper [sort { $a->{sort_key} cmp $b->{sort_key} } @lottery_box_items]; | |
#use List::Util qw( shuffle ); | |
#say Dumper [ shuffle (@lottery_box_items) ]; | |
__END__ | |
[ | |
{ | |
'weight' => 3, | |
'box_id' => 1, | |
'class' => 0, | |
'id' => 1, | |
'item_id' => 100 | |
}, | |
$VAR1->[0], | |
$VAR1->[0], | |
{ | |
'weight' => 3, | |
'box_id' => 1, | |
'class' => 0, | |
'id' => 2, | |
'item_id' => 110 | |
}, | |
$VAR1->[3], | |
$VAR1->[3], | |
{ | |
'weight' => 3, | |
'box_id' => 1, | |
'class' => 0, | |
'id' => 3, | |
'item_id' => 120 | |
}, | |
$VAR1->[6], | |
$VAR1->[6], | |
{ | |
'weight' => 3, | |
'box_id' => 1, | |
'class' => 0, | |
'id' => 4, | |
'item_id' => 130 | |
}, | |
$VAR1->[9], | |
$VAR1->[9], | |
{ | |
'weight' => 1, | |
'box_id' => 1, | |
'class' => 1, | |
'id' => 5, | |
'item_id' => 200 | |
} | |
] | |
[ | |
{ | |
'weight' => 1, | |
'box_id' => 1, | |
'class' => 1, | |
'id' => 5, | |
'sort_key' => '3102311ebb', | |
'item_id' => 200 | |
}, | |
{ | |
'weight' => 3, | |
'box_id' => 1, | |
'class' => 0, | |
'id' => 4, | |
'sort_key' => '87f5a13348', | |
'item_id' => 130 | |
}, | |
$VAR1->[1], | |
$VAR1->[1], | |
{ | |
'weight' => 3, | |
'box_id' => 1, | |
'class' => 0, | |
'id' => 1, | |
'sort_key' => '8d49b76d31', | |
'item_id' => 100 | |
}, | |
$VAR1->[4], | |
$VAR1->[4], | |
{ | |
'weight' => 3, | |
'box_id' => 1, | |
'class' => 0, | |
'id' => 2, | |
'sort_key' => '8fd0d297c2', | |
'item_id' => 110 | |
}, | |
$VAR1->[7], | |
$VAR1->[7], | |
{ | |
'weight' => 3, | |
'box_id' => 1, | |
'class' => 0, | |
'id' => 3, | |
'sort_key' => 'e8ae5c9483', | |
'item_id' => 120 | |
}, | |
$VAR1->[10], | |
$VAR1->[10] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment