chctl.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace refill;
  3. class chctl
  4. {
  5. protected $mSpeedtable;
  6. static $cache_names = [
  7. ['quality'=> 1,'name' => 'channel-ctl-oil-common-limit'],
  8. ['quality'=> 2,'name' => 'channel-ctl-oil-fast-limit'],
  9. ['quality'=> 1,'name' => 'channel-ctl-phone-common-limit'],
  10. ['quality'=> 2,'name' => 'channel-ctl-phone-fast-limit']
  11. ];
  12. public function __construct()
  13. {
  14. }
  15. public function load()
  16. {
  17. foreach (self::$cache_names as $cache)
  18. {
  19. $quality = $cache['quality'];
  20. $cache_name = $cache['name'];
  21. $data = rcache($cache_name,"provider-");
  22. $data = unserialize($data['data']);
  23. $cfgs = $data ?? [];
  24. foreach ($cfgs as $items)
  25. {
  26. foreach ($items as $item) {
  27. $name = $item['name'];
  28. $amount = $item['amount'];
  29. $card_type = $item['type'];
  30. $opened = $item['opened'] == 1 ? true : false;
  31. $sort = $item['sort'];
  32. $speed = $item['speed'];
  33. $key = $this->prefix($name,$amount,$card_type,$quality);
  34. $this->mSpeedtable[$key] = new ctl_item($name,$card_type,$amount,$speed,$sort,0,$opened);
  35. }
  36. }
  37. }
  38. }
  39. public function match($names,int $spec, int $card_type,int $quality)
  40. {
  41. $ctl_items = [];
  42. foreach ($names as $name) {
  43. $key = $this->prefix($name,$spec,$card_type,$quality);
  44. $ctl_items[] = $this->mSpeedtable[$key];
  45. }
  46. $usable_items = [];
  47. foreach ($ctl_items as $item) {
  48. if($item->opened()) {
  49. $usable_items[] = $item;
  50. }
  51. }
  52. $ascending = function ($l, $r) {
  53. $lproity = $l->priority();
  54. $rproity = $r->priority();
  55. $lover = $l->speed_overload() ? 1 : 0;
  56. $rover = $r->speed_overload() ? 1 : 0;
  57. if($lover == $rover) {
  58. return $lproity < $rproity ? -1 : 1;
  59. }
  60. else {
  61. return $lover < $rover ? -1 : 1;
  62. }
  63. };
  64. usort($usable_items, $ascending);
  65. $result = [];
  66. foreach ($usable_items as $item) {
  67. $result[] = $item->name();
  68. }
  69. return $result;
  70. }
  71. private function prefix($name,$spec,$card_type,$quality)
  72. {
  73. return "{$name}-{$spec}-{$card_type}-{$quality}";
  74. }
  75. }