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

read(60100, _) ->
    {ok, []};
read(60101, _) ->
    {ok, []};
read(60102, _) ->
    {ok, []};
read(60103, _) ->
    {ok, []};
read(60104, Bin0) ->
    <<ServerId:64, Bin1/binary>> = Bin0, 
    <<GuildId:64, _Bin2/binary>> = Bin1, 
    {ok, [ServerId, GuildId]};
read(60105, Bin0) ->
    <<Opty:8, _Bin1/binary>> = Bin0, 
    {ok, [Opty]};
read(60106, Bin0) ->
    <<Opty:8, _Bin1/binary>> = Bin0, 
    {ok, [Opty]};
read(60107, Bin0) ->
    <<CityId:8, _Bin1/binary>> = Bin0, 
    {ok, [CityId]};
read(60108, _) ->
    {ok, []};
read(60109, _) ->
    {ok, []};
read(60110, _) ->
    {ok, []};
read(60112, Bin0) ->
    <<CityId:8, _Bin1/binary>> = Bin0, 
    {ok, [CityId]};
read(60115, _) ->
    {ok, []};
read(60120, Bin0) ->
    <<RoleId:64, _Bin1/binary>> = Bin0, 
    {ok, [RoleId]};
read(60121, Bin0) ->
    <<FactId:8, Bin1/binary>> = Bin0, 
    {Name, _Bin2} = pt:read_string(Bin1), 
    {ok, [FactId, Name]};
read(60122, _) ->
    {ok, []};
read(60123, _) ->
    {ok, []};
