Last active
October 31, 2015 14:59
-
-
Save lmdexpr/d67197bdd238f09f0002 to your computer and use it in GitHub Desktop.
CODE RUNNER B
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "code_runner_B" | |
scalaVersion := "2.11.7" | |
libraryDependencies ++= Seq( | |
"net.databinder.dispatch" %% "dispatch-core" % "0.11.2", | |
"org.slf4j" % "slf4j-simple" % "1.6.4", | |
"io.spray" %% "spray-json" % "1.3.2" | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coderunner.b | |
import scala.util._ | |
import scala.concurrent._ | |
import scala.concurrent.duration.Duration | |
import dispatch._, Defaults._ | |
import spray.json._ | |
object Main extends App { | |
val myhost = host("game.coderunner.jp") | |
val token = "念のため、変えとくよ" | |
val query = myhost / "enter" <<? Map("token" -> token) | |
val infoQuery = myhost / "infoJson" <<? Map("token" -> token) | |
var wait_time = 200 | |
case class Friend( | |
id:String, | |
power:Int, | |
damage:Int | |
) | |
case class Log( | |
id:String, | |
enemy:Int, | |
time:Int, | |
hp:Int | |
) | |
case class Info( | |
power:Int, | |
damage:Int, | |
hp:Int, | |
maxhp:Int, | |
hps:Array[Int], | |
friend:Array[Friend], | |
log:Array[Log] | |
) | |
object MyJsonProtocol extends DefaultJsonProtocol { | |
implicit val FriendFormat = jsonFormat3(Friend) | |
implicit val LogFormat = jsonFormat4(Log) | |
implicit val InfoFormat = jsonFormat7(Info) | |
} | |
def post(message:String) { | |
for ( res <- Http(query > as.String).either ) | |
res match { | |
case Right(content) => | |
if (content contains "Too") { | |
wait_time += 1 | |
println("NOW : " + wait_time.toString) | |
} | |
println(message + " : "+ content) | |
case Left(err) => println("ERROR!!! " + err) | |
} | |
} | |
def loop() { | |
import MyJsonProtocol._ | |
val raw = Await.result(Http(infoQuery OK as.String), Duration.Inf) | |
if (raw == "OUT ROOM") post(raw) | |
else { | |
val json = raw.parseJson.convertTo[Info] | |
if (json.power >= json.hp || (json.power >= (json.hp / 2) && json.friend.exists(_.power >= json.power))) | |
post(json.power.toString) | |
} | |
} | |
while (true) { | |
loop() | |
Thread.sleep(wait_time) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment