Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const HIGHNOON = require('./lib/highnoon')
- const FIRESTORE = admin.firestore()
- let PLAYERS = {}
- async function run () {
- const promises = []
- const waivers = await FIRESTORE.collection('waivers').get().then(wwdocs => wwdocs.docs.map(d => d.data()))
- PLAYERS = await FIRESTORE.collection('players').get().then(playerDocs => {
- const tmp = playerDocs.docs.map(pd => ({ id: pd.id, ...pd.data() }))
- return _.keyBy(tmp, 'id')
- })
- console.log(`We have ${waivers.length} waivers!`)
- const byLeague = _.groupBy(waivers, 'leagueId')
- _.forEach(byLeague, (wires, leagueId) => {
- console.log(`${wires.length} waivers found for ${leagueId}`)
- // process the waivers for this league
- promises.push(processLeagueWaivers(wires, leagueId))
- })
- // console.log(`Across ${totalLeagues} leagues!`)
- return Promise.all(promises)
- }
- run().then(() => console.log('Completed.'))
- async function processLeagueWaivers(wires, leagueId) {
- console.log(chalk.blue(`Processing ${wires.length} wires...`))
- const claimed = {}
- const owlConfig = await HIGHNOON.getOwlConfig()
- // get the league
- const league = await FIRESTORE.collection('standardLeagues').doc(leagueId).get().then(ld => ld.data())
- // get the league users
- const leagueUsers = await FIRESTORE.collection('userLeagues').where('leagueId', '==', leagueId).get().then(lud => lud.docs.map(lu => lu.data()))
- const keyedLeagueUsers = _.keyBy(leagueUsers, 'userId')
- // get the league rosters
- const leagueRosters = await FIRESTORE.collection('standardLeagueRosters').where('leagueId', '==', leagueId).get().then(rosterDocs => {
- const theRosters = rosterDocs.docs.map(rd => rd.data())
- return _.keyBy(theRosters, 'userId')
- })
- // sort them by losses, then by leagueScore
- let priority = _.orderBy(leagueUsers, ['losses', 'leagueScore'], ['desc', 'asc'])
- // console.log(priority)
- let sortedWaiver = _.orderBy(wires, ['priority'], ['asc'])
- // go through the priority order. If they get a waiver, move them to the bottom. If not, remove them.
- do {
- let user = priority[0].userId
- let waiverIndex = sortedWaiver.findIndex(sw => sw.userId === user)
- if (waiverIndex > -1 && sortedWaiver.length) {
- console.log(chalk.blue(`Getting ${waiverIndex} from sortedWaiver with a length of: ${sortedWaiver.length}`))
- let waiver = sortedWaiver[waiverIndex]
- if (!claimed[waiver.wants]) {
- if (waiver.for === 'empty') {
- if (Object.keys(leagueRosters[user].players).length >= league.numRoster) {
- // this claim is invalid
- console.log(chalk.magenta(`Roster already full, moving on...`))
- sortedWaiver.splice(waiverIndex, 1)
- } else {
- let { id, name, role, teamShortName } = PLAYERS[waiver.wants]
- leagueRosters[user].players[waiver.wants] = {
- id,
- name,
- points: 0,
- role,
- status: 'bench',
- teamShortName
- }
- console.log(`Added ${name} to ${user} in exchange for: ${waiver.for}`)
- claimed[waiver.wants] = true
- priority.push(priority.shift)
- sortedWaiver.splice(waiverIndex, 1)
- await FIRESTORE.collection('standardLeagueRosters').doc(`${leagueId}-${user}-${owlConfig.currentWeek}`).update(leagueRosters[user])
- await HIGHNOON.saveLeagueHistory({ userId: user, leagueId, type: 'Waiver', who: keyedLeagueUsers[user].displayName, message: `Obtained ${PLAYERS[waiver.wants].name} through waiver wire.` })
- await HIGHNOON.sendNotification({ userId: user, icon: 'exchange-alt', link: `/leagueStandard/${leagueId}`, message: `You obtained ${PLAYERS[waiver.wants].name} through waiver wire!`, leagueId })
- }
- } else {
- // now the fun part
- if (leagueRosters[user].players[waiver.for]) {
- // they have who they want to trade for, awesome!
- let { id, name, role, teamShortName } = PLAYERS[waiver.wants]
- delete leagueRosters[user].players[waiver.for]
- leagueRosters[user].players[waiver.wants] = {
- id,
- name,
- points: 0,
- role,
- status: 'bench',
- teamShortName
- }
- // console.log(leagueRosters[user].players)
- console.log(`Added ${name} to ${user} in exchanged for: ${waiver.for}`)
- await FIRESTORE.collection('standardLeagueRosters').doc(`${leagueId}-${user}-${owlConfig.currentWeek}`).update(leagueRosters[user])
- await HIGHNOON.saveLeagueHistory({ userId: user, leagueId, type: 'Waiver', who: keyedLeagueUsers[user].displayName, message: `Obtained ${PLAYERS[waiver.wants].name} through waiver wire.` })
- await HIGHNOON.sendNotification({ userId: user, icon: 'exchange-alt', link: `/leagueStandard/${leagueId}`, message: `You obtained ${PLAYERS[waiver.wants].name} through waiver wire!`, leagueId })
- claimed[waiver.wants] = true
- priority.push(priority.shift)
- sortedWaiver.splice(waiverIndex, 1)
- } else {
- // they don't have that player. Invalid waiver.
- console.log(`You don't have ${PLAYERS[waiver.for].name}, so this waiver is invalid.`)
- sortedWaiver.splice(waiverIndex, 1)
- }
- }
- } else {
- // remove it
- console.log(chalk.red(`${waiver ? waiver.wants : '???'} was already taken. Deleting this waiver.`))
- sortedWaiver.splice(waiverIndex, 1)
- }
- } else {
- console.log(chalk.grey('No waivers for this user, removing them.'))
- priority.splice(0, 1)
- }
- } while (sortedWaiver.length)
- console.log(chalk.yellow('--------------'))
- return Promise.resolve(true)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement