chctl.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $this->mSpeedtable = [];
  15. }
  16. public function load()
  17. {
  18. $this->mSpeedtable = [];
  19. foreach (self::$cache_names as $cache)
  20. {
  21. $quality = $cache['quality'];
  22. $cache_name = $cache['name'];
  23. $data = rcache($cache_name,"provider-");
  24. $data = unserialize($data['data']);
  25. $cfgs = empty($data) ? [] : $data;
  26. foreach ($cfgs as $items)
  27. {
  28. foreach ($items as $item) {
  29. $name = $item['name'];
  30. $amount = $item['amount'];
  31. $card_type = $item['type'];
  32. $opened = $item['opened'] == 1 ? true : false;
  33. $sort = $item['sort'];
  34. $speed = $item['speed'];
  35. $key = $this->prefix($name,$amount,$card_type,$quality);
  36. $this->mSpeedtable[$key] = new ctl_item($name,$card_type,$amount,$speed,$sort,0,$opened);
  37. }
  38. }
  39. }
  40. }
  41. public function match($names,int $spec, int $card_type,int $quality)
  42. {
  43. $ctl_items = [];
  44. foreach ($names as $name)
  45. {
  46. $key = $this->prefix($name,$spec,$card_type,$quality);
  47. if(array_key_exists($key,$this->mSpeedtable)) {
  48. $ctl_items[] = $this->mSpeedtable[$key];
  49. }
  50. }
  51. if(empty($ctl_items)) {
  52. return $names;
  53. }
  54. $usable_items = [];
  55. foreach ($ctl_items as $item) {
  56. if($item->opened()) {
  57. $usable_items[] = $item;
  58. }
  59. }
  60. $ascending = function ($l, $r) {
  61. $lproity = $l->priority();
  62. $rproity = $r->priority();
  63. $lover = $l->speed_overload() ? 1 : 0;
  64. $rover = $r->speed_overload() ? 1 : 0;
  65. if($lover == $rover) {
  66. return $lproity < $rproity ? -1 : 1;
  67. }
  68. else {
  69. return $lover < $rover ? -1 : 1;
  70. }
  71. };
  72. usort($usable_items, $ascending);
  73. $result = [];
  74. foreach ($usable_items as $item) {
  75. $result[] = $item->name();
  76. }
  77. return $result;
  78. }
  79. private function prefix($name,$spec,$card_type,$quality)
  80. {
  81. return "{$name}-{$spec}-{$card_type}-{$quality}";
  82. }
  83. }