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