Created
December 29, 2019 18:15
-
-
Save BalmungSan/b049a0429591c306acc71f3f1fa3db5d to your computer and use it in GitHub Desktop.
Simple Ammonite-Scala script to shuffle a list of participants of "amigo secreto".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env amm | |
import $ivy.`com.sun.mail:javax.mail:1.6.2` | |
import javax.mail._ | |
import javax.mail.internet._ | |
import java.time.{ZoneId, ZonedDateTime} | |
import java.util.{Date, Properties} | |
import scala.jdk.CollectionConverters._ | |
final case class Participant(name: String, email: String) | |
final case class Matched(participant: Participant, friend: String) | |
def randomMatch(participants: List[Participant]): List[Matched] = { | |
val names = participants.map(_.name) | |
@annotation.tailrec | |
def loop(): List[Matched] = { | |
val shuffled = scala.util.Random.shuffle(names) | |
val zipped = (participants zip shuffled) | |
val selfMatch = zipped.exists { | |
case (participant, friend) => | |
participant.name == friend | |
} | |
if (selfMatch) loop() | |
else zipped map { | |
case (participant, friend) => | |
Matched(participant, friend) | |
} | |
} | |
loop() | |
} | |
def sendEmails(user: String, password: String)(matches: List[Matched]): Unit = { | |
val props = new Properties() | |
props.putAll( | |
Map( | |
"mail.smtp.host" -> "outlook.office365.com", | |
"mail.smtp.port" -> "587", | |
"mail.smtp.auth" -> "true", | |
"mail.smtp.starttls.enable" -> "true" | |
).asJava | |
) | |
val session = Session.getInstance( | |
props, | |
new Authenticator() { | |
override protected def getPasswordAuthentication(): PasswordAuthentication = | |
new PasswordAuthentication(user, password) | |
} | |
) | |
val todaysDate = Date.from( | |
ZonedDateTime.now().withZoneSameInstant(ZoneId.of("America/Bogota")).toInstant | |
) | |
matches.foreach { | |
case Matched(Participant(participantName, participantEmail), friendName) => | |
val email = new MimeMessage(session) | |
email.setFrom(new InternetAddress(user)) | |
email.setRecipient(Message.RecipientType.TO, new InternetAddress(participantEmail)) | |
email.setSubject("Amigo secreto") | |
email.setSentDate(todaysDate) | |
val multipart = new MimeMultipart() | |
val msg = new MimeBodyPart() | |
msg.setContent( | |
s"Hola ${participantName}, tu amigo secreto es ${friendName}.", | |
"text/html" | |
) | |
multipart.addBodyPart(msg) | |
email.setContent(multipart) | |
Transport.send(email) | |
} | |
} | |
@main | |
def main(emailUser: String, emailPassword: String, arguments: Seq[String]): Unit = { | |
val participants = arguments.toList.map { argument => | |
val Array(name, email) = argument.split(":") | |
Participant(name, email) | |
} | |
val matches = randomMatch(participants) | |
sendEmails(emailUser, emailPassword)(matches) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment