factory_processor.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/14
  6. * Time: 下午12:07
  7. */
  8. namespace room;
  9. use search\IProcessor;
  10. use errcode;
  11. class factory_processor implements IProcessor
  12. {
  13. private $mFactory;
  14. private $mRoomClients;
  15. private $mLastBuilding;
  16. public function __construct()
  17. {
  18. $this->mFactory = new factory();
  19. $this->mRoomClients = [];
  20. $this->mLastBuilding = 0;
  21. $this->init();
  22. }
  23. private function init()
  24. {
  25. global $config;
  26. $room_addrs = $config['room_factory']['rooms_addr'];
  27. foreach ($room_addrs as $addr) {
  28. $room_client = new room_client($addr['host'],$addr['port']);
  29. $this->mRoomClients[] = $room_client;
  30. }
  31. }
  32. public function onRequest($bufid, $body)
  33. {
  34. $input = json_decode($body,true);
  35. if($input == false) {
  36. return false;
  37. }
  38. $act = $input['act'];
  39. if(!empty($act) && $act == proto_type::act_factory) {
  40. $ret = $this->onFactory($input);
  41. factory_server::instance()->write($bufid,$ret);
  42. return true;
  43. }
  44. else {
  45. return false;
  46. }
  47. }
  48. public function onClose($bufid)
  49. {
  50. }
  51. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. private function onFactory($input)
  53. {
  54. $op = $input['op'];
  55. if($op == 'create')
  56. {
  57. $ret = $this->mFactory->create($input);
  58. if($ret != false)
  59. {
  60. $roomid = $ret['room'];
  61. $userid = $ret['creator'];
  62. if($roomid <= 0 || $userid <= 0) {
  63. return $this->error(errcode::ErrRoomCreate);
  64. }
  65. $ret = $this->invite($roomid,$userid);
  66. if($ret != false) {
  67. return $this->success($ret);
  68. } else {
  69. return $this->error(errcode::ErrRoomInvite);
  70. }
  71. }
  72. return $this->error(errcode::ErrRoomCreate);
  73. }
  74. elseif($op == 'invite')
  75. {
  76. $roomid = intval($input['room']);
  77. $userid = intval($input['user']);
  78. if($roomid <= 0 || $userid <= 0) {
  79. return $this->error(errcode::ErrRoomInvite);
  80. }
  81. $ret = $this->invite($roomid,$userid);
  82. if($ret != false) {
  83. return $this->success($ret);
  84. } else {
  85. return $this->error(errcode::ErrRoomInvite);
  86. }
  87. }
  88. else {
  89. return $this->error(errcode::ErrRoomFactoryOp);
  90. }
  91. }
  92. private function invite($roomid,$user)
  93. {
  94. foreach ($this->mRoomClients as $client)
  95. {
  96. if($client->contain_room($roomid))
  97. {
  98. $ret = $client->invite($roomid,$user);
  99. if($ret != false) {
  100. return $ret;
  101. }
  102. }
  103. }
  104. $client = $this->room_client();
  105. if($client != false)
  106. {
  107. $ret = $client->invite($roomid,$user);
  108. if($ret != false) {
  109. $client->add_room($roomid);
  110. }
  111. }
  112. return $ret;
  113. }
  114. private function room_client()
  115. {
  116. $count = count($this->mRoomClients);
  117. if($count <= 0) return false;
  118. $pos = $this->mLastBuilding;
  119. if($pos < $count) {
  120. $this->mLastBuilding++;
  121. return $this->mRoomClients[$pos];
  122. } else {
  123. $this->mLastBuilding = 0;
  124. return $this->mRoomClients[0];
  125. }
  126. }
  127. private function success($datas) {
  128. $code = errcode::Success;
  129. $data['code'] = $code;
  130. $data['message'] = errcode::msg($code);
  131. $data['data'] = $datas;
  132. return json_encode($data);
  133. }
  134. private function error($code,$message = '')
  135. {
  136. if (empty($message)) {
  137. $message = errcode::msg($code);
  138. }
  139. $data = array();
  140. $data['code'] = $code;
  141. $data['message'] = $message;
  142. $data['datas'] = null;
  143. return json_encode($data);
  144. }
  145. }