用户:
Nomen查看:1 回复:6 评论:1 创建时间:2023-05-08T11:47:20
好耶!NomenSQLite已经发布了,你难道还没看吗?
接下来,我将为所有萌新展示这个东西到底如何使用这个东西
它的使用过程相当的简单,我想我不用向高级开发者展示它的api了(
在开始之前,你需要先将NomenSQLite的本体粘贴到一个文件里,名字叫NomenSQLite就行
高贵的传送门: https://shequ.codemao.cn/community/539617
话不多说,直接上代码
const NomenSQLite = require('./NomenSQLite.js').NomenSQLite;
const Tables = {
'NomenSQLite': [//这是一个表格
{ name: 'userkey', type: NomenSQLite.DataType.TEXT, notnull: true }, // 这是一行数据,类型为TEXT,名字叫userKey,不能为空
{ name: 'bag', type: NomenSQLite.DataType.TEXT, notnull: false },
{ name: 'coin', type: NomenSQLite.DataType.NUMBER, notnull: false }
],
};
var NomenUser = null;
(async () => {
//初始化并启动NomenSQLite
let Nomen = await NomenSQLite.launch(Tables, NomenSQLite.LaunchAction.CREATE_OR_LAUNCH, (a, b) => { console.error(b) });
//然后初始化NomenSQLite这个表格的管理器
NomenUser = Nomen.operate('NomenSQLite');
})()
const BAG = []//玩家背包初始状态
const COIN = 0;//玩家金币初始状态
//当玩家进入的时候
world.onPlayerJoin(async ({ entity }) => {
//如果数据库和NomenSQLite没有准备好就重试
while (true) {
if (NomenUser && NomenUser.select) break;
}
//读取玩家的数据,使用玩家唯一的userKey进行标识
let result = await NomenUser.select('userkey', entity.player.userKey);
//如果玩家存在存档
if (result.length) {
//将玩家的金币设置为存档中的金币
entity.coin = result[0].coin;
//将玩家的背包解析出来
entity.bag = JSON.parse(result[0].bag);
} else {
//如果不存在存档,就插入一个新的存档
await NomenUser.insert({
userkey: entity.player.userKey, //玩家唯一标识符
bag: JSON.stringify(BAG), //玩家背包
coin: COIN //玩家金币
})
}
})
world.onPlayerLeave(({ entity }) => {
//更新玩家云存档
NomenUser.update(`userkey=${entity.player.userKey}`//使用userKey搜寻玩家的旧存档
, {
bag: JSON.stringify(entity.bag), //玩家背包
coin: entity.coin //玩家金币
})
})
你学会了吗?
============
Box3-Tools在征集代码!Box3-Tools是一个大型代码仓库,储存了大量的项目代码和模块工具,我们欢迎你的贡献!
Nomen小队正在召集成员!小队内众多大佬为你答疑解惑!
不要忘记给我一个Star哦