chctl.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace refill;
  3. use Log;
  4. use mtopcard;
  5. class chctl
  6. {
  7. protected $mSpeedtable;
  8. static $cache_names = [
  9. ['quality'=> 1,'name' => 'channel-ctl-oil-common-limit'],
  10. ['quality'=> 2,'name' => 'channel-ctl-oil-fast-limit'],
  11. ['quality'=> 1,'name' => 'channel-ctl-phone-common-limit'],
  12. ['quality'=> 2,'name' => 'channel-ctl-phone-fast-limit'],
  13. ['quality'=> 3,'name' => 'channel-ctl-phone-card-limit'],
  14. ['quality'=> 4,'name' => 'channel-ctl-phone-third-limit'],
  15. ['quality'=> 5,'name' => 'channel-ctl-phone-slow-limit'], //24 hour
  16. ['quality'=> 6,'name' => 'channel-ctl-phone-slow6-limit'],//6 hour
  17. ['quality'=> 7,'name' => 'channel-ctl-phone-slow2-limit'] //2 hour
  18. ];
  19. public function __construct()
  20. {
  21. $this->mSpeedtable = [];
  22. }
  23. public function load()
  24. {
  25. $this->mSpeedtable = [];
  26. foreach (self::$cache_names as $cache)
  27. {
  28. $quality = $cache['quality'];
  29. $cache_name = $cache['name'];
  30. $data = rcache($cache_name,"provider-");
  31. $data = unserialize($data['data']);
  32. $cfgs = empty($data) ? [] : $data;
  33. foreach ($cfgs as $items)
  34. {
  35. foreach ($items as $item) {
  36. $name = $item['name'];
  37. $amount = $item['amount'];
  38. $card_type = $item['type'];
  39. $opened = $item['opened'] == 1;
  40. $sort = $item['sort'];
  41. $speed = $item['speed'];
  42. $key = $this->prefix($name,$amount,$card_type,$quality);
  43. // Log::record("Load: key={$key} quality={$quality} name={$name} amount={$amount} type={$card_type} opened={$opened} sort={$sort} speed={$speed}",Log::DEBUG);
  44. $this->mSpeedtable[$key] = new ctl_item($name,$card_type,$amount,$speed,$sort,0,$opened,$quality);
  45. }
  46. }
  47. }
  48. }
  49. public function match($names,int $spec, int $card_type,int $quality)
  50. {
  51. if($card_type == mtopcard\ThirdRefillCard)
  52. {
  53. $result = [];
  54. foreach ($names as $name) {
  55. $result[$name] = false;
  56. }
  57. return $result;
  58. }
  59. $ctl_items = [];
  60. foreach ($names as $name)
  61. {
  62. $key = $this->prefix($name,$spec,$card_type,$quality);
  63. if(array_key_exists($key,$this->mSpeedtable)) {
  64. $ctl_items[] = $this->mSpeedtable[$key];
  65. }
  66. }
  67. //去掉已经关闭通道
  68. $usable_items = [];
  69. foreach ($ctl_items as $item)
  70. {
  71. if($item->opened()) {
  72. $usable_items[] = $item;
  73. }
  74. }
  75. //不过载的排在前面
  76. $ascending = function ($l, $r)
  77. {
  78. $lproity = $l->priority();
  79. $rproity = $r->priority();
  80. $lover = $l->speed_overload() ? 1 : 0;
  81. $rover = $r->speed_overload() ? 1 : 0;
  82. if($lover == $rover)
  83. {
  84. if($lover) {
  85. return $lproity > $rproity ? -1 : 1; //如果都过载保优先级高的
  86. }
  87. else {
  88. return $lproity < $rproity ? -1 : 1;
  89. }
  90. }
  91. else {
  92. return $lover < $rover ? -1 : 1;
  93. }
  94. };
  95. usort($usable_items, $ascending);
  96. $result = [];
  97. foreach ($usable_items as $item) {
  98. $name = $item->name();
  99. $result[$name] = $item->speed_overload();
  100. }
  101. return $result;
  102. }
  103. private function prefix($name,$spec,$card_type,$quality)
  104. {
  105. return "{$name}-{$spec}-{$card_type}-{$quality}";
  106. }
  107. }