%% ---------------------------------------------------------------------------
%% @doc MYSQL server_kv表
%% @author hek
%% @since  2016-11-14
%% @deprecated
%% ---------------------------------------------------------------------------
-module(lib_server_kv).
-include("common.hrl").
-include("common_rank.hrl").
-include("server_kv.hrl").
-include("def_fun.hrl").
-include("clusters_zone.hrl").
-include("predefine.hrl").
-include("server.hrl").
-export([
    %% 重设世界等级
    reset_world_lv/1,
    %% 获取value
    get_value/1,
    %% 获取value
    get_value/2,
    %% 更新kv
    update_server_kv/1,
    update_server_kv_db/1,
    update_server_kv_ets/1

    ,update_world_lv_center/2
    ,delete_server_kv_ets/1
]).

%% 重设世界等级
reset_world_lv(RankMaps) ->
    update_world_lv(RankMaps, utime:unixtime()).

    % case ets:lookup(?SERVER_KV, ?SERVER_KV_WORLD_LV) of
    %     [] ->
    %         update_world_lv(RankMaps, utime:unixtime());
    %     [Kv] ->
    %         NowTime = utime:unixtime(),
    %         case utime:is_same_date(Kv#server_kv.time+10, NowTime) of
    %             true -> ok;
    %             false -> update_world_lv(RankMaps, NowTime)
    %         end
    % end.

update_world_lv(RankMaps, NowTime)->
    OldLv = get_value(?SERVER_KV_WORLD_LV, 1),
    case maps:get(?RANK_TYPE_LV, RankMaps, []) of
        [] -> WorldLv = 1;
        LvRanks ->
            case [Value || #common_rank_role{value = Value, rank = Rank} <- LvRanks, Rank =< ?WORLD_LV_LEN, Rank =/= 0] of
                List when length(List) > 0 -> WorldLv = max(1, round(lists:sum(List) / length(List)));
                _ ->  WorldLv = 1
            end
    end,
    KvR = #server_kv{key = ?SERVER_KV_WORLD_LV, value = WorldLv, time = NowTime},
    update_server_kv(KvR),
    WorldLv > OldLv andalso
        [ lib_player:update_world_lv(RolePid, WorldLv)|| #ets_online{pid = RolePid} <- ets:tab2list(?ETS_ONLINE)],
    %% 更新分区服务器世界等级
    ServerId = config:get_server_id(),
    clusters_node:apply_cast(?MODULE, update_world_lv_center, [ServerId, WorldLv]).

%% 更新世界等级时通知到跨服中心调用执行
update_world_lv_center(ServerId, WorldLv) ->
    czone:update_svr_world_level(ServerId, WorldLv),
    ok.

%% 获取value
%% KeyValueList :: [{key, defalut_value}|...]
%% @return      :: [{key, value}|...]
get_value(KeyValueList) ->
    get_value(KeyValueList, []).

%% 获取value
%% @reutrn      :: value
get_value([], List) ->
    lists:reverse(List);
get_value([{Key, DefaultValue}|T], List) ->
    Value = get_value(Key, DefaultValue),
    get_value(T, [{Key, Value}|List]);

get_value(Key, DefaultValue) ->
    case ets:lookup(?SERVER_KV, Key) of
        [#server_kv{value = Value}] -> Value;
        _ -> DefaultValue
    end.

%% 更新kv
update_server_kv(KvR) ->
    update_server_kv_db(KvR),
    update_server_kv_ets(KvR).

update_server_kv_db(KvR) ->
    #server_kv{key = Key, value = Value, time = Time} =  KvR,
    StrValue = util:term_to_string(Value),
    ReSQL = <<"replace into server_kv set `key` = ~p, `value` = '~s', `time` = ~w">>,
    db:execute(io_lib:format(ReSQL, [Key, StrValue, Time ])).

update_server_kv_ets(KvR) ->
    ets:insert(?SERVER_KV, KvR).

delete_server_kv_ets(Key) ->
    ets:delete(?SERVER_KV, Key).