用户:
我是一位小萌新查看:0 回复:0 评论:0 创建时间:2022-04-27T09:38:04
求助 为什么userKey会报错。报错信息:
world.onPlayerJoin(({ entity, }) => {
entity.bag = ['铁剑','铁战盔']
entity.coin = 50
entity.magic = 0
entity.ex = 1
entity.jm = 0
entity.cd = 0
entity.zb = []
entity.h = '无'
entity.bo = '无'
entity.le= '无'
entity.sh= '无'
entity.r = '无'
entity.l = '无'
loadPlayer(entity)
entity.enableDamage = true //允许这个玩家受伤
entity.id = entity.player.userKey//把userKey作为玩家的id
world.addCollisionFilter('#' + entity.id, '#' + entity.id)//让id跟玩家id一样的实体不会相互碰撞, 比如玩家的飞刀不会碰到玩家, 自己的飞刀之间也不会碰撞
})
async function savePlayer(entity) {//定义保存玩家状态的函数
if (entity.player.userKey) {//拥有userKey的玩家, 则玩家不是游客, 可以保存
await db.sql`
--尝试向player表插入一条记录, 向各个字段写入玩家身上对应的属性值
INSERT INTO player (
userName,
bag,
coin,
magic,
ex,
jm,
h,
bo,
le,
sh,
r,
l,
userKey
)
VALUES(
${entity.player.name},
${JSON.stringify(entity.bag)},
${entity.coin},
${entity.magic},
${entity.ex},
${entity.jm},
${JSON.stringify(entity.h)},
${JSON.stringify(entity.bo)},
${JSON.stringify(entity.le)},
${JSON.stringify(entity.sh)},
${JSON.stringify(entity.r)},
${JSON.stringify(entity.l)},
${entity.player.userKey}
)
ON CONFLICT(userKey)--如果玩家记录已经存在, 则不需要插入, 而是更新各个字段的值
DO UPDATE SET
userName=excluded.userName,
bag=excluded.bag,
coin=excluded.coin,
magic=excluded.magic,
ex=excluded.ex,
jm=excluded.jm,
h=excluded.h,
bo=excluded.bo,
le=excluded.le,
sh=excluded.sh,
r=excluded.r,
l=excluded.l
`
}
}
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`
CREATE TABLE IF NOT EXISTS player (
bag TEXT DEFAULT '',
coin INTEGER DEFAULT 0,
magic INTEGER DEFAULT 0,
ex INTEGER DEFAULT 0,
jm INTEGER DEFAULT 0,
h TEXT DEFAULT '',
bo TEXT DEFAULT '',
le TEXT DEFAULT '',
sh TEXT DEFAULT '',
r TEXT DEFAULT '',
l TEXT DEFAULT '',
userKey TEXT PRIMARY KEY UNIQUE DEFAULT ''--玩家的唯一识别码, TEXT类型用于存储字符串
)
`
showPlayers() //每次运行代码都能查看数据库里所有记录
}
async function loadPlayer(entity) {
const data = (await (db.sql`SELECT * FROM player WHERE userKey=${entity.player.userKey} limit 1`))[0]
if (data) { //如果存在这个玩家的存档
entity.bag = JSON.parse(data.bag)
entity.coin = data.coin
entity.magic = data.magic
entity.ex = data.ex
entity.jm = data.jm
entity.h = JSON.parse(data.h)
entity.bo = JSON.parse(data.bo)
entity.le = JSON.parse(data.le)
entity.sh = JSON.parse(data.sh)
entity.r = JSON.parse(data.r)
entity.l = JSON.parse(data.l)
}
}
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`CREATE TABLE IF NOT EXISTS player (
userName TEXT DEFAULT '',--玩家名字
bag TEXT DEFAULT '',
coin INTEGER DEFAULT 0,
magic INTEGER DEFAULT 0,
ex INTEGER DEFAULT 0,
jm INTEGER DEFAULT 0,
h TEXT DEFAULT '',
bo TEXT DEFAULT '',
le TEXT DEFAULT '',
sh TEXT DEFAULT '',
r TEXT DEFAULT '',
l TEXT DEFAULT '',
userKey TEXT PRIMARY KEY UNIQUE DEFAULT ''--玩家的识别码
)`
showPlayers()
}
createTable()