123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/12/14
- * Time: 下午12:07
- */
- namespace room;
- use search\IProcessor;
- use errcode;
- class factory_processor implements IProcessor
- {
- private $mFactory;
- private $mRoomClients;
- private $mLastBuilding;
- public function __construct()
- {
- $this->mFactory = new factory();
- $this->mRoomClients = [];
- $this->mLastBuilding = 0;
- $this->init();
- }
- private function init()
- {
- global $config;
- $room_addrs = $config['room_factory']['rooms_addr'];
- foreach ($room_addrs as $addr) {
- $room_client = new room_client($addr['host'],$addr['port']);
- $this->mRoomClients[] = $room_client;
- }
- }
- public function onRequest($bufid, $body)
- {
- $input = json_decode($body,true);
- if($input == false) {
- return false;
- }
- $act = $input['act'];
- if(!empty($act) && $act == proto_type::act_factory) {
- $ret = $this->onFactory($input);
- factory_server::instance()->write($bufid,$ret);
- return true;
- }
- else {
- return false;
- }
- }
- public function onClose($bufid)
- {
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private function onFactory($input)
- {
- $op = $input['op'];
- if($op == 'create')
- {
- $ret = $this->mFactory->create($input);
- if($ret != false)
- {
- $roomid = $ret['room'];
- $userid = $ret['creator'];
- if($roomid <= 0 || $userid <= 0) {
- return $this->error(errcode::ErrRoomCreate);
- }
- $ret = $this->invite($roomid,$userid);
- if($ret != false) {
- return $this->success($ret);
- } else {
- return $this->error(errcode::ErrRoomInvite);
- }
- }
- return $this->error(errcode::ErrRoomCreate);
- }
- elseif($op == 'invite')
- {
- $roomid = intval($input['room']);
- $userid = intval($input['user']);
- if($roomid <= 0 || $userid <= 0) {
- return $this->error(errcode::ErrRoomInvite);
- }
- $ret = $this->invite($roomid,$userid);
- if($ret != false) {
- return $this->success($ret);
- } else {
- return $this->error(errcode::ErrRoomInvite);
- }
- }
- else {
- return $this->error(errcode::ErrRoomFactoryOp);
- }
- }
- private function invite($roomid,$user)
- {
- foreach ($this->mRoomClients as $client)
- {
- if($client->contain_room($roomid))
- {
- $ret = $client->invite($roomid,$user);
- if($ret != false) {
- return $ret;
- }
- }
- }
- $client = $this->room_client();
- if($client != false)
- {
- $ret = $client->invite($roomid,$user);
- if($ret != false) {
- $client->add_room($roomid);
- }
- }
- return $ret;
- }
- private function room_client()
- {
- $count = count($this->mRoomClients);
- if($count <= 0) return false;
- $pos = $this->mLastBuilding;
- if($pos < $count) {
- $this->mLastBuilding++;
- return $this->mRoomClients[$pos];
- } else {
- $this->mLastBuilding = 0;
- return $this->mRoomClients[0];
- }
- }
- private function success($datas) {
- $code = errcode::Success;
- $data['code'] = $code;
- $data['message'] = errcode::msg($code);
- $data['data'] = $datas;
- return json_encode($data);
- }
- private function error($code,$message = '')
- {
- if (empty($message)) {
- $message = errcode::msg($code);
- }
- $data = array();
- $data['code'] = $code;
- $data['message'] = $message;
- $data['datas'] = null;
- return json_encode($data);
- }
- }
|