用户:
蓝矮爱乐查看:6 回复:11 评论:6 创建时间:2022-03-13T16:37:46
啊啊啊岛3终于好了
今天出个教程系列(非常低级的)
//注意:time不是云数据形式
world.onPlayerJoin(({entity})=>{
entity.time=0
entity.tickTime=world.onTick(()=>{//利用world.onTick计时,误差会减小
entity.time+=喵/1000//1tick是喵毫秒,1秒等于1000毫秒,所以要用喵/1000
entity.time=Math.floor(entity.time*1000)/1000//避免计时器后面有多余的0
})
world.onPlayerLeave(({entity:entity_01})=>{//玩家离开时关闭玩家计时,避免onTick任务叠加,服务器会崩溃
if(entity_01==entity){//判断离开的玩家是不是自己,不然会关闭别人的tick计数
entity.tickTime.cancel()
}
})
})
db.sql`CREATE TABLE IF NOT EXISTS topT(--创建表
userK TEXT NOT NULL,
PlayerName TEXT NOT NULL,
topTime INTEGER NOT NULL
)`
async function getTop() {
R = []
for await(const row of db.sql`SELECT * FROM topT ORDER BY topTime ASC`) {
R.push(`${row.PlayerName}:${row.topTime}秒`)
}
}
async function insertTop(PlayerName, topTime) {//缩短添加数据的代码使代码简便
try {
await db.sql`INSERT INTO topT VALUES (${PlayerName}, ${topTime})`;
} catch (e) {
world.say(`insert sql error: ${e}`) ;
}
}
world.onEntityContact(async({entity,other})=>{
if(entity.isPlayer&&other.id === '终点线'){//玩家碰到名为终点线的实体
world.say(`${entity.player.name}到达了终点,用时${entity.time}`)//广播玩家
r=[]
m=[]
for await(const row of db.sql`SELECT * FROM topT ORDER BY topTime ASC`)//读取通关列表,并遍历通关列表的每一个元素 {
r.push(row.PlayerName)
m.push(row.topTime)
}
if((!(r.includes(entity.player.name)))&&(entity.time<=m[r.indexOf(entity.player.name)]))//如果玩家记录比之前的记录快
{
insertTop(entity.player.name,entity.time)
}else{
q=[]
for await(const row of db.sql`SELECT * FROM topT ORDER BY topTime ASC`) {
if(row.PlayerName!=entity.player.name){
q.push([row.PlayerName,row.topTime])
}
await db.sql`DELETE FROM topT`
for(const w of q){
insertTop(w[0],w[1])
insertTop(entity.player.name,entity.time)
}
}
}
entity.time = 0
}
})
topDialog = async(entity)=>{
getTop()
await sleep(256);
entity.player.dialog({type:'select',title:'排行榜',options:R})
};
/如果要显示通关排行榜,请用topDialog(entity)函数显示通关排行榜/你这个有一些问题(我没运行过,要是说错了记得指出),首先,如果多次碰到终点线会被多次记录,应该加一个判定,如果玩家是第一次碰到终点线才会被记录。 然后,函数:topDialog中对话框是select选择对话框形式,而R是选择钮,正文则是空的。应该改成text文本对话框,并把正文设置为R。 不过整体来说还是很棒的imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"
点赞0
评论
FML饭米粒改正建议:1.何必在onPlayerJoin里面套个onPlayerLeave呢?而且还不cancle,就等于每进一个人都要加一个检测 2.计时过于复杂,使用onTick更容易造成卡顿,建议改为进入时设置玩家变量startTime=newDate().valueOf()作为开始时间,结束后用newDate().valueOf()与开始时间做差即可,单位ms
点赞0
评论