用户:
array_已退查看:11 回复:12 评论:11 创建时间:2022-07-01T20:11:01
array_已退
createTable()
var a = []
console.clear()
async function createTable() {
try {
await db.sql`CREATE TABLE IF NOT EXISTS p (
userName TEXT DEFAULT "",
die INTEGER DEFAULT 0,
won INTEGER DEFAULT 0,
userKey TEXT PRIMARY KEY UNIQUE DEFAULT ""
)`
} catch (e) { world.say(`创建错误:${e}`) }
}
async function loadPlayer(entity) {
try {
const data = (await (db.sql`SELECT * FROM p WHERE userKey=${entity.player.userKey} limit 1`))[0]
if (data) {
entity.die = data.die
entity.won = data.won
}
} catch (e) { world.say(`登录错误${e}`) }
}
async function savePlayer(entity) {//定义保存玩家状态的函数
if (entity.player.userKey) {//拥有userKey的玩家, 则玩家不是游客, 可以保存
try {
await db.sql`
INSERT INTO p (
userName,
die,
won,
userKey
)
VALUES(
${entity.player.name},
${entity.die},
${entity.won},
${entity.player.userKey}
)
ON CONFLICT(userKey)
DO UPDATE SET
userName=excluded.userName,
die=excluded.die,
won=excluded.won
`
} catch (e) { world.say(`保存错误:${e}`) }
}
}
async function getTop100(n) {
try {
i = 0;
x = [];
if (n == '0') {
for await (const row of db.sql`SELECT * FROM p ORDER BY die DESC LIMIT 100`) {
i++
x.push(`第${i}名:${row.userName},喵${row.die}次`)
}
}
else if (n == '1') {
for await (const row of db.sql`SELECT * FROM p ORDER BY won DESC LIMIT 100`) {
i++
x.push(`第${i}名:${row.userName},获胜${row.won}次`)
}
}
return x
} catch (e) { world.say('获取排行错误:' + e) }
}
createTable()
gamestart()
console.log(a)
a.push([120])
async function time() {
for (let j = 30; j > 0; j -= 5) {
world.say(`还剩下${j}秒`)
await sleep(5000)
}
world.say('游戏结束!')
for (p of world.querySelectorAll('player')) {
if (!p.win) {
p.hurt(100)
}
}
await sleep(2000)
gamestart()
await time()
}
function gamestart() {
a = []
for (let x = 1; x < 49; x++) {
for (let z = 10; z < 116; z++) {
if (z % 5 != 0) {
voxels.setVoxel(x, 29, z, 'blue_glass')
}
}
}
for (let z = 10; z < 116; z++) {
if (z % 5 == 0) {
if (Math.random() <= 0.4 + Math.random() * 0.2) {
a.push([z, 0])
}
else {
a.push([z, 1])
}
}
}
for (let z = 10; z < 117; z++) {
voxels.setVoxel(25, 29, z, '')
}
a.push([120])
for (p of world.querySelectorAll('player')) {
p.hp = 100
p.win = false
p.position = new Box3Vector3(Math.ceil(Math.random() * 45), 32, 120)
}
}
world.onInteract(({ entity, targetEntity }) => {
entity.player.dialog({
type: Box3DialogType.TEXT,
title: '查看排行榜',
content: `TA叫${targetEntity.player.name}TA喵了${targetEntity.die}次\nTA获胜了${targetEntity.won}次`,
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
})
});
world.onVoxelContact(({ entity, voxel, z }) => { //当触碰方块
if (voxel == voxels.id('lava02')) { //如果方块类型是岩浆
entity.hurt(100) //受伤
}
else if (voxel == voxels.id('red')) {
if (!entity.win) {
world.say(`${entity.player.name}获胜了!`)
entity.win = true
entity.won += 1
savePlayer(entity)
}
}
else if (voxel == voxels.id('blue_glass')) {
if (entity.position.x < 25) {
entity.q = [1, 25]
}
else {
entity.q = [25, 49]
}
for (let i = 0; i < a.length; i++) {
if (a[i][0] < z && z < a[i + 1][0]) {
if ((a[i][1] == 0 && entity.q[0] == 1) || (a[i][1] == 1 && entity.q[0] == 25)) {
for (let x = entity.q[0]; x < entity.q[1]; x++) {
for (let z = a[i][0]; z < a[i + 1][0]; z++) {
voxels.setVoxel(x, 29, z, '')
}
}
}
}
}
}
})
world.onDie(({ entity }) => {
world.say(`${entity.player.name}淘汰了!`)
entity.die += 1
savePlayer(entity)
})
world.onPlayerJoin(async ({ entity }) => {
entity.enableInteract = true
entity.player.enableDoubleJump = false
entity.player.scale = 0.8
entity.die = 0
entity.won = 0
loadPlayer(entity)
entity.enableDamage = true
entity.player.onPress(async ({ button }) => {
if (button == Box3ButtonType.ACTION1) {
x = await entity.player.dialog({
type: Box3DialogType.SELECT,
title: '查看排行榜',
content: `你喵了${entity.die}次\n你获胜了${entity.won}次`,
options: ['喵次数排行榜', '获胜次数排行榜'],
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
})
console.log(x.index)
if (x) {
getTop100(x.index).then(async (v) => {
await entity.player.dialog({
type: Box3DialogType.SELECT,
title: '排行榜',
content: '排行榜(最多100名)',
options: v,
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
})
});
}
}
})
})
time()点赞0
评论