猫史档案馆


【求助】SQL阴间玩意儿

用户:卑微OIer卑微OIer查看:0 回复:4 评论:0 创建时间:2023-01-02T19:35:00


async function createTable() {
    await db.sql`
        CREATE TABLE IF NOT EXISTS player (
            cpos TEXT NOT NULL, -- curtainly postion
            spos TEXT NOT NULL, -- save position
            level TEXT NOT NULL, -- level
            userKey TEXT PRIMARY KEY UNIQUE DEFAULT  
        )   ·`
    ,
}
createTable();
async function loadPlayer(entity) {
    const data = (await (db.sql`SELECT * FROM player WHERE userKey=${entity.player.userKey} limit 1`))[0]
    if (data) {
        entity.position = new Box3Vector3(JSON.parse(data.cpos)[0], JSON.parse(data.cpos)[1], JSON.parse(data.cpos)[2]);
        entity.player.spawnPoint = new Box3Vector3(JSON.parse(data.spos)[0], JSON.parse(data.spos)[1], JSON.parse(data.spos)[2]);
        entity.levelRecord = JSON.parse(data.level);
    }
}
async function savePlayer(entity) {
    if (entity.player.userKey) {
        await db.sql`
            INSERT INTO player (
                "cpos",
                "spos",
                "level",
                "userKey"
            )
            VALUES(
                ${JSON.stringify([entity.position.x, entity.position.y, entity.position.z])},
                ${JSON.stringify([entity.player.spawnPoint.x, entity.player.spawnPoint.y, entity.player.spawnPoint.z])},
                ${JSON.stringify(entity.levelRecord)},
                ${entity.player.userKey}
            )
            ON CONFLICT(userKey)
            DO UPDATE SET
                "cpos"=excluded."cpos",
                "spos"=excluded."spos",
                "level"=excluded."level",
        `
    }
}
world.onPlayerJoin(async({entity})=>{await loadPlayer();})
world.onPlayerLeave(async({entity})=>{await savePlayer();})


回复

上一页1 页 / 共 1下一页
yee剑走天下yee剑走天下

ohhhhhhhhhhhhhhhhhh)洋葱会SQL力!(喜

点赞3


评论


AokenAoken

。。

点赞0


评论


烟魂烟魂

芜湖

点赞0


评论


烟魂烟魂

第一次听说你会

点赞0


评论