asjon/scripts/scuola.coffee
2015-04-30 14:46:11 +02:00

86 lines
2.4 KiB
CoffeeScript

# Description:
# utilità per orario scolastico e interrogazioni
#
# Dependencies:
# "moment": "2.10.2"
#
# Configuration:
# None
#
# Commands:
# hubot cosa abbiamo domani?
#
# Author:
# Enrico Fasoli (fazo96)
moment = require 'moment'
fs = require 'fs'
moment.locale 'it'
interr = undefined
orario = undefined
dateformats = ['YYYY-MM-DD','DD-MM-YYYY',"DD MMMM YYYY", "DD MMMM","DD"]
fs.readFile './config/orario.json', (err, data) ->
try
orario = JSON.parse data
catch e
console.log e
orario = undefined
fs.readFile './config/interrogazioni.json', (err, data) ->
try
interr = JSON.parse data
catch e
console.log e
interr = undefined
interr.sort (a,b) ->
if moment(a.day,'YYYY-MM-DD').isBefore(b.day,'day')
return -1
if moment(a.day,'YYYY-MM-DD').isAfter(b.day,'day')
return 1
return 0
module.exports = (robot) ->
robot.respond /(?:che )?cosa abbiamo (?:per )?(domani|oggi)(?:\?)?/i, (res) ->
if res.match[1] is 'domani'
index = moment().add(1, 'days').weekday()
else index = moment().weekday()
if orario?[index]?
res.send orario[index].join ', '
else res.send 'non ho informazioni per domani'
robot.respond /(?:dimmi )?(?:l\')?orario(?: scolastico)?/i, (res) ->
unless orario?.push?
return res.send 'non conosco l\'orario scolastico :('
ris = orario.map (x,i) ->
moment().weekday(i).format('dddd')+': '+x.join(', ')
res.send 'Orario scolastico della 5IA\n'+ris.join('\n')
robot.respond /interrogat(?:o|i) (oggi|domani|il (.+))(?:\?)?/i, (res) ->
unless interr?.push?
return res.send 'non conosco gli interrogati :('
if res.match[1] is 'domani'
day = moment().add(1, 'days')
else if res.match[1] is 'oggi'
day = moment()
else
q = res.match[1].replace(/il|\?/i, '')
day = moment q, dateformats
ris = interr.filter (x) ->
moment(x.day).isSame day, 'day'
ris = ris.map (x) ->
'('+x.day+') '+x.materia+': '+x.list.join(', ')
res.send ris.join('\n') or 'nessun interrogato'
robot.respond 'interrogati', (res) ->
unless interr?.push?
res.send 'non conosco gli interrogati :('
ris = interr.map (x) ->
gg = moment(x.day, 'YYYY-MM-DD')
s = gg.format('dddd Do MMMM YYYY')+' - '+gg.fromNow()
'('+s+') '+x.materia+': '+x.list.join(', ')
res.send ris.join('\n') or 'nessun interrogato'