%% ---------------------------------------------------------------------------
%% @doc lib_game.erl

%% @author  hjh
%% @email  hejiahua@163.com
%% @since  2017-01-13
%% @deprecated 游戏基础相关
%% ---------------------------------------------------------------------------
-module(lib_game).
-compile(export_all).

-include("server.hrl").
-include("game.hrl").
-include("common.hrl").

make_record(setting, [Type, Subtype, IsOpen]) ->
    #setting{
        key = {Type, Subtype}
        , type = Type
        , subtype = Subtype
        , is_open = IsOpen
    }.

login_setting(#player_status{id = RoleId} = Player) ->
    % 加载默认配置
    CfgList = [tuple_to_list(T)||T<-data_setting:get_values()],
    % 数据库
    SqlList = db_player_setting_select(RoleId),
    F = fun(T, Map) ->
        Setting = make_record(setting, T),
        maps:put(Setting#setting.key, Setting, Map)
    end,
    SettingMap = lists:foldl(F, maps:new(), CfgList++SqlList),
    StatusSetting = #status_setting{setting_map = SettingMap},
    %% 前端引导事件
    ClientEvent = db_client_event_select(RoleId),
    Player#player_status{setting = StatusSetting, client_event = ClientEvent}.

db_player_setting_select(RoleId) ->
    Sql = io_lib:format(?sql_player_setting_select, [RoleId]),
    db:get_all(Sql).

db_player_setting_replace(RoleId, Setting) ->
    #setting{type = Type, subtype = Subtype, is_open = IsOpen} = Setting,
    Sql = io_lib:format(?sql_player_setting_replace, [RoleId, Type, Subtype, IsOpen]),
    db:execute(Sql).

db_client_event_select(RoleId) ->
    Sql = io_lib:format(?sql_client_event_select, [RoleId]),
    [
        {Id, State} || [Id, State] <- db:get_all(Sql)
    ].

db_client_event_replace(RoleId, Id, State) ->
    Sql = io_lib:format(?sql_client_event_replace, [RoleId, Id, State]),
    db:execute(Sql).

%% 获取开服合服时间
send_game_info(Player) ->
    OpenTime = util:get_open_time(),
    MergeTime = util:get_merge_time(),
    MergeStartTime = util:get_merge_day_start_time(),
    MergeCount = util:get_merge_count(),
    UnixDate = utime:unixdate(),
    WorldLv = util:get_world_lv(),
    {ok, BinData} = pt_102:write(10201, [OpenTime, MergeTime, MergeStartTime, MergeCount, UnixDate, WorldLv]),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData),
    ok.

%% 发送战场连杀信息
send_battle_continue_kill(RoleId, Kill) ->
    {ok, Bin} = pt_102:write(10208, [Kill]),
    lib_server_send:send_to_uid(RoleId, Bin).

%% 发送战场击杀信息
send_battle_kill_info(Scene, PoolId, CopyId, Killer, Dead, KillType) ->
    {ok, Bin} = pt_102:write(10209, [Killer, Dead, KillType]),
    lib_server_send:send_to_scene(Scene, PoolId, CopyId, Bin).
