# HG changeset patch # User Jaroslav Tulach # Date 1272228368 -7200 # Node ID 29287b0a1a0be089638e543eca19031aff1c30c1 # Parent a47345ebbdd764f2436b2a55c6d0cf274b54837a Adding description of individual lonely games. More verbose subject added too. diff -r a47345ebbdd7 -r 29287b0a1a0b emailer/src/main/resources/cz/xelfi/quoridor/emailer/Bundle.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/src/main/resources/cz/xelfi/quoridor/emailer/Bundle.properties Sun Apr 25 22:46:08 2010 +0200 @@ -0,0 +1,10 @@ + + +MSG_SUBJECT=Play Quoridor! +MSG_SUBJECT_WARN=Play Quoridor or some of your games will be closed! + +MSG_HEADER=Visit http://quoridor.xelfi.cz\n\n +MSG_GAME=You have not played for {1} days in game {0}.\n + + + diff -r a47345ebbdd7 -r 29287b0a1a0b emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala --- a/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala Sun Apr 25 21:41:00 2010 +0200 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala Sun Apr 25 22:46:08 2010 +0200 @@ -50,15 +50,15 @@ val props = java.lang.System.getProperties props.put("mail.smtp.host", args(3)); - val session = javax.mail.Session.getDefaultInstance(props, null) + val session = if (args(3).equals("print")) null else javax.mail.Session.getDefaultInstance(props, null) val notify = new Notify() { - protected def allPlayers(): Map[String,List[Node]] = { + protected def allPlayers(): Map[String,List[Info]] = { val u = new URL(api, "games"); val conn = u.openConnection conn.setRequestProperty("Accept", "text/xml") - val ret = new HashMap[String,List[Node]] + val ret = new HashMap[String,List[Info]] val before = System.currentTimeMillis - okDelay val games = XML.load(conn.getInputStream) @@ -76,7 +76,9 @@ } if (name != null) { val prev = ret.getOrElse(name, Nil) - val next = g :: prev + val id = (g \\ "@id").text + val info = new Info(modified, id) + val next = info :: prev ret.put(name, next); } } @@ -84,8 +86,8 @@ return ret; } - protected def emails(ids : Collection[String]): List[String] = { - var ret : List[String] = Nil; + protected def emails(ids : Collection[String]): Map[String,String] = { + var ret = new HashMap[String,String] for (val p <- ids) { val u = new URL(api, "users/" + p + "?loginID=" + loginID) val conn = u.openConnection @@ -98,13 +100,21 @@ } }) if (!res.isEmpty) { - ret = res(0).text :: ret; + ret.put(p, res(0).text) } } return ret } protected def sendEmail(address : String, subject : String, text : String) : Unit = { + Console.println("Sending message to") + Console.println(" email: " + address.split('@')(0) + "@... ") + Console.println(" subject: " + subject) + Console.println(" " + text) + if (session == null) { + return; + } + val message = new MimeMessage(session) message.setFrom(new InternetAddress("quoridor@xelfi.cz")) message.addRecipient(Message.RecipientType.TO, new InternetAddress(address)) diff -r a47345ebbdd7 -r 29287b0a1a0b emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala --- a/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala Sun Apr 25 21:41:00 2010 +0200 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala Sun Apr 25 22:46:08 2010 +0200 @@ -26,31 +26,49 @@ package cz.xelfi.quoridor.emailer -import scala.xml._ import java.net.URL -import scala.io._ +import java.util.ResourceBundle +import java.text.MessageFormat import scala.collection.Map import scala.collection.mutable.HashMap -import javax.mail.internet._ -import javax.mail.Message -import javax.mail.Transport abstract class Notify { - protected def allPlayers(): Map[String,List[Node]] - protected def emails(ids : Collection[String]): List[String] + protected def allPlayers(): Map[String,List[Info]] + protected def emails(ids : Collection[String]): Map[String,String] protected def sendEmail(address : String, subject : String, text : String) : Unit + case class Info(lastMove : Long, gameId : String) { + } + def process() { val ret = allPlayers() + val name2Address = emails(ret.keySet) - for (val address <- emails(ret.keySet)) { - Console.println("Sending message to " + address.split('@')(0)) + for (val player <- ret.keySet) { + val address = name2Address.getOrElse(player, null) - sendEmail( - "quoridor@xelfi.cz", - "Play Quoridor!", - "Visit http://quoridor.xelfi.cz" - ) + val rb = ResourceBundle.getBundle("cz.xelfi.quoridor.emailer.Bundle") + val sb = new StringBuilder + sb.append(rb.getString("MSG_HEADER")) + var subject = rb.getString("MSG_SUBJECT") + val mf = new MessageFormat(rb.getString("MSG_GAME")) + for (val info <- ret(player)) { + val delay : Long = (System.currentTimeMillis - info.lastMove) / (3600 * 1000 * 24) + if (delay > 14) { + subject = rb.getString("MSG_SUBJECT_WARN") + } + val arr = new Array[Object](2); + arr(0) = "http://quoridor.xelfi.cz/games/" + info.gameId + arr(1) = java.lang.Long.valueOf(delay) + sb.append(mf.format(arr)) + } + if (address != null) { + sendEmail( + "quoridor@xelfi.cz", + subject, + sb.toString + ) + } } } } diff -r a47345ebbdd7 -r 29287b0a1a0b emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala --- a/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala Sun Apr 25 21:41:00 2010 +0200 +++ b/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala Sun Apr 25 22:46:08 2010 +0200 @@ -31,10 +31,16 @@ @Test class MainTest { - @Test def testCheckLazyPlayers() = { - Main.main(null) + val arr = new Array[String](4) + /* + arr(0) = "http://quoridor.xelfi.cz/api/" + arr(1) = "" + arr(2) = "" + arr(3) = "print" + Main.main(arr) + */ } } diff -r a47345ebbdd7 -r 29287b0a1a0b pom.xml --- a/pom.xml Sun Apr 25 21:41:00 2010 +0200 +++ b/pom.xml Sun Apr 25 22:46:08 2010 +0200 @@ -38,7 +38,7 @@ 1.17 1.0-SNAPSHOT 1.60 - 1.0 + 1.1 1.8