Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import annotation.tailrec
- import reflect.BeanInfo
- import util.matching.Regex
- import util.parsing.combinator.{Parsers, RegexParsers}
- object RefactoredAkaraLogParser {
- // イベントデータ
- trait Event {
- val event: String
- }
- case class Ignore(line: String) extends Event {
- val event = "__ignore__"
- }
- @BeanInfo case class Message(value: String) extends Event {
- def this() = this(null)
- val event = "message"
- }
- @BeanInfo case class NewTurn(player: String) extends Event {
- val event = "newTurn"
- }
- @BeanInfo case class TimeLimit(max: Double, fine: Double, easy: Double) extends Event {
- val event = "timeLimit"
- }
- @BeanInfo case class BestMove(move: Move, feeling: String, ballots: List[Ballot]) extends Event {
- val event = "bestMove"
- }
- @BeanInfo case class Ballot(
- votes: Double, move: Move, nps: Int, elapsed: Int, name: String, score: String)
- @BeanInfo case class Pondering(ballots: List[Ballot]) extends Event {
- val event = "pondering"
- }
- @BeanInfo
- case class Position(x: Int, y: Int)
- trait Move {
- val from: Position
- val to: Position
- val koma: String
- }
- @BeanInfo
- case class MoveWithoutTurn(from: Position, to: Position, koma: String) extends Move
- @BeanInfo
- case class MoveWithTurn(turn: String, from: Position, to: Position, koma: String) extends Move
- trait EndOfTurn {
- val move: Move
- val time: Int
- }
- @BeanInfo
- case class EndOfMyTurn(move: Move, time: Int) extends EndOfTurn with Event {
- val event = "endOfTurn(own)"
- }
- @BeanInfo
- case class EndOfOpponentsTurn(move: Move, time: Int, lookahead: String) extends EndOfTurn with Event {
- val event = "endOfTurn(opponent)"
- }
- trait RegexPatternParsers extends RegexParsers {
- //It is needed to disable RegexParser's implicit conversion Regex --> Parser[String]
- override def regex(r: Regex): Parser[String] = super.regex(r)
- implicit def pattern(r: Regex): Parser[List[String]] = new Parser[List[String]] {
- def apply(in: Input) = {
- val source = in.source
- val offset = in.offset
- r.findPrefixMatchOf(source.subSequence(offset, source.length)) match {
- case Some(matched) =>
- Success(matched.subgroups, in.drop(matched.end))
- case None =>
- Failure("string matching regex `" + r + "expected but `" + in.first + "' found", in.drop(offset))
- }
- }
- }
- }
- object ParserCore extends RegexPatternParsers {
- // 構文解析
- private val moveInfo: Parser[Move] = """([+-])?(\d)(\d)(\d)(\d)([A-Z]{2})""".r ^? ({
- case List(null, oldPosX, oldPosY, newPosX, newPosY, koma) =>
- MoveWithoutTurn(
- Position(oldPosX.toInt, oldPosY.toInt),
- Position(newPosX.toInt, newPosY.toInt),
- koma)
- case List(turn, oldPosX, oldPosY, newPosX, newPosY, koma) if turn == "+" || turn == "-" =>
- MoveWithTurn(
- turn match {
- case "+" => "1st"
- case "-" => "2nd"
- },
- Position(oldPosX.toInt, oldPosY.toInt),
- Position(newPosX.toInt, newPosY.toInt),
- koma)
- }, move => "Illegal move: " + move)
- private val inCmd = """^([^>]+)> (.*)$""".r
- private val newTurn: Parser[Event] = (
- inCmd ^? {
- case List("csa", "START:akara+ipsj50-99999-9999+shimizu+Akara2010+20101011130107") => NewTurn("shimizu")
- }
- | """^(.+) turn starts\.$""".r ^? ({
- case List("My") => NewTurn("Akara2010")
- case List("Opponent's") => NewTurn("shimizu")
- }, player => "The player name is illegal: " + player)
- )
- //残りの実装
- }
- }
Add Comment
Please, Sign In to add comment