/** * 设置数据库操作类和缓存操作类 * @param array $config * 数据库操作类的key是dbo * 缓存操作类的key是cache */ public function setConfig($config) { if (is_array($config) && isset($config['dbo']) && isset($config['cache'])) { if ($config['dbo'] instanceof DBHelper || $config['dbo'] instanceof DBHelper2) { $this->dbhelper = $config['dbo']; } else { throw new Exception('dbo config error: type must be DBHelper or DBHelper2'); } if ($config['cache'] instanceof Cache) { $this->cache = $config['cache']; } else { throw new Exception('cache config error: type must be Cache'); } $this->storage_type = "cache"; } else { $appConfigInstance = get_app_config(); $this->cache = $appConfigInstance->getTableServer("id_sequence")->getCacheInstance(); $this->dbhelper = $appConfigInstance->getTableServer("id_sequence")->getDBHelperInstance(); if (empty($this->cache) || empty($this->dbhelper)) { throw new Exception('config error: dbo or cache not set'); } $this->storage_type = $appConfigInstance->getGlobalConfig("id_sequence_storage"); } if (empty($this->storage_type) || !in_array($this->storage_type, array(self::STORAGE_TYPE_CACHE, self::STORAGE_TYPE_DATABASE))) { $this->storage_type = self::STORAGE_TYPE_CACHE; } }
/** * Create an ILogger implementation instance. * * @param array $options the options for create logger * current supported options list as below: * storage : the storage handler for log( file, DB , etc.). the default * storage is file. * log_level : the log level. one of the const LOG_OFF,LOG_DEBUG,LOG_INFO,LOG_ERROR. * The default log level is LOG_ERROR * @return ILogger */ public static function getLogger($options = null) { if (isset($options['storage'])) { $storage = strtolower(preg_replace('/[^A-Z0-9_\\.-]/i', '', $options['storage'])); } else { $app_config = get_app_config(); $storage = $app_config->getGlobalConfig('log_storage'); if ($storage === false) { $storage = 'file'; } } if (isset($options['log_level'])) { // set the log level $log_level = intval($options['log_level']); } else { // default log level is log error message $log_level = ELEX_LOG_ERROR; } $storageClass = 'Log' . ucfirst($storage) . 'Storage'; if (!class_exists($storageClass, false)) { $path = FRAMEWORK . '/log/storage/' . $storageClass . '.class.php'; if (file_exists($path)) { require_once $path; } else { // user the file storage $storageClass = 'LogFileStorage'; require_once FRAMEWORK . '/log/storage/LogFileStorage' . '.class.php'; } } $storageInstance = new $storageClass($options); $instance = new Logger($storageInstance); $instance->setLogLevel($log_level); return $instance; }
public function clearUserEventLog() { $req = RequestFactory::createDeleteRequest(get_app_config($this->gameuid)); $req->setKey('gameuid', $this->gameuid); $req->setTable($this->getTableName()); $req->addKeyValue('gameuid', $this->gameuid); return $req->execute(); }
/** * 设置数据库操作类和缓存操作类 * @param array $config * 数据库操作类的key是dbo * 缓存操作类的key是cache */ private function setConfig() { $appConfigInstance = get_app_config(); $this->cache = $appConfigInstance->getTableServer($this->table_name)->getCacheInstance(); $this->dbhelper = $appConfigInstance->getTableServer($this->table_name)->getDBHelperInstance(); if (empty($this->cache) || empty($this->dbhelper)) { throw new Exception('config error: dbo or cache not set'); } }
/** * 设置数据库操作类 * @param array $config * 数据库操作类的key是dbo */ public function setConfig($config) { if (is_array($config) && isset($config['dbo'])) { if ($config['dbo'] instanceof DBHelper || $config['dbo'] instanceof DBHelper2) { $this->dbhelper = $config['dbo']; } else { throw new Exception('dbo config error: type must be DBHelper or DBHelper2'); } } else { $appConfigInstance = get_app_config(); $this->dbhelper = $appConfigInstance->getTableServer($this->table_name)->getDBHelperInstance(); if (empty($this->dbhelper)) { throw new Exception('config error: dbo not set'); } } }
public function invoke($api, $param) { if (empty($api) || strpos($api, '.') === false) { return 'api not exist'; } $api_arr = explode('.', $api); $class = $api_arr[0]; $method = $api_arr[1]; require_once APP_ROOT . "/data/services/{$class}.php"; $ins = new $class(); require_once FRAMEWORK . '/rest/Signature.class.php'; $param['t'] = time(); $sig_key = get_app_config()->getGlobalConfig(AppConfig::SIGNATURE_KEY); $param['k'] = Signature::sign($param, $sig_key, 'k'); if (method_exists($ins, $method)) { return $ins->{$method}($param); } else { return "method {$method} not exist in class {$class}."; } }
/** * 从数据库删除一条数据 * @param $gameuid * @param $key_values array,名称做key,数值做value */ protected function deleteFromDb($gameuid, $key_values) { if (empty($key_values)) { return false; } $req = RequestFactory::createDeleteRequest(get_app_config($gameuid)); $req->setKey($this->getKeyName(), $gameuid); $req->setTable($this->getTableName()); foreach ($key_values as $key => $value) { $req->addKeyValue($key, $value); } $req->execute(); return true; }
/** * Submit Source code to server * @global type $login_uid * @global type $login_username * @global type $logged * @param type $arg * @return mixed if success, return sid. otherwise error message */ function submit_source($pid, $cid, $language, $source) { if (!is_logged()) { return "Invalid login"; } $pid = intval(trim($pid)); $source = trim($source); if ($cid) { $problem = new ContestProblem($cid); if (!is_contest_accessible($cid)) { return "You can't access to the contest"; } if (is_contest_ended($cid) && !is_contest_modifiable($cid)) { return "Contest is finished"; } } else { $problem = new ProblemTbl(); } if (!$problem->Get($pid)) { return "Invalid Problem ID!"; } $acutal_cid = $problem->detail['cid']; if (!$cid && $acutal_cid) { // this is a problem automaticly added after the end of contest if (!is_contest_accessible($acutal_cid)) { return "You can't access to this problem"; } if (!is_contest_modifiable($acutal_cid) && !is_contest_ended($acutal_cid)) { return "Contest is not finished. Can't submit to normal problem"; } } $sdata = array(); $sdata["contest"] = $cid; if ($language < 1 || $language > 4) { return "Invalid language!"; } $sdata['language'] = $language; $app_config = get_app_config(); $codelength = strlen($source); if ($codelength > $app_config['max_sourcecode_length']) { return "Size of your submittion exceeds limitation."; } if ($codelength == 0) { return "You can't submit an empty source code"; } $sdata['uid'] = get_uid(); $sdata['time'] = date("Y-m-d H:i:s"); if ($cid) { $sdata['pid'] = $problem->detail['pid']; $cpid = $pid; $pid = $sdata['pid']; } else { $sdata['pid'] = $pid; } $sdata['codelength'] = $codelength; $sdata['sourcecode'] = mysql_real_escape_string($source); $status = new StatusTbl(); $status->detail = $sdata; $status_id = $status->Add(); $user = new UserTbl(get_uid()); $user->Get(); $user->update['submissions'] = $user->detail['submissions'] + 1; $user->Update(); $problem = new ProblemTbl($pid); $problem->Get(); $problem->update['submissions'] = $problem->detail['submissions'] + 1; $problem->Update(); if ($cid) { $con_status = new ContestStatus($cid); $con_status->detail = array('cid' => $cid, 'sid' => $status_id, 'cpid' => $cpid); $con_status->Add(); } $queue = new QueueTbl(); $queue->detail['sid'] = $status_id; if ($cid) { $queue->detail['cid'] = $cid; $queue->detail['cpid'] = $cpid; } $queue->Add(); return $status_id; }
/** * 是否关闭了服务 * @return bool */ public function isServiceClosed($gameuid = null) { return get_app_config($gameuid)->isServiceClosed(); }
private function record_action($amf_entry, $start) { $gameuid = $GLOBALS['gameuid']; $now = microtime(true); $app_config = get_app_config(); $cache_helper = $app_config->getDefaultCacheInstance(); $interval = intval(($now - $start) * 1000); $statistic_key = $gameuid . "_" . $amf_entry; $micro_start_as_str = strval($start * 1000) . '.'; $cache_helper->set($statistic_key, array('gameuid' => $gameuid, 'action' => $amf_entry, 'start' => substr($micro_start_as_str, 0, strpos($micro_start_as_str, '.')), 'interval' => $interval), 0); }
protected function validate() { if (!get_app_config()->getGlobalConfig("debug_mode")) { $authcode = $this->user_account['secretid']; if ($authcode != $this->getParam('authcode')) { $this->throwException("authcode error", GameStatusCode::AUTH_CODE_ERROR); } } }
/** * 将uid从映射表中删除 * * @param string $uid 用户uid * @return bool 是否删除成功,成功返回true,否则返回false */ public function deleteMapping($uid) { $this->logger->writeInfo("tring to delete uid[{$uid}] from uid_gameuid_mapping."); $req = RequestFactory::createDeleteRequest(get_app_config()); $req->setTable($this->getTableName()); $req->addKeyValue("uid", $uid); $affected_rows = $req->execute(); if (intval($affected_rows) > 0) { return true; } return false; }
private function commitToDB_bak($logs = null) { $key = $this->getMemkey(); if ($logs == null) { $logs = $this->getFromCache($key, $this->gameuid); } if (empty($logs)) { return; } if (PLATFORM == "test" || PLATFORM == "facebook_tw") { $contents = ''; $fomat = "g[%d]a[%d]c[%d]m[%d]e[%d]d[%d]it[%d]t[%d] \r\n"; foreach ($logs as $log) { $contents .= sprintf($fomat, $this->gameuid, $log['action_type'], intval($log['coin']), intval($log['money']), intval($log['experience']), intval($log['coupon']), intval($log['content']), $log['create_time']); } if (!empty($contents)) { // $logger = LogFactory::getLogger(array( // 'prefix' => "action_log", // 文件名的前缀 // 'log_dir' => APP_ROOT.'/log/', // 文件所在的目录 // 'archive' => ILogger::ARCHIVE_YEAR_MONTH, // 文件存档的方式 // 'log_level' => 1 // )); $this->logger_self->writeError($contents); } $logs = array(); $this->setToCache($key, $logs, $this->gameuid); } else { // 插入到数据库 $req = RequestFactory::createInsertRequest(get_app_config($this->gameuid)); // 不需要缓存数据 $req->setNoCache(true); $req->setKey("gameuid", $this->gameuid); $req->setTable($this->getTableName()); $req->setColumns("gameuid,action_type,coin,money,experience,create_time,content,coupon"); foreach ($logs as $log) { $req->addValues(array($this->gameuid, $log['action_type'], $log['coin'], $log['money'], $log['experience'], $log['create_time'], intval($log['content']), intval($log['coupon']))); } $affected_rows = $req->execute(); if ($affected_rows > 0) { // 将缓存清空 $logs = array(); $this->setToCache($key, $logs, $this->gameuid); } } }
/** * to decide whether user can take an action in a day again. * this is very useful to decide the steal action, send notification action, send feed action, etc. * * @param int $gameuid the user who issue the action * @param int $action_id the action id user issued * @param int $max_allowed the max allowed time everyday * @return boolean if the action can be issued again, return true, or return false */ function can_action_happen($gameuid, $action_id, $max_allowed) { $cache_helper = get_app_config($gameuid)->getDefaultCacheInstance(); $flag_key = sprintf(CACHE_KEY_CAN_ACTION_HAPPEN_FLAG, $gameuid, $action_id); $action_everyday_count = $cache_helper->get($flag_key); $count = 0; $now = time(); $last_update = $now; if ($action_everyday_count === false) { $count = 1; } else { $count = $action_everyday_count['count'] + 1; $last_update = $action_everyday_count['last_update']; } if ($count > $max_allowed) { return false; } if (date('z', $last_update) != date('z', $now)) { $cache_helper->delete($flag_key); } else { $date = getdate(strtotime('next day')); $next_day_time = mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']); $cache_helper->set($flag_key, array("last_update" => $now, "count" => $count), $next_day_time - $now); } return true; }
protected function sendNotificationThroughPlatform($to_uids, $content) { include_once FRAMEWORK . '/json/JSON.php'; $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); $data = $json->decode($content); $template_id = $data['tpl_id']; if (empty($template_id)) { return; } $title_data = $data['title_data']; $body_data = $data['body_data']; $app_name = get_app_config()->getGlobalConfig('app_name'); $this->platform_handler->api_client->notifications_send($app_name . "_notification_{$template_id}", $title_data, $body_data, $to_uids); }