Skip to content

Commit

Permalink
track num offline -> online transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jul 27, 2024
1 parent 281e8ab commit 57df611
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
19 changes: 11 additions & 8 deletions aurora/slashcommands/townless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { Aurora } from 'earthmc'
import { fastMerge, fetchError, paginatorInteraction } from '../../bot/utils/fn.js'
import { lastSeenPlayers } from "../../bot/constants.js"
import { getResidents } from "../../bot/utils/aurora.js"

const embed = (len: number, desc: string, footer?: { text: string, iconURL?: string }) => {
const builder = new EmbedBuilder()
Expand All @@ -22,7 +23,7 @@ const embed = (len: number, desc: string, footer?: { text: string, iconURL?: str
const townlessLastSeen = async () => {
//#region Cache these
// TODO
const residents = await Aurora.Residents.all()
const residents = await getResidents()
if (!residents) {
console.warn(`[AURORA] Error getting townless, could not get residents!`)
return null
Expand Down Expand Up @@ -79,22 +80,24 @@ export default {
}

// Separate online and offline items
const now = Date.now()

const online = townless.filter(p => p.online)
const offline = townless.filter(p => !p.online).sort((a, b) => b.timestamp - a.timestamp)
const offline = townless.filter(p => !p.online && ((now - p.timestamp) / 60000) <= 60)
.sort((a, b) => b.timestamp - a.timestamp)

// Concatenate online items (in original order) and sorted offline items
townless = fastMerge(online, offline)

const allData = townless.map(p => {
if (p.online) return `${p.name}`

const minSinceSeen = ((Date.now() - p.timestamp) / 60000)
if (minSinceSeen >= 1) {
return `${p.name} (${minSinceSeen.toFixed(0)}m ago)`
}
const diff = now - p.timestamp
const minSinceSeen = diff / 60000

const secSinceSeen = ((Date.now() - p.timestamp) / 1000)
return `${p.name} (${secSinceSeen.toFixed(0)}s ago)`
return minSinceSeen >= 1 ?
`${p.name} (Seen ${minSinceSeen.toFixed(0)}m ago)` :
`${p.name} (Seen ${(diff / 1000).toFixed(0)}s ago)`
}).join('\n').match(/(?:^.*$\n?){1,15}/mg)

// console.log("---- Online Townless ----", onlineTownlessData)
Expand Down
1 change: 1 addition & 0 deletions bot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export interface V3Player extends Entity {
}

export interface SeenPlayer extends SquaremapOnlinePlayer {
transitions: number
online: boolean
timestamp: number
}
Expand Down
12 changes: 9 additions & 3 deletions bot/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,15 @@ async function updateLastSeen() {

const now = Date.now()

ops.forEach(op => {
op['timestamp'] = now
lastSeenPlayers.set(op.name, op as SeenPlayer)
ops.forEach((op: SeenPlayer) => {
op.timestamp = now

const seen = lastSeenPlayers.get(op.name)
if (seen?.online) {
op.transitions = op.transitions ? op.transitions + 1 : 0
}

lastSeenPlayers.set(op.name, op)
})

lastSeenPlayers.forEach(v => {
Expand Down
18 changes: 12 additions & 6 deletions bot/utils/aurora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import type {
SquaremapPlayersResponse
} from "earthmc"

import { db } from "../constants.js"
import type { DBAlliance, DBResident, DBSquaremapNation, DBSquaremapTown } from '../types.js'
import type {
DBAlliance, DBResident,
DBSquaremapNation, DBSquaremapTown
} from '../types.js'

import {
divideArray,
fastMerge,
sortByOrder
} from "./fn.js"

import { db } from "../constants.js"

const auroraDoc = () => db.collection("aurora").doc("data")

const residentDataCollection = () => auroraDoc().collection("residentData")
Expand All @@ -38,10 +42,12 @@ const getOnlinePlayerData = async () => {
return await res.body.json() as SquaremapPlayersResponse
}

async function getResidents() {
return cache.get('aurora_residents') ?? residentDataCollection().get().then(async snapshot => {
return snapshot.docs.flatMap(doc => doc.data().residentArray)
}).catch(() => {})
async function getResidents(): Promise<DBResident[]> {
const cachedResidents = cache.get('aurora_residents')
if (cachedResidents) return cachedResidents

const snapshot = await residentDataCollection().get()
return snapshot.docs.flatMap(doc => doc.data().residentArray)
}

async function setResidents(residents: DBResident[]) {
Expand Down

0 comments on commit 57df611

Please sign in to comment.