%%% -------------------------------------------------------
%%% @author huangyongxing@yeah.net
%%% @doc
%%% @end
%%% -------------------------------------------------------
-module(cli_pt_100).
-export([
        encode/2
        , decode/2
    ]).

%% 数据封包

%% 账号验证
encode(10000, [AccId, Time, AccName, Sign]) ->
    BinAccount  = pt:write_string(AccName),
    BinSign     = pt:write_string(Sign),
    {ok, <<AccId:32, Time:32, BinAccount/binary, BinSign/binary>>};

%% 创建角色
encode(10003, [Realm, Career, Sex, Name, Source]) ->
    NameBin = pt:write_string(Name),
    SourceBin = pt:write_string(Source),
    {ok, <<Realm:8, Career:8, Sex:8, NameBin/binary, SourceBin/binary>>};

%% 选择角色进入游戏
encode(10004, [RoleId, SvrName, Time, Ticket, NetworkType]) ->
    SvrNameBin = pt:write_string(SvrName),
    TicketBin = pt:write_string(Ticket),
    {ok, <<RoleId:64, SvrNameBin/binary, Time:32, TicketBin/binary, NetworkType:8>>};

encode(10006, []) ->
    {ok, <<>>};

encode(_Cmd, _Data) ->
    {error, unknown_data}.

%% 数据解包

%% 账号验证
decode(10000, <<_Career:8, _Time:64, ListBin/binary>>) ->
    {List, _Rest} = pt:read_array(fun cli_login:read_single_role/1, ListBin),
    {ok, List};

%% 创建角色
decode(10003, <<Code:8, Id:64>>) ->
    {ok, [Code, Id]};

%% 选择角色进入游戏
decode(10004, <<Result:8>>) ->
    {ok, Result};

%% 收到心跳回应应当忽略（靠定时器做定期心跳）
decode(10006, <<>>) ->
    ignore;

decode(_Cmd, _BinData) ->
    ignore.

