用户:不喝只因汤的屑嘶令查看:1 回复:4 评论:1 创建时间:2022-09-06T22:49:49
错误:sql错误:错误:sqlite错误没有这样的表播放器
在查询包装排水完成
<隔离虚拟机>:12796:41
//封装成函数是因为它会被多次用到(显示玩家状态, 显示与NPC交互结果)
function textDialog(entity, content, title) {
return entity.player.dialog({ //弹出对话框
type: Box3DialogType.TEXT, //对话框类型为纯文本
content, //文本内容
title, //对话框左上角标题内容
})
}
world.onPlayerJoin(({ entity }) => { //玩家进入游戏时在这里初始化玩家的资产
entity.player.Lv = 1
entity.player.exp = 0
entity.player.experience = 100
entity.player.money = 100 //钱
entity.player.atk = 5 //攻击力
entity.player.itemList = [] //物品
loadPlayer(entity)
entity.player.onPress(async ({ button }) => { //每当有按钮被按下
if (button == Box3ButtonType.ACTION1) { //如果按钮是右键
const selection = await entity.player.dialog({
type: Box3DialogType.SELECT, //对话框类型为选择框
content: `${entity.player.name}个人信息:\n\n等级${entity.player.Lv}:(${entity.player.exp}/${entity.player.experience})\n\n你有${entity.player.money}元,\n血量: ${entity.hp}/${entity.maxHp}\n${entity.player.atk}点攻击力`,
options: ['保存','查看背包','重启','喵一口鸡汤(回城)','设置'],
})
if (selection) { //确保对话框不是点击x关闭
if (selection.value === '保存') { //被点击的是保存按钮
await savePlayer(entity) //等待写入结束才运行下一行
textDialog(entity, '保存完毕')
}else if (selection.value === '喵一口鸡汤(回城)') {
entity.hurt(entity.maxHp)
}
}
}
})
})
async function savePlayer(entity) {//定义保存玩家状态的函数
if (entity.player.userKey) {//拥有userKey的玩家, 则玩家不是游客, 可以保存
await db.sql`
${entity.player.name},
entity.player.Lv = 1
entity.player.exp = 0
entity.player.experience = 100
entity.player.money = 100 //钱
entity.player.atk = 5 //攻击力
${JSON.stringify(entity.itemList)},--itemList是数组, 需要用JSON.stringify才能存入类型为TEXT的字段
${entity.player.userKey}
`
}
}
async function showPlayers() {
console.clear() // 清空控制台
const playerList = await db.sql`SELECT * FROM player`
for (const p of playerList) {
console.log(JSON.stringify(p))
}
}
async function createTable() {
await db.sql
showPlayers() //每次运行代码都能查看数据库里所有记录
}
async function loadPlayer(entity) {
const data = (await (db.sql`SELECT * FROM player WHERE userKey=${entity.player.userKey} limit 1`))[0]
if (data) { //如果存在这个玩家的存档
entity.Lv = data.Lv
entity.exp = data.exp
entity.experience = data.experience
entity.money = data.money
entity.atk = data.atk
entity.itemList = JSON.parse(data.itemList) //恢复道具列表, 这里的JSON.parse用于把字符串变回数组
}
}
async function showPlayers() {
console.clear() // 清空控制台
const playerList = await db.sql`SELECT * FROM player`
for (const p of playerList) {
console.log(JSON.stringify(p))
}
}
async function createTable() {
await db.sql
showPlayers()
}
createTable()