GithubHelp home page GithubHelp logo

bholtbholt / remote-resistance Goto Github PK

View Code? Open in Web Editor NEW
16.0 2.0 1.0 1.67 MB

Play Resistance with your friends anywhere – a real-time remote version of the game. Visit resistance.quest

Home Page: https://resistance.quest

CSS 0.04% HTML 0.48% TypeScript 68.21% JavaScript 1.31% Svelte 29.94% Procfile 0.01%
typescript svelte tailwindcss redis express websockets

remote-resistance's Introduction

Remote Resistance

Play Resistance with your friends anywhere – a real-time remote version of the game. Play with friends at resistance.quest!

Installation

Prerequisites

  • Download the latest version of Node and NPM.
  • Download the latest version of Redis.

Startup

  • Run npm run setup, which copies .env and runs npm install.
  • Run npm start and visit http://localhost:3000/

Tech

Structure and Content

  • ./actions: Public events emitted and listened for with socket.io.
  • ./components: App UI, written in Svelte.
  • ./entry: CSS + JavaScript entry files.
  • ./server: App server.
  • ./stores: Reactive Svelte stores used by the UI. Stores are similar to Models in design. Anything that should submit an public action should use a store.
  • ./tests: Jest tests. All tests live in a flat directory so import statements match elsewhere. Store tests are prefixed with __store__* by convention.
  • ./types: Global types for the app.

Deploying

Remote Resistance is served with Heroku. Use the following commands for deploying:

# Deploying the main branch
git push heroku main

# Deploying a non-main branch
git push heroku other-branch:main

# Reset to main branch (must do after deploying another branch)
git push -f heroku main:main

Additional Heroku Commands

# Tail logs
heroku logs --tail

# Run the Heroku app locally
npm run build
heroku local web

# Redeploy without changes (deploys twice and reverts empty commit)
git commit --allow-empty -m "Redeploy"; git push heroku main
git reset HEAD~; git push -f heroku main

History

The app hosts multiple rooms via unique URL and shares history with any visitor to that URL. History is built from public actions and emitted with websockets on app load once with history::init. It follows an Event-Driven Architecture pattern.

If the visitor loses their connection, arrives late, or refreshes the page, the history replays events to bring them to the current state.

Players are "logged in" via SessionStorage and can only join a game prior to it starting. If a player loses their connection they will re-join the game if their login key matches a player in the game.

Replay History in Development

In development, you can run npm run injectHistory to generate a state in the app on a given namespace. injectHistory adds events into a Redis key as if the events were running in a specific game. It requires HISTORY and NAME env variables.

  • HISTORY: the variable name of the export const for a given state. All states live in ./tests/history-states.ts.
  • NAME: the game URL, without the prepended slash. ie. pizza, not /pizza. This can be any string.

After you've injected history, visit the name of the game: localhost:3000/pizza.

# From the CLI
HISTORY=withPlayers NAME=pizza npm run injectHistory
HISTORY=roundOneStart NAME=pizza npm run injectHistory
HISTORY=roundOneTeamApproved NAME=pizza npm run injectHistory

# OR In your .env file
HISTORY=withPlayers
NAME=pizza

Rounds use the following pattern:

  • round{Number}Start, as in roundOneStart
  • round{Number}Team, as in roundOneTeam
  • round{Number}VotesApproved, as in roundOneVotesApproved
  • round{Number}VotesRejected, as in roundOneVotesRejected
  • round{Number}VotesPending, as in roundOneVotesPending
  • round{Number}TeamApproved, as in roundOneTeamApproved
  • round{Number}TeamRejected, as in roundOneTeamRejected
  • round{Number}NewVote, as in roundOneNewVote
  • round{Number}LastVote, as in roundOneLastVote
  • round{Number}MissionPassed, as in roundOneMissionPassed
  • round{Number}MissionFailed, as in roundOneMissionFailed

History states outside of rounds:

  • withPlayers before the game has started

Rounds alternate resistance then spy win conditions:

  • roundTwoStart = Round 1 resistance win
  • roundThreeStart = Round 2 spy win
  • roundFourStart = Round 3 resistance win
  • roundFiveStart = Round 4 spy win

AdminController

AdminController.svelte is a tool for controlling player state. Change the logged-in player or the leader, see the spies, and log-out. It's turned on for development.

Testing

Tests are written with Svelte Testing Library and Jest.

Run npm run test for the Jest watcher.

Test Helpers

  • AppFixture.svelte: For wrapping a given Svelte component for isolated testing. Takes the socket connection and component.
  • history-states.ts: Actions to rebuild history to any given state.
  • test-helper.ts: Helper functions.

Test setup

Most tests need the following boilerplate:

