Adding description of individual lonely games. More verbose subject added too.
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 25 Apr 2010 22:46:08 +0200
changeset 24029287b0a1a0b
parent 239 a47345ebbdd7
child 241 b6a67364aff7
Adding description of individual lonely games. More verbose subject added too.
emailer/src/main/resources/cz/xelfi/quoridor/emailer/Bundle.properties
emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala
emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala
emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala
pom.xml
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emailer/src/main/resources/cz/xelfi/quoridor/emailer/Bundle.properties	Sun Apr 25 22:46:08 2010 +0200
     1.3 @@ -0,0 +1,10 @@
     1.4 +
     1.5 +
     1.6 +MSG_SUBJECT=Play Quoridor!
     1.7 +MSG_SUBJECT_WARN=Play Quoridor or some of your games will be closed!
     1.8 +
     1.9 +MSG_HEADER=Visit http://quoridor.xelfi.cz\n\n
    1.10 +MSG_GAME=You have not played for {1} days in game {0}.\n
    1.11 +
    1.12 +
    1.13 +
     2.1 --- a/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala	Sun Apr 25 21:41:00 2010 +0200
     2.2 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala	Sun Apr 25 22:46:08 2010 +0200
     2.3 @@ -50,15 +50,15 @@
     2.4      val props = java.lang.System.getProperties
     2.5      props.put("mail.smtp.host", args(3));
     2.6  
     2.7 -    val session = javax.mail.Session.getDefaultInstance(props, null)
     2.8 +    val session = if (args(3).equals("print")) null else javax.mail.Session.getDefaultInstance(props, null)
     2.9  
    2.10      val notify = new Notify() {
    2.11 -      protected def allPlayers(): Map[String,List[Node]] = {
    2.12 +      protected def allPlayers(): Map[String,List[Info]] = {
    2.13          val u = new URL(api, "games");
    2.14          val conn = u.openConnection
    2.15          conn.setRequestProperty("Accept", "text/xml")
    2.16  
    2.17 -        val ret = new HashMap[String,List[Node]]
    2.18 +        val ret = new HashMap[String,List[Info]]
    2.19  
    2.20          val before = System.currentTimeMillis - okDelay
    2.21          val games = XML.load(conn.getInputStream)
    2.22 @@ -76,7 +76,9 @@
    2.23                }
    2.24              if (name != null) {
    2.25                val prev = ret.getOrElse(name, Nil)
    2.26 -              val next = g :: prev
    2.27 +              val id = (g \\ "@id").text
    2.28 +              val info = new Info(modified, id)
    2.29 +              val next = info :: prev
    2.30                ret.put(name, next);
    2.31              }
    2.32            }
    2.33 @@ -84,8 +86,8 @@
    2.34          return ret;
    2.35        }
    2.36  
    2.37 -      protected def emails(ids : Collection[String]): List[String] = {
    2.38 -        var ret : List[String] = Nil;
    2.39 +      protected def emails(ids : Collection[String]): Map[String,String] = {
    2.40 +        var ret = new HashMap[String,String]
    2.41          for (val p <- ids) {
    2.42            val u = new URL(api, "users/" + p + "?loginID=" + loginID)
    2.43            val conn = u.openConnection
    2.44 @@ -98,13 +100,21 @@
    2.45                }
    2.46            })
    2.47            if (!res.isEmpty) {
    2.48 -            ret = res(0).text :: ret;
    2.49 +            ret.put(p, res(0).text)
    2.50            }
    2.51          }
    2.52          return ret
    2.53        }
    2.54  
    2.55        protected def sendEmail(address : String, subject : String, text : String) : Unit = {
    2.56 +        Console.println("Sending message to")
    2.57 +        Console.println("  email: " + address.split('@')(0) + "@... ")
    2.58 +        Console.println("  subject: " + subject)
    2.59 +        Console.println("  " + text)
    2.60 +        if (session == null) {
    2.61 +          return;
    2.62 +        }
    2.63 +
    2.64          val message = new MimeMessage(session)
    2.65          message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
    2.66          message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
     3.1 --- a/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala	Sun Apr 25 21:41:00 2010 +0200
     3.2 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala	Sun Apr 25 22:46:08 2010 +0200
     3.3 @@ -26,31 +26,49 @@
     3.4  
     3.5  package cz.xelfi.quoridor.emailer
     3.6  
     3.7 -import scala.xml._
     3.8  import java.net.URL
     3.9 -import scala.io._
    3.10 +import java.util.ResourceBundle
    3.11 +import java.text.MessageFormat
    3.12  import scala.collection.Map
    3.13  import scala.collection.mutable.HashMap
    3.14 -import javax.mail.internet._
    3.15 -import javax.mail.Message
    3.16 -import javax.mail.Transport
    3.17  
    3.18  abstract class Notify {
    3.19 -  protected def allPlayers(): Map[String,List[Node]]
    3.20 -  protected def emails(ids : Collection[String]): List[String]
    3.21 +  protected def allPlayers(): Map[String,List[Info]]
    3.22 +  protected def emails(ids : Collection[String]): Map[String,String]
    3.23    protected def sendEmail(address : String, subject : String, text : String) : Unit
    3.24  
    3.25 +  case class Info(lastMove : Long, gameId : String) {
    3.26 +  }
    3.27 +
    3.28    def process() {
    3.29      val ret = allPlayers()
    3.30 +    val name2Address = emails(ret.keySet)
    3.31  
    3.32 -    for (val address <- emails(ret.keySet)) {
    3.33 -        Console.println("Sending message to " + address.split('@')(0))
    3.34 +    for (val player <- ret.keySet) {
    3.35 +        val address = name2Address.getOrElse(player, null)
    3.36  
    3.37 -        sendEmail(
    3.38 -          "quoridor@xelfi.cz",
    3.39 -          "Play Quoridor!",
    3.40 -          "Visit http://quoridor.xelfi.cz"
    3.41 -        )
    3.42 +        val rb = ResourceBundle.getBundle("cz.xelfi.quoridor.emailer.Bundle")
    3.43 +        val sb = new StringBuilder
    3.44 +        sb.append(rb.getString("MSG_HEADER"))
    3.45 +        var subject = rb.getString("MSG_SUBJECT")
    3.46 +        val mf = new MessageFormat(rb.getString("MSG_GAME"))
    3.47 +        for (val info <- ret(player)) {
    3.48 +          val delay : Long = (System.currentTimeMillis - info.lastMove) / (3600 * 1000 * 24)
    3.49 +          if (delay > 14) {
    3.50 +            subject = rb.getString("MSG_SUBJECT_WARN")
    3.51 +          }
    3.52 +          val arr = new Array[Object](2);
    3.53 +          arr(0) = "http://quoridor.xelfi.cz/games/" + info.gameId
    3.54 +          arr(1) = java.lang.Long.valueOf(delay)
    3.55 +          sb.append(mf.format(arr))
    3.56 +        }
    3.57 +        if (address != null) {
    3.58 +          sendEmail(
    3.59 +              "quoridor@xelfi.cz",
    3.60 +              subject,
    3.61 +              sb.toString
    3.62 +          )
    3.63 +        }
    3.64      }
    3.65    }
    3.66  }
     4.1 --- a/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala	Sun Apr 25 21:41:00 2010 +0200
     4.2 +++ b/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala	Sun Apr 25 22:46:08 2010 +0200
     4.3 @@ -31,10 +31,16 @@
     4.4  
     4.5  @Test
     4.6  class MainTest {
     4.7 -
     4.8      @Test
     4.9      def testCheckLazyPlayers() = {
    4.10 -      Main.main(null)
    4.11 +      val arr = new Array[String](4)
    4.12 +      /*
    4.13 +      arr(0) = "http://quoridor.xelfi.cz/api/"
    4.14 +      arr(1) = ""
    4.15 +      arr(2) = ""
    4.16 +      arr(3) = "print"
    4.17 +      Main.main(arr)
    4.18 +      */
    4.19      }
    4.20  }
    4.21  
     5.1 --- a/pom.xml	Sun Apr 25 21:41:00 2010 +0200
     5.2 +++ b/pom.xml	Sun Apr 25 22:46:08 2010 +0200
     5.3 @@ -38,7 +38,7 @@
     5.4        <webidorVersion>1.17</webidorVersion>
     5.5        <visidorVersion>1.0-SNAPSHOT</visidorVersion>
     5.6        <freemarkerVersion>1.60</freemarkerVersion>
     5.7 -      <emailerVersion>1.0</emailerVersion>
     5.8 +      <emailerVersion>1.1</emailerVersion>
     5.9        <statisticsVersion>1.8</statisticsVersion>
    5.10    </properties>
    5.11    <modules>