emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 244 32dd025e4b76
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.emailer
    19 
    20 import java.net.URL
    21 import java.util.ResourceBundle
    22 import java.text.MessageFormat
    23 import scala.collection.Map
    24 import scala.collection.mutable.HashMap
    25 
    26 abstract class Notify {
    27   protected def allPlayers(): Map[String,List[Info]]
    28   protected def emails(ids : Collection[String]): Map[String,String]
    29   protected def sendEmail(address : String, subject : String, text : String) : Unit
    30   protected def resignGame(id : Info): Unit
    31 
    32   case class Info(lastMove : Long, gameId : String, toMove : String) {
    33   }
    34 
    35   def process() {
    36     val ret = allPlayers()
    37     val name2Address = emails(ret.keySet)
    38 
    39     for (val player <- ret.keySet) {
    40         val address = name2Address.getOrElse(player, null)
    41 
    42         val rb = ResourceBundle.getBundle("cz.xelfi.quoridor.emailer.Bundle")
    43         val sb = new StringBuilder
    44         sb.append(rb.getString("MSG_HEADER"))
    45         var subject = rb.getString("MSG_SUBJECT")
    46         val mf = new MessageFormat(rb.getString("MSG_GAME"))
    47         val mfCancel = new MessageFormat(rb.getString("MSG_GAME_CANCEL"))
    48         for (val info <- ret(player)) {
    49           val delay : Long = (System.currentTimeMillis - info.lastMove) / (3600 * 1000 * 24)
    50           val arr = new Array[Object](2);
    51           arr(0) = "http://quoridor.xelfi.cz/games/" + info.gameId
    52           arr(1) = java.lang.Long.valueOf(delay)
    53           if (delay > 21) {
    54             subject = rb.getString("MSG_SUBJECT_CANCEL")
    55             sb.append(mfCancel.format(arr))
    56             resignGame(info)
    57           } else if (delay > 14) {
    58             subject = rb.getString("MSG_SUBJECT_WARN")
    59             sb.append(mf.format(arr))
    60           }
    61       }
    62         if (address != null) {
    63           sendEmail(
    64               address,
    65               subject,
    66               sb.toString
    67           )
    68         }
    69     }
    70   }
    71 }