factory_processor.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. use scope_trace;
  12. class factory_processor implements IProcessor
  13. {
  14. private $mFactory;
  15. private $mRoomClients;
  16. private $mLastBuilding;
  17. public function __construct()
  18. {
  19. $this->mFactory = new factory();
  20. $this->mRoomClients = [];
  21. $this->mLastBuilding = 0;
  22. $this->init();
  23. }
  24. private function init()
  25. {
  26. global $config;
  27. $room_addrs = $config['room_factory']['rooms_addr'];
  28. foreach ($room_addrs as $addr) {
  29. $room_client = new room_client($addr['host'],$addr['port']);
  30. $this->mRoomClients[] = $room_client;
  31. }
  32. }
  33. public function onRequest($bufid, $body)
  34. {
  35. new scope_trace(__METHOD__);
  36. $input = json_decode($body,true);
  37. if($input == false) {
  38. return false;
  39. }
  40. $act = $input['act'];
  41. if(!empty($act) && $act == proto_type::act_factory) {
  42. $ret = $this->onFactory($input);
  43. factory_server::instance()->write($bufid,$ret);
  44. return true;
  45. }
  46. else {
  47. return false;
  48. }
  49. }
  50. public function onClose($bufid)
  51. {
  52. }
  53. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. private function onFactory($input)
  55. {
  56. $op = $input['op'];
  57. if($op == 'create')
  58. {
  59. $ret = $this->mFactory->create($input);
  60. if($ret != false)
  61. {
  62. $invite = $input['invite'];
  63. if($invite)
  64. {
  65. $roomid = $ret['room'];
  66. $inviter = $ret['creator'];
  67. if($roomid <= 0 || $inviter <= 0) {
  68. return $this->error(errcode::ErrRoomCreate);
  69. }
  70. $result = $this->invite($roomid,$inviter,[$inviter]);
  71. if($result != false) {
  72. $ret = array_merge($ret,$result);
  73. return $this->success($ret);
  74. } else {
  75. return $this->error(errcode::ErrRoomInvite);
  76. }
  77. }
  78. else {
  79. return $this->success($ret);
  80. }
  81. }
  82. return $this->error(errcode::ErrRoomCreate);
  83. }
  84. elseif($op == 'invite')
  85. {
  86. $roomid = intval($input['room']);
  87. $inviter = intval($input['inviter']);
  88. $invitees = $input['invitees'];
  89. if($roomid <= 0 || $inviter <= 0 || empty($invitees)) {
  90. return $this->error(errcode::ErrRoomInvite);
  91. }
  92. $ret = $this->invite($roomid,$inviter,$invitees);
  93. if($ret != false) {
  94. return $this->success($ret);
  95. } else {
  96. return $this->error(errcode::ErrRoomInvite);
  97. }
  98. }
  99. elseif($op == 'leave')
  100. {
  101. $roomid = intval($input['room']);
  102. $user = intval($input['user']);
  103. if($roomid <= 0 || $user <= 0) {
  104. return $this->error(errcode::ErrRoomInvite);
  105. }
  106. $ret = $this->leave($roomid,$user);
  107. if($ret != false) {
  108. return $this->success($ret);
  109. } else {
  110. return $this->error(errcode::ErrRoomInvite);
  111. }
  112. }
  113. else {
  114. return $this->error(errcode::ErrRoomFactoryOp);
  115. }
  116. }
  117. private function invite($roomid, $inviter,$invitees)
  118. {
  119. foreach ($this->mRoomClients as $client)
  120. {
  121. if($client->contain_room($roomid))
  122. {
  123. $ret = $client->invite($roomid,$inviter,$invitees);
  124. if($ret != false) {
  125. return $ret;
  126. }
  127. }
  128. }
  129. $client = $this->room_client();
  130. if($client != false)
  131. {
  132. $ret = $client->invite($roomid,$inviter,$invitees);
  133. if($ret != false) {
  134. $client->add_room($roomid);
  135. }
  136. }
  137. return $ret;
  138. }
  139. private function leave($roomid, $user)
  140. {
  141. foreach ($this->mRoomClients as $client)
  142. {
  143. if($client->contain_room($roomid))
  144. {
  145. $ret = $client->leave($roomid,$user);
  146. if($ret != false) {
  147. return $ret;
  148. }
  149. }
  150. }
  151. $client = $this->room_client();
  152. if($client != false) {
  153. return $client->leave($roomid,$user);
  154. } else {
  155. return false;
  156. }
  157. }
  158. private function room_client()
  159. {
  160. $count = count($this->mRoomClients);
  161. if($count <= 0) return false;
  162. $pos = $this->mLastBuilding;
  163. if($pos < $count) {
  164. $this->mLastBuilding++;
  165. return $this->mRoomClients[$pos];
  166. } else {
  167. $this->mLastBuilding = 0;
  168. return $this->mRoomClients[0];
  169. }
  170. }
  171. private function success($datas) {
  172. $code = errcode::Success;
  173. $data['code'] = $code;
  174. $data['message'] = errcode::msg($code);
  175. $data['data'] = $datas;
  176. return json_encode($data);
  177. }
  178. private function error($code,$message = '')
  179. {
  180. if (empty($message)) {
  181. $message = errcode::msg($code);
  182. }
  183. $data = array();
  184. $data['code'] = $code;
  185. $data['message'] = $message;
  186. $data['datas'] = null;
  187. return json_encode($data);
  188. }
  189. }