import { render } from '@testing-library/svelte';
import { get } from 'svelte/store';
import AppFixture from './AppFixture.svelte';
import Component from './Component.svelte';
import { currentPlayerId } from '../stores/player';
import { createHistoryEvent, historyState, players } from './history-states';
const socket = require('socket.io-client')('test');

test('should do a thing', () => {
  const [player] = players;
  currentPlayerId.set(player.id);

  const { getByRole } = render(AppFixture, {
    socket,
    component: Component,
    historyState: historyState,
  });

  const element = getByRole();
});

Using history-states is the easiest way to build up a true state in the application with little effort. Import the history events needed to land at any given state.

Troubleshooting

Tests are failing as a group, but pass individually
Jest runs tests with shared state, so you need to add afterEach(() => { …; return; }) to undo the state.
The app is running, but the loading state never ends
You probably have a typo in your ENV URLS, likely `VITE_CORS_ORIGIN_URL`. Make sure there are no trailing slashes at the end of the URL. The socket queries against `window.location.pathname`, which returns something like `/game-id`.

remote-resistance's People

Contributors

bholtbholt avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

remote-resistance's Issues

Do votes need confirmation?

I've used a confirmation model the entire game, but maybe it doesn't need that.

This could be a game rules option, where confirmation can be turned off

Abstract socket.emit events into its own file

This should make it easier to understand what events need to be called for a state to move forward.

WIP:

// pre game
emit('ruleset::generate', ruleset);
emit('rounds::init', ruleset);
emit('leader::init', $players);
emit('appstate::set', 'IN_GAME');

// team selection
emit('team::selection', this.value);
emit('team::confirmation', $team);
emit('phase::set', 'TEAM_VOTE');

// team vote
emit('teamvote::cast', { playerId: $currentPlayer.id, vote: playerVote });

// if votes fail
emit('rounds::update', [$currentRound.index, update]);
emit('leader::change');

emit('phase::set', 'TEAM_REVEAL');

// pick new team
emit('teamvote::reset');
emit('team::reset');
emit('phase::set', 'TEAM_SELECTION');

// start mission
emit('phase::set', 'MISSION_START');

emit('missionvote::cast', { playerId: $currentPlayer.id, vote: playerVote });

emit('rounds::update', [
  $currentRound.index,
  {
    winner: $missionPassed ? 'resistance' : 'spies',
    missionPhase: {
      team: $team,
      votes: $missionVotes,
      result: $missionPassed ? 'successful' : 'failed',
    },
  },
]);
emit('leader::change');
emit('phase::set', 'MISSION_REVEAL');

emit('missionvote::reset');
emit('team::reset');
emit('teamvote::reset');
emit('phase::set', 'TEAM_SELECTION');

// end game
emit('appstate::set', 'PRE_GAME');
emit('missionvote::reset');
emit('team::reset');
emit('teamvote::reset');
emit('rounds::increment');
emit('phase::set', 'TEAM_SELECTION');
emit('rounds::reset');
window.sessionStorage.removeItem('hideRoleReveal');

Block bots from scanning for WP content

