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.
bulk challenge WIP - for lichess-org#8059
- Loading branch information
Showing
8 changed files
with
124 additions
and
34 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
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
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,63 @@ | ||
package lila.challenge | ||
|
||
import akka.stream.scaladsl._ | ||
import chess.variant.Variant | ||
import chess.{ Clock, Mode, Situation, Speed } | ||
import org.joda.time.DateTime | ||
import scala.util.chaining._ | ||
|
||
import lila.common.LilaStream | ||
import lila.game.{ Game, Player } | ||
import lila.rating.PerfType | ||
import lila.setup.SetupBulk.ScheduledBulk | ||
import lila.user.User | ||
|
||
final class ChallengeBulkApi( | ||
gameRepo: lila.game.GameRepo, | ||
userRepo: lila.user.UserRepo, | ||
onStart: lila.round.OnStart | ||
)(implicit | ||
ec: scala.concurrent.ExecutionContext, | ||
mat: akka.stream.Materializer | ||
) { | ||
|
||
def apply( | ||
by: User, | ||
scheduled: ScheduledBulk | ||
): Fu[Int] = { | ||
val perfType = PerfType(scheduled.variant, Speed(scheduled.clock)) | ||
val startClock = scheduled.startClocksAt isBefore DateTime.now | ||
Source(scheduled.games) | ||
.mapAsyncUnordered(8) { game => | ||
userRepo.pair(game.white, game.black) map2 { case (white, black) => | ||
(game.id, white, black) | ||
} | ||
} | ||
.mapConcat(_.toList) | ||
.map[Game] { case (id, white, black) => | ||
Game | ||
.make( | ||
chess = | ||
chess.Game(situation = Situation(scheduled.variant), clock = scheduled.clock.toClock.some), | ||
whitePlayer = Player.make(chess.White, white.some, _(perfType)), | ||
blackPlayer = Player.make(chess.Black, black.some, _(perfType)), | ||
mode = scheduled.mode, | ||
source = lila.game.Source.Api, | ||
pgnImport = None | ||
) | ||
.withId(id) | ||
.start | ||
.pipe { g => | ||
if (startClock) g.startClock.fold(g)(_.game) else g | ||
} | ||
} | ||
.mapAsyncUnordered(8) { game => | ||
(gameRepo insertDenormalized game) >>- onStart(game.id) | ||
} | ||
.toMat(LilaStream.sinkCount)(Keep.right) | ||
.run() | ||
.addEffect { nb => | ||
lila.mon.api.challenge.bulk.createNb(by.id).increment(nb).unit | ||
} | ||
} | ||
} |
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
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
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
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
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