%% ---------------------------------------------------------------------------
%% @doc pp_game.erl

%% @author  hjh
%% @email  hejiahua@163.com
%% @since  2016-12-23
%% @deprecated 游戏基础相关
%% ---------------------------------------------------------------------------
-module(pp_game).
-export([handle/3,sum_roles_online_time/3]).

-include("server.hrl").
-include("game.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("welfare.hrl").
-include("def_module.hrl").

%% 获取开服合服时间
handle(10201, Player, []) ->
    lib_game:send_game_info(Player),
    {ok, Player};

%% 系统开关设置列表
handle(10202, Player, [Type]) ->
    #player_status{setting = StatusSetting} = Player,
    #status_setting{setting_map = SettingMap} = StatusSetting,
    F = fun(_Key, Setting, List) ->
        #setting{type = TmpType, subtype = Subtype, is_open = IsOpen} = Setting,
        case TmpType == Type of
            true -> [{Subtype, IsOpen}|List];
            false -> List
        end
    end,
    List = maps:fold(F, [], SettingMap),
    {ok, BinData} = pt_102:write(10202, [Type, List]),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData);

%% 系统开关设置
handle(10203, Player, [Type, Subtype, IsOpen]) when IsOpen == 1 orelse IsOpen == 0 ->
    #player_status{id = RoleId, setting = StatusSetting} = Player,
    #status_setting{setting_map = SettingMap} = StatusSetting,
    Keys = data_setting:get_keys(),
    case lists:member({Type, Subtype}, Keys) of
        true ->
            ErrorCode = ?SUCCESS,
            Setting = lib_game:make_record(setting, [Type, Subtype, IsOpen]),
            lib_game:db_player_setting_replace(RoleId, Setting),
            NewSettingMap = maps:put(Setting#setting.key, Setting, SettingMap);
        false ->
            ErrorCode = ?ERRCODE(err102_setting_key_not_exist),
            NewSettingMap = SettingMap
    end,
    NewPlayer = Player#player_status{setting = StatusSetting#status_setting{setting_map = NewSettingMap} },
    {ok, BinData} = pt_102:write(10203, [ErrorCode, Type, Subtype, IsOpen]),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData),
    {ok, NewPlayer};

%% 上传客户端版本号
handle(10204, Player, [ClientVer]) ->
    NewPlayer = Player#player_status{client_ver = ClientVer},
    {ok, BinData} = pt_102:write(10204, [?SUCCESS]),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData),
    {ok, NewPlayer};

handle(10206,Player,[0])->
    #player_status{
        id=RoleId,
        accid=AccId,
        accname=AccName
    }=Player,
    RoleList = lib_login:get_roles_attr(AccId, AccName),
    TotalsTime = welfare_api:get_now_online_time(Player),
    Total    = sum_roles_online_time(RoleList,RoleId,0),
    {ok,Bin} = pt_102:write(10206,[0,TotalsTime+Total]),
    lib_server_send:send_to_uid(RoleId,Bin),
    {ok,Player};
handle(10206,Player,[_OpTy])->
    #player_status{id = RoleId} = Player,
    TotalsTime = welfare_api:get_now_online_time(Player),
    {ok,Bin} = pt_102:write(10206,[_OpTy,TotalsTime]),
    lib_server_send:send_to_uid(RoleId,Bin),
    {ok,Player};

%% 终身引导的事件列表
handle(10210, Player, [])->
    #player_status{client_event = ClientEvent, sid = Sid} = Player,
    {ok,Bin} = pt_102:write(10210, [ClientEvent]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok,Player};
%% 终身引导的事件状态变更:初步限定，id大于0小于100
handle(10211, Player, [Id, State]) when Id > 0 andalso Id =< 100 ->
    #player_status{client_event = ClientEvent, sid = Sid, id = RoleId} = Player,
    NewClientEvent =
        case lists:keytake(Id, 1, ClientEvent) of
            {value, _Left, {Id, State}} ->
                ClientEvent;
            {value, Left, _} ->
                lib_game:db_client_event_replace(RoleId, Id, State),
                [{Id, State} | Left];
            _ ->
                lib_game:db_client_event_replace(RoleId, Id, State),
                [{Id, State} | ClientEvent]
        end,
    {ok,Bin} = pt_102:write(10211, [Id, State]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok, Player#player_status{client_event = NewClientEvent}};


handle(_Cmd, _Player, _Data) ->
    {error, atom_to_list(?MODULE) ++ " no match"}.



sum_roles_online_time([],_RoleId,Total)->
    Total;
sum_roles_online_time([[Id|_]|RoleList],RoleId,Total)->
    case Id /= RoleId of
        true->
            case db:get_row(io_lib:format(?sql_select_online, [Id,?MOD_WELFARE,?COUNTER_WELFARE_ONLINE_TIME])) of
                [TotalsTime, LastUpdate] ->
                    case utime:is_today(LastUpdate) of
                        true->
                            sum_roles_online_time(RoleList,RoleId,Total+TotalsTime);
                        _->
                            sum_roles_online_time(RoleList,RoleId,Total)
                    end;
                _ ->
                    sum_roles_online_time(RoleList,RoleId,Total)
            end;
        _->
            sum_roles_online_time(RoleList,RoleId,Total)
    end.