/sitemap.xml
/robots.txt
/wp-login.php
"//wp-includes/wlwmanifest.xml"
"//xmlrpc.php?rsd"
"//blog/wp-includes/wlwmanifest.xml"
2021-11-26T23:32:56.568804+00:00 heroku[router]: at=info method=GET path="//blog/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=7fbcdf03-f7dc-4f7a-87f9-56695d86385b fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=657 protocol=https
2021-11-26T23:32:56.795829+00:00 heroku[router]: at=info method=GET path="//web/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=2a6e3140-95b5-4955-bf24-c1baee4c1e83 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=656 protocol=https
2021-11-26T23:32:57.022341+00:00 heroku[router]: at=info method=GET path="//wordpress/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=6a7463fc-db6b-4193-9e3e-1a3209d6f4f0 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=662 protocol=https
2021-11-26T23:32:57.248991+00:00 heroku[router]: at=info method=GET path="//website/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=747cf773-8b35-4eda-9a3d-e1b8c8f03bb1 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=660 protocol=https
2021-11-26T23:32:57.475702+00:00 heroku[router]: at=info method=GET path="//wp/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=696fe40a-5986-439a-b2b1-de9ab3e4b297 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=655 protocol=https
2021-11-26T23:32:57.703713+00:00 heroku[router]: at=info method=GET path="//news/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=4f3c94a5-9c43-4b98-8060-1b9c63a0f0f7 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=657 protocol=https
2021-11-26T23:32:57.930348+00:00 heroku[router]: at=info method=GET path="//2020/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=32718dcc-81d7-44b1-a319-beb7bd4963a6 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=657 protocol=https
2021-11-26T23:32:58.157349+00:00 heroku[router]: at=info method=GET path="//2019/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=a9d7afdb-e8cd-4851-ae55-07fd1a940b21 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=657 protocol=https
2021-11-26T23:32:58.383993+00:00 heroku[router]: at=info method=GET path="//shop/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=e4cd433a-b474-48d3-992a-f5695c72b71d fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=657 protocol=https
2021-11-26T23:32:58.622661+00:00 heroku[router]: at=info method=GET path="//wp1/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=521a2127-e3f2-4d34-bb9f-438981b7c117 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=656 protocol=https
2021-11-26T23:32:58.849395+00:00 heroku[router]: at=info method=GET path="//test/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=f3e0f667-4868-4a6a-ba98-d397809fe6f4 fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=657 protocol=https
2021-11-26T23:32:59.076849+00:00 heroku[router]: at=info method=GET path="//wp2/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=be2827db-adbb-4fba-b251-7fa67b48c7bc fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=656 protocol=https
2021-11-26T23:32:59.303819+00:00 heroku[router]: at=info method=GET path="//site/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=3a5f6523-f7a0-493d-8556-277300b5781a fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=657 protocol=https
2021-11-26T23:32:59.531486+00:00 heroku[router]: at=info method=GET path="//cms/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=b85dc4e3-c4b9-489e-9f60-ffeb8864fe0d fwd="165.22.101.166" dyno=web.1 connect=0ms service=1ms status=404 bytes=656 protocol=https
2021-11-26T23:32:59.768107+00:00 heroku[router]: at=info method=GET path="//sito/wp-includes/wlwmanifest.xml" host=resistance.quest request_id=3a74568e-a5ad-46b3-b079-626ffe214d67 fwd="165.22.101.166" dyno=web.1 connect=0ms service=11ms status=404 bytes=657 protocol=https

PlayerText component

Should be something like:

<span class="whitespace-nowrap">😍 Name</span>

Used in the titles, but current the names break across lines

Change mockHistory with ENV variable

                                                   .
                                                 .o8
oooo d8b  .ooooo.  ooo. .oo.  .oo.    .ooooo.  .o888oo  .ooooo.
`888""8P d88' `88b `888P"Y88bP"Y88b  d88' `88b   888   d88' `88b
 888     888ooo888  888   888   888  888   888   888   888ooo888
 888     888    .o  888   888   888  888   888   888 . 888    .o
d888b    `Y8bod8P' o888o o888o o888o `Y8bod8P'   "888" `Y8bod8P'

                             o8o               .
                             `"'             .o8
oooo d8b  .ooooo.   .oooo.o oooo   .oooo.o .o888oo  .oooo.   ooo. .oo.    .ooooo.   .ooooo.
`888""8P d88' `88b d88(  "8 `888  d88(  "8   888   `P  )88b  `888P"Y88b  d88' `"Y8 d88' `88b
 888     888ooo888 `"Y88b.   888  `"Y88b.    888    .oP"888   888   888  888       888ooo888
 888     888    .o o.  )88b  888  o.  )88b   888 . d8(  888   888   888  888   .o8 888    .o
d888b    `Y8bod8P' 8""888P' o888o 8""888P'   "888" `Y888""8o o888o o888o `Y8bod8P' `Y8bod8P'

Paused or dropped connections

How to handle when the connection is dropped? The events don't continue to play.

Ideas:

  • maybe remove "once" from history::init
  • Maybe sockets.io has a reconnection method
  • maybe a timeout, if an event hasn't happened for a while it retriggers

Javascript error:

https://remote-resistance.herokuapp.com/socket.io/?EIO=4&transport=polling&t=NqbOcEb
net::ERR_INTERNET_DISCONNECTED

Text Pass

  • join next game? How can I make the reset trigger? Clear players, while also retaining them
  • capitalization
  • punctuation

Lighten light mode

Try:

    bg-indigo-400
    dark:bg-gray-800
    bg-fixed bg-gradient-to-b
    from-indigo-400
    via-rose-200
    to-orange-100
    dark:from-gray-800 dark:to-purple-900

Morning of a morning sunrise instead of sunset. Lots of text colours need to change, but it's much, much lighter and different from dark mode

Rejoin game

If the player loses their connection (specifically session storage key), allow them to rejoin the game (login as player).

  • rejoin game button
  • select player to become
  • prompts other players to approve
  • on success, logs in player

consider:

  • replaying events
  • slow connections
  • hijacking other players (is it a feature?)

Fix avatar selection

Avatar disabling on the player form only fires onMount because of how the form is set up. Look at voting forms and team selection form to switch it to Svelte style (holding the variable in memory) and then avatars will probably disable properly

Also, randomize the avatar so it's not always the first

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.