public function main()
 {
     Debug::info("Start Reload server...\n");
     //        common\connection::sendToChannel(1, 'server reloading...');
     common\connection::reloadServ();
     common\connection::sendOne($this->fd, 'RELOADSERVER', 'server reload over.');
 }
 public static function sendToChannel($cmd, $data, $channel = 'ALL')
 {
     if (empty(self::$serv)) {
         return;
     }
     $sendData = array('e_no' => -1, 'e_msg' => '', 'cmd' => $cmd, 'data' => $data, 'broadcast' => true, 'channel' => $channel);
     $rs = json_encode($sendData);
     //$serv->task($data, $taskid);
     //如果你有两个task,那分别是0和1
     self::$serv->task($rs, 0);
     Debug::error("send data:{$rs}\ncmd[{$cmd}] To channel[{$channel}] over with task process.\n");
 }
Beispiel #3
0
 public function main()
 {
     //---------------------------------------------------
     echo "Debug test:\n";
     Debug::log("debug log\n");
     Debug::info("debug info\n");
     Debug::debug("debug debug\n");
     Debug::error("debug error\n");
     Debug::error('debug error', 'aa', 'bb', 'cc');
     //pdo.php ping() 就是在执行sql的时候,失败的话,检查一下状态码。进行重连
     //---------------------------------------------------
     echo "\n\nDB option:\n";
     $user = $this->testModel->getAll();
     echo "get userMode one record.\n";
     print_r($user[0]);
     //---------------------------------------------------
     //fd opt
     echo "\n\nfd and connection option:\n";
     $uid = 10000;
     $uinfo = $this->connection->get($uid);
     if ($uinfo) {
         echo "already exist user.\n";
         print_r($uinfo);
     } else {
         echo "new add user.\n";
         $this->connection->add($uid, $this->fd);
         $this->connection->addFd($this->fd, $uid);
         print_r($this->connection->get($uid));
     }
     common\connection::sendOne($this->fd, 1, 'test send me');
     echo "send me sucess.\n";
     common\connection::sendToChannel(1, 'test send all');
     echo "send all sucess.\n";
     //加入到room1
     $this->connection->addChannel($uid, 'ROOM1');
     common\connection::sendToChannel(1, 'test send ROOM1', 'ROOM1');
     echo "send all room1.\n";
     //---------------------------------------------------
     echo "\n\ncache option:\n";
     $userinfo = array('id' => $uid, 'name' => 'ansen', 'sex' => 2);
     $this->cache->set('c_uid_' . $uid, json_encode($userinfo));
     echo "cache set userinfo sucess.\n";
     echo "get cache userinfo.\n";
     print_r(json_decode($this->cache->get('c_uid_' . $uid), true));
     //---------------------------------------------------
     echo "\n\nrank option:\n";
     $this->rankCache->addRank($rankType = 'global', $key = 'global_u1', $score = 1000, $length = 5);
     $this->rankCache->addRank($rankType = 'global', $key = 'global_u2', $score = 2000, $length = 5);
     $this->rankCache->addRank($rankType = 'global', $key = 'global_u3', $score = 500, $length = 5);
     $this->rankCache->addRank($rankType = 'global', $key = 'global_u4', $score = 100, $length = 5);
     $this->rankCache->addRank($rankType = 'global', $key = 'global_u5', $score = 6000, $length = 5);
     echo "add rank u1 u2 u3 u4 u5 over.\n";
     echo "get rank1:\n";
     $list1 = $this->rankCache->getRank($rankType = 'global', $start = 0, $limit = 100, $score = true, $desc = 0);
     print_r($list1);
     echo "get rank by score:\n";
     $list2 = $this->rankCache->getRankByScore($rankType = 'global', $start = 200, $end = 2000, $scores = true, $offset = 0, $count = 0);
     print_r($list2);
     echo "get rank by key(global_u3): 第";
     //排名第几
     echo $this->rankCache->getRankByKey($rankType = 'global', $key = 'global_u3', $desc = 0) . "位\n\n\n";
     //---------------------------------------------------
     echo "\n\ncd option:\n";
     $this->cdModel->addCd(10000, 'ansen');
     $this->cdModel->updCd(10000, 'ansen', array('cdCount' => 1, 'cdTimeStamp' => 234234));
     $cd = $this->cdModel->getCd(10000, 'ansen');
     print_r($cd);
     echo "get user keep login days:";
     echo $this->cdModel->getKeepLoginDays(10000);
     echo " days\n";
     //---------------------------------------------------
     //        echo "\n\nreids store struct option:\n";
     //        $username = '******';
     //        $password = '******';
     //
     //        $userid = $this->cache->increment("global:nextUserId");
     //        $this->cache->set("username:$username:id", $userid);
     //        $this->cache->set("uid:$userid:username", $username);
     //        $this->cache->set("uid:$userid:password", $password);
     //        //设置此用户密钥
     //        $authcookie = md5(time());
     //        $this->cache->set("uid:$userid:auth", md5(time()));
     //        $this->cache->set("auth:$authcookie", $userid);
     return 'finish action.';
 }
