Пример #1
0
 public function process()
 {
     //只处理文本消息和自定义菜单消息
     if (!($this->_msgType != 'text' || $this->_msgType == 'event')) {
         interface_log(DEBUG, 0, "msgType:" . $this->_msgType);
         return $this->makeHint("你发的不是文字或菜单消息");
     }
     try {
         $STO = new SingleTableOperation("userinput", "ES");
         if ($this->_msgType == 'event' && $this->_event == 'CLICK') {
             $mode = $this->_eventKey;
             //更新用户mode
             $ret = $STO->getObject(array("userId" => $this->_fromUserName));
             if (!empty($ret)) {
                 $STO->updataObject(array('mode' => $mode), array("userId" => $this->_fromUserName));
             } else {
                 $STO->addObject(array("userId" => $this->_fromUserName, 'mode' => $mode));
             }
             return $this->makeHint("模式设置成:" . $mode);
         } else {
             $text = $this->_content;
             $ret = $STO->getObject(array("userId" => $this->_fromUserName));
             if (empty($ret)) {
                 $STO->addObject(array("userId" => $this->_fromUserName));
                 return $this->makeHint($text);
             } else {
                 $mode = $ret[0]['mode'];
                 $STO->updataObject(array('input' => $ret[0]['input']), array("userId" => $this->_fromUserName));
                 return $this->makeHint($text);
             }
         }
     } catch (DB_Exception $e) {
         interface_log(ERROR, EC_DB_OP_EXCEPTION, "query db error" . $e->getMessage());
     }
 }
Пример #2
0
 public static function getToken($force = false)
 {
     try {
         $STO = new SingleTableOperation();
         $STO->setTableName("ctoken");
         if ($force == false) {
             $ret = $STO->getObject();
             interface_log(DEBUG, 0, "token data get from ctoken: " . json_encode($ret));
             if (count($ret) == 1) {
                 $token = $ret[0]['token'];
                 $expire = $ret[0]['expire'];
                 $addTimestamp = $ret[0]['addTimestamp'];
                 $current = time();
                 if ($addTimestamp + $expire + 30 < $current) {
                     return $token;
                 }
             }
         }
         $para = array("grant_type" => "client_credential", "appid" => WX_API_APPID, "secret" => WX_API_APPSECRET);
         $url = WX_API_URL . "token";
         interface_log(DEBUG, 0, "url:" . $url . "  req data:" . json_encode($para));
         $ret = doCurlGetRequest($url, $para);
         interface_log(DEBUG, 0, "response data:" . $ret);
         $retData = json_decode($ret, true);
         if (!$retData || $retData['errcode']) {
             interface_log(ERROR, EC_OTHER, "requst wx to get token error");
             return false;
         }
         //从返回数据中获取得到的access_token和它的过期时间,更新ctoken表
         $token = $retData['access_token'];
         $expire = $retData['expires_in'];
         $STO->delObject();
         $STO->addObject(array('token' => $token, "expire" => $expire, "addTimestamp" => time()));
         return $token;
     } catch (DB_Exception $e) {
         interface_log(ERROR, EC_DB_OP_EXCEPTION, "operate ctoken error! msg:" . $e->getMessage());
         return false;
     }
 }
Пример #3
0
 public function process()
 {
     try {
         $oTableOperate = new SingleTableOperation('cUser', "MYZL");
         $data = $oTableOperate->getObject(array('userId' => $this->_args['userId']));
         if (count($data)) {
             $this->_retValue = EC_RECORD_EXIST;
             $this->_retMsg = "user already exist!";
             interface_log(ERROR, $this->_retValue, $this->_retMsg);
             return false;
         }
         //添加用户记录,设置相关的字段,这里可以根据自己的策略设置
         $oTableOperate->addObject(array('userId' => $this->_args['userId'], 'password' => $this->_args['password'], 'email' => $this->_args['email'], 'money' => 100, 'bulletNum' => 5, 'xsft' => 5, 'hdcx' => 5, 'chxs' => 5, 'sszm' => 5, 'addTimeStamp' => getCurrentTime()));
         $this->_responseText = MYZL_HINT_ADDUSER_SUC;
     } catch (DB_Exception $e) {
         $errorNum = $oTableOperate->getErrorNum();
         $this->_retMsg = $oTableOperate->getErrorInfo() . $e->getMessage();
         $this->_retValue = genRetCode($errorNum);
         interface_log(ERROR, $this->_retValue, $this->_retMsg);
         return false;
     }
     return true;
 }
