summaryrefslogtreecommitdiff
path: root/docs/overview
diff options
context:
space:
mode:
Diffstat (limited to 'docs/overview')
-rw-r--r--docs/overview/architecture.md58
-rw-r--r--docs/overview/database.md175
-rw-r--r--docs/overview/module.md314
3 files changed, 547 insertions, 0 deletions
diff --git a/docs/overview/architecture.md b/docs/overview/architecture.md
new file mode 100644
index 0000000..6413386
--- /dev/null
+++ b/docs/overview/architecture.md
@@ -0,0 +1,58 @@
+# Architecture Overview
+
+The basic architecture of Rally the Troops is a single server process storing all data in an sqlite database.
+This server handles both HTTP requests for the "meta-site" and websocket requests for the rules when playing a game.
+
+## Meta-site
+
+The meta-site consists of all the web pages showing the login, signup, active game lists, the create a game page, forum, etc.
+These pages are all created using PUG templates in the "view" directory.
+The "join" page is the most complicated, and also uses javascript and server sent events for live updates.
+
+## Database
+
+See the [database overview](/docs/overview/database) for a brief introduction to the tables involved.
+
+## Client/Server
+
+When playing a game, the browser (client) connects to the server with a web socket.
+The query string indicates which game and role to connect to.
+
+The game state is stored in a JSON object. A redacted version of this game state is sent to the clients, which then update the game display in real time.
+One part of the view object is a list of possible actions a player can take.
+When a player clicks on a possible action, a message is sent to the server with this action.
+The server then processes the action, updates the game state, and sends out a new view object to all the clients that are connected to the game.
+
+The client code for connecting to the server, sending actions, and managing the player list, game log, chat, and notes is shared between all modules.
+The following files contain the code and styling for the client display:
+
+* <a href="https://git.rally-the-troops.com/common/server/tree/public/fonts/fonts.css">public/fonts/fonts.css</a>
+* <a href="https://git.rally-the-troops.com/common/server/tree/public/common/client.css">public/common/client.css</a>
+* <a href="https://git.rally-the-troops.com/common/server/tree/public/common/client.js">public/common/client.js</a>
+
+## Tools
+
+The "tools" directory holds a number of other useful scripts for administrating the server and debugging modules.
+
+ bash tools/export-game.sh game_id > file.json
+ Export full game state to a JSON file.
+
+ node tools/import-game.js file.json
+ Import a game from an export JSON file.
+
+ node tools/patchgame.js game_id
+ Patch game state for one game (by replaying the action log).
+
+ node tools/patchgame.js title_id
+ Patch game state for all active games of one module.
+
+ bash tools/undo.sh game_id
+ Undo an action by removing the last entry in the replay log and running patchgame.js
+
+ bash tools/showgame.sh game_id
+ Print game state JSON object for debugging.
+
+<!--
+ bash tools/gencovers.sh
+ Generate cover images and thumbnails. Requires imagemagick.
+-->
diff --git a/docs/overview/database.md b/docs/overview/database.md
new file mode 100644
index 0000000..db96df1
--- /dev/null
+++ b/docs/overview/database.md
@@ -0,0 +1,175 @@
+# Database Overview
+
+The database uses the following schemas, somewhat simplified and redacted to highlight the important bits.
+
+## Users
+
+The user table is pretty simple, it just holds the user name and e-mail.
+
+ create table users (
+ user_id integer primary key,
+ name text unique collate nocase,
+ mail text unique collate nocase
+ );
+
+Passwords are hashed and salted with SHA-256.
+
+ create table user_password (
+ user_id integer primary key,
+ password text,
+ salt text
+ );
+
+The login session cookie is a 48-bit random number.
+
+ create table logins (
+ sid integer primary key,
+ user_id integer,
+ expires real -- julianday
+ );
+
+Webhook notification settings are kept in the webhooks table.
+The error column keeps any error message such as timeouts or HTTP failure statuses.
+It is null if the hook is operational.
+
+ create table webhooks (
+ user_id integer primary key,
+ url text,
+ format text,
+ prefix text,
+ error text
+ );
+
+The contact list keeps track of friends (positive relation) and blacklisted users (negative relation).
+
+ create table contacts (
+ me integer,
+ you integer,
+ relation integer,
+ primary key (me, you)
+ );
+
+
+## Modules
+
+The game modules to load are registered in the titles table. The title_id must match the directory name for the module.
+
+ create table titles (
+ title_id text primary key,
+ title_name text,
+ bgg integer,
+ is_symmetric boolean
+ );
+
+## Games
+
+Each game session uses a handful of tables.
+The main table holds the status (open/active/finished), setup information (scenario, options), whose turn it is (active), and the final result.
+
+ create table games (
+ game_id integer primary key,
+ status integer,
+
+ title_id text,
+ scenario text,
+ options text,
+
+ player_count integer,
+ join_count integer,
+ invite_count integer,
+ user_count integer,
+
+ owner_id integer,
+ notice text,
+ pace integer,
+ is_private boolean,
+ is_random boolean,
+ is_match boolean,
+
+ ctime datetime,
+ mtime datetime,
+ moves integer,
+ active text,
+ result text,
+
+ is_opposed boolean generated as ( user_count = join_count and join_count > 1 ),
+ is_ready boolean generated as ( player_count = join_count and invite_count = 0 )
+ );
+
+The players table connects users to the games they play.
+
+ create table players (
+ game_id integer,
+ role text,
+ user_id integer,
+ is_invite integer,
+ is_active boolean,
+ primary key (game_id, role)
+ );
+
+The game state is represented by a JSON blob.
+
+ create table game_state (
+ game_id integer primary key,
+ state text
+ );
+
+Each action taken in stored in the game_replay log. This is primarily used for
+the detailed "Sherlock" replay view, but is also used to patch running games when
+fixing bugs.
+
+ create table game_replay (
+ game_id integer,
+ replay_id integer,
+ role text,
+ action text,
+ arguments json,
+ primary key (game_id, replay_id)
+ );
+
+Whenever who is active changes, we take a snapshot of the game state
+so we can provide the coarse turn-by-turn rewind view that is available when
+playing. This table is also used when rewinding games. The replay_id syncs each
+snapshot with the corresponding action in the game_replay table.
+
+ create table game_snap (
+ game_id integer,
+ snap_id integer,
+ replay_id integer,
+ state text,
+ primary key (game_id, snap_id)
+ );
+
+Game chat is kept in another table, and there's also a table to track whether a
+user has any unread in-game chat messages, and one table to track finished but not yet
+seen games.
+
+ create table game_chat (
+ game_id integer,
+ chat_id integer,
+ user_id integer,
+ time datetime,
+ message text,
+ primary key (game_id, chat_id)
+ );
+
+ create table unread_chats (
+ user_id integer,
+ game_id integer,
+ primary key (user_id, game_id)
+ );
+
+ create table unseen_games (
+ user_id integer,
+ game_id integer,
+ primary key (user_id, game_id)
+ );
+
+## Other tables
+
+There are several other tables to deal with tournaments, password reset tokens, email
+notifications, messages, and the forum.
+
+See the full
+[schema.sql](https://git.rally-the-troops.com/common/server/tree/schema.sql)
+for more details on these.
diff --git a/docs/overview/module.md b/docs/overview/module.md
new file mode 100644
index 0000000..2a12461
--- /dev/null
+++ b/docs/overview/module.md
@@ -0,0 +1,314 @@
+# Module Overview
+
+A module consists of a directory containing certain mandatory files which are loaded by the server.
+All the other resources needed for a module are also put in this directory.
+Note that all of the files in the module will be available on the web.
+
+The module must be put in the server "public" directory, matching the name of title_id.
+If our example game has the title "My Example Module" and a title_id of "example",
+the module directory should be "public/example/".
+
+## Two halves make one
+
+A module consists of two separate parts:
+
+1. The rules work with actions and their consequences.
+2. The client present the game and possible actions to the players.
+
+The rules run on the server. The client runs in the player's browser.
+The rules create a view object that is passed to the client, containing the visible game state
+and a list of possible actions. The client presents this to the players, and sends the chosen
+actions back to the rules for execution.
+
+
+## Metadata
+
+Each module needs to be registered in the database so that the system can find it.
+To do this we create a title.sql file that we source to register the module.
+
+ insert or replace into titles
+ ( title_id, title_name, bgg )
+ values
+ ( 'example', 'Example', 123 )
+ ;
+
+The bgg column should have the <a href="httsp://www.boardgamegeek.com/">boardgamegeek.com</a> game id.
+
+After creating this file, source it into the database and restart the server program.
+
+ $ sqlite3 db < public/example/title.sql
+
+## Cover image
+
+Each game needs a cover image! Create a file containing the high resolution cover image named cover.png or cover.jpg. After you have created or modified the cover image, run the following script to generate the small cover images and thumbnails that the site uses.
+
+ $ bash tools/gencovers.sh
+
+<i>This script requires ImageMagick (convert), netpbm (pngtopnm), and libjpeg (cjpeg) tools to be installed.</i>
+
+## About text
+
+The game landing page on the server has a bit of text introducing the game, and links to rules and other reference material.
+
+Put the about text in the about.html file.
+
+ <p>
+ Dolorum fugiat dolor temporibus. Debitis ea non illo sed
+ debitis cupiditate ipsum illo. Eos eos molestias illo
+ quisquam dicta.
+
+ <ul>
+ <li> <a href="info/rules.html">Rules</a>
+ </ul>
+
+## Options
+
+When creating a game, the scenarios and common options are handled by the system.
+However, if your game uses custom options these need to be added as form fields.
+
+Add a create.html file to inject form fields into the create game page.
+
+ <p>
+ Player count:
+ <br>
+ <select name="players">
+ <option value="2">2 Player</option>
+ <option value="3">3 Player</option>
+ <option value="4">4 Player</option>
+ </select>
+
+ <p>
+ <label>
+ <input type="checkbox" value="true" name="house_rule">
+ House rule
+ </label>
+
+This file may be empty if your game doesn't have any custom options.
+
+## Client HTML
+
+The game needs a play.html file using the following template:
+
+ <!doctype html>
+ <html lang="en">
+ <head>
+ <meta name="viewport" content="width=device-width,
+ height=device-height,
+ user-scalable=no,
+ interactive-widget=resizes-content,
+ viewport-fit=cover">
+ <meta name="theme-color" content="#444">
+ <meta charset="UTF-8">
+ <title>
+ GAME TITLE
+ </title>
+ <link rel="icon" href="favicon.svg">
+ <link rel="stylesheet" href="/fonts/fonts.css">
+ <link rel="stylesheet" href="/common/client.css">
+ <link rel="stylesheet" href="play.css">
+ <script defer src="/common/client.js"></script>
+ <script defer src="play.js"></script>
+ </head>
+ <body>
+
+ <header>
+ <div id="toolbar">
+ <details>
+ <summary><img src="/images/cog.svg"></summary>
+ <menu>
+ <li><a href="info/rules.html" target="_blank">Rules</a>
+ <li class="separator">
+ </menu>
+ </details>
+ </div>
+ </header>
+
+ <aside>
+ <div id="roles"></div>
+ <div id="log"></div>
+ </aside>
+
+ <main data-min-zoom="0.5" data-max-zoom="2.0">
+ GAME AREA
+ </main>
+
+ <footer id="status"></footer>
+
+ </body>
+
+## Client CSS
+
+Put the game specific stylesheet in play.css.
+
+If the stylesheet is very small you could also include it inline in play.html.
+
+## Client Program
+
+As you saw above, the client page references a play.js script which should
+contain the code for updating the game. This script must provide a couple of
+functions that are called by the common client code whenever the game state
+changes.
+
+### The view object
+
+The view object is passed from the server rules module to the client!
+
+It must contain both some common properties that you shouldn't touch ("log" and
+"prompt" and "actions") and all the information you need to display the current
+board state.
+
+### Client script globals
+
+These global variables are provided by the framework for use with your code.
+
+<dl>
+
+<dt>var player
+<dd>This variable holds the name of the current role the player has in this browser window.
+The value may change if the client is in replay mode.
+
+<dt>var view
+<dd>This object contains the view for the role as returned by the rules view method.
+
+</dl>
+
+### Client script functions
+
+These functions are provided to your client code to build the game interface and communicate with the server.
+
+<dl>
+
+<dt>function send_action(verb, noun)
+<dd>Call this when player performs an action (such as clicking on a piece).
+If the action is not in the legal list of actions in the view object,
+this function does nothing and returns false. Returns true if the action
+is legal.
+
+<dt>function action_button(verb, label)
+<dd>Show a push button in the top bar for a simple action (if the action is legal).
+
+<dt>function action_button_with_argument(verb, noun, label)
+<dd>Show a push button in the top bar for an action taking a specific argument (if the action with the argument is legal).
+
+
+<dt>function confirm_send_action(verb, noun, question)
+<dd>Same as send_action but pop up a confirmation dialog first.
+
+<dt>function confirm_action_button(verb, label, question)
+<dd>Same as action_button but pop up a confirmation dialog first.
+
+<dt>function send_query(what)
+<dd>Send a "query" message to the rules. The result will be passed to on_reply.
+
+</dl>
+
+### function on_update()
+
+This function is called whenever the "view" object has updated.
+It should update the visible game representation and highlight possible actions.
+
+The client code has already updated the prompt message and log when this is called.
+
+The view.actions object contains the list of legal actions to present.
+
+### function on_prompt(text)
+
+Optional function to implement custom HTML formatting of the prompt text.
+If present, this function must return an HTML _string_.
+
+### function on_log(text)
+
+Optional function to implement custom HTML formatting of log messages.
+If present, this function must return an HTML _element_ representing the given text.
+
+### function on_reply(what, response)
+
+This function is invoked with the result of send_query.
+The what parameter matches the argument to send_query and is used to identify different queries.
+
+See rommel-in-the-desert and wilderness-war for examples of using the query mechanism.
+
+## Rules Program
+
+The rules.js script is loaded by the server.
+Certain properties and functions must be provided by the rules module.
+
+> NOTE: See the [module rules](/docs/module/rules) documentation if you want to
+> use the shared rules framework that provides a structured approach to
+> implementing game rules.
+
+
+### Scenarios
+
+A list of scenarios! If there are no scenarios, it should be a list with one element "Standard".
+
+ exports.scenarios = [ "Standard" ]
+
+### Roles
+
+A list of roles, or a function returning a list of roles.
+
+ exports.roles = [ "White", "Black" ]
+
+ exports.roles = function (scenario, options) {
+ if (scenario === "3P")
+ return [ "White", "Black", "Red" ]
+ else
+ return [ "White", "Black" ]
+ }
+
+### Setup
+
+ exports.setup = function (seed, scenario, options) {
+ var game = {
+ seed: seed,
+ log: [],
+ undo: [],
+ active: "White",
+ }
+ ...
+ return game
+ }
+
+Create the initial game object with a random number seed, scenario, and options object.
+
+The "setup" function takes three parameters provided by the server: the random seed (generated by the server when starting the game), the scenario (a string with the scenario name) and options (a plain javascript object with key/value pairs for the options chosen by the user when creating the game).
+
+The options object is filled in with values from the create.html form fields.
+
+### View
+
+ exports.view = function (game, player) {
+ var view = {
+ log: game.log,
+ ...
+ }
+ if (game.active === player) {
+ view.actions = {}
+ // generate list of actions
+ }
+ return view
+ }
+
+Given a game object, a player role, return the view object that is used by the client to display the game state.
+This should contain game state known to the player.
+
+### Action
+
+ exports.action = function (game, player, verb, noun) {
+ // handle action
+ return game
+ }
+
+Perform an action taken by a player. Return a game object representing the new state. It's okay to mutate the input game object.
+
+### Query
+
+ exports.query = function (game, player, what) {
+ if (what === "discard")
+ return game.discard[player]
+ return null
+ }
+
+A custom query for information that is normally not presented.
+For example showing a supply line overview, or a list of cards in a discard pile.