Skip to content
DataCenterS

基类 / DataCenterS

DataCenterS Class

服务端数据中心,管理所有玩家的数据。

使用示例:创建一个名为DataCenterSExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,保存并运行游戏,玩家加入时会输出当前玩家的等级以及当前所有玩家的等级,玩家离开时当前玩家会升级并且输出(pie上玩家离开需要通过点x键)
ts
@Component
export default class DataCenterSExample extends Script {

    protected onStart(): void {
        if (SystemUtil.isServer()) {
            DataCenterS.onPlayerJoin.add(this.onPlayerJoin, this);
            DataCenterS.onPlayerLeave.add(this.onPlayerLeave, this);
        }
    }

    //玩家加入且数据就绪
    private onPlayerJoin(player: mw.Player): void {
        let playerData = DataCenterS.getData(player, PlayerModuleData);
        console.log("玩家加入,当前玩家等级为:", playerData.getlevel());
        console.log("显示当前所有玩家的等级:");
        const playerIds = DataCenterS.getReadyPlayerIds();
        playerIds.forEach(playerId => {
            let playerData = DataCenterS.getData(playerId, PlayerModuleData);
            console.log("玩家playerId为:" + playerId, "的等级:", playerData.getlevel());
        });
    }

    //玩家离开
    private onPlayerLeave(player: mw.Player): void {
        let playerData = DataCenterS.getData(player, PlayerModuleData);
        playerData.levelUp();
        console.log("玩家离开,等级提升为:", playerData.getlevel());
    }

}

class PlayerModuleData extends Subdata {
    @Decorator.persistence()
    private level: number = 0;

    public getlevel(): number {
        return this.level;
    }

    //玩家升级
    public levelUp(): void {
        this.level++;
        //保存数据
        this.save(false);
    }
}
@Component
export default class DataCenterSExample extends Script {

    protected onStart(): void {
        if (SystemUtil.isServer()) {
            DataCenterS.onPlayerJoin.add(this.onPlayerJoin, this);
            DataCenterS.onPlayerLeave.add(this.onPlayerLeave, this);
        }
    }

    //玩家加入且数据就绪
    private onPlayerJoin(player: mw.Player): void {
        let playerData = DataCenterS.getData(player, PlayerModuleData);
        console.log("玩家加入,当前玩家等级为:", playerData.getlevel());
        console.log("显示当前所有玩家的等级:");
        const playerIds = DataCenterS.getReadyPlayerIds();
        playerIds.forEach(playerId => {
            let playerData = DataCenterS.getData(playerId, PlayerModuleData);
            console.log("玩家playerId为:" + playerId, "的等级:", playerData.getlevel());
        });
    }

    //玩家离开
    private onPlayerLeave(player: mw.Player): void {
        let playerData = DataCenterS.getData(player, PlayerModuleData);
        playerData.levelUp();
        console.log("玩家离开,等级提升为:", playerData.getlevel());
    }

}

class PlayerModuleData extends Subdata {
    @Decorator.persistence()
    private level: number = 0;

    public getlevel(): number {
        return this.level;
    }

    //玩家升级
    public levelUp(): void {
        this.level++;
        //保存数据
        this.save(false);
    }
}

Table of contents

Properties

onPlayerJoin: Action1<Player>
玩家进入游戏的委托,当委托被调用的时候,可以保证玩家的数据是就绪的
onPlayerLeave: Action1<Player>
玩家离开游戏的委托,可用作玩家最后的数据处理

Methods

getData<T: extends Subdata<T>>(player: string number Player, SubdataType: TypeName<T: extends Subdata<T>>): T: extends Subdata<T> server
获取玩家的子数据
getReadyPlayerIds(): number[] server
获取在线且数据就绪的所有玩家ID
setDataSource(gameId: string): void server
设置数据来源

Properties

onPlayerJoin

Static Readonly onPlayerJoin: Action1<Player>

