Skip to content

Commit

Permalink
scalachess 12 - probably with runtime breakage
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 29, 2022
1 parent 37b53b8 commit 3ff3bab
Show file tree
Hide file tree
Showing 26 changed files with 57 additions and 157 deletions.
2 changes: 1 addition & 1 deletion app/controllers/Editor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class Editor(env: Env) extends LilaController(env):
OptionResult(env.game.gameRepo game id) { game =>
Redirect {
if (game.playable) routes.Round.watcher(game.id, "white").url
else editorUrl(get("fen").fold(Forsyth >> game.chess)(FEN.apply), game.variant)
else editorUrl(get("fen").fold(Forsyth >> game.chess)(FEN(_)), game.variant)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/views/round/bits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import lila.app.templating.Environment.{ given, * }
import lila.app.ui.ScalatagsTemplate.{ *, given }
import lila.game.{ Game, Pov }
import lila.common.LangPath
import lila.common.Json.given

object bits:

Expand Down Expand Up @@ -127,7 +128,7 @@ object bits:
)(implicit ctx: Context) =
views.html.game.side(
pov,
(data \ "game" \ "initialFen").asOpt[String].map(chess.format.FEN.apply),
(data \ "game" \ "initialFen").asOpt[chess.format.FEN],
tour,
simul = simul,
userTv = userTv,
Expand Down
2 changes: 1 addition & 1 deletion app/views/team/tournaments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object tournaments:
if (t.variant.exotic) t.variant.name else t.perfType.trans,
t.position.isDefined option frag("", trans.thematic()),
"",
t.mode.fold(trans.casualTournament, trans.ratedTournament)(),
if t.mode.rated then trans.ratedTournament() else trans.casualTournament(),
"",
t.durationString
)
Expand Down
2 changes: 1 addition & 1 deletion app/views/tournament/finishedList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object finishedList:
if (t.variant.exotic) t.variant.name else t.perfType.trans,
t.position.isDefined option frag("", trans.thematic()),
"",
t.mode.fold(trans.casualTournament, trans.ratedTournament)(),
if t.mode.rated then trans.ratedTournament() else trans.casualTournament(),
"",
t.durationString
)
Expand Down
2 changes: 1 addition & 1 deletion app/views/tournament/side.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object side:
separator,
tour.durationString
),
tour.mode.fold(trans.casualTournament, trans.ratedTournament)(),
if tour.mode.rated then trans.ratedTournament() else trans.casualTournament(),
separator,
"Arena",
(isGranted(_.ManageTournament) || (ctx.userId
Expand Down
4 changes: 2 additions & 2 deletions modules/analyse/src/main/Info.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ case class Info(

def encode: String =
List(
best ?? (_.piotr),
best ?? (_.chars),
variation take Info.LineMaxPlies mkString " ",
mate ?? (_.value.toString),
cp ?? (_.value.toString)
Expand Down Expand Up @@ -69,7 +69,7 @@ object Info:
case Array(cp, ma) => Info(ply, Eval(strCp(cp), strMate(ma), None)).some
case Array(cp, ma, va) => Info(ply, Eval(strCp(cp), strMate(ma), None), va.split(' ').toList).some
case Array(cp, ma, va, be) =>
Info(ply, Eval(strCp(cp), strMate(ma), Uci.Move piotr be), va.split(' ').toList).some
Info(ply, Eval(strCp(cp), strMate(ma), Uci.Move fromChars be), va.split(' ').toList).some
case _ => none

def decodeList(str: String, fromPly: Int): Option[List[Info]] = {
Expand Down
20 changes: 10 additions & 10 deletions modules/common/src/main/Form.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ object Form:
}

object fen:
given Formatter[FEN] = formatter.stringFormatter[FEN](_.value, FEN.apply)
given Formatter[FEN] = formatter.stringFormatter[FEN](_.value, FEN(_))
val playableStrict = playable(strict = true)
def playable(strict: Boolean) = of[FEN]
.transform[FEN](f => FEN(f.value.trim), identity)
Expand Down Expand Up @@ -221,13 +221,13 @@ object Form:
given Formatter[String] = stringFormat
given Formatter[Boolean] = booleanFormat

given [A, T](using
bts: SameRuntime[A, T],
stb: SameRuntime[T, A],
given autoFormat[A, T](using
sr: SameRuntime[A, T],
rs: SameRuntime[T, A],
base: Formatter[A]
): Formatter[T] with
def bind(key: String, data: Map[String, String]) = base.bind(key, data) map bts.apply
def unbind(key: String, value: T) = base.unbind(key, stb(value))
def bind(key: String, data: Map[String, String]) = base.bind(key, data) map sr.apply
def unbind(key: String, value: T) = base.unbind(key, rs(value))

given Formatter[chess.variant.Variant] =
formatter.stringFormatter[chess.variant.Variant](_.key, chess.variant.Variant.orDefault)
Expand All @@ -236,12 +236,12 @@ object Form:
def transform[B](to: A => B, from: B => A): Formatter[B] = new Formatter[B]:
def bind(key: String, data: Map[String, String]) = f.bind(key, data) map to
def unbind(key: String, value: B) = f.unbind(key, from(value))
def into[B](using bts: SameRuntime[A, B], stb: SameRuntime[B, A]): Formatter[B] =
transform(bts.apply, stb.apply)
def into[B](using sr: SameRuntime[A, B], rs: SameRuntime[B, A]): Formatter[B] =
transform(sr.apply, rs.apply)

extension [A](m: Mapping[A])
def into[B](using bts: SameRuntime[A, B], stb: SameRuntime[B, A]): Mapping[B] =
m.transform(bts.apply, stb.apply)
def into[B](using sr: SameRuntime[A, B], rs: SameRuntime[B, A]): Mapping[B] =
m.transform(sr.apply, rs.apply)

object strings:
def separator(sep: String) = of[List[String]](
Expand Down
2 changes: 0 additions & 2 deletions modules/common/src/main/Iso.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,3 @@ object Iso:
given IntIso[Centis] = int[Centis](Centis.apply, _.centis)

given StringIso[Lang] = string[Lang](Lang.apply, _.toString)

given StringIso[FEN] = string[FEN](FEN.apply, _.value)
2 changes: 0 additions & 2 deletions modules/common/src/main/Json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ object Json:
JsString(c.name)
}

given Format[FEN] = stringIsoFormat[FEN]

given Reads[Uci] = Reads.of[String] flatMapResult { str =>
JsResult.fromTry(Uci(str) toTry s"Invalid UCI: $str")
}
Expand Down
2 changes: 1 addition & 1 deletion modules/common/src/main/Lila.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ object Lila extends Lila:

trait Lila
extends lila.base.LilaTypes
with lila.base.NewTypes
with lila.base.LilaModel
with lila.base.LilaUserId
with cats.syntax.OptionSyntax
with cats.syntax.ListSyntax
with ornicar.scalalib.Zeros
with lila.base.LilaLibraryExtensions:

export ornicar.scalalib.newtypes.*
export ornicar.scalalib.OrnicarBooleanWrapper

trait IntValue extends Any:
Expand Down
6 changes: 5 additions & 1 deletion modules/common/src/main/base/LilaModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package lila.base

import alleycats.Zero
import cats.Show
import org.joda.time.DateTime
import ornicar.scalalib.newtypes.*

trait LilaModel extends NewTypes:
trait LilaModel:

trait OpaqueDate[A](using A =:= DateTime) extends TotalWrapper[A, DateTime]

trait Percent[A]:
def apply(a: A): Double
Expand Down
5 changes: 0 additions & 5 deletions modules/common/src/main/base/LilaTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,3 @@ trait LilaTypes:
def zero = Duration.Zero
given Zero[JsObject] with
def zero = JsObject(Seq.empty)
// given Zero[JsResult] with
// def zero = JsError(Seq.empty)

// given Ordering[DateTime] with
// def compare = Ordering.fromLessThan[DateTime](_ isBefore _)
3 changes: 2 additions & 1 deletion modules/common/src/main/base/LilaUserId.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package lila.base

import cats.Show
import ornicar.scalalib.newtypes.*

trait LilaUserId extends NewTypes:
trait LilaUserId:

trait UserIdOf[U]:
def apply(a: U): UserId
Expand Down
100 changes: 0 additions & 100 deletions modules/common/src/main/newtypes.scala

This file was deleted.

4 changes: 2 additions & 2 deletions modules/evalCache/src/main/BSONHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ private object BSONHandlers:
str.toIntOption map { c =>
Score cp Cp(c)
}
private def movesWrite(moves: Moves): String = Uci writeListPiotr moves.value.toList
private def movesWrite(moves: Moves): String = Uci writeListChars moves.value.toList
private def movesRead(str: String): Option[Moves] = Moves from {
Uci readListPiotr str flatMap (_.toNel)
Uci readListChars str flatMap (_.toNel)
}
private val scoreSeparator = ':'
private val pvSeparator = '/'
Expand Down
8 changes: 5 additions & 3 deletions modules/evalCache/src/main/EvalCacheSocketHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,33 @@ import play.api.libs.json.*
import chess.format.FEN
import lila.socket.*
import lila.user.User
import lila.common.Json.given

final private class EvalCacheSocketHandler(
api: EvalCacheApi,
truster: EvalCacheTruster,
upgrade: EvalCacheUpgrade
)(using ec: scala.concurrent.ExecutionContext):
)(using scala.concurrent.ExecutionContext):

def evalGet(
sri: Socket.Sri,
d: JsObject,
push: JsObject => Unit
): Unit =
for {
fen <- d str "fen" map FEN.apply
fen <- d.get[FEN]("fen")
variant = Variant orDefault ~d.str("variant")
multiPv = (d int "mpv") | 1
path <- d str "path"
}
} yield {
def pushData(data: JsObject) = push(Socket.makeMessage("evalHit", data))
api.getEvalJson(variant, fen, multiPv) foreach {
_ foreach { json =>
pushData(json + ("path" -> JsString(path)))
}
}
if (d.value contains "up") upgrade.register(sri, variant, fen, multiPv, path)(pushData)
}

def untrustedEvalPut(sri: Socket.Sri, userId: UserId, data: JsObject): Unit =
truster cachedTrusted userId foreach {
Expand Down
4 changes: 2 additions & 2 deletions modules/game/src/main/BSONHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ object BSONHandlers:
black = Pocket(black.map(_.role))
)
},
promoted = r.str("t").view.flatMap(chess.Pos.piotr).to(Set)
promoted = r.str("t").view.flatMap(chess.Pos.fromChar(_)).to(Set)
)
def writes(w: BSON.Writer, o: Crazyhouse.Data) =
BSONDocument(
"p" -> {
o.pockets.white.roles.map(_.forsythUpper).mkString +
o.pockets.black.roles.map(_.forsyth).mkString
},
"t" -> o.promoted.map(_.piotr).mkString
"t" -> o.promoted.map(_.toChar).mkString
)

private[game] given gameDrawOffersHandler: BSONHandler[GameDrawOffers] = tryHandler[GameDrawOffers](
Expand Down
7 changes: 3 additions & 4 deletions modules/game/src/main/PgnStorage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ private object PgnStorage:
def decode(bytes: ByteArray, plies: Int): Decoded =
monitor(_.game.pgn.decode("huffman")) {
val decoded = Encoder.decode(bytes.value, plies)
val unmovedRooks = decoded.unmovedRooks.asScala.view.flatMap(chessPos).to(Set)
val unmovedRooks = decoded.unmovedRooks.asScala.view.map(Pos(_)).toSet
Decoded(
pgnMoves = decoded.pgnMoves.toVector,
pieces = decoded.pieces.asScala.view.flatMap { case (k, v) =>
chessPos(k).map(_ -> chessPiece(v))
pieces = decoded.pieces.asScala.view.map { (k, v) =>
Pos(k) -> chessPiece(v)
}.toMap,
positionHashes = decoded.positionHashes,
unmovedRooks = UnmovedRooks(unmovedRooks),
Expand All @@ -54,7 +54,6 @@ private object PgnStorage:
)
}

private def chessPos(sq: Integer): Option[Pos] = Pos(sq)
private def chessRole(role: JavaRole): Role =
role match
case JavaRole.PAWN => Pawn
Expand Down
2 changes: 1 addition & 1 deletion modules/push/src/main/PushApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ final private class PushApi(
private def describeChallenge(c: Challenge) =
import lila.challenge.Challenge.TimeControl.*
List(
c.mode.fold("Casual", "Rated"),
if c.mode.rated then "Rated" else "Casual",
c.timeControl match {
case Unlimited => "Unlimited"
case Correspondence(d) => s"$d days"
Expand Down
Loading

0 comments on commit 3ff3bab

Please sign in to comment.