Skip to content

Commit

Permalink
public seeks must use random color WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Aug 28, 2024
1 parent cfabe95 commit cdee6ec
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 76 deletions.
28 changes: 6 additions & 22 deletions modules/lobby/src/main/Biter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import lila.core.user.{ GameUsers, WithPerf }
final private class Biter(
userApi: lila.core.user.UserApi,
gameRepo: lila.core.game.GameRepo,
newPlayer: lila.core.game.NewPlayer,
fixedColor: scalalib.cache.ExpireSetMemo[GameId]
newPlayer: lila.core.game.NewPlayer
)(using Executor)(using idGenerator: lila.core.game.IdGenerator):

def apply(hook: Hook, sri: Sri, user: Option[LobbyUser]): Fu[JoinHook] =
Expand All @@ -26,7 +25,7 @@ final private class Biter(
for
users <- userApi.gamePlayersAny(ByColor(lobbyUserOption.map(_.id), hook.userId), hook.perfType)
(joiner, owner) = users.toPair
ownerColor <- assignCreatorColor(owner, joiner, hook.realColor)
ownerColor <- assignCreatorColor(owner, joiner)
game <- idGenerator.withUniqueId:
makeGame(
hook,
Expand All @@ -35,7 +34,6 @@ final private class Biter(
_ <- gameRepo.insertDenormalized(game)
yield
lila.mon.lobby.hook.join.increment()
rememberIfFixedColor(hook.realColor, game)
JoinHook(sri, hook, game, ownerColor)

private def join(seek: Seek, lobbyUser: LobbyUser): Fu[JoinSeek] =
Expand All @@ -44,31 +42,17 @@ final private class Biter(
.gamePlayersLoggedIn(ByColor(lobbyUser.id, seek.user.id), seek.perfType)
.orFail(s"No such seek users: $seek")
(joiner, owner) = users.toPair
ownerColor <- assignCreatorColor(owner.some, joiner.some, seek.realColor)
ownerColor <- assignCreatorColor(owner.some, joiner.some)
game <- idGenerator.withUniqueId:
makeGame(
seek,
ownerColor.fold(ByColor(owner, joiner), ByColor(joiner, owner)).map(some)
)
_ <- gameRepo.insertDenormalized(game)
yield
rememberIfFixedColor(seek.realColor, game)
JoinSeek(joiner.id, seek, game, ownerColor)

private def rememberIfFixedColor(color: TriColor, game: Game) =
if color != TriColor.Random
then fixedColor.put(game.id)
yield JoinSeek(joiner.id, seek, game, ownerColor)

private def assignCreatorColor(
creatorUser: Option[WithPerf],
joinerUser: Option[WithPerf],
color: TriColor
): Fu[Color] =
color match
case TriColor.Random =>
userApi.firstGetsWhite(creatorUser.map(_.id), joinerUser.map(_.id)).map { Color.fromWhite(_) }
case TriColor.White => fuccess(chess.White)
case TriColor.Black => fuccess(chess.Black)
private def assignCreatorColor(creatorUser: Option[WithPerf], joinerUser: Option[WithPerf]): Fu[Color] =
userApi.firstGetsWhite(creatorUser.map(_.id), joinerUser.map(_.id)).map { Color.fromWhite(_) }

private def makeGame(hook: Hook, users: GameUsers) = lila.core.game
.newGame(
Expand Down
17 changes: 4 additions & 13 deletions modules/lobby/src/main/Hook.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import lila.core.perf.UserWithPerfs
import lila.core.rating.RatingRange
import lila.core.socket.Sri
import lila.rating.PerfType
import lila.core.pool.IsClockCompatible

// realtime chess, volatile
case class Hook(
Expand All @@ -18,15 +19,12 @@ case class Hook(
variant: Variant.Id,
clock: Clock.Config,
mode: Int,
color: String,
user: Option[LobbyUser],
ratingRange: String,
createdAt: Instant,
boardApi: Boolean
):

val realColor: TriColor = TriColor.orDefault(color)

val realVariant = Variant.orDefault(variant)

val realMode = Mode.orDefault(mode)
Expand All @@ -38,7 +36,6 @@ case class Hook(
mode == h.mode &&
variant == h.variant &&
clock == h.clock &&
(realColor.compatibleWith(h.realColor)) &&
ratingRangeCompatibleWith(h) && h.ratingRangeCompatibleWith(this) &&
(userId.isEmpty || userId != h.userId)

Expand Down Expand Up @@ -80,15 +77,11 @@ case class Hook(
.add("rating" -> rating)
.add("variant" -> realVariant.exotic.option(realVariant.key))
.add("ra" -> realMode.rated.option(1))
.add("c" -> Color.fromName(color).map(_.name))

def randomColor = color == "random"

def compatibleWithPools(using isClockCompatible: lila.core.pool.IsClockCompatible) =
realMode.rated && realVariant.standard && randomColor &&
isClockCompatible(clock)
def compatibleWithPools(using isClockCompatible: IsClockCompatible) =
realMode.rated && realVariant.standard && isClockCompatible(clock)

def compatibleWithPool(poolClock: chess.Clock.Config)(using lila.core.pool.IsClockCompatible) =
def compatibleWithPool(poolClock: chess.Clock.Config)(using IsClockCompatible) =
compatibleWithPools && clock == poolClock

private lazy val speed = Speed(clock)
Expand All @@ -102,7 +95,6 @@ object Hook:
variant: chess.variant.Variant,
clock: Clock.Config,
mode: Mode,
color: String,
user: Option[UserWithPerfs],
sid: Option[String],
ratingRange: RatingRange,
Expand All @@ -115,7 +107,6 @@ object Hook:
variant = variant.id,
clock = clock,
mode = mode.id,
color = color,
user = user.map(LobbyUser.make(_, blocking)),
sid = sid,
ratingRange = ratingRange.toString,
Expand Down
16 changes: 4 additions & 12 deletions modules/lobby/src/main/Seek.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,19 @@ case class Seek(
variant: Variant.Id,
daysPerTurn: Option[Days],
mode: Int,
color: String,
user: LobbyUser,
ratingRange: String,
createdAt: Instant
):

inline def id = _id

val realColor = TriColor.orDefault(color)

val realVariant = Variant.orDefault(variant)

val realMode = Mode.orDefault(mode)

def compatibleWith(h: Seek) =
user.id != h.user.id &&
compatibilityProperties == h.compatibilityProperties &&
(realColor.compatibleWith(h.realColor)) &&
ratingRangeCompatibleWith(h) && h.ratingRangeCompatibleWith(this)

private def ratingRangeCompatibleWith(s: Seek) =
Expand All @@ -57,8 +52,7 @@ case class Seek(
"rating" -> rating,
"variant" -> Json.obj("key" -> realVariant.key),
"perf" -> Json.obj("key" -> perfType.key),
"mode" -> realMode.id,
"color" -> (Color.fromName(color).so(_.name): String)
"mode" -> realMode.id
)
.add("days" -> daysPerTurn)
.add("provisional" -> perf.provisional.yes)
Expand All @@ -68,32 +62,30 @@ object Seek:
given UserIdOf[Seek] = _.user.id

val idSize = 8
def makeId = ThreadLocalRandom.nextString(idSize)

def make(
variant: chess.variant.Variant,
daysPerTurn: Option[Days],
mode: Mode,
color: String,
user: UserWithPerfs,
ratingRange: RatingRange,
blocking: lila.core.pool.Blocking
): Seek = Seek(
_id = ThreadLocalRandom.nextString(idSize),
_id = makeId,
variant = variant.id,
daysPerTurn = daysPerTurn,
mode = mode.id,
color = color,
user = LobbyUser.make(user, blocking),
ratingRange = ratingRange.toString,
createdAt = nowInstant
)

def renew(seek: Seek) = Seek(
_id = ThreadLocalRandom.nextString(idSize),
_id = makeId,
variant = seek.variant,
daysPerTurn = seek.daysPerTurn,
mode = seek.mode,
color = seek.color,
user = seek.user,
ratingRange = seek.ratingRange,
createdAt = nowInstant
Expand Down
2 changes: 1 addition & 1 deletion modules/lobby/src/main/SeekApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class SeekApi(
.foldLeft(List.empty[Seek] -> Set.empty[String]) {
case ((res, h), seek) if seek.user.id == user.id => (seek :: res, h)
case ((res, h), seek) =>
val seekH = List(seek.variant, seek.daysPerTurn, seek.mode, seek.color, seek.user.id).mkString(",")
val seekH = List(seek.variant, seek.daysPerTurn, seek.mode, seek.user.id).mkString(",")
if h contains seekH then (res, h)
else (seek :: res, h + seekH)
}
Expand Down
2 changes: 0 additions & 2 deletions modules/setup/src/main/HookConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ case class HookConfig(
variant = variant,
clock = clock,
mode = if lila.core.game.allowRated(variant, clock.some) then mode else Mode.Casual,
color = color.name,
user = user,
blocking = blocking,
sid = sid,
Expand All @@ -71,7 +70,6 @@ case class HookConfig(
variant = variant,
daysPerTurn = makeDaysPerTurn,
mode = mode,
color = color.name,
user = u,
blocking = blocking,
ratingRange = ratingRange
Expand Down
16 changes: 3 additions & 13 deletions ui/lobby/css/app/_hook-list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
@media (max-width: at-most($xx-small)) {
padding: 1em 0.4em;

&:nth-child(3) {
&:nth-child(2) {
max-width: 13vw;
direction: rtl;
text-align: left;
Expand All @@ -79,14 +79,8 @@
background: $m-bg--fade-50;

&:first-child {
width: 16px;
}

&:first-child ::before {
font-size: 1.2em;
line-height: 1.3;
padding-inline-start: 2em;
}

&:last-child ::before {
margin-inline-end: 8px;
line-height: 0.9;
Expand All @@ -100,12 +94,8 @@
@media (max-width: at-most($xx-small)) {
padding: 0.5em 0.4em;

&:first-child {
padding-inline-start: 0.7em;
}

// player name
&:nth-child(2) {
&:first-child {
@include ellipsis;
max-width: 25vw;
}
Expand Down
2 changes: 0 additions & 2 deletions ui/lobby/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export interface Hook {
u?: string; // username
rating?: number;
ra?: 1; // rated
c?: Color;
action: 'cancel' | 'join';
disabled?: boolean;
}
Expand All @@ -43,7 +42,6 @@ export interface Seek {
rating: number;
mode: number;
days?: number;
color: string;
perf: {
key: Exclude<Perf, 'fromPosition'>;
};
Expand Down
4 changes: 3 additions & 1 deletion ui/lobby/src/seekRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export function sort(ctrl: LobbyController) {
}

export function initAll(ctrl: LobbyController) {
ctrl.data.seeks.forEach(function (seek) {
ctrl.data.seeks.forEach(seek => {
seek.action = ctrl.me && seek.username === ctrl.me.username ? 'cancelSeek' : 'joinSeek';
console.log(ctrl.me, seek);
console.log(ctrl.me?.username, seek.username, seek.action);
});
sort(ctrl);
}
Expand Down
3 changes: 1 addition & 2 deletions ui/lobby/src/view/correspondence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ function renderSeek(ctrl: LobbyController, seek: Seek): VNode {
},
},
tds([
h('span.is.color-icon.' + (seek.color || 'random')),
seek.rating
? h('span.ulpt', { attrs: { 'data-href': '/@/' + seek.username } }, seek.username)
: 'Anonymous',
Expand Down Expand Up @@ -61,7 +60,7 @@ export default function (ctrl: LobbyController): MaybeVNodes {
'thead',
h(
'tr',
['', 'player', 'rating', 'time', 'mode'].map(header => h('th', ctrl.trans(header))),
['player', 'rating', 'time', 'mode'].map(header => h('th', ctrl.trans(header))),
),
),
h(
Expand Down
5 changes: 2 additions & 3 deletions ui/lobby/src/view/realTime/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ function renderPlot(ctrl: LobbyController, hook: Hook) {
}

function renderHook(ctrl: LobbyController, hook: Hook): string {
const color = hook.c || 'random';
let html = '<div class="inner">';
if (hook.rating) {
html += '<a class="opponent ulpt is color-icon ' + color + '" href="/@/' + hook.u + '">';
html += '<a class="opponent ulpt is color-icon" href="/@/' + hook.u + '">';
html += ' ' + hook.u;
if (ctrl.opts.showRatings) html += ' (' + hook.rating + (hook.prov ? '?' : '') + ')';
html += '</a>';
} else {
html += '<span class="opponent anon ' + color + '">' + ctrl.trans('anonymous') + '</span>';
html += '<span class="opponent anon">' + ctrl.trans('anonymous') + '</span>';
}
html += '<div class="inner-clickable">';
html += `<div>${hook.clock}</div>`;
Expand Down
2 changes: 0 additions & 2 deletions ui/lobby/src/view/realTime/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function renderHook(ctrl: LobbyController, hook: Hook) {
},
},
tds([
h('span.is.color-icon.' + (hook.c || 'random')),
hook.rating
? h('span.ulink.ulpt', { attrs: { 'data-href': '/@/' + hook.u } }, hook.u)
: noarg('anonymous'),
Expand Down Expand Up @@ -76,7 +75,6 @@ export const render = (ctrl: LobbyController, allHooks: Hook[]) => {
'thead',
h('tr', [
h('th'),
h('th', ctrl.trans('player')),
h(
'th',
{
Expand Down
4 changes: 1 addition & 3 deletions ui/lobby/src/view/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { h } from 'snabbdom';
import { MaybeVNodes } from 'common/snabbdom';

export function tds(bits: MaybeVNodes): MaybeVNodes {
return bits.map(function (bit) {
return h('td', [bit]);
});
return bits.map(bit => h('td', [bit]));
}

export const perfNames = {
Expand Down

0 comments on commit cdee6ec

Please sign in to comment.