猫史档案馆


【box3】代码岛数据库---稿件征集!

用户:YDS尹子YDS尹子查看:12 回复:13 评论:12 创建时间:2021-07-18T17:39:36


https://shequ.codemao.cn/reader/113726/291780

请阅读↑

center_image


回复

上一页1 页 / 共 1下一页
该账号已停用该账号已停用

啊我没有邮箱可以发在帖子里吗?

点赞1


评论


JSR代码喵JSR代码喵

你漏发网址了

点赞0


评论


YDS尹子YDS尹子

www.yxtnt.cn/box3DataBase.html

点赞0


评论


该账号已停用该账号已停用

名字:飞奔的车

类型:代码 .txt

图片:

center_image

代码:

world.querySelectorAll(".车").forEach(async (entity) => {
    while (true) {
        entity.position.z += 1.8
        if (entity.position.z > 126) {
            entity.position = new Box3Vector3(entity.position.x, 13, 0);
        }
        entity.onEntityContact(async ({ other }) => {
            if (other.isPlayer) {
                other.enableDamage = true
                other.hurt(100)
                world.addCollisionFilter('.车','player');
                world.say(other.player.name+" 被车撞了")
                other.player.forceRespawn()
                world.removeCollisionFilter('.车','player');
                return;
            }
        })
        await sleep(50)
    }
});

点赞0


评论


化石茄子的铁镐化石茄子的铁镐

尹子没邮箱啊

点赞0


评论


async_functionasync_function

名称:sql模板

类型:代码(.js)

代码:

async function createTable() {
    await db.sql`
     CREATE TABLE IF NOT EXISTS players (
      userName varchar(50),
      money INTEGER DEFAULT 0
     )
    `;
}; (async function () { createTable() })()

async function savePlayer(entity) {//定义保存玩家状态的函数
    if (entity.player.userKey) {//拥有userKey的玩家, 则玩家不是游客, 可以保存
        await db.sql`

 --尝试向player表插⼊⼀条记录, 向各个字段写⼊玩家身上对应的属性值
    INSERT INTO players (
    userName,
    money
    )
    VALUES(
       ${entity.player.name},
       ${entity.money}
    )
    ON CONFLICT(userKey)--如果玩家记录已经存在, 则不需要插⼊, ⽽是更新各个字段的值
    DO UPDATE SET
    userName=excluded.userName,
    money=excluded.money
 `
    }
}
//players
async function showPlayers() {
    const playerList = await db.sql`SELECT * FROM players`
    for (const p of playerList) {
        console.log(JSON.stringify(p))
    }
}

async function loadPlayer(entity) {
    const data = (await (db.sql`SELECT * FROM players WHERE userKey=${entity.player.userKey} limit 1`))[0]
    if (data) { //如果存在这个玩家的存档
        entity.money = data.money
    }
}

async function showPlayers() {
    console.clear() // 清空控制台
    const playerList = await db.sql`SELECT * FROM players`
    for (const p of playerList) {
        console.log(JSON.stringify(p))
    }
}

world.onPlayerJoin(({ entity }) => {
    if ((await(db.sql`SELECT * FROM players WHERE userKey=${entity.player.userKey} limit 1`))[0] == null) {
        entity.money = 100
        await savePlayer(entity)
    } else {
        await loadPlayer(entity)
    }
})

world.onPlayerLeave(({ entity }) => {
    savePlayer(entity)
})
//模板--怎么回事啊提供

点赞1


评论


成熟的强袭编程猫548成熟的强袭编程猫548

我顶

点赞0


评论


成熟的强袭编程猫548成熟的强袭编程猫548

再顶

点赞0


评论


指令者指令者

名称:跑(hui)酷(tu)代码

类型:代码 .txt

图片:center_image

代码:

(async function () {
    var i, j; function mathRandomInt(a, b) {
        if (a > b) {
            var c = a;
            a = b;
            b = c;
        }
        return Math.floor(Math.random() * (b - a + 1) + a);
    }
    world.say('毁图喵即将爆炸!');
    console.warn('毁图喵即将爆炸!');
    for (i = 1; i <= 9; i++) {
        await sleep(1000);
        console.clear();
        world.say(['当前还剩', 10 - i, '秒爆炸!'].join(''));
        console.error((['当前还剩', 10 - i, '秒爆炸!'].join('')));
    }
    await sleep(1000);
    console.clear(); world.say('轰~~~');
    console.warn('你失败了~');
    for (j = 1; j <= 100000; j++) {
        voxels.setVoxel((mathRandomInt(0, 128)), (mathRandomInt(0, 128)), (mathRandomInt(0, 128)), (voxels.id("air")));
        voxels.setVoxel((mathRandomInt(0, 128)), (mathRandomInt(0, 128)), (mathRandomInt(0, 128)), (mathRandomInt(0, 541)));
        await sleep(1);
    }
    console.log('你熬过了输入者对你的折磨');
    world.say('毁图结束');
})();