Пример #4
0
#!/usr/local/services/php/bin/php
<?php 
require_once dirname(__FILE__) . '/../common/Common.php';
$maxMoney = 100;
$minMoney = 10;
try {
    $oTable = new SingleTableOperation('cWaitingUser', 'MYZL');
    while (1) {
        $data = $oTable->getObject(array('_sortExpress' => 'addTimeStamp'));
        matcher_log(DEBUG, 0, json_encode($data));
        if (count($data) < 2) {
            sleep(2);
            continue;
        } else {
            $length = count($data);
            for ($i = 0; $i + 1 < $length; $i = $i + 2) {
                $userId1 = $data[$i]['userId'];
                if ($i >= $length) {
                    continue;
                }
                $userId2 = $data[$i + 1]['userId'];
                DbFactory::getInstance('MYZL')->autoCommit();
                //minus bullets
                DbFactory::getInstance('MYZL')->update("update cUser set bulletNum=bulletNum-1 where userId IN('" . $userId1 . "', '" . $userId2 . "')");
                //删除
                $oTable->delObject(array('userId' => array($userId1, $userId2)));
                //添加
                $oTable->addObject(array('_tableName' => 'cFight', 'user1' => $userId1, 'user2' => $userId2, 'first' => $userId1, 'current' => $userId1, 'operation' => START, 'maxMoney' => $maxMoney, 'minMoney' => $minMoney, 'operator' => 0));
                matcher_log(DEBUG, 0, "add {$userId1} and {$userId2} to fight");
                DbFactory::getInstance('MYZL')->tryCommit();
            }
Пример #5
0
<?php

require_once dirname(__FILE__) . '/../common/Common.php';
$fightId = $argv[1];
try {
    $oTable = new SingleTableOperation('cFight');
    $data = $oTable->getObject(array('fightId' => $fightId));
    if (empty($data)) {
        echo 'fight with id:' . $fightId . ' not exists!';
        exit(0);
    }
    $fightInfo = $data[0];
    if (!$fightInfo['lastInfo']) {
        echo "lastInfo is empty, fightId:" . $fightId;
        exit(0);
    }
    $lastInfo = json_decode($fightInfo['lastInfo'], true);
    unset($lastInfo['fightId']);
    $oTable->updateObject($lastInfo, array('fightId' => $fightId));
} catch (DB_Exception $e) {
    echo 'mysql error!' . $oTable->getErrorInfo() . $e->getMessage();
}
Пример #6
0
 public function process()
 {
     //return $this->makeHint("系统正在升级!请稍候使用");
     try {
         if ($this->_msgType != 'image') {
             $contentStr = FF_HINT_TYPE_ERROR;
             if ($this->_msgType == 'text') {
                 $contents = (string) trim($this->_postObject->Content);
                 if ($contents == 'Hello2BizUser') {
                     $contentStr = FF_HINT_HELLO;
                 }
             }
             if ($this->_msgType == 'event') {
                 $event = (string) trim($this->_postObject->Event);
                 if ($event == 'subscribe') {
                     $contentStr = FF_HINT_HELLO;
                 }
             }
             return $this->makeHint($contentStr);
         }
         $this->_url = trim($this->_postObject->PicUrl);
         //下载图片到本地
         $fileName = $this->downloadPic();
         $this->_url = FF_URL_HEADER . $fileName;
         // 1.检验是否有脸
         $ret = faceStub::detect($this->_url);
         if ($ret === false) {
             return $this->makeHint(FF_HINT_FACE_ERROR);
         }
         if (count($ret['face']) == 0) {
             return $this->makeHint(FF_HINT_NO_FACE);
         }
         if (count($ret['face']) > 1) {
             return $this->makeHint(FF_HINT_MULTIPLE_FACE);
         }
         $faceId = $ret['face'][0]['face_id'];
         $oTable = new SingleTableOperation();
         // 插入face到cFace
         $oTable->setTableName('cface');
         $oTable->addObject(array('faceId' => $faceId, 'personName' => $this->_fromUserName, 'url' => $ret['url']));
         // 2.找到最像的脸
         $ret = faceStub::search($faceId, GROUP_NAME, 2);
         if ($ret === false) {
             return $this->makeHint(FF_HINT_FACE_ERROR);
         }
         $candidates = $ret['candidate'];
         if (count($candidates) == 0) {
             return $this->makeHint(FF_HINT_FACE_NO_CANDIDATE);
         }
         // 查找查询用户,看是否已上传过face
         $oTable->setTableName('cperson');
         $ret = $oTable->getObject(array('personName' => $this->_fromUserName));
         if (count($ret)) {
             $userFaceIds = array();
             foreach ($ret as $item) {
                 $userFaceIds[] = $item['faceId'];
             }
             $newCandidate = array();
             // 从结果集中删除掉用户自己
             foreach ($candidates as $item) {
                 if (!in_array($item['face_id'], $userFaceIds)) {
                     $newCandidate[] = $item;
                 }
             }
             $candidates = $newCandidate;
             // 更新用户的faceId
             $oTable->updateObject(array('faceId' => $faceId), array('personName' => $this->_fromUserName));
             // 更新faceplusplus的person:先删除person的face,再加入该faceId
             $result = faceStub::removeFaceFromPerson($this->_fromUserName, 'all');
             if ($result === false) {
                 return $this->makeHint(FF_HINT_FACE_ERROR);
             }
             $result = faceStub::addFaceToPerson($this->_fromUserName, $faceId);
             if ($result === false) {
                 return $this->makeHint(FF_HINT_FACE_ERROR);
             }
         } else {
             // 插入记录到cPerson
             $oTable->addObject(array('personName' => $this->_fromUserName, 'faceId' => $faceId));
             // 请求faceplusplus创建person和,并加到group中
             $result = faceStub::createPerson($this->_fromUserName, $faceId, GROUP_NAME);
             if ($result === false) {
                 return $this->makeHint(FF_HINT_FACE_ERROR);
             }
         }
         if (count($candidates) == 0) {
             return $this->makeHint(FF_HINT_FACE_NO_CANDIDATE);
         }
         // 从数据库中查询face的url
         $oTable->setTableName('cface');
         $ret = $oTable->getObject(array('faceId' => $candidates[0]['face_id']));
         if (count($ret) == 0) {
             return $this->makeHint(FF_HINT_FACE_NO_CANDIDATE);
         }
         $url = $ret[0]['url'];
         $resultStr = sprintf(SUCC_TPL_FINDFACE, $this->_fromUserName, $this->_toUserName, $this->_time, $url, $url);
         $oTable->setTableName('cstatus');
         $ret = $oTable->getObject(array('key' => 'lastTrain'));
         $lastTrain = $ret[0]['value'];
         date_default_timezone_set(PRC);
         $now = date('YmdG');
         if ($now != $lastTrain) {
             faceStub::train(GROUP_NAME, 'search');
             $oTable->updateObject(array('value' => $now), array('key' => 'lastTrain'));
         }
         return $resultStr;
     } catch (Exception $e) {
         interface_log(ERROR, EC_DB_OP_EXCEPTION, $e->getMessage());
         return $this->makeHint(FF_HINT_INNER_ERROR);
     }
 }
Пример #7
0
 private function getOperandAndOperator()
 {
     //查询用户是否在准备列表中
     $STO = new SingleTableOperation("cWaitingUser", "MYZL");
     $waitingRet = $STO->getObject(array("userId" => $this->_fromUserName));
     interface_log(DEBUG, 0, var_export($waitingRet, true));
     //查询用户信息
     $STO->setTableName("cUser");
     $userInfo = $STO->getObject(array("userId" => $this->_fromUserName));
     $this->userInfo = $userInfo;
     interface_log(DEBUG, 0, var_export($userInfo, true));
     //处理用户关注公众帐号的动作
     if ($this->_msgType == 'event' && trim((string) $this->_postObject->Event) == "subscribe") {
         if (empty($userInfo)) {
             //首次关注,添加用户信息
             $this->interfaceName = "AddUser";
             return true;
         } else {
             //取消关注后再次关注,给用户欢迎信息
             $this->interfaceName = "WelcomeBack";
             return true;
         }
     }
     //获取用户操作自定义菜单的EvenetKey
     $eventKey = trim((string) $this->_postObject->EventKey);
     //获取EventKey中的操作码和操作数
     $tmp = explode("_", $eventKey);
     $operator = trim($tmp[0]);
     $operand = trim($tmp[1]);
     interface_log(DEBUG, 0, 'operator:' . $operator . '  operand:' . $operand);
     //处理“下一步”的情况
     if ($operator == "NEXT") {
         //如果用户在操作准备列表中,则说明匹配尚未成功,设置interfaceName为WaitOp,稍后给到提示
         if (!empty($waitingRet)) {
             $this->interfaceName = "WaitOp";
             return true;
         }
         //执行GetOp类,获取当前用户需要做的的操作
         require_once dirname(__FILE__) . '/../interface/GetOp.php';
         $instance = new GetOp();
         //执行GetOp类的基本函数
         if (!$instance->verifyCommonInput($this->_postObject)) {
             $ret = $instance->renderOutput();
             if ($ret['retVal'] == EC_INVALID_INPUT) {
                 $this->interfaceName = "InputErrorHint";
                 return true;
             }
         }
         if (!$instance->initialize()) {
             $ret = $instance->renderOutput();
             if ($ret['retVal'] == EC_DB_OP_EXCEPTION) {
                 $this->interfaceName = "DbErrorHint";
                 return true;
             }
         }
         $instance->prepareData();
         if (!$instance->process()) {
             //如果GetOp类的process函数返回false,获取结果
             $ret = $instance->renderOutput();
             if ($ret['retVal'] == EC_DB_OP_EXCEPTION) {
                 $this->interfaceName = "DbErrorHint";
                 return true;
             }
             if ($ret['retVal'] == EC_USER_NOT_EXIST) {
                 //在用户不存在的情况下,设定interfaceName为AddUser,表示需要添加用户
                 $this->interfaceName = "AddUser";
                 return true;
             }
             if ($ret['retVal'] == EC_FIGHT_NOT_EXIST) {
                 //如果游戏记录不存在,表示用户需要加入到准备队列,设置interfaceName为Ready
                 $this->interfaceName = "Ready";
                 return true;
             }
             if ($ret['retVal'] == EC_MULTIPLE_FIGHT) {
                 $this->interfaceName = "MultiFightHint";
                 return true;
             }
         }
         $ret = $instance->renderOutput();
         //在没有返回错误码的情况下,获取GetOp返回的游戏记录数据
         $this->fightInfo = $ret['retData'];
         $this->retStr = $instance->getResponseText();
         interface_log(DEUBUG, 0, "this->retStr:" . $this->retStr);
         if ($this->fightInfo['operation'] == START || $this->fightInfo['operation'] == FIRST_END) {
             //当操作码为START或者FIRST_END,说明是游戏开始阶段
             if ($this->fightInfo['first'] == $this->_fromUserName) {
                 //当前用户为开始游戏的一方,设定interfaceName为Start
                 $this->interfaceName = "Start";
             } else {
                 //当前用户不是开始游戏的一方,设定interfaceName为WaitStart,稍后代码逻辑中给到提示
                 $this->interfaceName = "WaitStart";
             }
             return true;
         }
         if ($this->fightInfo['operation'] == SECOND_END) {
             //获取最后操作结果的情况
             $this->interfaceName = "SecondEndHint";
             return true;
         }
         if ($this->fightInfo['operator'] != $this->_fromUserName) {
             //等待对方操作的情况
             $this->interfaceName = "WaitOperation";
             $this->value = $this->fightInfo['operation'];
             return true;
         } else {
             if ($this->fightInfo['operation'] == PUT_MAGIC) {
                 if ($userInfo[0]['xsft'] <= 0 && $userInfo[0]['hdcx'] <= 0 && $userInfo[0]['chxs'] <= 0 && $userInfo[0]['sszm'] <= 0) {
                     //在用户没有道具的情况下,NEXT的操作就是使用道具,但是值为空
                     $this->interfaceName = "PutMagic";
                     $this->value = "";
                 } else {
                     //提示用户按使用道具的自定义菜单使用道具
                     $this->interfaceName = "PutMagicHint";
                 }
             }
             if ($this->fightInfo['operation'] == CHIP_IN) {
                 //提示用户下注
                 $this->interfaceName = "ChipInHint";
             }
             if ($this->fightInfo['operation'] == SHOOT) {
                 //如果操作码为SHOOT,NEXT操作就是Shoot
                 $this->interfaceName = "Shoot";
             }
         }
     } else {
         if ($operator == "CHIPIN") {
             $this->interfaceName = "ChipIn";
             $this->value = (int) $operand;
             interface_log(DEBUG, 0, "this->value" . $this->value);
         } else {
             if ($operator == "PUTMAGIC") {
                 $this->interfaceName = "PutMagic";
                 $this->value = $operand;
             } else {
                 $this->interfaceName = "InputErrorHint";
                 return true;
             }
         }
     }
 }