| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace tw\lib\robot;
- use app\admin\model\store\StoreProduct;
- use think\facade\Log;
- /**
- * ## 需求
- * 前端排行榜功能,爲避免出現無人上榜或榜單比較蕭條的情況,需要定制一批機器人上班
- *
- * ## 排行榜機制
- * 定時執行:對表執行訂單統計排序查詢,結果就作爲榜單數據,存入 redis。前端 API 直接從 redis 取數據。
- *
- * ## 機器人機制
- * 機器人個人信息不入庫。按自己的定時器 定時隨機指定部分機器人“下單“並大概率盈利(所謂下單並不入庫)。
- * 根據下單情況,所有機器人維護一個單獨的”內部排行榜“。
- *
- * 隨機性:如果榜單需要 20 名,機器人則最少需要 20 x 3 = 60 名,才不會在榜單上全是老面孔,同時又有老面孔。
- * 訂單金額根據實際商品金額生成。
- *
- * ## 最終排行榜
- * 定時執行訂單統計時,同時取出機器人排行榜,混合後作爲最終結果 存入 redis。前端 API 不受影響。
- */
- class BoardBot
- {
- protected $win_rate = 80; // 盈利概率
- /**
- * 機器人下單,需要定時執行
- *
- * 結果存入機器人自己的排行榜
- */
- public function make_orders()
- {
- // make choice which activities randomly
- $c = config('activity.clearance_cate_id');
- $l = config('activity.lucky_cate_id');
- $a = config('activity.lucky_a_cate_id');
- $b = config('activity.lucky_b_cate_id');
- $acts = [
- $c, $l, $a, $b,
- ];
- $rates = [
- $c => config('activity.clearrance_rate'),
- $l => config('activity.luck_rate'),
- $a => config('activity.luck_a_rate'),
- $b => config('activity.luck_b_rate'),
- ];
- // get activities products' prices.
- $act_idx = tw_rand(0, count($acts));
- echo "act_idx = $act_idx" . PHP_EOL;
- $act = $acts[$act_idx];
- $rate = $rates[$act];
- // all prices
- $prices = StoreProduct::where('is_del', 0)
- ->where('is_show', 1)
- ->where('cate_id', $act)
- ->field('price,cost')
- ->select()
- ->toArray();
- // make choice whome participates this order- time.
- $robot_ids = array_keys($this->robots);
- $selected = [];
- $total = count($robot_ids);
- if ($total < 10) {
- $msg = "robots num is not enough. num=$total";
- Log::error($msg);
- echo $msg . PHP_EOL;
- return;
- }
- $selnum = tw_rand($total / 3, $total - 5);
- for ($i = 0; $i < $selnum; $i++) {
- $idx = tw_rand(0, count($robot_ids));
- $selected[] = $robot_ids[$idx];
- // each win by rate.
- $price = tw_rand(0, count($prices));
- $win = tw_rand(0, 100) < 80;
- if ($win) {
- $selected[$robot_ids[$idx]] = $price * $rate;
- }
- array_splice($robot_ids, $idx, 1);
- }
- }
- /**
- * 獲取機器人的榜單
- *
- * @first: select first-N as the on-board
- *
- */
- public function get_board($first = 16)
- {
- }
- }
|