/*
文件名:WorldClass.nut
路径:BaseClass/WorldClass/WorldClass.nut
创建日期:2024-04-07	21:25
文件用途:游戏世界类
*/
class World {
    constructor() {}
    // 根据UID获取Session
    function GetSessionByUid(Uid) {
        return Sq_GameWorld_GetSessionByUid(Sq_Get_GameWorld(), Uid);
    }
    // 根据Session获取玩家
    function GetUserBySession(Session) {
        local CUser = Sq_GameWorld_GetUserBySession(Sq_Get_GameWorld(), Session);
        if (CUser)
            return User(CUser);
        else
            return null;
    }
    // 根据UID获取玩家
    function GetUserByUid(Uid) {
        local CUser = Sq_GameWorld_GetUserByUid(Sq_Get_GameWorld(), Uid);
        if (CUser)
            return User(CUser);
        else
            return null;
    }
    // 根据名字获取玩家
    function GetUserByName(Name) {
        local CUser = Sq_GameWorld_GetUserByName(Sq_Get_GameWorld(), Name);
        if (CUser)
            return User(CUser);
        else
            return null;
    }
    // 获取玩家数量
    function GetUserCount() {
        return Sq_GameWorld_GetUserCount(Sq_Get_GameWorld());
    }
    // 给所有玩家发包
    function SendAll(Pack) {
        Sq_GameWorld_SendAll(Sq_Get_GameWorld(), Pack.C_Object);
    }
    // 给所有玩家发送公告
    function SendNotiPacketMessage(String, Type) {
        local Pack = Packet();
        Pack.Put_Header(0, 12);
        Pack.Put_Byte(Type);
        Pack.Put_Short(0);
        Pack.Put_Byte(0);
        Pack.Put_Int(String.len());
        Pack.Put_Binary(String);
        Pack.Finalize(true);
        World.SendAll(Pack)
        Pack.Delete();
    }

    //发送公告消息  字符串数组
    function SendNotiForColorPacketMessage(StringArr, Type) {

        local ColorArr = [];
        local SendStrBlob = blob(1);
        SendStrBlob.writen(0xc2, 'c');
        SendStrBlob.writen(0x80, 'c');
        foreach(sobj in StringArr) {
            local StrPoint = Str_Ptr(sobj[0]);
            local StrPointB = Sq_Point2Blob(StrPoint, sobj[0].len());
            if (sobj[1]) {
                SendStrBlob.writen(0xc2, 'c');
                SendStrBlob.writen(0x80, 'c');
                SendStrBlob.writeblob(StrPointB);
                SendStrBlob.writen(0xc2, 'c');
                SendStrBlob.writen(0x80, 'c');

                local ColorBlob = blob(104);
                for (local i = 0; i< 3; i++) {
                    ColorBlob.writen(sobj[2][i], 'c');
                }
                for (local i = 0; i< 101; i++) {
                    ColorBlob.writen(0xff, 'c');
                }
                local ColorPoint = Sq_New_Point(104);
                Sq_WriteBlobToAddress(ColorPoint, ColorBlob);
                ColorArr.append(ColorPoint);

            } else {
                SendStrBlob.writeblob(StrPointB);
            }
        }

        local SendStrLen = SendStrBlob.len();
        local SendStrPoint = Sq_New_Point(SendStrLen);
        Sq_WriteBlobToAddress(SendStrPoint, SendStrBlob);

        local Pack = Packet();
        Pack.Put_Header(0, 370);
        Pack.Put_Byte(Type);
        Pack.Put_Short(0);
        Pack.Put_Byte(3);
        Pack.Put_Int(SendStrLen);
        Pack.Put_BinaryEx(SendStrPoint, SendStrLen);
        Pack.Put_Byte(ColorArr.len());
        foreach(color in ColorArr) {
            Pack.Put_BinaryEx(color, 104);
        }
        Pack.Finalize(true);
        World.SendAll(Pack)
        Pack.Delete();
        // SendItemSpace(0);

        Sq_Delete_Point(SendStrPoint);
        for (local i = 0; i< ColorArr.len(); i++) {
            Sq_Delete_Point(ColorArr[i]);
        }
    }

    //发送公告消息  字符串数组 带ID
    function SendNotiForColorAIdPacketMessage(StringArr, Type) {

        local ColorArr = [];
        local SendStrBlob = blob(1);
        SendStrBlob.writen(0xc2, 'c');
        SendStrBlob.writen(0x80, 'c');
        foreach(sobj in StringArr) {
            local StrPoint = Str_Ptr(sobj[0]);
            local StrPointB = Sq_Point2Blob(StrPoint, sobj[0].len());
            if (sobj[1]) {
                SendStrBlob.writen(0xc2, 'c');
                SendStrBlob.writen(0x80, 'c');
                SendStrBlob.writeblob(StrPointB);
                SendStrBlob.writen(0xc2, 'c');
                SendStrBlob.writen(0x80, 'c');

                local ColorBlob = blob(104);
                for (local i = 0; i< 3; i++) {
                    ColorBlob.writen(sobj[2][i], 'c');
                }
                if (sobj[3]) {
                    ColorBlob.seek(4);
                    ColorBlob.writen(sobj[3], 'i');
                } else {
                    for (local i = 0; i< 101; i++) {
                        ColorBlob.writen(0xff, 'c');
                    }
                }
                local ColorPoint = Sq_New_Point(104);
                Sq_WriteBlobToAddress(ColorPoint, ColorBlob);
                ColorArr.append(ColorPoint);

            } else {
                SendStrBlob.writeblob(StrPointB);
            }
        }

        local SendStrLen = SendStrBlob.len();
        local SendStrPoint = Sq_New_Point(SendStrLen);
        Sq_WriteBlobToAddress(SendStrPoint, SendStrBlob);

        local Pack = Packet();
        Pack.Put_Header(0, 370);
        Pack.Put_Byte(Type);
        Pack.Put_Short(0);
        Pack.Put_Byte(3);
        Pack.Put_Int(SendStrLen);
        Pack.Put_BinaryEx(SendStrPoint, SendStrLen);
        Pack.Put_Byte(ColorArr.len());
        foreach(color in ColorArr) {
            Pack.Put_BinaryEx(color, 104);
        }
        Pack.Finalize(true);
        World.SendAll(Pack)
        Pack.Delete();
        // SendItemSpace(0);

        Sq_Delete_Point(SendStrPoint);
        for (local i = 0; i< ColorArr.len(); i++) {
            Sq_Delete_Point(ColorArr[i]);
        }
    }


    //通过UID和CID获取玩家
    function GetUserByUidCid(Uid, Cid) {
        local SUser = GetUserByUid(Uid);
        if (SUser) {
            if (SUser.GetState() >= 3) {
                if (SUser.GetCID() == Cid) {
                    return SUser;
                }
            }
        }
        return null;
    }
    //指定角色移动区域
    function MoveArea(SUser, TownIndex, AreaIndex, Xpos, Ypos) {
        Sq_GameWorld_MoveArea(Sq_Get_GameWorld(), SUser.C_Object, TownIndex, AreaIndex, Xpos, Ypos);
    }
    //副本进出
    function SendDungeonInOut(SUser, Index, Model) {
        Sq_GameWorld_SendDungeonInOut(Sq_Get_GameWorld(), SUser.C_Object, Index, Model);
    }
    //给所有玩家发送队伍包
    function SendPartyInfoToAll(Party) {
        Sq_GameWorld_SendPartyInfoToAll(Sq_Get_GameWorld(), Party.C_Object);
    }
}