Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jun 9, 2024
1 parent 1297e13 commit 17ea052
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
11 changes: 5 additions & 6 deletions bot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,17 @@ export type MCSessionProfile = MCUserProfile & {
}

export type MapDB = {
getAlliance(name: string): Promise<any>
getAlliances(skipCache: boolean): Promise<any>
setAlliances(alliances: any[]): Promise<WriteResult>
getAlliance(name: string): Promise<DBAlliance>
getAlliances(skipCache: boolean): Promise<DBAlliance[]>
setAlliances(alliances: DBAlliance[]): Promise<WriteResult>
getResidents(): Promise<DBResident[]>
setResidents(residents: DBResident[]): Promise<void>
getTowns(): Promise<any>
setTowns(towns: any[]): Promise<void>
getNations(): Promise<any>
setNations(nations: any[]): Promise<void>
getResidents(): Promise<any>
setResidents(residents: any[]): Promise<void>
}


export type DBAlliance = {
allianceName: string
leaderName: string,
Expand Down
33 changes: 8 additions & 25 deletions bot/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,18 @@ async function updateAlliances(map: MapInstance) {
if (!nations) return console.warn(`Couldn't update ${mapToString(map)} alliances, failed to fetch nations.`)

const alliances = await map.db.getAlliances(true) as DBAlliance[]
if (!alliances) {
console.log("Couldn't update alliances, failed to fetch from DB.")
return
}
if (!alliances) return console.log("Couldn't update alliances, failed to fetch from DB.")

const alliancesAmt = alliances.length
for (let index = 0; index < alliancesAmt; index++) {
const a = alliances[index]

if (nations.length > 0) {
const existing = nations.filter(n => a.nations.includes(n.name))

// No nations exist in the alliance anymore, disband it.
if (existing.length < 2) {
console.log(`Alliance '${a.allianceName}' has no nations.`)

// TODO: Bring back disband logic (once bug is confirmed fixed)

return
}
const existing = nations.filter(n => a.nations.includes(n.name))
if (existing.length > 1) a.nations = existing.map(n => n.name)
else {
console.log(`Alliance '${a.allianceName}' has no nations.`)

a.nations = existing.map(n => n.name)
// TODO: Bring back disband logic (once bug is confirmed fixed)
}

const noInvite = "No discord invite has been set for this alliance"
Expand All @@ -155,21 +145,14 @@ async function sendEmptyAllianceNotif(map: MapInstance) {
const nations = await map.emc.Nations.all()
if (!nations) return console.warn(`Couldn't check empty ${mapToString(map)} alliances, failed to fetch nations.`)

const alliances = await map.db.getAlliances(true) as DBAlliance[]
if (!alliances) return
const alliances = await map.db.getAlliances(true)
if (!alliances) return console.log("Couldn't send notifs! Failed to fetch alliances from DB.")

const emptyAlliances: string[] = []
const alliancesAmt = alliances.length

for (let index = 0; index < alliancesAmt; index++) {
const a = alliances[index]

if (nations.length < 1) {
emptyAlliances.push(a.allianceName)
console.log(`Alliance '${a.allianceName}' has less than 2 nations.`)

continue
}

const existing = nations.filter(n => a.nations.includes(n.name))
if (existing.length < 2) {
Expand Down

0 comments on commit 17ea052

Please sign in to comment.