# HG changeset patch # User Jaroslav Tulach # Date 1259262416 -3600 # Node ID ac818c32c6e9ce95ad872d25d214120f287fad71 # Parent 07e3bcb65c1d303df62fc4689ff31274fb4d8592 Scala version of an emailer. So far it finds the lazy players. It needs to send them email yet. diff -r 07e3bcb65c1d -r ac818c32c6e9 emailer/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/pom.xml Thu Nov 26 20:06:56 2009 +0100 @@ -0,0 +1,98 @@ + + + 4.0.0 + + all-quoridor + org.apidesign + 1.0 + + org.apidesign + emailer + 1.0 + 2008 + + src/main/scala + src/test/scala + + + org.scala-tools + maven-scala-plugin + + + + compile + testCompile + + + + + ${scala.version} + + -target:jvm-1.5 + + + + + + + + scala-tools.org + Scala-Tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + scala-tools.org + Scala-Tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + org.scala-lang + scala-library + ${scala.version} + + + junit + junit + 4.4 + test + + + org.specs + specs + 1.2.5 + test + + + + + + + org.scala-tools + maven-scala-plugin + + ${scala.version} + + + + + + 2.7.0 + + Emails lazy players + jar + Obtains list of players without any activity for a while and sends them an email. + + + + diff -r 07e3bcb65c1d -r ac818c32c6e9 emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala Thu Nov 26 20:06:56 2009 +0100 @@ -0,0 +1,111 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.emailer + +import scala.xml._ +import java.net.URL +import scala.io._ +import scala.collection.Map +import scala.collection.mutable.HashMap + +object Main { + def main(args: Array[String]) { + val api = new URL("http://quoridor.xelfi.cz/api/"); + + val id = login(api, "emailer", "otravuj") + Console.println("id: " + id) + + val ret = allPlayers( + api, + 3600 * 1000 * 24 + ) + for (val address <- emails(api, id, ret.keySet)) { + Console.println(address) + } + } + + def login(url : URL, name : String, password : String) : String = { + val u = new URL(url, "login?name=" + name + "&password=" + password); + val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection] + conn.setRequestMethod("PUT") + conn.setDoInput(true) + conn.connect + val reply = Source.fromInputStream(conn.getInputStream) + return reply.mkString("") + } + + def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = { + val u = new URL(url, "games"); + val conn = u.openConnection + conn.setRequestProperty("Accept", "text/xml") + + val ret = new HashMap[String,List[Node]] + + val before = System.currentTimeMillis - okDelay + val games = XML.load(conn.getInputStream) + for (val g : Node <- games \\ "gameId") { + val modified = (g \\ "@modified").text.toLong + if (modified < before) { + val status = (g \\ "@status").text; + val name = + if (status == "blackMove") { + (g \\ "@black").text + } else if (status == "whiteMove") { + (g \\ "@white").text + } else { + null + } + if (name != null) { + val prev = ret.getOrElse(name, Nil) + val next = g :: prev + ret.put(name, next); + } + } + } + return ret; + } + + def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = { + var ret : List[String] = Nil; + for (val p <- ids) { + val u = new URL(url, "users/" + p + "?loginID=" + loginID) + val conn = u.openConnection + conn.setRequestProperty("Accept", "text/xml") + val xml = XML.load(conn.getInputStream) + val res = (xml \\ "property").filter({ + n => n.attribute("name") match { + case Some(attr) => attr.text == "email" + case _ => false + } + }) + if (!res.isEmpty) { + ret = res(0).text :: ret; + } + } + return ret + } +} diff -r 07e3bcb65c1d -r ac818c32c6e9 emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala Thu Nov 26 20:06:56 2009 +0100 @@ -0,0 +1,17 @@ +package cz.xelfi.quoridor.emailer + +import org.junit._ +import Assert._ + +@Test +class AppTest { + + @Test + def testOK() = assertTrue(true) + +// @Test +// def testKO() = assertTrue(false) + +} + + diff -r 07e3bcb65c1d -r ac818c32c6e9 emailer/src/test/scala/cz/xelfi/quoridor/emailer/MySpec.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MySpec.scala Thu Nov 26 20:06:56 2009 +0100 @@ -0,0 +1,17 @@ +package org.apidesign.emailer + +import org.specs._ +import org.specs.runner.{ConsoleRunner, JUnit4} + +class MySpecTest extends JUnit4(MySpec) +//class MySpecSuite extends ScalaTestSuite(MySpec) +object MySpecRunner extends ConsoleRunner(MySpec) + +object MySpec extends Specification { + "This wonderful system" should { + "save the world" in { + val list = Nil + list must beEmpty + } + } +} diff -r 07e3bcb65c1d -r ac818c32c6e9 pom.xml --- a/pom.xml Thu Nov 19 09:35:29 2009 +0100 +++ b/pom.xml Thu Nov 26 20:06:56 2009 +0100 @@ -38,6 +38,7 @@ visidor webidor freemarkerdor + emailer Quoridor related projects Master project that agregates all quoridor related functionality.