read(60124, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (60100,[
    Switch,
    State,
    Etime
] = Src_Data) ->
    Data = <<
        Switch:8,
        State:8,
        Etime:32
    >>,
    {ok, pt:encode(60100, Data, Src_Data)};

write (60101,[
    CityList
] = Src_Data) ->
    BinList_CityList = [
        item_to_bin_0(CityList_Item) || CityList_Item <- CityList
    ], 

    CityList_Len = length(CityList), 
    Bin_CityList = list_to_binary(BinList_CityList),

    Data = <<
        CityList_Len:16, Bin_CityList/binary
    >>,
    {ok, pt:encode(60101, Data, Src_Data)};

write (60102,[
    IsSignUp,
    LastSign,
    GuildList
] = Src_Data) ->
    BinList_GuildList = [
        item_to_bin_1(GuildList_Item) || GuildList_Item <- GuildList
    ], 

    GuildList_Len = length(GuildList), 
    Bin_GuildList = list_to_binary(BinList_GuildList),

    Data = <<
        IsSignUp:8,
        LastSign:32,
        GuildList_Len:16, Bin_GuildList/binary
    >>,
    {ok, pt:encode(60102, Data, Src_Data)};

write (60103,[
    FreeHelpNum,
    CurHelpNum,
    HelpList
] = Src_Data) ->
    BinList_HelpList = [
        item_to_bin_2(HelpList_Item) || HelpList_Item <- HelpList
    ], 

    HelpList_Len = length(HelpList), 
    Bin_HelpList = list_to_binary(BinList_HelpList),

    Data = <<
        FreeHelpNum:8,
        CurHelpNum:16,
        HelpList_Len:16, Bin_HelpList/binary
    >>,
    {ok, pt:encode(60103, Data, Src_Data)};

write (60104,[
    Result,
    GuildId
] = Src_Data) ->
    Data = <<
        Result:32,
        GuildId:64
    >>,
    {ok, pt:encode(60104, Data, Src_Data)};

write (60105,[
    Opty,
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_3(RankList_Item) || RankList_Item <- RankList
    ], 

    RankList_Len = length(RankList), 
    Bin_RankList = list_to_binary(BinList_RankList),

    Data = <<
        Opty:8,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(60105, Data, Src_Data)};

write (60106,[
    Result,
    Opty,
    Help
] = Src_Data) ->
    Data = <<
        Result:32,
        Opty:8,
        Help:8
    >>,
    {ok, pt:encode(60106, Data, Src_Data)};

write (60107,[
    CityId,
    Result
] = Src_Data) ->
    Data = <<
        CityId:8,
        Result:32
    >>,
    {ok, pt:encode(60107, Data, Src_Data)};

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

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

write (60110,[
    CityInfo
] = Src_Data) ->
    BinList_CityInfo = [
        item_to_bin_4(CityInfo_Item) || CityInfo_Item <- CityInfo
    ], 

    CityInfo_Len = length(CityInfo), 
    Bin_CityInfo = list_to_binary(BinList_CityInfo),

    Data = <<
        CityInfo_Len:16, Bin_CityInfo/binary
    >>,
    {ok, pt:encode(60110, Data, Src_Data)};

write (60111,[
    GuildInfo
] = Src_Data) ->
    BinList_GuildInfo = [
        item_to_bin_5(GuildInfo_Item) || GuildInfo_Item <- GuildInfo
    ], 

    GuildInfo_Len = length(GuildInfo), 
    Bin_GuildInfo = list_to_binary(BinList_GuildInfo),

    Data = <<
        GuildInfo_Len:16, Bin_GuildInfo/binary
    >>,
    {ok, pt:encode(60111, Data, Src_Data)};

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

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

write (60114,[
    OccupyCity,
    CityList
] = Src_Data) ->
    BinList_OccupyCity = [
        item_to_bin_6(OccupyCity_Item) || OccupyCity_Item <- OccupyCity
    ], 

    OccupyCity_Len = length(OccupyCity), 
    Bin_OccupyCity = list_to_binary(BinList_OccupyCity),

    BinList_CityList = [
        item_to_bin_7(CityList_Item) || CityList_Item <- CityList
    ], 

    CityList_Len = length(CityList), 
    Bin_CityList = list_to_binary(BinList_CityList),

    Data = <<
        OccupyCity_Len:16, Bin_OccupyCity/binary,
        CityList_Len:16, Bin_CityList/binary
    >>,
    {ok, pt:encode(60114, Data, Src_Data)};

write (60115,[
    Logs
] = Src_Data) ->
    BinList_Logs = [
        item_to_bin_8(Logs_Item) || Logs_Item <- Logs
    ], 

    Logs_Len = length(Logs), 
    Bin_Logs = list_to_binary(BinList_Logs),

    Data = <<
        Logs_Len:16, Bin_Logs/binary
    >>,
    {ok, pt:encode(60115, Data, Src_Data)};

write (60116,[
    FactId,
    ServerNum,
    Name,
    City
] = Src_Data) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        FactId:8,
        ServerNum:16,
        Bin_Name/binary,
        City:8
    >>,
    {ok, pt:encode(60116, Data, Src_Data)};

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

write (60118,[
    CityName
] = Src_Data) ->
    Bin_CityName = pt:write_string(CityName), 

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

write (60119,[
    GuildName,
    CityName
] = Src_Data) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_CityName = pt:write_string(CityName), 

    Data = <<
        Bin_GuildName/binary,
        Bin_CityName/binary
    >>,
    {ok, pt:encode(60119, Data, Src_Data)};

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

write (60121,[
    Result,
    FactId,
    Name
] = Src_Data) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Result:32,
        FactId:8,
        Bin_Name/binary
    >>,
    {ok, pt:encode(60121, Data, Src_Data)};

write (60122,[
    Result,
    IsBuy,
    Reward,
    Price,
    Display
] = Src_Data) ->
    BinList_Reward = [
        item_to_bin_10(Reward_Item) || Reward_Item <- Reward
    ], 

    Reward_Len = length(Reward), 
    Bin_Reward = list_to_binary(BinList_Reward),

    BinList_Price = [
        item_to_bin_11(Price_Item) || Price_Item <- Price
    ], 

    Price_Len = length(Price), 
    Bin_Price = list_to_binary(BinList_Price),

    Data = <<
        Result:32,
        IsBuy:8,
        Reward_Len:16, Bin_Reward/binary,
        Price_Len:16, Bin_Price/binary,
        Display:16
    >>,
    {ok, pt:encode(60122, Data, Src_Data)};

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

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

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


item_to_bin_0 ({
    CityId,
    ServerId,
    FactId,
    ServerNum,
    GuildId,
    GuildName,
    GuildPower,
    ChiefId,
    ChiefName,
    ChiefPower,
    IsReceivedReward
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_ChiefName = pt:write_string(ChiefName), 

    Data = <<
        CityId:8,
        ServerId:16,
        FactId:16,
        ServerNum:16,
        GuildId:64,
        Bin_GuildName/binary,
        GuildPower:64,
        ChiefId:64,
        Bin_ChiefName/binary,
        ChiefPower:32,
        IsReceivedReward:8
    >>,
    Data.
item_to_bin_1 ({
    ServerId,
    ServerNum,
    FactId,
    GuildId,
    GuildName,
    GuildPower,
    ChiefName
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_ChiefName = pt:write_string(ChiefName), 

    Data = <<
        ServerId:16,
        ServerNum:16,
        FactId:16,
        GuildId:64,
        Bin_GuildName/binary,
        GuildPower:64,
        Bin_ChiefName/binary
    >>,
    Data.
item_to_bin_2 ({
    ServerId,
    ServerNum,
    FactId,
    GuildId,
    GuildName,
    GuildPower,
    ChiefName,
    ChiefPower,
    HelpNum,
    MyHelpNum
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_ChiefName = pt:write_string(ChiefName), 

    Data = <<
        ServerId:64,
        ServerNum:16,
        FactId:16,
        GuildId:64,
        Bin_GuildName/binary,
        GuildPower:64,
        Bin_ChiefName/binary,
        ChiefPower:64,
        HelpNum:16,
        MyHelpNum:16
    >>,
    Data.
item_to_bin_3 ({
    RoleName,
    RolePower,
    Sex,
    Lv,
    VipFlag,
    SupVipType,
    Position
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        Bin_RoleName/binary,
        RolePower:64,
        Sex:8,
        Lv:16,
        VipFlag:8,
        SupVipType:8,
        Position:8
    >>,
    Data.
item_to_bin_4 ({
    CityId,
    Blood,
    MaxBlood,
    GuildId,
    Group,
    GuildName,
    GuardNum
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Data = <<
        CityId:8,
        Blood:64,
        MaxBlood:64,
        GuildId:64,
        Group:8,
        Bin_GuildName/binary,
        GuardNum:8
    >>,
    Data.
item_to_bin_5 ({
    GuildId,
    Group
}) ->
    Data = <<
        GuildId:64,
        Group:16
    >>,
    Data.
item_to_bin_6 (
    CityId
) ->
    Data = <<
        CityId:8
    >>,
    Data.
item_to_bin_7 ({
    CityId,
    ServerNum,
    GuildName,
    ChiefName,
    ChiefInfo
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_ChiefName = pt:write_string(ChiefName), 

    Bin_ChiefInfo = pt:write_picture(ChiefInfo), 

    Data = <<
        CityId:8,
        ServerNum:16,
        Bin_GuildName/binary,
        Bin_ChiefName/binary,
        Bin_ChiefInfo/binary
    >>,
    Data.
item_to_bin_8 ({
    Time,
    CityList
}) ->
    BinList_CityList = [
        item_to_bin_9(CityList_Item) || CityList_Item <- CityList
    ], 

    CityList_Len = length(CityList), 
    Bin_CityList = list_to_binary(BinList_CityList),

    Data = <<
        Time:32,
        CityList_Len:16, Bin_CityList/binary
    >>,
    Data.
item_to_bin_9 ({
    CityId,
    ServerNum,
    GuildName,
    ChiefName,
    ChiefInfo
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_ChiefName = pt:write_string(ChiefName), 

    Bin_ChiefInfo = pt:write_picture(ChiefInfo), 

    Data = <<
        CityId:8,
        ServerNum:16,
        Bin_GuildName/binary,
        Bin_ChiefName/binary,
        Bin_ChiefInfo/binary
    >>,
    Data.
item_to_bin_10 ({
    Type,
    TypeId,
    Num
}) ->
    Data = <<
        Type:8,
        TypeId:32,
        Num:32
    >>,
    Data.
item_to_bin_11 ({
    Type,
    TypeId,
    Num
}) ->
    Data = <<
        Type:8,
        TypeId:32,
        Num:32
    >>,
    Data.
