猫史档案馆


为何报错???

用户:DebugDynamoDebugDynamo查看:0 回复:2 评论:0 创建时间:2021-09-30T18:52:03


async function initTable() {//创建玩家数据表
    return await db.sql`
        CREATE TABLE IF NOT EXISTS "player" (
            "exp" INT NOT NULL,--经验
            "coin" INT NOT NULL,--金币
            "item" TEXT NOT NULL,--物品列表
            "userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL --用户识别码
        )
    `;
}
async function saveUser(user) {//玩家存档
    await db.sql`
        INSERT INTO "player" (-- player虽然是小写, 建议统一用""包住
            "exp",-- 经验
            "coin",-- 金币
            "item",-- 道具
            "userKey"-- 用户识别码
        ) VALUES (
            ${user.exp}, --尝试新建经验值
            ${user.coin}, --尝试新建金币值
            ${JSON.stringify(user.item)}, --尝试新建道具值
            ${user.player.userKey} --尝试新建当前玩家识别码
        )
        ON CONFLICT("userKey") -- 如果已经存在同样的用户识别码
        DO UPDATE SET
            "exp"=excluded."exp",-- 更新经验值
            "coin"=excluded."coin",-- 虽然是小写, 建议统一用""包住
            "item"=excluded."item",-- 虽然是小写, 建议统一用""包住
    `
}
async function loadUser(user) {//玩家读档
    const [data] = await db.sql`SELECT * FROM "player" WHERE "userKey"=${user.player.userKey} LIMIT 1`
    if (data) {//如果这个玩家已经有存档
        user.exp = data.exp
        user.coin = data.coin
        user.item = JSON.parse(data.item)
    }
    else {//玩家无存档
        saveUser(user)
    }
}
async function poll(fn, msg) {//无限轮询执行sql语句直到成功
    while (true) {
        try {
            return await fn()//一旦成功执行, 停止无限轮询, 并返回查询结果
        } catch (e) {
            const m = e.message//需要简略显示的异常消息
            //sql偶尔会执行超时, 但如果反复显示timeout 15秒以上一直不停, 大概是数据库出了故障只能等官方修复
            if (m.includes('timeout')) {
                world.say(m)
            }
            else {
                world.say(msg || e.stack)//如果sql执行出错, 广播错误消息, 用来排查错误原因
            }
        }
        await sleep(2000) //每2秒重试一次
    }
}

poll(initTable, '等待pg数据库启动中...')
console.clear()
world.onPlayerJoin(async ({ entity }) => {
    //初始化玩家属性
    entity.exp = 0
    entity.coin = 50
    entity.item = ['新手宝箱']
    if (entity.exp % 300 == 0) {
        entity.leven = entity.exp % 300
    }
    entity.bag = []
    await loadUser(entity)//尝试读取存

    entity.player.onPress(async ({ button }) => {//右键显示状态栏
        await saveUser(entity)
        if (button == Box3ButtonType.ACTION1) {
            if (entity.exp % 300 == 0) {
                entity.leven = entity.exp % 300
            }
            const sel = await entity.player.dialog({
                title: `等级:${entity.leven}`,
                type: Box3DialogType.SELECT,
                content: `
                金币:${entity.coin}
                经验:${entity.exp}
                装备:[${entity.bag}]
                `,
                options: ['道具栏'],
            })
            if (sel) {
                if (sel.index == 0) {
                    const n1 = await entity.player.dialog({
                        type: Box3DialogType.SELECT,
                        options: entity.item,//这报错很明显是SQL()我知道问题你要咋修  SQL别找我()()()
                    })
                    if (!n1 || n1 == null) {
                        return
                    }
                    if (n1.value == '新手宝箱') {
                        const n1 = await entity.player.dialog({
                            type: Box3DialogType.SELECT,
                            content: `一套新手的装备`,
                            options: ['打开', '丢弃']
                        })
                        if (!n1 || n1 == null) {
                            return
                        }
                        if (n1.index == 0) {
                            world.say(`玩家${entity.player.name}打开了新手宝箱,成为了新手[doge]`)
                            entity.item.push('新手剑', '新手魔法帽', '新手魔法斗篷')
                            entity.item.splice('新手宝箱', 1)
                        } else {
                            const n1 = await entity.player.dialog({
                                type: Box3DialogType.SELECT,
                                content: `确定丢弃吗`,
                                options: ['确定', '返回']
                            })
                            if (!n1 || n1 == null) {
                                return
                            }
                            if (n1.index == 0) {
                                entity.player.directMessage('丢弃成功')
                                entity.item.splice('新手宝箱', 1)
                            }
                        }
                    }
                }
            }
        }
    })
})

球球了


回复

上一页1 页 / 共 1下一页
DebugDynamoDebugDynamo

这代码为啥报错了??

点赞0


评论


星星上的雪花星星上的雪花

这行:

"item"=excluded."item",-- 虽然是小写, 建议统一用""包住

代码最后的“,”逗号删去

点赞0


评论