/*
文件名:SocketBase.nut
路径:BaseClass/Socket/SocketBase.nut
创建日期:2024-04-10	10:13
文件用途:套接字连接
*/
//todo 公告接口

class Socket {

    /*
     * @函数作用: 发送包给插件网关
     * @参数 Table
     * @返回值
     */
    function SendGateway(T) {
        local str = Json.Encode(T);
        if (PacketDebugModel) print("发送包给网关: " + str);
        if (getroottable().rawin("Sq_SendPackToGateway")) Sq_SendPackToGateway(str);
    }


}

//网关链接建立回调Map
if (!getroottable().rawin("OnGatewaySocketConnectFunc")) OnGatewaySocketConnectFunc <- {};
//插件网关建立连接时
function OnGatewaySocketConnect() {
    print("############凌众插件连接建立############");
    print("############凌众插件连接建立############");
    print("############凌众插件连接建立############");
    print("############凌众插件连接建立############");
    local Jso = {
        op = 10001,
        channel = Sq_Game_GetConfig(),
        type = 1
    }
    Socket.SendGateway(Jso);

    foreach(value in OnGatewaySocketConnectFunc) {
        value();
    }
}
//网关包回调Map
if (!getroottable().rawin("GatewaySocketPackFuncMap")) GatewaySocketPackFuncMap <- {}
//插件网关收到包时
function OnGatewaySocketMsg(Msg) {
    if (PacketDebugModel) print("收到网关包:" + Msg);
    local Jso = Json.Decode(Msg);
    if (!("op" in Jso)) {
        if (PacketDebugModel) print("收到空op包");
        return;
    }
    if (Jso.op in GatewaySocketPackFuncMap) {
        GatewaySocketPackFuncMap[Jso.op](Jso);
    } else {
        // print(Msg);
        local SUser = World.GetUserByUidCid(Jso.uid, Jso.cid);
        if (SUser) {
            local Pack = Packet();
            Pack.Put_Header(1, 130);
            Pack.Put_Byte(1);
            Pack.Put_Int(Msg.len());
            Pack.Put_Binary(Msg);
            Pack.Finalize(true);
            SUser.Send(Pack);
            Pack.Delete();
        }
    }
}



//公用接口
/*
 * @函数作用: 调用玩家移动城镇
 * @参数 Jso  需要参数uid cid town regionId
 * @返回值
 */
GatewaySocketPackFuncMap[20240408] <- function(Jso) {
    local SUser = World.GetUserByUidCid(Jso.uid, Jso.cid);
    if (SUser) {
        World.MoveArea(SUser, Jso.town, Jso.regionId, Jso.X, Jso.Y);
    }
}
/*
 * @函数作用: 让指定玩家组队并进入副本
 * @参数 Jso  需要参数uid cid Member   最外层uid cid 确定队长  Member 里面是一个uid 和 cid 信息对象的List  注意队长不要放进去
 * @返回值
 */
GatewaySocketPackFuncMap[20240416] <- function(Jso) {
    local SUser = World.GetUserByUidCid(Jso.uid, Jso.cid);
    if (SUser) {
        local Gm = GameManager();
        local SParty = Gm.GetParty();
        SParty.Create(SUser);
        foreach(_Index, PlayerInfo in Jso.Member) {
            local UserBuf = World.GetUserByUidCid(PlayerInfo.uid, PlayerInfo.cid);
            if (UserBuf) {
                SParty.Join(UserBuf);
            }
        }
        SParty.SendIpInfo();
        World.SendPartyInfoToAll(SParty);
        if (Jso.map != 0) {
            local T = {
                op = 2024041602,
                map = Jso.map
            }
            SUser.SendJso(T);
        }
    }
}


/***********************************************
 ****************                ****************
 ************************************************/

//客户端包回调Map
if (!getroottable().rawin("ClientSocketPackFuncMap")) ClientSocketPackFuncMap <- {}


//收到来自客户端的包 只有130
function OnClientSocketMsg(C_User, C_Pack_Str) {
    if (PacketDebugModel) print("收到客户端包: " + C_Pack_Str);

    local Jso = Json.Decode(C_Pack_Str);
    if (Jso.op in ClientSocketPackFuncMap) {
        local SUser = User(C_User);
        ClientSocketPackFuncMap[Jso.op](SUser, Jso);
    } else {
        local SUser = User(C_User);
        if (SUser) {
            Jso.uid <- SUser.GetUID();
            Jso.cid <- SUser.GetCID();
            Socket.SendGateway(Jso);
        }
    }
}