用户:
DebugDynamo查看:1 回复:3 评论:1 创建时间:2022-01-19T16:19:34
async function initTable() {//创建玩家数据表
return await db.sql`
CREATE TABLE IF NOT EXISTS "playerene" (
"exp" INT NOT NULL,--经验
"lv" INT NOT NULL,--物品列表
"userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL --用户识别码
)
`;
}
async function saveUser(user) {//玩家存档
await db.sql`
INSERT INTO "playerene" (-- player虽然是小写, 建议统一用""包住
"exp",-- 经验
"lv",-- 道具
"userKey"-- 用户识别码
) VALUES (
${user.exp}, --尝试新建经验值
${user.lv}, --尝试新建金币值
${user.player.userKey} --尝试新建当前玩家识别码
)
ON CONFLICT("userKey") -- 如果已经存在同样的用户识别码
DO UPDATE SET
"exp"=excluded."exp",-- 更新经验值
"lv"=excluded."lv"-- 虽然是小写, 建议统一用""包住
`
}
async function loadUser(user) {//玩家读档
const [data] = await db.sql`SELECT * FROM "playerene" WHERE "userKey"=${user.player.userKey} LIMIT 1`
if (data) {//如果这个玩家已经有存档
user.exp = data.exp
user.lv = data.lv
}
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数据库启动中...')
async function save(entity) {
await sleep(1000)
while (true) {
if (entity.exp >= (entity.lv) * 30) { //自动升级
while (entity.exp >= (entity.lv) * 30) { entity.exp -= (entity.lv) * 30; entity.lv += 1; };
entity.maxHp += 5
entity.hp = entity.maxHp
duanwei(entity)
}
await saveUser(entity)
await sleep(1000)
}
}
world.onPlayerJoin(async ({ entity }) => {
save(entity)
})
async function duanwei(entity) {
if (entity.lv < 10) {
entity.interactHint = `新手 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(0, 0, 0)
} else if (entity.lv < 20) {
entity.interactHint = `常客 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(1, 1, 0)
} else if (entity.lv < 30) {
entity.interactHint = `高手 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(1, 1, 0)
} else if (entity.lv < 40) {
entity.interactHint = `老手 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(0, 1, 1)
} else if (entity.lv < 50) {
entity.interactHint = `大佬 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(0, 1, 1)
} else if (entity.lv < 60) {
entity.interactHint = `巨佬 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(0, 1, 1)
} else if (entity.lv < 70) {
entity.interactHint = `大神 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(1, 0, 1)
} else if (entity.lv < 80) {
entity.interactHint = `巨神 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(1, 0, 1)
} else if (entity.lv < 90) {
entity.interactHint = `天使 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(1, 0, 1)
} else if (entity.lv < 95) {
entity.interactHint = `神仙 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(1, 0, 0)
} else if (entity.lv < 100) {
entity.interactHint = `传说 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(1, 0, 0)
} else if (entity.lv < 200) {
entity.interactHint = `无限传说 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(1, 0, 0)
} else {
entity.interactHint = `NAN大魔王 → [${entity.lv}级]`
entity.interactColor = new Box3RGBColor(0, 1, 0)
}
}
world.onPlayerJoin(async({entity})=>{
entity.lv = 1
entity.exp = 0
await loadUser(entity)
entity.onVoxelContact(()=>{
entity.exp += 1
})
entity.enableInteract = true
entity.interactRadius = 3
duanwei(entity)
})