-module(pt_130).
-include("common.hrl").
-export([
        read/2,
        write/2
    ]).

read(13001, _) ->
    {ok, []};
read(13004, Bin0) ->
    <<RoleId:64, _Bin1/binary>> = Bin0, 
    {ok, [RoleId]};
read(13006, _) ->
    {ok, []};
read(13007, _) ->
    {ok, []};
read(13008, Bin0) ->
    <<Pos:8, Bin1/binary>> = Bin0, 
    <<SType:8, Bin2/binary>> = Bin1, 
    <<Type:8, Bin3/binary>> = Bin2, 
    <<Id:32, Bin4/binary>> = Bin3, 
    <<Auto:8, _Bin5/binary>> = Bin4, 
    {ok, [Pos, SType, Type, Id, Auto]};
read(13009, _) ->
    {ok, []};
read(13010, Bin0) ->
    <<Pos1:8, Bin1/binary>> = Bin0, 
    <<Pos2:8, Bin2/binary>> = Bin1, 
    <<Type:8, _Bin3/binary>> = Bin2, 
    {ok, [Pos1, Pos2, Type]};
read(13011, _) ->
    {ok, []};
read(13012, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(13013, Bin0) ->
    <<Server:16, Bin1/binary>> = Bin0, 
    <<RoleId:64, Bin2/binary>> = Bin1, 
    <<ModId:16, _Bin3/binary>> = Bin2, 
    {ok, [Server, RoleId, ModId]};
read(13014, Bin0) ->
    <<Server:16, Bin1/binary>> = Bin0, 
    <<GuildId:64, Bin2/binary>> = Bin1, 
    <<ModId:16, _Bin3/binary>> = Bin2, 
    {ok, [Server, GuildId, ModId]};
read(13016, _) ->
    {ok, []};
read(13017, Bin0) ->
    <<Mod:16, Bin1/binary>> = Bin0, 
    <<RoleId:64, _Bin2/binary>> = Bin1, 
    {ok, [Mod, RoleId]};
read(13034, _) ->
    {ok, []};
read(13035, Bin0) ->
    <<Num:16, _Bin1/binary>> = Bin0, 
    {ok, [Num]};
read(13051, _) ->
    {ok, []};
read(13080, _) ->
    {ok, []};
read(13082, _) ->
    {ok, []};
read(13083, _) ->
    {ok, []};
read(13084, Bin0) ->
    <<Longitude:32, Bin1/binary>> = Bin0, 
    <<Latitude:32, _Bin2/binary>> = Bin1, 
    {ok, [Longitude, Latitude]};
read(13086, _) ->
    {ok, []};
read(13087, _) ->
    {ok, []};
read(13088, _) ->
    {ok, []};
read(13089, Bin0) ->
    <<Id:32, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(13090, Bin0) ->
    <<Id:32, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(13091, Bin0) ->
    <<RoleId:64, Bin1/binary>> = Bin0, 
    <<Source:8, _Bin2/binary>> = Bin1, 
    {ok, [RoleId, Source]};
read(_Cmd, _R) -> {error, no_match}.

write (13000,[
    Res
] = Src_Data) ->
    Data = <<
        Res:8
    >>,
    {ok, pt:encode(13000, Data, Src_Data)};

write (13001,[
    RoleId,
    Platform,
    ServerId,
    ServerNum,
    ServerName,
    Figure,
    BattleAttr,
    Scene,
    X,
    Y,
    DungeonId,
    Exp,
    ExpLim,
    Gold,
    Bgold,
    Fgold,
    Coin,
    Honour,
    GuildG,
    Power,
    GuildId,
    GuildName,
    PkCd,
    PkValue,
    TeamId,
    MateId,
    RTime,
    MateTitle,
    GodId,
    GodHp,
    GodHpLim
] = Src_Data) ->
    Bin_Platform = pt:write_string(Platform), 

    Bin_ServerName = pt:write_string(ServerName), 

    Bin_Figure = pt:write_figure(Figure), 

    Bin_BattleAttr = pt:write_battle_attr(BattleAttr), 

    Bin_GuildName = pt:write_string(GuildName), 

    Data = <<
        RoleId:64,
        Bin_Platform/binary,
        ServerId:16,
        ServerNum:16,
        Bin_ServerName/binary,
        Bin_Figure/binary,
        Bin_BattleAttr/binary,
        Scene:32,
        X:16,
        Y:16,
        DungeonId:32,
        Exp:64,
        ExpLim:64,
        Gold:32,
        Bgold:32,
        Fgold:32,
        Coin:32,
        Honour:32,
        GuildG:32,
        Power:64,
        GuildId:64,
        Bin_GuildName/binary,
        PkCd:16,
        PkValue:16,
        TeamId:64,
        MateId:64,
        RTime:32,
        MateTitle:64,
        GodId:16,
        GodHp:64,
        GodHpLim:64
    >>,
    {ok, pt:encode(13001, Data, Src_Data)};

write (13002,[
    Exp
] = Src_Data) ->
    Data = <<
        Exp:64
    >>,
    {ok, pt:encode(13002, Data, Src_Data)};

write (13003,[
    Lv,
    Exp,
    ExpLim
] = Src_Data) ->
    Data = <<
        Lv:16,
        Exp:64,
        ExpLim:64
    >>,
    {ok, pt:encode(13003, Data, Src_Data)};

write (13004,[
    Figure,
    ServerNum,
    EquipList,
    ModulePower,
    ModulePowerMine,
    MateRole,
    MateStage,
    MateStar,
    Online
] = Src_Data) ->
    Bin_Figure = pt:write_figure(Figure), 

    BinList_EquipList = [
        item_to_bin_0(EquipList_Item) || EquipList_Item <- EquipList
    ], 

    EquipList_Len = length(EquipList), 
    Bin_EquipList = list_to_binary(BinList_EquipList),

    BinList_ModulePower = [
        item_to_bin_2(ModulePower_Item) || ModulePower_Item <- ModulePower
    ], 

    ModulePower_Len = length(ModulePower), 
    Bin_ModulePower = list_to_binary(BinList_ModulePower),

    BinList_ModulePowerMine = [
        item_to_bin_3(ModulePowerMine_Item) || ModulePowerMine_Item <- ModulePowerMine
    ], 

    ModulePowerMine_Len = length(ModulePowerMine), 
    Bin_ModulePowerMine = list_to_binary(BinList_ModulePowerMine),

    Bin_MateRole = pt:write_string(MateRole), 

    Data = <<
        Bin_Figure/binary,
        ServerNum:16,
        EquipList_Len:16, Bin_EquipList/binary,
        ModulePower_Len:16, Bin_ModulePower/binary,
        ModulePowerMine_Len:16, Bin_ModulePowerMine/binary,
        Bin_MateRole/binary,
        MateStage:8,
        MateStar:8,
        Online:8
    >>,
    {ok, pt:encode(13004, Data, Src_Data)};

write (13005,[
    Type
] = Src_Data) ->
    Data = <<
        Type:8
    >>,
    {ok, pt:encode(13005, Data, Src_Data)};

write (13006,[
    Coin,
    Gold,
    Bgold,
    Fgold,
    Gfame,
    Honor
] = Src_Data) ->
    Data = <<
        Coin:32,
        Gold:32,
        Bgold:32,
        Fgold:32,
        Gfame:32,
        Honor:32
    >>,
    {ok, pt:encode(13006, Data, Src_Data)};

write (13007,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_4(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(13007, Data, Src_Data)};

write (13008,[
    Res
] = Src_Data) ->
    Data = <<
        Res:8
    >>,
    {ok, pt:encode(13008, Data, Src_Data)};

write (13009,[
    Res
] = Src_Data) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:encode(13009, Data, Src_Data)};

write (13010,[
    Res
] = Src_Data) ->
    Data = <<
        Res:8
    >>,
    {ok, pt:encode(13010, Data, Src_Data)};

write (13011,[
    ExpPlus,
    WorldLv
] = Src_Data) ->
    Data = <<
        ExpPlus:16,
        WorldLv:16
    >>,
    {ok, pt:encode(13011, Data, Src_Data)};

write (13012,[
    Res,
    Type,
    Cd
] = Src_Data) ->
    Data = <<
        Res:32,
        Type:8,
        Cd:32
    >>,
    {ok, pt:encode(13012, Data, Src_Data)};

write (13013,[
    Server,
    RoleId,
    ModId,
    Power,
    Figure,
    ServerName
] = Src_Data) ->
    Bin_Figure = pt:write_figure(Figure), 

    Bin_ServerName = pt:write_string(ServerName), 

    Data = <<
        Server:16,
        RoleId:64,
        ModId:16,
        Power:32,
        Bin_Figure/binary,
        Bin_ServerName/binary
    >>,
    {ok, pt:encode(13013, Data, Src_Data)};

write (13014,[
    Server,
    GuildId,
    ModId,
    RoleId,
    Figure
] = Src_Data) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Server:16,
        GuildId:64,
        ModId:16,
        RoleId:64,
        Bin_Figure/binary
    >>,
    {ok, pt:encode(13014, Data, Src_Data)};

write (13015,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_6(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(13015, Data, Src_Data)};

write (13016,[
    Cur,
    Max
] = Src_Data) ->
    Data = <<
        Cur:8,
        Max:8
    >>,
    {ok, pt:encode(13016, Data, Src_Data)};

write (13017,[
    Mod,
    Picture
] = Src_Data) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Mod:16,
        Bin_Picture/binary
    >>,
    {ok, pt:encode(13017, Data, Src_Data)};

write (13020,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_7(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(13020, Data, Src_Data)};

write (13032,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_8(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(13032, Data, Src_Data)};

write (13033,[
    Power,
    BattleAttr
] = Src_Data) ->
    Bin_BattleAttr = pt:write_battle_attr(BattleAttr), 

    Data = <<
        Power:64,
        Bin_BattleAttr/binary
    >>,
    {ok, pt:encode(13033, Data, Src_Data)};

write (13034,[
    PkValue,
    Time
] = Src_Data) ->
    Data = <<
        PkValue:16,
        Time:32
    >>,
    {ok, pt:encode(13034, Data, Src_Data)};

write (13035,[
    Res,
    Num,
    CurNum
] = Src_Data) ->
    Data = <<
        Res:32,
        Num:16,
        CurNum:16
    >>,
    {ok, pt:encode(13035, Data, Src_Data)};

write (13036,[
    Type,
    Exp,
    Ratio
] = Src_Data) ->
    Data = <<
        Type:16,
        Exp:64,
        Ratio:16
    >>,
    {ok, pt:encode(13036, Data, Src_Data)};

write (13040,[
    Career,
    Sex,
    Turn,
    TurnStage
] = Src_Data) ->
    Data = <<
        Career:8,
        Sex:8,
        Turn:8,
        TurnStage:8
    >>,
    {ok, pt:encode(13040, Data, Src_Data)};

write (13041,[
    TurnStage
] = Src_Data) ->
    Data = <<
        TurnStage:8
    >>,
    {ok, pt:encode(13041, Data, Src_Data)};

write (13051,[
] = Src_Data) ->
    Data = <<
    >>,
    {ok, pt:encode(13051, Data, Src_Data)};

write (13080,[
    Accname
] = Src_Data) ->
    Bin_Accname = pt:write_string(Accname), 

    Data = <<
        Bin_Accname/binary
    >>,
    {ok, pt:encode(13080, Data, Src_Data)};

write (13082,[
    Res
] = Src_Data) ->
    Data = <<
        Res:8
    >>,
    {ok, pt:encode(13082, Data, Src_Data)};

write (13083,[
    Res,
    Ver,
    Path
] = Src_Data) ->
    Bin_Path = pt:write_string(Path), 

    Data = <<
        Res:8,
        Ver:32,
        Bin_Path/binary
    >>,
    {ok, pt:encode(13083, Data, Src_Data)};

write (13084,[
    Res
] = Src_Data) ->
    Data = <<
        Res:8
    >>,
    {ok, pt:encode(13084, Data, Src_Data)};

write (13085,[
    RoleId,
    Path,
    Ver
] = Src_Data) ->
    Bin_Path = pt:write_string(Path), 

    Data = <<
        RoleId:64,
        Bin_Path/binary,
        Ver:32
    >>,
    {ok, pt:encode(13085, Data, Src_Data)};

write (13086,[
    Anger,
    AngerMax
] = Src_Data) ->
    Data = <<
        Anger:16,
        AngerMax:16
    >>,
    {ok, pt:encode(13086, Data, Src_Data)};

write (13087,[
    ServerTime,
    LoginTimeBeforeLast,
    LastLoginTime,
    LastLogoutTime
] = Src_Data) ->
    Data = <<
        ServerTime:32,
        LoginTimeBeforeLast:32,
        LastLoginTime:32,
        LastLogoutTime:32
    >>,
    {ok, pt:encode(13087, Data, Src_Data)};

write (13088,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_9(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(13088, Data, Src_Data)};

write (13089,[
    Res,
    Id
] = Src_Data) ->
    Data = <<
        Res:32,
        Id:32
    >>,
    {ok, pt:encode(13089, Data, Src_Data)};

write (13090,[
    Res,
    Id
] = Src_Data) ->
    Data = <<
        Res:32,
        Id:32
    >>,
    {ok, pt:encode(13090, Data, Src_Data)};

write (13091,[
    Source,
    Role
] = Src_Data) ->
    Bin_Role = pt:write_picture(Role), 

    Data = <<
        Source:8,
        Bin_Role/binary
    >>,
    {ok, pt:encode(13091, Data, Src_Data)};

write(_Cmd, _R) -> ?DEBUG("no_match:~p,~p~n", [_Cmd, _R]), {ok, pt:pack(0, <<>>)}.


item_to_bin_0 ({
    GoodsId,
    TypeId,
    Cell,
    OtherData
}) ->
    BinList_OtherData = [
        item_to_bin_1(OtherData_Item) || OtherData_Item <- OtherData
    ], 

    OtherData_Len = length(OtherData), 
    Bin_OtherData = list_to_binary(BinList_OtherData),

    Data = <<
        GoodsId:64,
        TypeId:32,
        Cell:16,
        OtherData_Len:16, Bin_OtherData/binary
    >>,
    Data.
item_to_bin_1 ({
    Type,
    Info
}) ->
    Bin_Info = pt:write_string(Info), 

    Data = <<
        Type:8,
        Bin_Info/binary
    >>,
    Data.
item_to_bin_2 ({
    Type,
    Power
}) ->
    Data = <<
        Type:8,
        Power:32
    >>,
    Data.
item_to_bin_3 ({
    Type,
    Power
}) ->
    Data = <<
        Type:8,
        Power:32
    >>,
    Data.
item_to_bin_4 ({
    Type,
    SkillList
}) ->
    BinList_SkillList = [
        item_to_bin_5(SkillList_Item) || SkillList_Item <- SkillList
    ], 

    SkillList_Len = length(SkillList), 
    Bin_SkillList = list_to_binary(BinList_SkillList),

    Data = <<
        Type:8,
        SkillList_Len:16, Bin_SkillList/binary
    >>,
    Data.
item_to_bin_5 ({
    Pos,
    SType,
    Id,
    Auto
}) ->
    Data = <<
        Pos:8,
        SType:8,
        Id:32,
        Auto:8
    >>,
    Data.
item_to_bin_6 ({
    Type,
    Value
}) ->
    Bin_Value = pt:write_string(Value), 

    Data = <<
        Type:8,
        Bin_Value/binary
    >>,
    Data.
item_to_bin_7 (
    SkillId
) ->
    Data = <<
        SkillId:32
    >>,
    Data.
item_to_bin_8 ({
    Type,
    Value
}) ->
    Data = <<
        Type:8,
        Value:32
    >>,
    Data.
item_to_bin_9 (
    Id
) ->
    Data = <<
        Id:32
    >>,
    Data.
