/*
文件名:LukeClass.nut
路径:Dps_A/ProjectClass/Luke/LukeClass.nut
创建日期:2024-07-15	20:46
文件用途:卢克服务的文件
*/
class Luke {
    //频道
    Channel = 19;
    //城镇
    Town = 18;

    //服务端区域移动添加玩家HOOK 让大家不可见
    function insert_user_hook(C_Area, C_User) {
        //如果有城镇配置
        if (Town) {
            local SUser = User(C_User);
            if (SUser.GetLocation().Town == Town) {
                if (SUser.GetLocation().Area > 1) Sq_WriteAddress(C_Area, 0x68, 1);
            }
        }
    }
    //服务端区域移动HOOK 让玩家不可以直接去另一个区域
    function move_area_hook(CUser, TownIndex, AreaIndex) {
        // return true;//TODO
        if (!CUser) return true;
        local SUser = User(CUser);
        //卢克频道
        if (Sq_Game_GetConfig().find("19") != null) {
            if (AreaIndex <= 1) {
                return true;
            } else {
                local Jso = {
                    op = 20084023,
                    uid = SUser.GetUID(),
                    cid = SUser.GetCID(),
                    regionId = AreaIndex
                }
                Socket.SendGateway(Jso);

                return false;
            }
        } else {
            return true;
        }
    }

    //玩家发送消息HOOK 为了攻坚队频道和团长公告
    function base_input_hook(CUser, CmdString) {
        if (!CUser) return true;
        local SUser = User(CUser);
        print(111);
        //卢克频道
        if (Sq_Game_GetConfig().find("19") != null) {
            local Localtion = SUser.GetLocation();
            if (Localtion.Area <= 1) {
                return true;
            } else {
                local Jso = {
                    op = 20084027,
                    uid = SUser.GetUID(),
                    cid = SUser.GetCID(),
                    msg = CmdString
                }
                Socket.SendGateway(Jso);
                return false;
            }
        } else {
            return true;
        }
    }

    function base_input_hook2(args) {
        local type = args[2];
        local SUser = User(args[1]);
        local msg = args[3];

        if ((type == 8 || type == 3) && Sq_Game_GetConfig().find("19") != null) {

            local Localtion = SUser.GetLocation();
            if (Localtion.Area <= 1) {
                return true;
            } else {
                local Jso = {
                    op = 20084027,
                    uid = SUser.GetUID(),
                    cid = SUser.GetCID(),
                    msg = msg
                }
                Socket.SendGateway(Jso);
            }
        }
        return;
    }

    //玩家消息分发
    function PlayerNotiMsgDistribute(Jso) {
        local SUser = World.GetUserByUidCid(Jso.uid, Jso.cid);
        if (!SUser) return;
        local CUserList = Jso.list;
        local RealList = [];
        foreach(_i, obj in CUserList) {
            local SUser = World.GetUserByUidCid(obj.uid, obj.cid);
            if (SUser && SUser.GetState() >= 3) RealList.append(SUser);
        }
        local SUserName = SUser.GetCharacName();
        local Type = Jso.type;

        Jso.msg = Jso.msg;
        if (Type == -1) {
            Jso.Name <- SUserName;
            foreach(_Index, Value in RealList) {
                local SendObj = Value;
                SendObj.SendJso(Jso);
            }
            Type = "长"
        }
        local NotiStr = "(攻坚队" + Type + ") " + "" + SUserName + " ： " + Jso.msg;
        foreach(_Index, Value in RealList) {
            local SendObj = Value;
            SendObj.SendNotiPacketMessage(NotiStr, 8);
        }

    }


    function LukeSendAreaUserCallBack(Jso) {
        local CUserList = Jso.list;
        local RealList = [];
        foreach(_i, obj in CUserList) {
            local SUser = World.GetUserByUidCid(obj.uid, obj.cid);
            if (SUser && SUser.GetState() >= 3) RealList.append(SUser);
        }

        foreach(_Index, Value in RealList) {
            local SUser = Value;
            local Pack = Packet();
            Pack.Put_Header(0, 24);
            Pack.Put_Byte(SUser.GetLocation().Town); //城镇
            Pack.Put_Byte(SUser.GetArea(1)); //区域
            Pack.Put_Short((RealList.len() - 1)); //几个玩家  要减去自己
            foreach(__Index, MapObj in RealList) {
                if (SUser.GetUID() == MapObj.GetUID()) continue;
                Pack.Put_Short(MapObj.GetUniqueId());
                Pack.Put_Short(MapObj.GetAreaPos().X);
                Pack.Put_Short(MapObj.GetAreaPos().Y);
                Pack.Put_Byte(MapObj.GetDirections()); //朝向
                Pack.Put_Byte(MapObj.GetVisibleValues()); //是否可见
            }
            Pack.Put_Byte(1); //是否可见
            Pack.Finalize(true);
            SUser.Send(Pack);
            Pack.Delete();
        }
    }

