用户:天天fUg-查看:0 回复:0 评论:0 创建时间:2024-05-18T15:11:36
world.onPlayerJoin(async({entity })=>{
var set喵 = new Date();
set喵.setDate(set喵.getDate()+0);
var sety = set喵.getFullYear();
var setm = set喵.getMonth()+1;
var setd = set喵.getDate();
entity.gett = sety*10000+setm*100+setd
})
async function savePlayer(entity) {//定义保存玩家状态的函数
if (entity.player.userKey) {//拥有userKey的玩家, 则玩家不是游客, 可以保存
await db.sql`
--尝试向player表插⼊⼀条记录, 向各个字段写⼊玩家身上对应的属性值
INSERT INTO player (
userName,
coin,
userKey,
qiandao
)
VALUES(
${entity.player.name},
${entity.coin},
${entity.player.userKey},
${entity.qiandao}
)
ON CONFLICT(userKey)--如果玩家记录已经存在, 则不需要插⼊, ⽽是更新各个字段的值
DO UPDATE SET
userName=excluded.userName,
coin=excluded.coin,
qiandao=excluded.qiandao
`
}
}
async function showPlayers() {
console.clear() // 清空控制台
const playerList = await db.sql`SELECT * FROM player`
for (const p of playerList) {
console.log(JSON.stringify(p))
}
}
async function createTable() {
await db.sql`
CREATE TABLE IF NOT EXISTS player (
coin INTEGER DEFAULT 0,--⾦钱, INTEGER类型⽤于存储整数
qiandao INTEGER DEFAULT 0,--, INTEGER类型⽤于存储整数
userName TEXT DEFAULT '',--名字
userKey TEXT PRIMARY KEY UNIQUE DEFAULT ''--玩家的唯⼀识别码, TEXT类型⽤于存储字符串
)
`
}
async function loadPlayer(entity) {
const data = (await (db.sql`SELECT * FROM player WHERE userKey=${entity.player.userKey} limit 1`))[0]
if (data) { //如果存在这个玩家的存档
entity.coin = data.coin //恢复⾦钱
entity.qiandao = data.qiandao //恢复⾦钱
}
}
async function showPlayers() {
console.clear() // 清空控制台
const playerList = await db.sql`SELECT * FROM player`
for (const p of playerList) {
console.log(JSON.stringify(p))
}
}
(async function () {
createTable()
})();
//控制⾯板
world.onPlayerJoin(({ entity }) => { //玩家进⼊游戏时
entity.coin = 0
entity.player.onPress(async ({ button,entity }) => { //每当有按钮被按下
if (button == Box3ButtonType.ACTION1) { //如果按钮是右键
const selection = await entity.player.dialog({
type: Box3DialogType.SELECT, //对话框类型为选择框
content: `您拥有 ${entity.coin} 点钞\n您的等级:Lv.${Math.floor(entity.coin/50)}`,
title: `系统菜单`,
options: [ '手动保存', '签到'],
})
if (selection) { //确保对话框不是点击x关闭
if (selection.value === '手动保存') { //被点击的是保存按钮
await savePlayer(entity) //等待写⼊结束才运⾏下⼀⾏
entity.player.dialog({ //弹出对话框
type: Box3DialogType.TEXT, //对话框类型为纯⽂本
content: '手动保存完毕',
title: '数据库',
})
}
if (selection.value === '签到') { //被点击的是保存按钮
await savePlayer(entity) //等待写⼊结束才运⾏下⼀⾏
if (entity.qiandao==entity.gett){
entity.player.dialog({ //弹出对话框
type: Box3DialogType.TEXT, //对话框类型为纯⽂本
content: '您已签到',
title: '提示',
})}else{
entity.qiandao=entity.gett
entity.coin+=10
entity.player.dialog({ //弹出对话框
type: Box3DialogType.TEXT, //对话框类型为纯⽂本
content: '签到成功',
title: '提示',
})
}
}
}
}
})
})
world.onPlayerJoin(async({ entity })=>{
loadPlayer(entity)
while (1 == 1) {
const rows = await db.sql`SELECT * FROM player ORDER BY coin DESC LIMIT 10`;
console.log([1].userName);
entity.player.directMessage("———————————————————————————")
entity.player.directMessage("---点钞排行榜(前10)---\n[十分钟更新一次或聊天区输入'排行榜']")
// 将搜索的结果在聊天中显示
for await (const row of rows) {
entity.player.directMessage(row.userName + ' | ' + row.coin)
}
entity.player.directMessage("———————————————————————————")
await sleep (600000)
}
})
world.onChat(async({ entity, message }) => {
if (entity && entity.isPlayer) {
if (message === '排行榜') {
const rows = await db.sql`SELECT * FROM player ORDER BY coin DESC LIMIT 10`;
console.log([1].userName);
entity.player.directMessage("———————————————————————————")
entity.player.directMessage("---点钞排行榜(前10)---\n[十分钟更新一次或聊天区输入'排行榜']")
// 将搜索的结果在聊天中显示
for await (const row of rows) {
entity.player.directMessage(row.userName + ' | ' + row.coin)
}
entity.player.directMessage("———————————————————————————")
}
}
})
world.onPlayerJoin(async({ entity })=>{
await sleep (500)
while (1 == 1) {
savePlayer(entity)
await sleep (1000)
}
})