玩家进入游戏的委托,当委托被调用的时候,可以保证玩家的数据是就绪的


onPlayerLeave

Static Readonly onPlayerLeave: Action1<Player>

玩家离开游戏的委托,可用作玩家最后的数据处理

Methods

getData

Static getData<T>(player, SubdataType): T server

获取玩家的子数据

Parameters

player string number Player玩家玩家userId玩家instanceId
SubdataType TypeName<T>数据类

Returns

T数据对象
使用示例:创建一个名为DataCenterSExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,保存并运行游戏,玩家加入时你将在在服务端日志中看到玩家等级为0的信息
ts
@Component
export default class DataCenterSExample extends Script {

 protected onStart(): void {
     if (SystemUtil.isServer()) {
         DataCenterS.onPlayerJoin.add((player)=>{
             let playerData = DataCenterS.getData(player, PlayerModuleData);
             console.log("玩家等级:", playerData.getlevel());
         });
     }
   }
}

class PlayerModuleData extends Subdata {
    @Decorator.persistence()
    private level: number = 0;

    public getlevel(): number {
        return this.level;
    }
}
@Component
export default class DataCenterSExample extends Script {

 protected onStart(): void {
     if (SystemUtil.isServer()) {
         DataCenterS.onPlayerJoin.add((player)=>{
             let playerData = DataCenterS.getData(player, PlayerModuleData);
             console.log("玩家等级:", playerData.getlevel());
         });
     }
   }
}

class PlayerModuleData extends Subdata {
    @Decorator.persistence()
    private level: number = 0;

    public getlevel(): number {
        return this.level;
    }
}

Type parameters

Textends Subdata<T>

getReadyPlayerIds

Static getReadyPlayerIds(): number[] server

获取在线且数据就绪的所有玩家ID

Returns

number[]玩家id数组
使用示例:创建一个名为DataCenterSExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,保存并运行游戏,按下F健你将在在服务端日志中看到所有数据就绪的玩家的playerid以及等级
ts
@Component
export default class DataCenterSExample extends Script {

    protected onStart(): void {
        if (SystemUtil.isClient()) {
            InputUtil.onKeyDown(Keys.F, () => {
                this.traceAllLevel();
            })
        }
    }

    //测试输出所有数据就绪的玩家的等级
    @mw.RemoteFunction(mw.Server)
    public traceAllLevel(): void {
        if (SystemUtil.isServer()) {
            const playerIds = DataCenterS.getReadyPlayerIds();
            playerIds.forEach(playerId => {
                let playerData = DataCenterS.getData(playerId, PlayerModuleData);
                console.log("玩家playerId为:" + playerId, "的等级:", playerData.getlevel());
            });
        }
    }
}

class PlayerModuleData extends Subdata {
    @Decorator.persistence()
    private level: number = 0;

    public getlevel(): number {
        return this.level;
    }
}
@Component
export default class DataCenterSExample extends Script {

    protected onStart(): void {
        if (SystemUtil.isClient()) {
            InputUtil.onKeyDown(Keys.F, () => {
                this.traceAllLevel();
            })
        }
    }

    //测试输出所有数据就绪的玩家的等级
    @mw.RemoteFunction(mw.Server)
    public traceAllLevel(): void {
        if (SystemUtil.isServer()) {
            const playerIds = DataCenterS.getReadyPlayerIds();
            playerIds.forEach(playerId => {
                let playerData = DataCenterS.getData(playerId, PlayerModuleData);
                console.log("玩家playerId为:" + playerId, "的等级:", playerData.getlevel());
            });
        }
    }
}

class PlayerModuleData extends Subdata {
    @Decorator.persistence()
    private level: number = 0;

    public getlevel(): number {
        return this.level;
    }
}

setDataSource

Static setDataSource(gameId): void server

设置数据来源

Parameters

gameId string数据源的gameId,需要在开发者后台进行授权 range:字符串长度依据 gameId 长度而定