<?php

namespace loyalsoft;

//use \GAME_ONLINE;

/**
 * Description of config
 * 尝试用这种写法来定义服务器配置
 * @author gwang (email: mail@wanggangzero.cn)
 * @copyright © 2015-2-3, SJZ LoyalSoft Corporation & gwang. All rights reserved.
 */
abstract class config {

    protected $testerArr;                                                       # 测试号数组
    protected $banedArr;                                                        # 封号数组
    public $paydb;                                                              # mysql配置
    public $nosql;                                                              # nosql配置
    public $stat;                                                               # 统计配置

    /**
     * 测试号
     * @param string $uid
     * @return bool
     */

    function isTester($uid) {
        return in_array($uid, $this->testerArr);
    }

    /**
     * 封号
     * @param type $uid
     */
    function isBaned($uid) {
        return in_array($uid, $this->banedArr);
    }

    // 测试号
    abstract protected function _InitTester();

    // 封号
    abstract protected function _InitBaneder();

    // 初始化外网paydb的设置
    abstract protected function _InitOuterNetPaydb();

    // 初始化外网nosql设置
    abstract protected function _InitOuterNetNosql();

    /**
     * 初始化内网paydb的设置
     */
    private function _InitIntranetPaydb() {
        $this->paydb->host = '192.168.10.16';
        $this->paydb->port = '3306';
        $this->paydb->name = 'ylsj2019_pay';
        $this->paydb->user = 'gwang';
        $this->paydb->password = 'wanggang1985';
        // todo: Ps. 可以设置从库, 主从架构, 主库写入, 从库查询
    }

    /**
     * 初始化paydb
     */
    private function _InitPaydb() {
        $this->paydb = new \stdclass();
        $this->paydb->persistant = false;                                       # 持久链接 or not.
        $this->paydb->driver = 'mysql';                                         # Must be MySQL. Don't support other database server yet.
        $this->paydb->encoding = 'UTF8';                                        # Encoding of database.
        $this->paydb->strictMode = false;                                       # Turn off the strict mode of MySQL.
        // $this->paydb->emulatePrepare = true;                                 # PDO::ATTR_EMULATE_PREPARES
        // $this->paydb->bufferQuery    = true;                                 # PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
        $this->paydb->prefix = '';                                              # 表前缀
        if (GAME_ONLINE) {                                                      # 外网版的paydb配置
            $this->_InitOuterNetPaydb();
        } else {                                                                # 内网版的paydb配置
            $this->_InitIntranetPaydb();
        }
    }

    private function _InitNosql() {
        $this->nosql = new \stdClass();
        if (GAME_ONLINE) {                                                      # 外网 配置
            $this->_InitOuterNetNosql();
        } else {                                                                # 内网
            $this->nosql->host = '192.168.10.16';                               # host/ip
            $this->nosql->port = 6004;                                          # 端口
            $this->nosql->pwd = 'wanggang1985';                                 # 密钥 
        }
    }

    private function _InitStatSettings() {
        $stat = new \stdClass();                                                 # 对象初始化
        $stat->usernumber = true;                                                # 玩家数量
        $stat->cmd = true;                                                       # 操作记录
    }

    /**
     * @return config 单例
     */
    static public function Inst() {
        static $config = null;
        if (is_null($config)) {
            $config = include __DIR__ . '/configs/config_' . PLAT . '.php';
        }
        return $config;
    }

    /**
     * 构造函数负责初始化配置数据
     */
    private function __construct() {
        $this->_InitTester();                                                   # 测试用户
        $this->_InitBaneder();                                                  # 封号用户
        $this->_InitPaydb();                                                    # mysql数据库配置
        $this->_InitNosql();                                                    # nosql数据库配置
        $this->_InitStatSettings();                                             # 统计配置
    }

}
