Guest User

Untitled

a guest
Jun 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import annotation.tailrec
  2. import reflect.BeanInfo
  3. import util.matching.Regex
  4. import util.parsing.combinator.{Parsers, RegexParsers}
  5. object RefactoredAkaraLogParser {
  6. // イベントデータ
  7. trait Event {
  8. val event: String
  9. }
  10.  
  11. case class Ignore(line: String) extends Event {
  12. val event = "__ignore__"
  13. }
  14.  
  15. @BeanInfo case class Message(value: String) extends Event {
  16. def this() = this(null)
  17. val event = "message"
  18. }
  19.  
  20. @BeanInfo case class NewTurn(player: String) extends Event {
  21. val event = "newTurn"
  22. }
  23.  
  24. @BeanInfo case class TimeLimit(max: Double, fine: Double, easy: Double) extends Event {
  25. val event = "timeLimit"
  26. }
  27.  
  28. @BeanInfo case class BestMove(move: Move, feeling: String, ballots: List[Ballot]) extends Event {
  29. val event = "bestMove"
  30. }
  31.  
  32. @BeanInfo case class Ballot(
  33. votes: Double, move: Move, nps: Int, elapsed: Int, name: String, score: String)
  34.  
  35. @BeanInfo case class Pondering(ballots: List[Ballot]) extends Event {
  36. val event = "pondering"
  37. }
  38.  
  39. @BeanInfo
  40. case class Position(x: Int, y: Int)
  41.  
  42. trait Move {
  43. val from: Position
  44. val to: Position
  45. val koma: String
  46. }
  47.  
  48. @BeanInfo
  49. case class MoveWithoutTurn(from: Position, to: Position, koma: String) extends Move
  50.  
  51. @BeanInfo
  52. case class MoveWithTurn(turn: String, from: Position, to: Position, koma: String) extends Move
  53.  
  54. trait EndOfTurn {
  55. val move: Move
  56. val time: Int
  57. }
  58.  
  59. @BeanInfo
  60. case class EndOfMyTurn(move: Move, time: Int) extends EndOfTurn with Event {
  61. val event = "endOfTurn(own)"
  62. }
  63.  
  64. @BeanInfo
  65. case class EndOfOpponentsTurn(move: Move, time: Int, lookahead: String) extends EndOfTurn with Event {
  66. val event = "endOfTurn(opponent)"
  67. }
  68.  
  69. trait RegexPatternParsers extends RegexParsers {
  70. //It is needed to disable RegexParser's implicit conversion Regex --> Parser[String]
  71. override def regex(r: Regex): Parser[String] = super.regex(r)
  72.  
  73. implicit def pattern(r: Regex): Parser[List[String]] = new Parser[List[String]] {
  74. def apply(in: Input) = {
  75. val source = in.source
  76. val offset = in.offset
  77. r.findPrefixMatchOf(source.subSequence(offset, source.length)) match {
  78. case Some(matched) =>
  79. Success(matched.subgroups, in.drop(matched.end))
  80. case None =>
  81. Failure("string matching regex `" + r + "expected but `" + in.first + "' found", in.drop(offset))
  82. }
  83. }
  84. }
  85. }
  86.  
  87. object ParserCore extends RegexPatternParsers {
  88. // 構文解析
  89. private val moveInfo: Parser[Move] = """([+-])?(\d)(\d)(\d)(\d)([A-Z]{2})""".r ^? ({
  90. case List(null, oldPosX, oldPosY, newPosX, newPosY, koma) =>
  91. MoveWithoutTurn(
  92. Position(oldPosX.toInt, oldPosY.toInt),
  93. Position(newPosX.toInt, newPosY.toInt),
  94. koma)
  95. case List(turn, oldPosX, oldPosY, newPosX, newPosY, koma) if turn == "+" || turn == "-" =>
  96. MoveWithTurn(
  97. turn match {
  98. case "+" => "1st"
  99. case "-" => "2nd"
  100. },
  101. Position(oldPosX.toInt, oldPosY.toInt),
  102. Position(newPosX.toInt, newPosY.toInt),
  103. koma)
  104. }, move => "Illegal move: " + move)
  105. private val inCmd = """^([^>]+)> (.*)$""".r
  106. private val newTurn: Parser[Event] = (
  107. inCmd ^? {
  108. case List("csa", "START:akara+ipsj50-99999-9999+shimizu+Akara2010+20101011130107") => NewTurn("shimizu")
  109. }
  110. | """^(.+) turn starts\.$""".r ^? ({
  111. case List("My") => NewTurn("Akara2010")
  112. case List("Opponent's") => NewTurn("shimizu")
  113. }, player => "The player name is illegal: " + player)
  114. )
  115. //残りの実装
  116. }
  117. }
Add Comment
Please, Sign In to add comment