Skip to content

Commit

Permalink
Optimize airdrop
Browse files Browse the repository at this point in the history
  • Loading branch information
leaftail1880 committed Feb 1, 2025
1 parent 6f7dc87 commit 97309ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/lib/rpg/airdrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { MinimapNpc, resetMinimapNpcPosition, setMinimapNpcPosition } from './mi
const logger = createLogger('Airdrop')

export class Airdrop {
static db = table<{ chicken: string; chest: string; loot: string; for?: string; looted?: true }>('airdrop')
static db = table<{ chicken: string; chest: string; loot: string; for?: string; looted?: true; type?: string }>(
'airdrop',
)

static chestTypeId = CustomEntityTypes.Loot

Expand Down Expand Up @@ -48,9 +50,12 @@ export class Airdrop {

status: 'restoring' | 'falling' | 'being looted' = 'restoring'

constructor(options: { loot: LootTable; forPlayerId?: string }, id?: string) {
type?: string

constructor(options: { loot: LootTable; forPlayerId?: string; type?: string }, id?: string) {
this.lootTable = options.loot
this.for = options.forPlayerId
this.type = options.type

if (!id) {
this.id = new Date().toISOString()
Expand All @@ -77,7 +82,7 @@ export class Airdrop {
if (event.entity.id !== this[name]?.id) return

event.entity.addTag(tag)
if (name === 'chest') event.entity.nameTag = `§l§f\\\\§r§b Аирдроп §f§l//§r`
if (name === 'chest') event.entity.nameTag = `§l§f<§r§b AirDrop §f§l>§r`
cleanup()
})
})
Expand Down
39 changes: 30 additions & 9 deletions src/modules/places/anarchy/airdrop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LocationInUnloadedChunkError, system, world } from '@minecraft/server'
import { Airdrop, Loot, Vector } from 'lib'
import { Airdrop, isNotPlaying, Loot, Vector } from 'lib'
import { t } from 'lib/text'
import { Anarchy } from 'modules/places/anarchy/anarchy'
import { CannonItem, CannonShellItem } from '../../features/cannon'
Expand Down Expand Up @@ -51,16 +51,33 @@ let airdrop: Airdrop | undefined
function timeout() {
system.runTimeout(
async () => {
if (!Anarchy.zone) return timeout()
try {
if (!Anarchy.zone) return

const online = world.getAllPlayers().length
if (online < 1) return timeout()
const online = world.getAllPlayers().filter(e => !isNotPlaying(e)).length
if (online < 1) return

if (airdrop) airdrop.delete()
const isPowerfull = online > 3
airdrop = await requestAirdrop(isPowerfull)
for (const [id, airdrop] of Object.entries(Airdrop.db)) {
if (airdrop) {
if (airdrop.type === '15m') {
const instance = Airdrop.instances.find(e => e.id === id)
if (instance) {
instance.delete()
} else {
Reflect.deleteProperty(Airdrop.db, id)
}
}
}
}
if (airdrop) airdrop.delete()

timeout()
const isPowerfull = online > 3
airdrop = await requestAirdrop(isPowerfull)
} catch (e) {
console.warn('Unable to request 15m airdrop:', e)
} finally {
timeout()
}
},
'airdrop',
// 15 min
Expand All @@ -69,11 +86,15 @@ function timeout() {
}

timeout()

export async function requestAirdrop(isPowerfull: boolean) {
const result = await randomLocationInAnarchy()
if (result) {
try {
const airdrop = new Airdrop({ loot: isPowerfull ? powerfull : base }).spawn(result.air).createMarkerOnMinimap()
const airdrop = new Airdrop({ loot: isPowerfull ? powerfull : base, type: '15m' })
.spawn(result.air)
.createMarkerOnMinimap()

world.say(
t.raw`§l§a>§r§7 ${isPowerfull ? 'Усиленный' : 'Обычный'} аирдроп появился на ${Vector.string(result.topmost, true)}!`,
)
Expand Down

0 comments on commit 97309ab

Please sign in to comment.