puzzle racer WIP

puzzle-racer-road-translate
Thibault Duplessis 2021-03-05 09:55:21 +01:00
parent 857f297b8d
commit aafb7d4593
7 changed files with 1297 additions and 1257 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -5,8 +5,6 @@ import play.api.libs.json._
import lila.common.Json._ import lila.common.Json._
import lila.common.LightUser import lila.common.LightUser
import lila.common.LightUser.lightUserWrites
import lila.storm
import lila.storm.StormJson import lila.storm.StormJson
import lila.storm.StormPuzzle import lila.storm.StormPuzzle
import lila.storm.StormSign import lila.storm.StormSign
@ -32,10 +30,12 @@ final class RacerJson(stormJson: StormJson, sign: StormSign, lightUserSync: Ligh
) )
private def playersJson(race: RacerRace) = JsArray { private def playersJson(race: RacerRace) = JsArray {
race.players.zipWithIndex.map { case (player, index) => race.players.map { case player =>
val user = player.userId flatMap lightUserSync
Json Json
.obj("index" -> (index + 1), "moves" -> player.moves) .obj("name" -> player.name, "moves" -> player.moves)
.add("user" -> player.userId.flatMap(lightUserSync)) .add("userId" -> player.userId)
.add("title" -> user.map(_.title))
} }
} }
} }

View File

@ -1,23 +1,29 @@
package lila.racer package lila.racer
import org.joda.time.DateTime import org.joda.time.DateTime
import lila.common.CuteNameGenerator
import lila.user.User import lila.user.User
case class RacerPlayer(id: RacerPlayer.Id, createdAt: DateTime, moves: Int) { case class RacerPlayer(id: RacerPlayer.Id, createdAt: DateTime, moves: Int) {
import RacerPlayer.Id import RacerPlayer.Id
def userId: Option[User.ID] = id match { lazy val userId: Option[User.ID] = id match {
case Id.User(id) => id.some case Id.User(name) => User.normalize(name).some
case _ => none case _ => none
}
lazy val name: String = id match {
case Id.User(n) => n
case Id.Anon(id) => CuteNameGenerator fromSeed id.hashCode
} }
} }
object RacerPlayer { object RacerPlayer {
sealed trait Id sealed trait Id
object Id { object Id {
case class User(userId: lila.user.User.ID) extends Id case class User(name: String) extends Id
case class Anon(sessionId: String) extends Id case class Anon(sessionId: String) extends Id
def apply(str: String) = def apply(str: String) =
if (str startsWith "@") Anon(str drop 1) if (str startsWith "@") Anon(str drop 1)
else User(str) else User(str)

View File

@ -218,13 +218,16 @@ interface Study {
setTab(tab: string): void; setTab(tab: string): void;
} }
interface LightUser { interface LightUserNoId {
id: string;
name: string; name: string;
title?: string; title?: string;
patron?: boolean; patron?: boolean;
} }
interface LightUser extends LightUserNoId {
id: string;
}
interface Navigator { interface Navigator {
deviceMemory?: number; // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory deviceMemory?: number; // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory
} }

View File

@ -29,8 +29,9 @@ export interface ServerState {
} }
export interface Player { export interface Player {
index: number; name: string;
user?: LightUser; userId?: string;
title?: string;
moves: number; moves: number;
} }

View File

@ -5,13 +5,19 @@ import { Player } from '../interfaces';
export const renderRace = (ctrl: RacerCtrl) => h('div.racer__race', ctrl.players().map(renderPlayer)); export const renderRace = (ctrl: RacerCtrl) => h('div.racer__race', ctrl.players().map(renderPlayer));
const renderPlayer = (player: Player) => const renderPlayer = (player: Player, index: number) =>
h('div.racer__race__player', [ h('div.racer__race__player', [
h('div.racer__race__player__name', player.user ? userName(player.user) : ['Anon', ' ', player.index]), h('div.racer__race__player__name', playerLink(player)),
h(`div.racer__race__player__car.car-${player.index}`, [player.moves]), h(`div.racer__race__player__car.car-${index}`, [player.moves]),
]); ]);
export const userName = (user: LightUser): Array<string | VNode> => export const playerLink = (player: Player) =>
user.title player.userId
? [h('span.utitle', user.title == 'BOT' ? { attrs: { 'data-bot': true } } : {}, user.title), ' ', user.name] ? h(
: [user.name]; 'a.user-link.ulpt',
{
attrs: { href: '/@/' + player.name },
},
player.title ? [h('span.utitle', player.title), player.name] : [player.name]
)
: h('anonymous', player.name);