emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala
changeset 240 29287b0a1a0b
parent 229 a11db9324580
child 242 ba048fb6c925
     1.1 --- a/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala	Wed Feb 17 22:21:43 2010 +0100
     1.2 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala	Sun Apr 25 22:46:08 2010 +0200
     1.3 @@ -26,31 +26,49 @@
     1.4  
     1.5  package cz.xelfi.quoridor.emailer
     1.6  
     1.7 -import scala.xml._
     1.8  import java.net.URL
     1.9 -import scala.io._
    1.10 +import java.util.ResourceBundle
    1.11 +import java.text.MessageFormat
    1.12  import scala.collection.Map
    1.13  import scala.collection.mutable.HashMap
    1.14 -import javax.mail.internet._
    1.15 -import javax.mail.Message
    1.16 -import javax.mail.Transport
    1.17  
    1.18  abstract class Notify {
    1.19 -  protected def allPlayers(): Map[String,List[Node]]
    1.20 -  protected def emails(ids : Collection[String]): List[String]
    1.21 +  protected def allPlayers(): Map[String,List[Info]]
    1.22 +  protected def emails(ids : Collection[String]): Map[String,String]
    1.23    protected def sendEmail(address : String, subject : String, text : String) : Unit
    1.24  
    1.25 +  case class Info(lastMove : Long, gameId : String) {
    1.26 +  }
    1.27 +
    1.28    def process() {
    1.29      val ret = allPlayers()
    1.30 +    val name2Address = emails(ret.keySet)
    1.31  
    1.32 -    for (val address <- emails(ret.keySet)) {
    1.33 -        Console.println("Sending message to " + address.split('@')(0))
    1.34 +    for (val player <- ret.keySet) {
    1.35 +        val address = name2Address.getOrElse(player, null)
    1.36  
    1.37 -        sendEmail(
    1.38 -          "quoridor@xelfi.cz",
    1.39 -          "Play Quoridor!",
    1.40 -          "Visit http://quoridor.xelfi.cz"
    1.41 -        )
    1.42 +        val rb = ResourceBundle.getBundle("cz.xelfi.quoridor.emailer.Bundle")
    1.43 +        val sb = new StringBuilder
    1.44 +        sb.append(rb.getString("MSG_HEADER"))
    1.45 +        var subject = rb.getString("MSG_SUBJECT")
    1.46 +        val mf = new MessageFormat(rb.getString("MSG_GAME"))
    1.47 +        for (val info <- ret(player)) {
    1.48 +          val delay : Long = (System.currentTimeMillis - info.lastMove) / (3600 * 1000 * 24)
    1.49 +          if (delay > 14) {
    1.50 +            subject = rb.getString("MSG_SUBJECT_WARN")
    1.51 +          }
    1.52 +          val arr = new Array[Object](2);
    1.53 +          arr(0) = "http://quoridor.xelfi.cz/games/" + info.gameId
    1.54 +          arr(1) = java.lang.Long.valueOf(delay)
    1.55 +          sb.append(mf.format(arr))
    1.56 +        }
    1.57 +        if (address != null) {
    1.58 +          sendEmail(
    1.59 +              "quoridor@xelfi.cz",
    1.60 +              subject,
    1.61 +              sb.toString
    1.62 +          )
    1.63 +        }
    1.64      }
    1.65    }
    1.66  }