%%%-----------------------------------
%%% module  : config
%%% @author  : xyao
%%% email   : jiexiaowen@gmail.com
%%% Created : 2011.06.12
%%% description: 获取 .config里的配置信息
%%%-----------------------------------
-module(config).
-export([
        get_log_level/0
        ,get_log_path/0
        ,get_ticket/0
        ,get_mysql/0
        ,get_mysql/1
        ,get_conn_num/0
        ,get_dbname/0
        ,get_card/0
        ,get_phone_gift/0
        ,get_cls_type/0
        ,get_cls_info/0
        ,get_cls_node/0
        ,get_cls_cookie/0
        ,get_platform/0
        ,get_server_num/0
        ,get_server_id/0
        ,get_server_debug/0
        ,is_dev_server/0
        ,get_branch/0
        ,get_merge_server_ids/0
        ,get_chat_monitor/0
        ,get_apple/0
        ,get_reg_server_id/0
        ,get_server_name/0
    ]).
-include("server_env.hrl").

-define(APP_NAME, main).

%% 日志系统配置文件
get_log_level() ->
    case application:get_env(?APP_NAME, log_level) of
        {ok, LogLevel} -> LogLevel;
        _ -> 3
    end.

get_log_path() ->
    case application:get_env(?APP_NAME, log_path) of
        {ok, LogPath} -> LogPath;
        _ -> "../logs"
    end.

%% 私钥
get_ticket() ->
    case application:get_env(?APP_NAME, ticket) of
        {ok, Ticket} -> Ticket;
        _ -> "ticket"
    end.

%% 获取新手卡
get_card() ->
    Key = case application:get_env(?APP_NAME, card_key) of
        {ok, _Key} -> _Key;
        _ -> "key"
    end,

    Ser = case application:get_env(?APP_NAME, card_server) of
        {ok, _Ser} -> _Ser;
        _ -> []
    end,
    {Key, Ser}.

%% 手机钱包开关(0关，1开)
get_phone_gift() ->
    case application:get_env(?APP_NAME, phone_gift) of
        {ok, Gift} -> Gift;
        _ -> 0
    end.


%% 获取mysql参数
get_mysql() ->
    Host1 = case application:get_env(?APP_NAME, db_host) of
        {ok, Host} -> Host;
        _ -> "localhost"
    end,
    Port1 = case application:get_env(?APP_NAME, db_port) of
        {ok, Port} -> Port;
        _ -> 3306
    end,
    User1 = case application:get_env(?APP_NAME, db_user) of
        {ok, User} -> User;
        _ -> "root"
    end,
    Pass1 = case application:get_env(?APP_NAME, db_pass) of
        {ok, Pass} -> Pass;
        _ -> "root"
    end,
    Name1 = case application:get_env(?APP_NAME, db_name) of
        {ok, Name} -> Name;
        _ -> "test"
    end,
    Encode1 = case application:get_env(?APP_NAME, db_encode) of
        {ok, Encode} -> Encode;
        _ -> utf8
    end,
    Conns1 = case application:get_env(?APP_NAME, db_connections) of
        {ok, Conns} when is_integer(Conns)-> Conns;
        _ -> 100
    end,
    [Host1, Port1, User1, Pass1, Name1, Encode1, Conns1].

%% DBKey = db_host|db_port|db_user|db_pass|db_name|db_encode|db_connections|db_max_connections
get_mysql(DBKey) ->
    case application:get_env(?APP_NAME, DBKey) of
        {ok, Value} -> Value;
        _ -> false
    end.

get_conn_num() ->
    case application:get_env(?APP_NAME, db_connections) of
        {ok, ConnectNum} when is_integer(ConnectNum) -> ConnectNum;
        _ -> 100
    end.

get_dbname() ->
    case application:get_env(?APP_NAME, db_name) of
        {ok, Name} -> Name;
        _ -> "test"
    end.

%% 跨服类型
get_cls_type() ->
    case application:get_env(?APP_NAME, cls_type) of
        {ok, Type} -> Type;
        _ -> 0
    end.

%% 跨服节点信息
get_cls_info() ->
    Node = get_cls_node(),
    Cookie = get_cls_cookie(),
    Ip = get_cls_ip(),
    Port = get_cls_port(),
    {Node, Cookie, Ip, Port}.