Beispiel #4
0
 /**
  * @param $rootPath
  * @param bool $run
  * @param null $configPath
  * @return \ZPHP\Server\IServer
  * @throws \Exception
  */
 public static function run($rootPath, $run = true, $configPath = null)
 {
     if (!defined('DS')) {
         define('DS', DIRECTORY_SEPARATOR);
     }
     self::$zPath = \dirname(__DIR__);
     self::setRootPath($rootPath);
     if (empty($configPath)) {
         if (!empty($_SERVER['HTTP_HOST'])) {
             $configPath = \str_replace(':', '_', $_SERVER['HTTP_HOST']);
         } elseif (!empty($_SERVER['argv'][1])) {
             $configPath = $_SERVER['argv'][1];
         }
     }
     if (!empty($configPath)) {
         self::setConfigPath($configPath);
     }
     \spl_autoload_register(__CLASS__ . '::autoLoader');
     Config::load(self::getConfigPath());
     self::$libPath = Config::get('lib_path', self::$zPath . DS . 'lib');
     if ($run && Config::getField('project', 'debug_mode', 0)) {
         Debug::start();
     }
     $appPath = Config::get('app_path', self::$appPath);
     self::setAppPath($appPath);
     $eh = Config::getField('project', 'exception_handler', __CLASS__ . '::exceptionHandler');
     \set_exception_handler($eh);
     \register_shutdown_function(Config::getField('project', 'fatal_handler', __CLASS__ . '::fatalHandler'));
     $timeZone = Config::get('time_zone', 'Asia/Shanghai');
     \date_default_timezone_set($timeZone);
     $serverMode = Config::get('server_mode', 'Http');
     $service = Server\Factory::getInstance($serverMode);
     if ($run) {
         $service->run();
     } else {
         return $service;
     }
     if ($run && Config::getField('project', 'debug_mode', 0)) {
         Debug::end();
     }
 }
 private function parsedataEof($data)
 {
     $dataEof = ZConfig::getField('socket', 'data_eof');
     if (($pos = strpos($data, $dataEof)) !== FALSE) {
         $data = substr($data, 0, $pos);
         Debug::log("get data[parse eof]: {$data}\n");
     }
     return $data;
 }
Beispiel #6
0
 private static function getReward()
 {
     //        mt_srand(self::make_seed());
     $type = self::getNowPacketType();
     Debug::error("typeIn\n");
     $itemRate = ZConfig::get('itemrate');
     if ($type == 'typeIn') {
         //吃币阶段
         $upCount = 5000;
         $randItemRateval = mt_rand(1, $upCount);
     } else {
         //吐币阶段
         $upCount = 1250;
         $randItemRateval = mt_rand(1, $upCount);
     }
     if ($randItemRateval > 1000) {
         $getItem = $itemRate[$upCount];
         $positions = $getItem['position'];
         $randval = mt_rand(0, count($positions) - 1);
         $randval = $positions[$randval];
     } else {
         $itemRateKey = array_keys($itemRate);
         $index = self::getRewardIndex($itemRateKey, $randItemRateval);
         $getItem = $itemRate[$index];
         $positions = $getItem['position'];
         if (count($positions) == 1) {
             $randval = $positions[0];
         } else {
             $randval = mt_rand(0, count($positions) - 1);
             $randval = $positions[$randval];
         }
     }
     //todo remove it
     //        if ($randval == 9 || $randval == 21) {
     //            $randval = 22;
     //        }
     //        $randval = 9;
     $mapD = ZConfig::get('map');
     $ret = $mapD[$randval];
     if (isset($ret['goodluck1'])) {
         $reward = ZConfig::getField('reward', 'goodluck1');
         $rewardKey = array_keys($reward);
         $randV = rand(1, 1000);
         $rewardIndex = self::getRewardIndex($rewardKey, $randV);
         $getReward = $reward[$rewardIndex];
         $getReward['from'] = array('name' => 'goodluck1', 'position' => $randval);
     } elseif (isset($ret['goodluck2'])) {
         $reward = ZConfig::getField('reward', 'goodluck2');
         $rewardKey = array_keys($reward);
         $randV = rand(1, 1000);
         $rewardIndex = self::getRewardIndex($rewardKey, $randV);
         $getReward = $reward[$rewardIndex];
         $getReward['from'] = array('name' => 'goodluck2', 'position' => $randval);
     } else {
         //default
         $rewardName = array_keys($ret)[0];
         if ($ret[$rewardName] == 'default') {
             $item = ZConfig::get('item');
             if (!($rate = $item[$rewardName])) {
                 throw new common\error('配置出错.');
             }
         } else {
             $rate = $ret[$rewardName];
         }
         $reward = ZConfig::getField('reward', 'default');
         $getReward = array('name' => $rewardName, 'rate' => $rate, 'position' => $randval, 'animationTime' => $reward['animationTime']);
     }
     return $getReward;
 }