Skip to content

Commit

Permalink
rename getJoinGameUrl as connectToGame
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerkrewson committed Jan 23, 2021
1 parent b264108 commit 178707b
Show file tree
Hide file tree
Showing 20 changed files with 29 additions and 31 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ That's it! Many existing games already offer these, and can work with rocketcrab

### Creating a config file

The config files, as mentioned above, should be fairly self explanatory. Along with the [config template](https://github.com/tannerkrewson/rocketcrab/blob/master/config/games/_template.ts), check out the config files of other games to see how they implement rocketcrab. The most important part, which will be explained here, is the `getJoinGameUrl` function. This function:
The config files, as mentioned above, should be fairly self explanatory. Along with the [config template](https://github.com/tannerkrewson/rocketcrab/blob/master/config/games/_template.ts), check out the config files of other games to see how they implement rocketcrab. The most important part, which will be explained here, is the `connectToGame` function. This function:

- is `async`, which will allow you to use `await` to make `GET` or `POST` requests.
- needs to return an object with these properties:
Expand All @@ -45,15 +45,15 @@ The config files, as mentioned above, should be fairly self explanatory. Along w
- `customQueryParams` (optional) a record of query params in addition to or to replace the automatic query params. See the [config template](https://github.com/tannerkrewson/rocketcrab/blob/master/config/games/_template.ts) for more info.
- `afterQueryParams` (optional) string that will be appended to the `playerURL` after automatic and custom query params are applied.

Here are three examples of different `getJoinGameUrl` functions:
Here are three examples of different `connectToGame` functions:

- For [Drawphone](https://github.com/tannerkrewson/rocketcrab/blob/a3f796af7f6b70100b1dcf9ab141d73fea41e049/config/games/drawphone.ts#L18-L25), a new game can be generated with a `POST` request to the `/new`, which will return a game code. In it's `getJoinGameUrl` function, the `/new` endpoint is called, and the resulting game code is returned in the `code` property, which will add the code as a query param called `code` to the `playerURL` that is opened in each player's `iframe`.
- For [Spyfall](https://github.com/tannerkrewson/rocketcrab/blob/a3f796af7f6b70100b1dcf9ab141d73fea41e049/config/games/spyfall.ts#L18-L24), a new game can be generated in the exact same way as Drawphone. But, game links are required to take the format `spyfall.tannerkrewson.com/abcd`, and not `drawphone.tannerkrewson.com/?code=abcd` like the above example. So, instead of returning the game code from `getJoinGameUrl` in the `code` property, the game code is directly appended to the `playerURL` before it is returned from `getJoinGameUrl`.
- For [Just One](https://github.com/tannerkrewson/rocketcrab/blob/a3f796af7f6b70100b1dcf9ab141d73fea41e049/config/games/justone.ts#L14-L19), we can pick our own game code! So, instead of calling to some endpoint to get a game code like Drawphone and Spyfall, the `getJoinGameUrl` function can itself generate a code, and we cross our fingers and hope it's unique! 😂 The resulting `playerURL` will look something like this: `https://just1.herokuapp.com/room/rocketcrab-d5b30ccdd25855e5`.
- For [Drawphone](https://github.com/tannerkrewson/rocketcrab/blob/a3f796af7f6b70100b1dcf9ab141d73fea41e049/config/games/drawphone.ts#L18-L25), a new game can be generated with a `POST` request to the `/new`, which will return a game code. In it's `connectToGame` function, the `/new` endpoint is called, and the resulting game code is returned in the `code` property, which will add the code as a query param called `code` to the `playerURL` that is opened in each player's `iframe`.
- For [Spyfall](https://github.com/tannerkrewson/rocketcrab/blob/a3f796af7f6b70100b1dcf9ab141d73fea41e049/config/games/spyfall.ts#L18-L24), a new game can be generated in the exact same way as Drawphone. But, game links are required to take the format `spyfall.tannerkrewson.com/abcd`, and not `drawphone.tannerkrewson.com/?code=abcd` like the above example. So, instead of returning the game code from `connectToGame` in the `code` property, the game code is directly appended to the `playerURL` before it is returned from `connectToGame`.
- For [Just One](https://github.com/tannerkrewson/rocketcrab/blob/a3f796af7f6b70100b1dcf9ab141d73fea41e049/config/games/justone.ts#L14-L19), we can pick our own game code! So, instead of calling to some endpoint to get a game code like Drawphone and Spyfall, the `connectToGame` function can itself generate a code, and we cross our fingers and hope it's unique! 😂 The resulting `playerURL` will look something like this: `https://just1.herokuapp.com/room/rocketcrab-d5b30ccdd25855e5`.

### The automatic query params

The `playerURL` returned from `getJoinGameUrl` is automatically appended with 3 query params. The resulting `playerURL` that is opened in every player's `iframe` will look something like this:
The `playerURL` returned from `connectToGame` is automatically appended with 3 query params. The resulting `playerURL` that is opened in every player's `iframe` will look something like this:

```
https://yourgame.com/?rocketcrab=true&name=Mary&ishost=true
Expand Down
2 changes: 1 addition & 1 deletion config/games/_protobowl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const game: ServerGame = {
category: ["trivia", "medium"],
players: "1+",
familyFriendly: true,
getJoinGameUrl: async () => {
connectToGame: async () => {
const id = randomBytes(8).toString("hex");
return {
playerURL: "https://protobowl.com/rocketcrab-" + id,
Expand Down
2 changes: 1 addition & 1 deletion config/games/_setwithfriends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const game: ServerGame = {

familyFriendly: true,

getJoinGameUrl: async () => {
connectToGame: async () => {
const roomName =
"rocketcrab-" + Math.random().toString(36).substring(8);

Expand Down
2 changes: 1 addition & 1 deletion config/games/_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const game: ServerGame = {
// see https://github.com/tannerkrewson/rocketcrab#step-2-creating-a-config-file
// note that this will be running on the rocketcrab server, and will run once per party, not per player.
// this function will be ran in a try catch, so no need to worry about crashing the server! 😆
getJoinGameUrl: async () => {
connectToGame: async () => {
// this is an example of one way it could be done. you could put any code here to do what you need to do!
// check other config files for more exmaples.

Expand Down
6 changes: 3 additions & 3 deletions config/games/alenros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ServerGame } from "../../types/types";
import { randomBytes } from "crypto";
import { newPromiseWebSocket } from "../../utils/utils";

const getJoinGameUrl = (wsUrl, baseUrl) => async () => {
const connectToGame = (wsUrl, baseUrl) => async () => {
const ws = newPromiseWebSocket(wsUrl);

// random 5-digit number
Expand Down Expand Up @@ -81,7 +81,7 @@ const game: Array<ServerGame> = [
"https://i.imgur.com/Zn9oupF.jpg",
"https://i.imgur.com/R0kbS1H.jpg",
],
getJoinGameUrl: getJoinGameUrl(
connectToGame: connectToGame(
"wss://fake-artist.herokuapp.com/sockjs/rocketcrab/rocketcrab/websocket",
"https://fake-artist.herokuapp.com/"
),
Expand Down Expand Up @@ -110,7 +110,7 @@ const game: Array<ServerGame> = [
"https://i.imgur.com/MasoMRt.jpg",
"https://i.imgur.com/3PBQodp.jpg",
],
getJoinGameUrl: getJoinGameUrl(
connectToGame: connectToGame(
"wss://insider-online.herokuapp.com/sockjs/rocketcrab/rocketcrab/websocket",
"https://insider-online.herokuapp.com/"
),
Expand Down
2 changes: 1 addition & 1 deletion config/games/codenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const game: ServerGame = {
"https://i.imgur.com/fFKfyEN.jpg",
],

getJoinGameUrl: async () => {
connectToGame: async () => {
const {
game: { name },
player: { credential },
Expand Down
2 changes: 1 addition & 1 deletion config/games/cosel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const game: ServerGame = {
"https://i.imgur.com/EEHgiYM.jpg",
],

getJoinGameUrl: async () => {
connectToGame: async () => {
const res = await fetch("https://warhol.cosel.io/api/games", {
method: "POST",
headers: { sessionId: Math.random().toString(36).substring(7) },
Expand Down
2 changes: 1 addition & 1 deletion config/games/drawphone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const game: ServerGame = {
familyFriendly: true,
minPlayers: 1,
maxPlayers: Infinity,
getJoinGameUrl: async () => {
connectToGame: async () => {
const newUrl = "https://drawphone.tannerkrewson.com/new";
const { gameCode } = await postJson(newUrl);
return {
Expand Down
2 changes: 1 addition & 1 deletion config/games/fishbowl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const game: ServerGame = {
category: ["easy"],
players: "4+",
familyFriendly: true,
getJoinGameUrl: async () => {
connectToGame: async () => {
const newGameUrl = "https://fishbowl-graphql.onrender.com/v1/graphql";
const {
data: {
Expand Down
2 changes: 1 addition & 1 deletion config/games/justone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const game: ServerGame = {
"https://i.imgur.com/hMeiX8k.jpg",
"https://i.imgur.com/plGWABz.jpg",
],
getJoinGameUrl: async () => {
connectToGame: async () => {
const id = randomBytes(8).toString("hex");
return {
playerURL: "https://just1.herokuapp.com/room/rocketcrab-" + id,
Expand Down
2 changes: 1 addition & 1 deletion config/games/longwave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const game: ServerGame = {
"https://i.imgur.com/cuGMgKv.jpg",
"https://i.imgur.com/u36UW03.jpg",
],
getJoinGameUrl: async () => {
connectToGame: async () => {
const id = randomBytes(8).toString("hex");
return {
playerURL: "https://longwave.web.app/rocketcrab-" + id,
Expand Down
2 changes: 1 addition & 1 deletion config/games/netgamesio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const gameTemplate = (
pictures,
...(guideUrl ? { guideUrl } : {}),
...(guideId ? { guideId } : {}),
getJoinGameUrl: async () => {
connectToGame: async () => {
const newGame = await fetch(
"https://netgames.io/games/" + urlId + "/new"
);
Expand Down
2 changes: 1 addition & 1 deletion config/games/qwiqwit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const game: ServerGame = {
"https://i.imgur.com/690VrZJ.jpg",
"https://i.imgur.com/X4BZKmA.jpg",
],
getJoinGameUrl: async () => {
connectToGame: async () => {
return {
playerURL:
"https://www.qwiqwit.com/autojoin/" +
Expand Down
2 changes: 1 addition & 1 deletion config/games/snakeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const game: ServerGame = {
"https://i.imgur.com/w568hdf.jpg",
"https://i.imgur.com/00tlDOB.jpg",
],
getJoinGameUrl: async () => {
connectToGame: async () => {
const newUrl = "https://snakeout.tannerkrewson.com/new";
const { gameCode } = await postJson(newUrl);
return {
Expand Down
2 changes: 1 addition & 1 deletion config/games/spyfall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const game: ServerGame = {
"https://i.imgur.com/gAYGUUC.jpg",
"https://i.imgur.com/8VMpYns.jpg",
],
getJoinGameUrl: async () => {
connectToGame: async () => {
const newUrl = "https://spyfall.tannerkrewson.com/new";
const { gameCode } = await postJson(newUrl);
return {
Expand Down
2 changes: 1 addition & 1 deletion config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const SERVER_GAME_LIST: Array<ServerGame> = fs
const getClientGameList = (): Array<ClientGame> =>
SERVER_GAME_LIST.map(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ getJoinGameUrl, ...clientGame }): ClientGame => clientGame
({ connectToGame, ...clientGame }): ClientGame => clientGame
);

export const getServerGameLibrary = (): ServerGameLibrary => ({
Expand Down
4 changes: 1 addition & 3 deletions pages/library.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ type LibraryProps = {
gameLibrary: ClientGameLibrary;
};

export const getServerSideProps: GetServerSideProps = async (): Promise<
any
> => {
export const getServerSideProps: GetServerSideProps = async (): Promise<any> => {
return {
props: {
gameLibrary: CLIENT_GAME_LIBRARY,
Expand Down
2 changes: 1 addition & 1 deletion server/rocketcrab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export const startGame = async (
});

try {
gameState.joinGameURL = await game.getJoinGameUrl();
gameState.joinGameURL = await game.connectToGame();
} catch (e) {
console.error(e);

Expand Down
6 changes: 3 additions & 3 deletions test/server/rocketcrab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ jest.mock("../../config", () => ({
{
id: "jd-foogame",
name: "FooGame",
getJoinGameUrl: async () => ({
connectToGame: async () => ({
playerURL: "foogame.com",
}),
} as ServerGame,
{ id: "lk-coolgame", name: "CoolGame" } as ServerGame,
({
id: "brokengame",
name: "BrokenGame",
getJoinGameUrl: async () => {
connectToGame: async () => {
throw Error;
},
} as unknown) as ServerGame,
Expand Down Expand Up @@ -433,7 +433,7 @@ describe("server/rocketcrab.ts", () => {
expect(mockParty.status).toBe(PartyStatus.ingame);
expect(mockParty.gameState.status).toBe(GameStatus.waitingforhost);

// TODO: test call to getJoinGameUrl
// TODO: test call to connectToGame
});

it("startGame fails if game doesn't exist", async () => {
Expand Down
2 changes: 1 addition & 1 deletion types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type BasedOn = {
};

export type ServerGame = ClientGame & {
getJoinGameUrl: () => Promise<JoinGameURL>;
connectToGame: () => Promise<JoinGameURL>;
};

export type JoinGameURL = {
Expand Down

0 comments on commit 178707b

Please sign in to comment.