Skip to content

Instantly share code, notes, and snippets.

@kiang
Last active April 25, 2022 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiang/0fc6f4648457e567bc599752056a4207 to your computer and use it in GitHub Desktop.
Save kiang/0fc6f4648457e567bc599752056a4207 to your computer and use it in GitHub Desktop.
get random gift
<?php
$fh = fopen(__DIR__ . '/gift.csv', 'r');
fgetcsv($fh, 2048);
fgetcsv($fh, 2048);
$pool = [];
$count = 0;
while ($line = fgetcsv($fh, 2048)) {
if (!empty($line[0])) {
$pool[$line[0]] = $line[1];
$count += $line[1];
}
}
$fh = fopen(__DIR__ . '/skip.csv', 'r');
$skip1 = [];
fgetcsv($fh, 2048);
fgetcsv($fh, 2048);
while ($line = fgetcsv($fh, 2048)) {
$skip1[$line[2]] = true;
}
$fh = fopen(__DIR__ . '/skip2.csv', 'r');
fgetcsv($fh, 2048);
fgetcsv($fh, 2048);
while ($line = fgetcsv($fh, 2048)) {
$skip1[$line[2]] = true;
}
$fh = fopen(__DIR__ . '/full.csv', 'r');
$count = 0;
$candidates = [];
fgetcsv($fh, 2048);
fgetcsv($fh, 2048);
while ($line = fgetcsv($fh, 2048)) {
if (!isset($skip1[$line[3]])) {
$candidates[] = [
$line[1],
$line[3]
];
}
}
shuffle($candidates);
$oFh = fopen(__DIR__ . '/result.csv', 'w');
fputcsv($oFh, ['gift', 'name', 'comment_id']);
foreach($candidates AS $candidate) {
$giftGet = false;
foreach($pool AS $k => $v) {
if(false === $giftGet && $v > 0) {
$giftGet = true;
$candidate[] = $k;
$pool[$k] -= 1;
echo "恭喜 {$candidate[0]} 獲得 {$k}\n";
sleep(1);
fputcsv($oFh, [$k, $candidate[0], $candidate[1]]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment