From 96fa76f4fdeea47dd35cd9a72c797a34454eec77 Mon Sep 17 00:00:00 2001 From: Jonathan Marsh Date: Wed, 16 Dec 2020 00:47:35 +0000 Subject: [PATCH] Fix seed in 'show seed' Added CI --- .github/workflows/pre-deploy.yml | 26 ++++++++++++++++ .gitignore | 1 + CHANGELOG.md | 48 ++++++++++++++++++++++++++++++ README.md | 48 +++--------------------------- package-lock.json | 2 +- package.json | 2 +- src/components/Start/StartForm.tsx | 38 ++++------------------- src/util/seed.spec.ts | 23 ++++++++++++++ src/util/seed.ts | 26 ++++++++++++++++ 9 files changed, 135 insertions(+), 79 deletions(-) create mode 100644 .github/workflows/pre-deploy.yml create mode 100644 CHANGELOG.md create mode 100644 src/util/seed.spec.ts create mode 100644 src/util/seed.ts diff --git a/.github/workflows/pre-deploy.yml b/.github/workflows/pre-deploy.yml new file mode 100644 index 0000000..e89956b --- /dev/null +++ b/.github/workflows/pre-deploy.yml @@ -0,0 +1,26 @@ +name: Pre-deploy + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [10.x, 12.x, 14.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm ci + - run: npm run build --if-present + - run: npm test diff --git a/.gitignore b/.gitignore index a307265..2fee687 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /node_modules /.pnp .pnp.js +.eslintcache # testing /coverage diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..fd3d1d3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,48 @@ +# Changelog + +This project is following [Semantic Versioning](https://semver.org/) + +## [1.4.1](https://github.com/j-m/word-guess/releases/tag/1.4.1) - Fix 'show seed' (December 16, 2020) + +- Fix 'show seed' seed +- Added tests +- Added GitHub action + +## [1.4.0](https://github.com/j-m/word-guess/releases/tag/1.4.0) - Start Menu Info (November 16, 2020) + +- Show seed button +- Project version +- GitHub link + +## [1.3.0](https://github.com/j-m/word-guess/releases/tag/1.3.0) - Continue Playing (November 16, 2020) + +- Added continue playing button if game lost +- Added guess counter if playing in `overtime` +- Created Heroku deployment + +## [1.2.0](https://github.com/j-m/word-guess/releases/tag/1.2.0) - Large Seed Fix (November 16, 2020) + +- Swapped to `bigint` to allow for arbitrarily long seeds (any words/sentences longer than 10 characters) +- Better lose animation + +## [1.1.0](https://github.com/j-m/word-guess/releases/tag/1.1.0) - Spaces Fix (November 15, 2020) + +- Fixed seeds representing multi-word games +- Fixed lives +- Breaking change but only bumped minor because project was private + +## [1.0.0](https://github.com/j-m/word-guess/releases/tag/1.0.0) - Seeding (November 15, 2020) + +- Added seed calculation +- Allow seed input in start menu +- Detect if input is a numeric (seed), alpha + space (sentence) , or alphanumeric (invalid) +- Lives separated into its own component + +## 0.1.0 - Proof of Concept + +- Added start menu +- Added Sentence components +- Added Alphabet components +- Added lives +- Added celebration/losing animations +- Added replay diff --git a/README.md b/README.md index 8a71fd6..ad90200 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ -![word guess banner logo](./assets/banner.png) - -## About +# ![word guess banner logo](./assets/banner.png) Word Guess Hangman without the hanging man @@ -23,45 +21,7 @@ Then simply run `npm i` and `npm start` and you're off! - Leaderboards - Compete mode -## Changelog - -This project is following [Semantic Versioning](https://semver.org/) - -### 1.4.0 - Start Menu Info (November 16, 2020) - -- Show seed button -- Project version -- GitHub link - -### 1.3.0 - Continue Playing (November 16, 2020) - -- Added continue playing button if game lost -- Added guess counter if playing in `overtime` -- Created Heroku deployment - -### 1.2.0 - Large Seed Fix (November 16, 2020) - -- Swapped to `bigint` to allow for arbitrarily long seeds (any words/sentences longer than 10 characters) -- Better lose animation - -### 1.1.0 - Spaces Fix (November 15, 2020) - -- Fixed seeds representing multi-word games -- Fixed lives -- Breaking change but only bumped minor because project was private - -### 1.0.0 - Seeding (November 15, 2020) - -- Added seed calculation -- Allow seed input in start menu -- Detect if input is a numeric (seed), alpha + space (sentence) , or alphanumeric (invalid) -- Lives separated into its own component - -### 0.1.0 - Proof of Concept +## Wiki -- Added start menu -- Added Sentence components -- Added Alphabet components -- Added lives -- Added celebration/losing animations -- Added replay +[Changelog](./CHANGELOG.md) +[Live deployment](https://j-m-word-guess.herokuapp.com/) diff --git a/package-lock.json b/package-lock.json index 7a5450e..f126820 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "word-guess", - "version": "1.4.0", + "version": "1.4.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 252370e..7427dcc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "word-guess", - "version": "1.4.0", + "version": "1.4.1", "private": true, "main": "src/index.tsx", "homepage": "./", diff --git a/src/components/Start/StartForm.tsx b/src/components/Start/StartForm.tsx index 08e92d6..c79517d 100644 --- a/src/components/Start/StartForm.tsx +++ b/src/components/Start/StartForm.tsx @@ -4,33 +4,7 @@ import { FaGithub } from 'react-icons/fa' import SentenceInput from './SentenceInput' import StartGameButton from './StartGameButton' - -const BASE: bigint = 28n - -function seed(input: string): bigint { - let seed: bigint = 0n - Object.values(input.split("")).forEach(letter => { - if (letter !== " ") { - seed += BigInt(letter.charCodeAt(0)) - 65n + 1n - } - seed *= BASE - }) - return seed -} - -function sentence(seed: bigint): string { - let sentence: string = "" - while (seed > BASE) { - seed /= BASE - const letter: number = Number(seed % BASE) - if (letter === 0) { - sentence += ' ' - } else { - sentence += String.fromCharCode(letter + 65 - 1) - } - } - return sentence.split("").reverse().join("") -} +import { sentenceToSeed, seedToSentence } from '../../util/seed' interface StartFormProps { start: (seed: bigint, input: string) => void @@ -57,18 +31,18 @@ export default class StartForm extends React.PureComponent { - this.setState(state => ({...state, seed: seed(state.input)})) + this.setState(state => ({...state, seed: sentenceToSeed(state.input.toUpperCase())})) } onWordChange = (input: string) => { @@ -91,9 +65,7 @@ export default class StartForm extends React.PureComponent - {this.state.error - ?

{this.state.error}

- : undefined} + {this.state.error &&

{this.state.error}

}
v{version} diff --git a/src/util/seed.spec.ts b/src/util/seed.spec.ts new file mode 100644 index 0000000..12e53d0 --- /dev/null +++ b/src/util/seed.spec.ts @@ -0,0 +1,23 @@ +import {sentenceToSeed, seedToSentence} from './seed' +describe('Seed', () => { + test('with 1 word', () => { + const input = "TEST" + const seed = sentenceToSeed(input) + const output = seedToSentence(seed) + expect(output).toBe(input) + }) + + test('works with multiple words', () => { + const input = "THIS IS A TEST" + const seed = sentenceToSeed(input) + const output = seedToSentence(seed) + expect(output).toBe(input) + }) + + test('works with a long sentence', () => { + const input = "THIS IS A MUCH LONGER TEST THAN THE OTHERS BUT PROBABLY STILL IS NOT AS LONG AS A GOOD TEST SHOULD BE BUT HEY HO" + const seed = sentenceToSeed(input) + const output = seedToSentence(seed) + expect(output).toBe(input) + }) +}) diff --git a/src/util/seed.ts b/src/util/seed.ts new file mode 100644 index 0000000..4460416 --- /dev/null +++ b/src/util/seed.ts @@ -0,0 +1,26 @@ +const BASE: bigint = 27n + +export function sentenceToSeed(input: string): bigint { + let seed: bigint = 0n + Object.values(input.split("")).forEach(letter => { + if (letter !== " ") { + seed += BigInt(letter.charCodeAt(0)) - 65n + 1n + } + seed *= BASE + }) + return seed +} + +export function seedToSentence(seed: bigint): string { + let sentence: string = "" + while (seed > BASE) { + seed /= BASE + const letter: bigint = seed % BASE + if (letter === 0n) { + sentence += ' ' + } else { + sentence += String.fromCharCode(Number(letter + 65n - 1n)) + } + } + return sentence.split("").reverse().join("") +}