猫史档案馆


【求纠错】SQL请大家帮个忙纠纠错吧

用户:冯XX冯XX查看:4 回复:1 评论:4 创建时间:2022-06-09T19:19:41


async function savePlayer(entity) {//定义保存玩家状态的函数     if (entity.player.userKey) {//拥有userKey的玩家, 则玩家不是游客, 可以保存         await db.sql`             --尝试向player表插入一条记录, 向各个字段写入玩家身上对应的属性值             INSERT INTO player (                 userName,                 money,                 喵,                 skill,                 level,                 title,                 attribute,                 bag,                 skill,                 userKey             )             VALUES(                 ${entity.player.name},                 ${entity.money},                 ${entity.喵},                 ${entity.skill},                 ${entity.level},                 ${entity.title},                 ${entity.attribute},                 ${JSON.stringify(entity.bag)},--itemList是数组, 需要用JSON.stringify才能存入类型为TEXT的字段                 ${JSON.stringify(entity.skills)},                 ${entity.player.userKey}             )             ON CONFLICT(userKey)--如果玩家记录已经存在, 则不需要插入, 而是更新各个字段的值             DO UPDATE SET

 

            userName=excluded.userName,             money=excluded.money,             skill=excluded.skill,             喵=excluded.喵,             level=excluded.level,             title=excluded.title,             attribute=excluded.attribute,             bag=excluded.bag,             skills=excluded.skills         `     } } async function loadPlayer(entity) {     const data = (await (db.sql`SELECT * FROM player WHERE userKey=${entity.player.userKey} limit 1`))[0]     if (data) { //如果存在这个玩家的存档         entity.money = data.money //恢复金钱         entity.level = data.level //恢复等级         entity.skill = data.skill //恢复技能         entity.attribute = data.attribute //恢复属性         entity.喵 = data.喵 //恢复武器         entity.title = data.title //恢复称号         entity.bag= JSON.parse(data.bag)          entity.skills= JSON.parse(data.skills)      } } 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 '',--玩家名字             money INTEGER DEFAULT 0,             weapon TEXT DEFAULT '' ,             attribute TEXT DEFAULT 0,             level INTEGER DEFAULT 0,             skill TEXT DEFAULT 0,             title TEXT DEFAULT 0,             bag TEXT DEFAULT '',             skills TEXT DEFAULT '',             userKey TEXT PRIMARY KEY UNIQUE DEFAULT ''--玩家的唯一识别码, TEXT类型用于存储字符串         )     `     showPlayers() //每次运行代码都能查看数据库里所有记录 } createTable() world.onPlayerJoin(({ entity }) => {     entity.player.forceRespawn()     entity.money=100     console.log(1)     entity.weapon=''     rand1=Math.random()     if(rand1<=0.1){rand='金'}     else if(rand1<=0.2){rand='木'}     else if(rand1<=0.3){rand='水'}     else if(rand1<=0.4){rand='火'}     else if(rand1<=0.5){rand='土'}     else if(rand1<=0.6){rand='风'}     else if(rand1<=0.7){rand='雷'}     else if(rand1<=0.8){rand='光'}     else if(rand1<=0.9){rand='暗'}     else{rand='冰'}     entity.level=1     entity.skill=''     entity.title='萌新'     entity.bag=[]     entity.skills=[]     entity.attribute=rand     loadPlayer(entity) }) world.onPlayerLeave(({ entity }) => { //当玩家离开地图时     savePlayer(entity) })


回复

上一页1 页 / 共 1下一页
冯XX冯XX

大家将就看吧,发出来后全乱了

点赞0


评论