forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attempt to compress game pgn as binary data
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package lila | ||
package mongodb | ||
|
||
object PgnBinary { | ||
|
||
def encode(pgn: String): Array[Byte] = { | ||
val ints = pgnToInts(pgn) | ||
val bytes = ints grouped 4 map { | ||
case Nil => 0 | ||
case a :: Nil => a | ||
case a :: b :: Nil => a + 32 | ||
|
||
def decode(bytes: Array[Byte]): String = "" | ||
|
||
private def pgnToInts(str: String): List[Int] = | ||
stringToInt get (str take 1) map (i => (str drop 1, i)) orElse { | ||
if (str startsWith "O-O-O") stringToInt("O-O-O") map (i => (str drop 5), i) | ||
else if (str startsWith "O-O") stringToInt("O-O") map (i => (str drop 3), i) | ||
else None | ||
} fold ((int, rest) => int :: consumeString(rest), Nil) | ||
|
||
private val symbols: List[String] = " " :: { | ||
('a' to 'h').map(_.toString) ++ | ||
(1 to 8).map(_.toString) ++ | ||
"RNBQKPx=+#".toList.map(_.toString) ++ | ||
Seq("O-O", "O-O-O") | ||
}.toList | ||
|
||
private val stringToInt: Map[String, Int] = symbols.zipWithIndex.toMap | ||
private val intToString: Map[Int, String] = (stringToByte map { | ||
case (a, b) ⇒ (b, a) | ||
}).toMap | ||
|
||
// utility methods for printing a byte array | ||
def showArray(b: Array[Byte]) = b.map(showByte).mkString(" ") | ||
def showByte(b: Byte) = pad(((b + 256) % 256).toHexString.toUpperCase) | ||
private def pad(s: String) = if (s.length == 1) "0" + s else s | ||
} |
Oops, something went wrong.