以上是普通版,下面是隐藏版:

(async function () {
    var i, j; function mathRandomInt(a, b) {
        if (a > b) {
            var c = a;
            a = b;
            b = c;
        }
        return Math.floor(Math.random() * (b - a + 1) + a);
    }
    console.warn('毁图喵即将爆炸!');
    for (i = 1; i <= 9; i++) {
        await sleep(1000);
        console.clear();
        console.error((['当前还剩', 10 - i, '秒爆炸!'].join('')));
    }
    await sleep(1000);
    console.clear(); world.say('轰~~~');
    console.warn('你失败了~');
    for (j = 1; j <= 100000; j++) {
        voxels.setVoxel((mathRandomInt(0, 128)), (mathRandomInt(0, 128)), (mathRandomInt(0, 128)), (voxels.id("air")));
        voxels.setVoxel((mathRandomInt(0, 128)), (mathRandomInt(0, 128)), (mathRandomInt(0, 128)), (mathRandomInt(0, 541)));
        await sleep(1);
    }
    console.log('你熬过了输入者对你的折磨');
})();

点赞0


评论


成熟的强袭编程猫548成熟的强袭编程猫548

顶一顶

点赞0


评论


JSR代码喵JSR代码喵

点赞0


评论


codewyxcodewyx

c喵ole.clear()

async function poll(fn, msg) {//无限轮询执行sql语句直到成功
    while (true) {
        try {
            return await fn()
        } catch (e) {
            c喵t m = e.message
            if (m.includes('timeout') || m.includes('relation')) {
                world.say(m)
            }
            else {
                world.say(msg || e.stack)
            }
        }
        await sleep(1000)
    }
}

async function initTable() {
    //一直轮询等待"SELECT 1"执行成功, 这是最简单的测试数据库是否在线的语句
    await poll(() => db.sql`SELECT 1`, '等待pg数据库启动中...')
    world.say('数据库启动完毕')

    await db.sql`CREATE TABLE IF NOT EXISTS player( --player全部是小写, 无需改动
        "userName" VARCHAR(30) NOT NULL, -- userName包含大写字母, 需要用""包住
        left_hand TEXT NOT NULL, -- left_hand全部是小写, 无需改动
        "magicPower" INT NOT NULL,  -- magicPower包含大写字母, 需要用""包住
        coin INT NOT NULL, -- coin全部是小写, 无需改动
        "userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL -- userKey包含大写字母, 需要用""包住
    )`;

}

world.onPlayerJoin(async ({ entity }) => {
    await loadPlayer(entity)
    entity.player.onPress(async ({ button }) => {
      if (button == Box3ButtonType.ACTION1) {
            c喵t sel = await entity.player.dialog({
                type: Box3DialogType.SELECT,
                content: `
                金币:${entity.coin}
                `,
                opti喵: ['保存'],
            })
            if (sel) {
                if (sel.value == '保存') {
                    await updatePlayer(entity)
                    entity.player.directMessage('保存完毕')
                }
            }
        }
    })
})

async function loadPlayer(user) {
    user.coin = 50
    //右边记得加上 ) 匹配左边的 poll(
    c喵t data = (await poll(() => db.sql`SELECT * FROM player WHERE "userKey"=${user.player.userKey};`))[0]
    if (data) {

        user.coin = data.coin

    } else {
        await insertPlayer(user)
    }
}

async function insertPlayer(user) {
    return await poll(() => db.sql`INSERT INTO player (-- 用poll确保在数据库短暂掉线后还能继续执行这个sql语句
        "userName",-- userName包含大写字母, 需要用""包住
    
        coin,

        "userKey"-- userKey包含大写字母, 需要用""包住
    ) VALUES (
        ${user.player.name},
      
        ${user.coin},
        
        ${user.player.userKey}
    )`)//尾部记得加上 ) 匹配上面的 poll(
}

async function updatePlayer(user) {
    if (user.coin > 2147483647) {//限制一些容易被刷的数值不超过pg的上限, 否则存进pg会出错
        user.coin = 2147483647//假如设为2147483648将出错, 游戏设计时要防止数值增长过快
    }
    return await poll(() => db.sql`UPDATE player SET  -- 用poll确保在数据库短暂掉线后还能继续执行这个sql语句
        "userName"=${user.player.name},-- userName包含大写字母, 需要用""包住
        coin=${user.coin} 
    WHERE "userKey"=${user.player.userKey}`)// userKey包含大写字母, 需要用""包住. 尾部加上 ) 匹配上面的 poll(
}
initTable()
//在控制台执行 db.sql`DROP TABLE player` 可以删除数据表, 修改数据表结构时会经常用到

PGSQL模板 emotion_doge 王翊轩~ JS代码

点赞0


评论


_SandX_沙叉_SandX_沙叉

喵s

是啥?????

点赞0


评论