    function LukePlayerMoveMapCallBack(Jso) {
        local MoveSUser = World.GetUserByUidCid(Jso.uid, Jso.cid);
        if (!MoveSUser) return;

        local CUserList = Jso.list;
        local RealList = [];
        foreach(_i, obj in CUserList) {
            local SUser = World.GetUserByUidCid(obj.uid, obj.cid);
            if (SUser && SUser.GetState() >= 3) RealList.append(SUser);
        }

        local Pack = Packet();
        Pack.Put_Header(0, 22);
        Pack.Put_Short(MoveSUser.GetUniqueId());
        Pack.Put_Short(Jso.XPos);
        Pack.Put_Short(Jso.YPos);
        Pack.Put_Byte(Jso.Direction);
        Pack.Put_Short(Jso.Code);
        Pack.Finalize(true);

        foreach(_Index, Value in RealList) {
            local SUser = Value;
            if (SUser.GetUniqueId() == MoveSUser.GetUniqueId()) continue;
            SUser.Send(Pack);
        }
        Pack.Delete();
    }

    function ClientGetConfigCallBack(SUser, Jso) {
        local evv = {
            op = 20084502,
            town_index = Town,
            channel_index = Channel
        }
        SUser.SendJso(evv);
    }

    function ClientCreateOrJoinParty(SUser, Jso) {
        Jso.uid <- SUser.GetUID();
        Jso.cid <- SUser.GetCID();
        Jso.PlayerName <- SUser.GetCharacName();
        Jso.PlayerLevel <- SUser.GetCharacLevel();
        Jso.PlayerJob <- SUser.GetCharacJob();
        Jso.PlayerGrowTypeJob <- SUser.GetCharacGrowType();
        Jso.IsPrepare <- false;
        Jso.PlayerSession <- World.GetSessionByUid(SUser.GetUID());
        Jso.PlayCoin <- SUser.GetCoin();
        Jso.PlayFatigue <- SUser.GetMaxFatigue() - SUser.GetFatigue();
        Socket.SendGateway(Jso);
    }

    //玩家上线
    function Login_Hook(SUser) {
        Timer.SetTimeOut(function(SUser) {
            //玩家上线发信息包
            local evv = {
                op = 20084502,
                town_index = Town,
                channel_index = Channel
            }
            SUser.SendJso(evv);
        }.bindenv(this), 5, SUser);

        local T = {
            op = 20084063,
            uid = SUser.GetUID(),
            cid = SUser.GetCID(),
        }
        Socket.SendGateway(T);
    }

    //团本配置信息获取回调
    function GetPluginConfig(Jso) {
        Town = Jso.Town;
    }

    constructor() {
        local ConfigPath = Sq_Game_GetConfig();
        Channel = ConfigPath.slice(ConfigPath.find("cfg/") + 4, ConfigPath.len());

        //注册HOOK
        Cb_Insert_User_Func.Luke <- insert_user_hook.bindenv(this); //区域添加角色
        Cb_Move_Area_Func.Luke <- move_area_hook.bindenv(this); //区域移动
        //Base_InputHookFunc_Handle.Luke <- base_input_hook.bindenv(this); //玩家发送消息
        Cb_reach_game_world_Func.Luke <- Login_Hook.bindenv(this); //上线HOOK
        Cb_Server_Chat_Log_Leave_Func.Luke <- base_input_hook2.bindenv(this); //玩家发送消息

        //注册收包
        GatewaySocketPackFuncMap.rawset(20084010, LukeSendAreaUserCallBack.bindenv(this)); //玩家移动后的区域广播包
        GatewaySocketPackFuncMap.rawset(20084012, LukePlayerMoveMapCallBack.bindenv(this)); //玩家移动回调
        //玩家消息分发
        GatewaySocketPackFuncMap.rawset(20084018, PlayerNotiMsgDistribute.bindenv(this)); //玩家打字发送的信息
        GatewaySocketPackFuncMap.rawset(20084778, GetPluginConfig.bindenv(this)); //服务端配置

        //注册来自客户端的收包
        ClientSocketPackFuncMap.rawset(20230718, ClientGetConfigCallBack.bindenv(this));
        ClientSocketPackFuncMap.rawset(20084501, ClientGetConfigCallBack.bindenv(this));
        ClientSocketPackFuncMap.rawset(20084001, ClientCreateOrJoinParty.bindenv(this));
        ClientSocketPackFuncMap.rawset(20084005, ClientCreateOrJoinParty.bindenv(this));
    }
}

ProjectInitFuncMap.P_Luke <- Luke();