%% 跨服节点
get_cls_node() ->
    case application:get_env(?APP_NAME, cls_node) of
        {ok, Node} -> Node;
        _ -> 'cls@127.0.0.1'
    end.

%% 跨服节点
get_cls_cookie() ->
    case application:get_env(?APP_NAME, cls_cookie) of
        {ok, Cookie} -> Cookie;
        _ -> erlang:get_cookie()
    end.

%% 获取跨服节点ip
get_cls_ip() ->
    case application:get_env(?APP_NAME, cls_node) of
        {ok, Node} ->
            [_T, Ip] = string:tokens(atom_to_list(Node), "@"),
            Ip;
        _ -> '127.0.0.1'
    end.

%% 获取跨服节点端口
get_cls_port() ->
    case application:get_env(?APP_NAME, cls_port) of
        {ok, Port} -> Port;
        _ -> 0
    end.

%% 获取平台名
get_platform() ->
    case application:get_env(?APP_NAME, platform) of
        {ok, Platform} -> Platform;
        _ -> ""
    end.

%% 获取当前所在的服名编号（玩家可见的服编号）
get_server_num() ->
    case application:get_env(?APP_NAME, server_num) of
        {ok, ServerNum} -> ServerNum;
        _ -> 0
    end.

%% 获取本服id（后台唯一服号）
get_server_id() ->
    case application:get_env(?APP_NAME, server_id) of
        {ok, Id} -> Id;
        _ -> 0
    end.

%% 获取当前是否测试服
get_server_debug() ->
    case application:get_env(?APP_NAME, is_debug) of
        {ok, Bool} -> Bool;
        _ -> false
    end
    orelse
    get_ticket() =:= "SDFSDESF123DFSDF".

%% 判断是否开发服版本
%% 此接口依赖于DEV_SERVER宏定义的设置
%% 【编译开发版的相关分支时，由编译管理页面自动加入】
-ifdef(DEV_SERVER).
is_dev_server() -> true.
-else.
is_dev_server() -> false.
-endif.

%% 判断版本的分支名
%% 【编译各分支时，由编译管理页面自动加入】
%% 分支名一般采用svn中相关分支名称，具体见编译管理页面的配置
%% 示例： develop、pre_stable、stable、...
-ifndef(CODE_BRANCH).
get_branch() -> undefined.
-else.
get_branch() -> ?CODE_BRANCH.
-endif.

%% 获取合服后所有的服id （有时候运维锅更新的信息有问题，干脆废弃这个模式）
%% get_merge_server_ids() ->
%%     case application:get_env(?APP_NAME, merge_server_ids) of
%%         {ok, Ids} -> Ids;
%%         _ -> []
%%     end.

%% 获取合服后所有的服id
get_merge_server_ids() ->
    case ets:lookup(?SERVER_ENV, merge_server_ids) of
        [#server_env{value = MergeServerIds}] -> MergeServerIds;
        _ ->
            %% undefined
            %% 这里是为了热更时兼容新旧版本处理，正常使用时一定不走这个分支
            ServerId = config:get_server_id(),
            %% 从数据库取得时初始化了ets表数据，后面就不会再执行了
            game_init:get_merge_servers_from_db(ServerId)
    end.

%% 是否监控聊天
get_chat_monitor()->
    case application:get_env(?APP_NAME, chat_monitor) of
        {ok, IsMonitor} -> IsMonitor;
        _ -> 0
    end.


%% 获取是否苹果审核服
%% false | "sy" | "ml" | ...
get_apple() ->
    case application:get_env(?APP_NAME, apple) of
        {ok, ApplePlat} -> ApplePlat;
        _ -> false
    end.

%% 获取注册系统服编号(r_server_id)
%% 来源于base_game表cf_name = r_server_id 的 cf_value
get_reg_server_id() ->
    case ets:lookup(?SERVER_ENV, reg_server_id) of
        [#server_env{value = RegServerId}] -> RegServerId;
        _ -> undefined
    end.

%% 获取游戏服名称
%% 来源于base_game表cf_name = game_title 的 cf_value
get_server_name() ->
    case ets:lookup(?SERVER_ENV, server_name) of
        [#server_env{value = ServerName}] -> ServerName;
        _ -> <<"Unknown Server"/utf8>>
    end.

