用户:
屑佬查看:0 回复:0 评论:0 创建时间:2023-05-27T20:00:17
class bag{
constructor(list1,list2,entity){
this.list1=[]
this.list2=[]
this.player=entity
this.min=list1.length
if(this.min>list2.length){
this.min=list2.length
}
for(this.i=0;this.i<=this.min-1;this.i+=1){
if(list1[this.i]!=undefined&&list2[this.i]!=undefined){
this.give(list1[this.i],list2[this.i])
}
}
}
getindexA(thing){
this.list=[]
for(this.i in this.list1){if(this.list1[this.i]==thing){this.list.push(this.i)}}
return this.list
}
getindexL(thing){
return this.getindexA(thing)[this.getindexA(thing).length-1]
}
getnumber(thing){
this.number=0
this.buf=this.getindexA(thing)
for(this.i in this.buf){this.number+=this.list2[this.buf[this.i]]}
return this.number
}
has(thing,number){
if(this.getnumber(thing)>=number)return true
return false
}
append(a,b){
this.list1.push(a)
this.list2.push(b)
}
give(thing,number){
if(number<0){
return false
}
if(this.getindexA(thing)[0]!=undefined){
if(this.list2[this.getindexL(thing)]+number<=100){
this.list2[this.getindexL(thing)]+=number
}else{this.number1=number
this.number1-=100-this.list2[this.getindexL(thing)]
this.list2[this.getindexL(thing)]=100}
}else{this.number1=number}
while(this.number1>0){
if(this.number1>=100){this.append(thing,100)
this.number1-=100}
else{this.append(thing,this.number1)
break
}
}
}
take(thing,number){
if(!this.has(thing,number))return false
this.number2=number
while(this.number2>0){
this.buf1=this.getindexL(thing)
if(this.list2[this.buf1]>=this.number2){
this.list2[this.buf1]-=this.number2
this.number2=0
}else{
this.number2-=this.list2[this.buf1]
this.list2[this.buf1]=0
}
if(this.list2[this.buf1]<=0){
this.list1.splice(this.buf1,1)
this.list2.splice(this.buf1,1)
}
}
for(this.i_take in this.player.armor){
if(thing==this.player.armor[this.i_take]){
if(!this.has(this.player.armor[this.i_take],1)){
this.player.armor[this.i_take]="无"
this.player.armor1[this.i_take].mesh=""
}
}
}
if(thing==this.player.hand){
if(!this.has(this.player.hand,1)){this.player.hand="无"
this.player.player.removeWearable(this.player.player.wearables('rightHand')[0])}
}
return true
}
show(){
this.list3=[]
for(this.i in this.list1){
this.list3.push(`${this.list1[this.i]}*${this.list2[this.i]}`)
}
return this.list3
}
}
createTable()
async function showPlayers() {
const playerList = await db.sql`SELECT * FROM SQL`
for (const p of playerList) {
console.log(JSON.stringify(p))
}
}
async function savePlayer(entity) {
if (entity.player.userKey) {
await db.sql`
INSERT INTO SQL (
userName,
bag1,
bag2,
userKey
)
VALUES(
${entity.player.name},
${JSON.stringify(entity.bag.list1)},
${JSON.stringify(entity.bag.list2)},
${entity.player.userKey}
)
ON CONFLICT(userKey)
DO UPDATE SET
userName=excluded.userName,
bag1=excluded.bag1,
bag2=excluded.bag2
`
}
}
async function loadPlayer(entity) {
const data = (await (db.sql`SELECT * FROM SQL WHERE userKey=${entity.player.userKey} limit 1`))[0]
if (data) {
entity.bag = new bag(JSON.parse(data.bag1),JSON.parse(data.bag2),entity)
}
}
world.onPlayerJoin(async({entity:e})=>{
e.bag=new bag([],[],e)
loadPlayer(e)
await sleep(1500)
while(e&&!e.destroyed){
await sleep(500)
}
savePlayer(e)
})
async function createTable() {
await db.sql`
CREATE TABLE IF NOT EXISTS SQL (
userName TEXT DEFAULT '',
bag1 TEXT NOT NULL,
bag2 TEXT NOT NULL,
userKey TEXT PRIMARY KEY UNIQUE DEFAULT ''
)
`
showPlayers()
}