BoardBot.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace tw\lib\robot;
  3. use app\admin\model\store\StoreProduct;
  4. use think\facade\Log;
  5. /**
  6. * ## 需求
  7. * 前端排行榜功能,爲避免出現無人上榜或榜單比較蕭條的情況,需要定制一批機器人上班
  8. *
  9. * ## 排行榜機制
  10. * 定時執行:對表執行訂單統計排序查詢,結果就作爲榜單數據,存入 redis。前端 API 直接從 redis 取數據。
  11. *
  12. * ## 機器人機制
  13. * 機器人個人信息不入庫。按自己的定時器 定時隨機指定部分機器人“下單“並大概率盈利(所謂下單並不入庫)。
  14. * 根據下單情況,所有機器人維護一個單獨的”內部排行榜“。
  15. *
  16. * 隨機性:如果榜單需要 20 名,機器人則最少需要 20 x 3 = 60 名,才不會在榜單上全是老面孔,同時又有老面孔。
  17. * 訂單金額根據實際商品金額生成。
  18. *
  19. * ## 最終排行榜
  20. * 定時執行訂單統計時,同時取出機器人排行榜,混合後作爲最終結果 存入 redis。前端 API 不受影響。
  21. */
  22. class BoardBot
  23. {
  24. protected $win_rate = 80; // 盈利概率
  25. /**
  26. * 機器人下單,需要定時執行
  27. *
  28. * 結果存入機器人自己的排行榜
  29. */
  30. public function make_orders()
  31. {
  32. // make choice which activities randomly
  33. $c = config('activity.clearance_cate_id');
  34. $l = config('activity.lucky_cate_id');
  35. $a = config('activity.lucky_a_cate_id');
  36. $b = config('activity.lucky_b_cate_id');
  37. $acts = [
  38. $c, $l, $a, $b,
  39. ];
  40. $rates = [
  41. $c => config('activity.clearrance_rate'),
  42. $l => config('activity.luck_rate'),
  43. $a => config('activity.luck_a_rate'),
  44. $b => config('activity.luck_b_rate'),
  45. ];
  46. // get activities products' prices.
  47. $act_idx = tw_rand(0, count($acts));
  48. echo "act_idx = $act_idx" . PHP_EOL;
  49. $act = $acts[$act_idx];
  50. $rate = $rates[$act];
  51. // all prices
  52. $prices = StoreProduct::where('is_del', 0)
  53. ->where('is_show', 1)
  54. ->where('cate_id', $act)
  55. ->field('price,cost')
  56. ->select()
  57. ->toArray();
  58. // make choice whome participates this order- time.
  59. $robot_ids = array_keys($this->robots);
  60. $selected = [];
  61. $total = count($robot_ids);
  62. if ($total < 10) {
  63. $msg = "robots num is not enough. num=$total";
  64. Log::error($msg);
  65. echo $msg . PHP_EOL;
  66. return;
  67. }
  68. $selnum = tw_rand($total / 3, $total - 5);
  69. for ($i = 0; $i < $selnum; $i++) {
  70. $idx = tw_rand(0, count($robot_ids));
  71. $selected[] = $robot_ids[$idx];
  72. // each win by rate.
  73. $price = tw_rand(0, count($prices));
  74. $win = tw_rand(0, 100) < 80;
  75. if ($win) {
  76. $selected[$robot_ids[$idx]] = $price * $rate;
  77. }
  78. array_splice($robot_ids, $idx, 1);
  79. }
  80. }
  81. /**
  82. * 獲取機器人的榜單
  83. *
  84. * @first: select first-N as the on-board
  85. *
  86. */
  87. public function get_board($first = 16)
  88. {
  89. }
  90. }