-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (122 loc) · 4.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require('dotenv').config()
const fp = require('lodash/fp')
const got = require('got')
const pEachSeries = require('p-each-series')
const rose = require('./fantamorto-rose-2019')
const { getDeadFromWikipedia } = require('./wikipedia')
const { readGoogleDoc, writeGoogleDoc } = require('./google-drive')
// read/write the list storage files
async function getSavedDeadList() {
if (!process.env.DEAD_SAVED_DOCUMENT) {
throw new Error('Missing DEAD_SAVED_DOCUMENT info, please check your .env file')
}
return await readGoogleDoc(process.env.DEAD_SAVED_DOCUMENT)
}
async function setSavedDeadList(deadList) {
if (!process.env.DEAD_SAVED_DOCUMENT) {
throw new Error('Missing DEAD_SAVED_DOCUMENT info, please check your .env file')
}
return await writeGoogleDoc(process.env.DEAD_SAVED_DOCUMENT, deadList)
}
async function getMaybeDeadList() {
if (!process.env.DEAD_MAYBE_DOCUMENT) {
throw new Error('Missing DEAD_MAYBE_DOCUMENT info, please check your .env file')
}
return await readGoogleDoc(process.env.DEAD_MAYBE_DOCUMENT)
}
async function setMaybeDeadList(deadList) {
if (!process.env.DEAD_MAYBE_DOCUMENT) {
throw new Error('Missing DEAD_MAYBE_DOCUMENT info, please check your .env file')
}
return await writeGoogleDoc(process.env.DEAD_MAYBE_DOCUMENT, deadList)
}
// returns the names of all the players of all teams from the object
const getAllNames = fp.pipe(
Object.values,
fp.map(Object.keys),
fp.flatten,
fp.uniq,
)
// returns an array with the team which contain the player received in input
function getTeamsContaining(player) {
const teams = Object.keys(rose)
return teams.filter(team => Object.keys(rose[team]).includes(player))
}
// returns an object with all the names and all the years
function getAllPlayers() {
const teamPlayers = Object.values(rose)
return teamPlayers.reduce((allPlayers, team) => {
const players = Object.keys(team)
players.forEach(player => {
allPlayers[player] = team[player]
})
return allPlayers
}, {})
}
// sends a message to the fantamorto slack channel
async function notifySlack(message) {
// don't do it if we're testing
if (process.env.NODE_ENV === 'development') {
console.log(`☠️ FANTAMORTO-BOT SAYS: ${message}`)
return
}
const body = { text: message }
return await got.post(process.env.SLACK_WEBHOOK, { body: JSON.stringify(body) })
}
async function checkMorti(event, context, callback = fp.noop) {
// notify aws lambda if there are any errors in promises
process.on('unhandledRejection', err => {
console.error(err.message)
callback(err)
process.exit(1)
})
const players = getAllNames(rose)
const [ deadFromWikipedia, maybeDead, savedDead ] = await Promise.all([
getDeadFromWikipedia(players),
getMaybeDeadList(),
getSavedDeadList(),
])
// check if someone new is dead!
const freshlyDead = deadFromWikipedia.filter(name => !savedDead.includes(name))
if (freshlyDead.length === 0) {
// we checked again and he's not dead, it was a fake news, empty the list
if (maybeDead.length > 0) {
await setMaybeDeadList([])
}
// notify aws lambda
console.info('--------------- NOBODY DIED ---------------')
return callback(null, 'Nobody died.')
}
await pEachSeries(freshlyDead, async (dead) => {
if (!maybeDead.includes(dead)) {
// they're dead but maybe they vandalized their page
maybeDead.push(dead)
await setMaybeDeadList(maybeDead)
console.info(`--------------- ${dead.toUpperCase()} MAYBE DIED ---------------`)
} else {
// no they really died, we double checked
maybeDead.splice(maybeDead.indexOf(dead), 1)
await setMaybeDeadList(maybeDead)
savedDead.push(dead)
await setSavedDeadList(savedDead)
await notifySlack(`⚰️ *${dead}* è deceduto. RIP in peace. ️✝️ 😿 🙏🏻`)
const winningTeams = getTeamsContaining(dead)
await notifySlack(`👏🏻 Congratulazioni ${winningTeams.length > 1 ? 'ai' : 'al'} team *${winningTeams.join(', ')}* 🎉 💪🏻 🎊 💯`)
// calculate the holy formuoli
const players = getAllPlayers()
const years = players[dead]
const points = Math.round(10 + (100 - years) / 10)
await notifySlack(`🎯 Utilizzando la formula descritta nel regolamento ${winningTeams.length > 1 ? 'avete' : 'hai'} fatto ben *${points}* punti${winningTeams.length > 1 ? ' ciascuno' : ''}! 📝 Aggiung${winningTeams.length > 1 ? 'ete' : 'i'}ci eventuali bonus e segna${winningTeams.length > 1 ? 'te' : ''}li nel documento.`)
console.info(`--------------- ${dead.toUpperCase()} DIED ---------------`)
}
})
// notify aws lambda
callback(null, `${freshlyDead.join(', ')} died!`)
}
module.exports = {
checkMorti,
}
// actually call the function if we're testing
if (process.env.NODE_ENV === 'development') {
checkMorti()
}