用户:
卑微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();})