Skip to content

0.40.0#709

Merged
nicolodavis merged 18 commits into
masterfrom
0.40.0
Sep 1, 2020
Merged

0.40.0#709
nicolodavis merged 18 commits into
masterfrom
0.40.0

Conversation

@delucis
Copy link
Copy Markdown
Member

@delucis delucis commented May 21, 2020

0.40.0

Contents

Breaking Changes

New Features

Breaking Changes

Matches in the Lobby API (Issue #703, PRs #666, #704)

Until now, the Lobby API has been slightly inconsistent in how it names an instance of a game, sometimes describing this as a “game” sometimes as a “room”. In release 0.40.0, this is standardised to “match”, making it clearer that you can play a game, like Chess or Tic-Tac-Toe, and the specific instance of a game is a match.

Changes

If you are using the provided React Lobby component, you shouldn’t see any breaking changes, but if you are consuming the Lobby REST API directly, you need to be aware of the following changes.

  1. In JSON returned by the games/{name} endpoint:

    • roomsmatches

    • roomIDmatchID

    {
    - "rooms": [
    + "matches": [
        {
    -     "roomID": "random-string",
    +     "matchID": "random-string",
          "players": {}        
        }
      ]
    }
  2. In JSON returned by the games/{name}/create endpoint:

    • gameIDmatchID
    {
    - "gameID": "random-string"
    + "matchID": "random-string"
    }
  3. In JSON returned by the games/{name}/{id} endpoint:

    • roomIDmatchID
    {
    - "roomID": "random-string",
    + "matchID": "random-string",
      "players": {}
    }
  4. In JSON returned by the games/{name}/{id}/playAgain endpoint:

    • nextRoomIDnextMatchID
    {
    - "nextRoomID": "random-string"
    + "nextMatchID": "random-string"
    }
  5. If writing Typescript and using the Server types namespace: Server.GameMetadataServer.MatchData

Migration Help

  • If you are writing Typescript, you can import types for the data returned by each of the API endpoints so you can know what shape to expect.

    import type { LobbyAPI } from 'boardgame.io';
    
    async function fetchMatches(gameName: string) {
      const response = await fetch(`http://localhost:8000/games/${gameName}`);
      const matches: LobbyAPI.MatchList = await response.json();
    }
  • Even better, we now provide a plain Javascript Lobby API client. For example, you can can do client.listMatches('chess') and get a typed JSON response as in the example above. See under “New Features” below for more details.

Match IDs & metadata in clients (Issue #763, PR #765)

Reflecting the changes in the Lobby API, there are some minor changes to options and props in clients.

  1. In React clients:

    • gameIDmatchID
    import { Client } from 'boardgame.io/react';
    
    const App = Client({ game });
    
    export default () => (
    - <App gameID="xyz" />
    + <App matchID="xyz" />
    );
  2. In plain JS clients:

    • gameIDmatchID

    • updateGameIDupdateMatchID

    • gameMetadatamatchData

    import { Client } from 'boardgame.io/client';
    
    const client = Client({
    - gameID: 'xyz',
    + matchID: 'xyz',
    });
    
    - client.updateGameID('abc');
    + client.updateMatchID('abc');
    
    - const metadata = client.gameMetadata;
    + const metadata = client.matchData;
  3. For React board components, the gameMetadata prop has been renamed:

    • gameMetadatamatchData
    - const Board = ({ gameMetadata }) => (
    + const Board = ({ matchData }) => (
      <ul>
    -   {gameMetadata
    +   {matchData
          .map(player => (
            <li key={player.id}>{player.name}</li>
          ))
        }
      </ul>
    );

Passing a custom uuid method to the Lobby API (PR #698)

By default, boardgame.io uses shortid to generate match IDs and user credentials. Previously this could be customised by passing your own uuid method when running the server. This now has to be done when the server is instantiated, not at run time:

const uuid = () => 'id';

const server = Server({
+ uuid,
});

server.run({
  port: 8000,
- uuid,
});

New Features

Timestamp metadata and match filtering in the Lobby API (Issue #735, PR #740)

Match data now contains createdAt and updatedAt timestamps.

The REST API also supports a query string to filter matches when listing them. Three queries have been added:

  • isGameover: allows listing only games that have or have not finished
  • updatedBefore: allows filtering games last updated before a specific time
  • updatedAfter: allows filtering games last updated after a specific time

An example query URL might look like:

https://lobby.api/games/chess?isGameover=false&updatedAfter=1592400449376

The actual filtering is performed by the storage backend, so if a storage implementation’s listGames method hasn’t been updated to support this yet, the API will continue to return unfiltered results.

Plain JS Lobby Client (PR #728)

The library now provides a lightweight, stateless wrapper around the Fetch API to simplify requests on the client. This is fully typed, so Typescript users should benefit from a better experience and if there are future API changes, these will be easier to refactor for.

Full documentation will be added to the Lobby docs, but here’s a taste:

import { LobbyClient } from 'boardgame.io/client';

const lobbyClient = new LobbyClient({ server: 'http://localhost:8000' });

lobbyClient.listMatches('tic-tac-toe', where: { isGameover: false })
  .then(data => console.log(data.matches));
   // => [
   //   {
   //     matchID: 'xyz',
   //     gameName: 'tic-tac-toe',
   //     players: [{ id: 0, name: 'Alice' }, { id: 1 }]
   //   },
   //   ...
   // ]

Expose the server’s API router (PR #698)

You can now access the Koa router instance used for the Lobby API when creating your server:

import { Server } from 'boardgame.io/server';

const server = Server({/* options */});

server.router.get('/custom-endpoint', (ctx, next) => {
  ctx.body = 'Custom Response'
});

server.run(8000);

c-w and others added 4 commits May 17, 2020 21:11
BREAKING CHANGES: Previously a custom `uuid` function used by the Lobby API to generate game IDs could be passed in the `lobbyConfig` option on `server.run`. This must now be passed directly to the server:

```js
const server = Server({
  uuid: () => 'id',
});
```

### All commits

* Expose API router
* refactor(server): Pass uuid directly to Server and createRouter
* docs(server): Move uuid from lobbyConfig on run to Server options

Co-authored-by: delucis <[email protected]>
* fix(api): Expose gameover metadata in rooms endpoints.

This adds the `gameover` metadata, if it exists, to both the all rooms and single room query endpoints.

Fixes #665.

* Refactor game metadata redacting into a function.

Co-authored-by: Chris Swithinbank <[email protected]>
Closes #703

* GameMetadata -> MatchMetadata

* rooms -> matches

* API exposes `matchID`, make Lobby use that instead of `gameID` or `roomID`.

* CreateGame -> CreateMatch

* gameID, roomID -> matchID

* Document Lobby API endpoints.

* nextRoomID -> nextMatchID

* Update error messages.

* gameList -> matchList

* Use "match" wherever it makes sense in lobby code.

* Replace "room" with "match" in documentation.

* Rename 'game' to 'match' in comments and test names.

Co-authored-by: Chris Swithinbank <[email protected]>

* doesGameRequireAuthentication -> doesMatchRequireAuthentication

* Rename gameID to matchID in database interface functions.

Co-authored-by: Chris Swithinbank <[email protected]>
@delucis delucis added this to the 0.40.0 milestone May 21, 2020
delucis and others added 7 commits May 27, 2020 22:42
* add createdAt, updatedAt to metadata, add listGames filters

* case-insensitive boolean parsing

* implement inmemory listGames filter
* feat(server): Create types for Lobby API data

Closes #707

This creates types for data returned by the Lobby API and uses them to 
type the response bodies in the server-side router.

* refactor(types): Include min and max players options in Game interface

* refactor(lobby): Convert connection class to Typescript

* feat(lobby): Create a plain JS LobbyClient

LobbyClient is a lightweight wrapper around `fetch` calls to a 
boardgame.io Lobby API server. Apart from the server’s base URL, it is 
stateless and serves mainly to allow argument validation and to abstract 
away a few request details. Unlike plain fetch, requests will throw 
errors if they don’t return `ok` (i.e. status 2xx).

* refactor(lobby): Use new LobbyClient class in React lobby connection

* fix(lobby): Include missing import for React types

* docs(api): Update Lobby docs to convert game to match more thoroughly

* docs: Correct inline documentation block

* docs: Update Lobby docs

- Add examples with the plain JS client
- Move the server config details to the Server doc

* feat(lobby): Add support for listMatches filtering to client

#740 added the ability to pass query string parameters to the 
listMatches API endpoint to filter the matches returned. This adds 
support for building the relevant query string to the lobby client’s 
listMatches method.
Add `createdAt` and `updatedAt` fields to mock match data
@delucis delucis marked this pull request as ready for review July 18, 2020 10:29
@delucis delucis requested a review from nicolodavis July 18, 2020 10:29
delucis added 3 commits July 18, 2020 12:38
Two tests for gameover & updatedAt in match metadata were 
mislabelled/mixed up a bit. This tidies up the confusion and in the 
process improves test coverage.
@delucis
Copy link
Copy Markdown
Member Author

delucis commented Jul 18, 2020

@nicolodavis What do you think about merging this now that you’ve released the 0.39.15 patch? I’ve been thinking it may make sense to release 0.40 without the move signature refactor (#662) or other changes we initially planned. There are already enough significant changes here that it may make sense to break that further work into 0.41. Let me know what you think — I’m also happy to take on the move signature refactor when the time comes if you’d like.

Edit: As this updates docs too, I guess we shouldn’t merge this without releasing 0.40 shortly after as well.

* refactor: Rename `gameID` to `matchID` in clients & transports

* docs: Update to replace `gameID` with `matchID`

* docs: Update example code to use `matchID` instead of `gameID`

* refactor: `gameMetadata` → `matchMetadata` in clients & transports

* refactor: `matchMetadata` → `matchData`

* refactor(types): `MatchMetadata` → `MatchData`
@adngdb
Copy link
Copy Markdown
Contributor

adngdb commented Aug 31, 2020

I am currently unable to build this branch locally, I get the following error:

Sorry, I forgot to npm install first. :-(

nicolodavis
nicolodavis previously approved these changes Sep 1, 2020
@nicolodavis nicolodavis merged commit 859d442 into master Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants