Lila (Lichess)

repository·master·Indexed 12 days ago

https://github.com/lichess-org/lila

The open-source engine powering lichess.org, a free, realtime online chess server. Built with Scala 3, Play 2.8, and MongoDB, Lila provides features such as tournaments, computer analysis, and a shared analysis board. The project includes documentation on its architecture, UI theme management, the Lichess Fast Action Trainer, and guidelines for contributing flairs.

Tokens
131.1K
Snippets
439
Records
534
Agent score
98%

What's inside Lila

  1. Overview of the gathering module

    master

    The gathering module provides reusable components designed to support specific Lichess game formats and event structures. It serves as a shared logic layer for the following modules:

    • modules/tournament: Logic for tournament management and participation.
    • modules/swiss: Logic for Swiss-system tournament structures.
    • modules/simul: Logic for simultaneous exhibition matches.
  2. License and redistribution terms for Lila

    master

    Lila is free software distributed under the GNU Affero General Public License (AGPLv3), or any later version. You are permitted to redistribute and/or modify the project under these terms.

    Important Notes:

    • The software is distributed WITHOUT ANY WARRANTY (including implied warranties of merchantability or fitness for a particular purpose).
    • Any file in the project that does not explicitly state otherwise and is not listed in the exceptions is part of lila and copyrighted by the lila authors.
    • For a full copy of the license, refer to the LICENSE file in the repository.
  3. Overview of Lichess (Lila) architecture and technology

    master

    Lila (li[chess in sca]la) is a free online chess server built with the following core technologies:

    • Language/Framework: Written in Scala 3 using a modified Play 2.8 framework. Templating is handled by scalatags.
    • Logic: Pure chess logic is maintained in the scalachess submodule.
    • Concurrency: The server is fully asynchronous, utilizing Scala Futures and Pekko streams.
    • Real-time/WebSockets: WebSocket connections are managed by a separate server (lila-ws) communicating via Redis.
    • Storage: MongoDB stores game data (over 12 billion games), which are indexed using Elasticsearch.
    • Frontend: The web client is built with TypeScript and snabbdom, using Sass for styling.
    • AI/Analysis: Computer analysis is distributed via fishnet, communicating with Stockfish in an AI cluster.
    • Proxying: nginx is used for HTTP requests and WebSocket proxying.
  4. How color themes work in Lichess UI

    master

    Lichess uses a hybrid approach of SCSS and CSS variables to manage themes.

    1. Theme Definitions: Each theme is defined in a partial SCSS file within ui/lib/css/theme/ (e.g., _theme.default.scss, _theme.light.scss). The _theme.default.scss file serves as the foundation.
    2. CSS Variables: Themeable colors are defined as CSS variables (prefixed with --) scoped to an HTML class (e.g., html.example-theme).
    3. SCSS Wrappers: A build script automatically generates SCSS wrapper variables starting with $c- that map to the CSS variables.

    Best Practice: Use the generated SCSS forms (e.g., $c-color) in your style rules instead of raw CSS variables whenever possible to ensure type safety and consistency.

    // Example of how CSS variables are mapped to SCSS wrappers in ui/lib/css/theme/gen/_wrap.scss
    $c-color: var(--c-color);
    $c-color-desaturated: var(--c-color-desaturated);
  5. Understand the role of the site module

    master
    The site module is the primary entry point for all non-embed client pages in the Lichess application. It is responsible for running the main application logic on standard web pages. Additionally, this module defines the window.site global object, which can be used to access site-level functionality from the browser environment.
  6. How the background image gallery works at runtime

    master

    The background gallery system operates through a combination of server-side parsing and client-side rendering:

    • Server-side: When lila starts, it parses gallery.json once and includes the array of image URLs in all dasher responses.
    • Client-side Selection:
      • If the dasher parameters contain a gallery image array, the client constructs a URL to the appropriate thumbnail background (columns-2.webp or columns-4.webp).
      • If no gallery is present in the dasher params, the client falls back to legacy behavior.
    • Rendering: The client creates a grid of div elements (2 or 4 columns) over the montage background. These divs handle:
      • Hover effects: Lightening their specific slice of the parent grid background.
      • Interactivity: Handling clicks and maintaining an outline for the current selection.
  7. Understand voice grammar terminology

    master

    The voice system uses specific terminology to manage how heard phrases are mapped to move and command processing logic:

    TermDefinition
    grammarA list of entries for input words recognized by Kaldi.
    entryA single grammar entry containing an input word, token representation, value code, classification tags, and a substitution list.
    wordA unit of recognition (e.g., "long castle").
    phraseOne or more words separated by spaces.
    tok(Token) A single character uniquely representing the Kaldi-recognizable characteristics of an input word.
    val(Value) The representation of a token/word in terms the move & command processing logic understands. Mappings are not necessarily bijective (e.g., 'captures' and 'takes' share a val but have different toks).
    xtoksAn exact phrase in the token space.
    xvalsAn exact phrase in the value space.
    htoksA heard phrase as a token string (may contain errors/ambiguities).
    hvalsA heard phrase in the value space (comma-separated vals).
    xtoksAn exact phrase in the token space.
    xvalsAn exact phrase in the value space.

    Prefix Convention:

    • x prefix: Represents exact data (what the system tries to map heard data to).
    • h prefix: Represents heard data (the raw input from the user).
  8. What are bits in lila?

    master
    In the lila project, bits are a collection of single-file modules designed for dynamic loading. They are used to provide modular functionality that can be loaded on demand, while still being able to export TypeScript types and loading stubs to ensure type safety and availability during the build or development process.
  9. Create or update a background image gallery

    master

    To manage the background image gallery, follow these steps to prepare assets and generate the necessary metadata and montage files:

    1. Organize Assets: Place your image files in lifat/background/gallery. The gallery order is determined by the alphanumeric sort order of the filenames.
    2. Generate Files: Run the make-gallery.js script.

    What the script does:

    • Uses imagemagick to create columns-2.webp and columns-4.webp. These are montage images containing thumbnails rendered at 160x90px in 2-column and 4-column grids respectively.
    • Generates gallery.json, which contains an ordered array of image asset URLs mapped to their gallery positions.
    # Run the gallery generation script
    node make-gallery.js
  10. Configure cron jobs for the lila server

    master

    The cron directory contains optional scripts designed to run on the lila server via cron. These scripts are primarily intended for production environments to manage database maintenance and background tasks. They are not required for local development.

    Jobs are categorized by the database they target: the production database (prod db) and the puzzle database (puzzle db).

    Each cron entry follows the standard format: m h dom mon dow user command

    ### running on prod db
    # m h dom mon dow user  command
    0  3 * * * root jobq -q mongo_decay mongosh --quiet 172.16.0.50:27017/lichess /home/lichess/deploy/cron/mongodb-report-score-decay.js
    0  4 * * * root jobq -q mongo_queue mongosh --quiet 172.16.0.50:27017/lichess /home/lichess/deploy/cron/mongodb-queue-stats.js
    0  5 * * * root jobq -q mongo_queue mongosh --quiet 172.16.0.50:27017/lichess /home/lichess/deploy/cron/mongodb-simul-cleanup.js
    */2 * * * * root jobq -q mongo_patron mongosh --quiet 172.16.0.50:27017/lichess /home/lichess/deploy/cron/mongodb-patron-denorm.js
    26 * * * * root jobq -q mongo_patron mongosh --quiet 172.16.0.50:27017/lichess /home/lichess/deploy/cron/mongodb-ublog-similar-incremental.js
    * * * * * root jobq -q mongo_fscday25 mongosh --quiet 172.16.0.50:27017/lichess /home/lichess/deploy/cron/mongodb-tournament-participation-trophies.js
    
    ### running on puzzle db
    # m h dom mon dow user  command
    14 */2 * * *  root  jobq -q mongo_puzzles mongosh --quiet rubik:27017/puzzler /home/lichess/deploy/cron/mongodb-puzzle-denormalize-themes.js
    27 */2 * * *  root  jobq -q mongo_puzzles mongosh --quiet rubik:27017/puzzler /home/lichess/deploy/cron/mongodb-puzzle-regen-paths.js
  11. Derive new colors using CSS functions

    master

    When you need a color variation, avoid creating a new unique variable if the color does not require a unique hue. Instead, use CSS functions to derive colors at runtime. This ensures the derived color remains theme-friendly when the user switches themes.

    Recommended methods:

    • color-mix(): Mix two colors together.
    • Relative <color> syntax: Adjust hue, saturation, or lightness.
    • calc(): Perform mathematical operations on color components.

    Example usage in SCSS:

    // Mix 30% of the primary color with the font color
    color: color-mix(in oklab, $c-primary 30%, $c-font);
    
    // Lighten a background color by 20% using relative color syntax
    background-color: hsl(from $c-bg h s calc(l + 20));
    color: color-mix(in oklab, $c-primary 30%, $c-font);
    background-color: hsl(from $c-bg h s calc(l + 20));
  12. Build the UI client

    master

    Use the ui/build script to generate assets for the website. When changes are compiled successfully, a new manifest is created, listing the public JavaScript and CSS assets. The server uses these manifests to communicate updated URLs to browsers.

    To build once:

    ui/build

    To run in watch mode (continuously rebuilds on changes):

    ui/build -w
    ui/build -w