Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/31 game settings section #41

Merged
merged 10 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions client/src/components/Radio/Radio.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@import 'styles/variables';
@import 'styles/helpers';

.Radio {
display: block;
position: relative;

&__Label {
color: $color-black;
padding-left: $spacing-large;
}

&__Input + &__Label::before {
border: $border-regular solid $color-black;
border-radius: 50%;
content: '';
display: block;
height: $spacing-regular;
left: 0;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: $spacing-regular;
}

&__Input:checked + &__Label::before {
background: $color-black;
}

&__Input:focus + &__Label::before {
@include focusStyles();
}
}
38 changes: 38 additions & 0 deletions client/src/components/Radio/Radio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { FC, InputHTMLAttributes, ChangeEvent } from 'react'
import classnames from 'classnames'

import './Radio.scss'

export interface RadioProps extends InputHTMLAttributes<HTMLInputElement> {
label: string
checked: boolean
value: string
bellangerq marked this conversation as resolved.
Show resolved Hide resolved
onChange: (event: ChangeEvent<HTMLInputElement>) => void
className?: string
}

const Radio: FC<RadioProps> = ({
label,
checked,
value,
onChange,
className,
...other
}) => {
const classes = classnames(className, 'Radio')
return (
<label className={classes}>
<input
type='radio'
{...other}
checked={checked}
className='Radio__Input sr-only'
value={value}
onChange={onChange}
/>
<span className='Radio__Label'>{label}</span>
</label>
)
}

export default Radio
2 changes: 2 additions & 0 deletions client/src/components/Radio/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Radio'
export { default } from './Radio'
32 changes: 32 additions & 0 deletions client/src/containers/Settings/Settings.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@import 'styles/variables';

.Settings {
h2 {
margin-bottom: $spacing-regular;
}

legend {
margin-bottom: $spacing-small;
}

&__Field {
& + & {
display: flex;
flex-wrap: wrap;
margin-top: $spacing-large;

> label {
flex-basis: 100%;
margin-bottom: $spacing-small;
}
}

label + label {
margin-top: $spacing-small;
}

[type='number'] {
margin-right: $spacing-small;
}
}
}
94 changes: 94 additions & 0 deletions client/src/containers/Settings/Settings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import React, { Component, ChangeEvent } from 'react'

import Radio from '../../components/Radio'
import { Settings as SettingsType } from '../../types/Settings'
import { Difficulty } from '../../types/Difficulty'
import './Settings.scss'

const difficulties = [
{ value: Difficulty.EASY, label: 'Facile' },
{ value: Difficulty.MEDIUM, label: 'Intermédiaire' },
{ value: Difficulty.HARD, label: 'Difficile' }
]

export interface SettingsProps {
settings: SettingsType
className?: string
}

export interface SettingsState {
difficulty: string
roundsToWin: string
cardSpeed: string
}

class Settings extends Component<SettingsProps, SettingsState> {
state: SettingsState = {
difficulty: this.props.settings.difficulty,
roundsToWin: this.props.settings.roundsToWin.toString(),
cardSpeed: this.props.settings.cardSpeed.toString()
}

handleDifficultyChange = (e: ChangeEvent<HTMLInputElement>) => {
this.setState({
difficulty: e.target.value
})
}

handleRoundsToWinChange = (e: ChangeEvent<HTMLInputElement>) => {
this.setState({
roundsToWin: e.target.value
})
}

handleCardSpeedChange = (e: ChangeEvent<HTMLInputElement>) => {
this.setState({
cardSpeed: e.target.value
})
}

render() {
return (
<div className='Settings'>
<h2>Paramètres de la partie</h2>
<fieldset className='Settings__Field'>
<legend>Niveau de difficulté</legend>
{difficulties.map(({ value, label }) => (
<Radio
key={value}
label={label}
value={value}
checked={value === this.state.difficulty}
onChange={this.handleDifficultyChange}
name='difficulty'
/>
))}
</fieldset>

<div className='Settings__Field'>
<label htmlFor='roundsToWin'>Durée de la partie :</label>
<input
type='number'
id='roundsToWin'
value={this.state.roundsToWin}
onChange={this.handleRoundsToWinChange}
/>
<span>manches de jeu.</span>
</div>

<div className='Settings__Field'>
<label htmlFor='cardSpeed'>Vitesse de la partie</label>
<input
type='number'
id='cardSpeed'
value={this.state.cardSpeed}
onChange={this.handleCardSpeedChange}
/>
<span>secondes entre chaque carte.</span>
</div>
</div>
)
}
}

export default Settings
2 changes: 2 additions & 0 deletions client/src/containers/Settings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Settings'
export { default } from './Settings'
15 changes: 12 additions & 3 deletions client/src/pages/Lobby/Lobby.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { FC, useEffect, useState } from 'react'
import axios, { AxiosResponse } from 'axios'
import moment from 'moment'
import { RouteComponentProps } from '@reach/router'
import { Game } from '../../types/Game'
import * as moment from 'moment'

import connectToGameHub from '../../utils/signalrConnector'

import { Game } from '../../types/Game'
import { Settings as SettingsType } from '../../types/Settings'
import { Difficulty } from '../../types/Difficulty'
import Settings from '../../containers/Settings'
import './Lobby.scss'
import PlayerBox from '../../components/PlayerBox'

Expand Down Expand Up @@ -48,10 +50,17 @@ const Lobby: FC<LobbyProps> = (props: LobbyProps) => {
return game.players && game.players[0].id === playerId
}

const defaultSettings: SettingsType = {
difficulty: Difficulty.EASY,
roundsToWin: 10,
cardSpeed: 1.5
}

return (
<div className='Lobby'>
{game && (
<>
<Settings settings={defaultSettings} />
<h1>Details de la partie ({game.id})</h1>
<h2>{`Créé ${moment.utc(game.creationDate).fromNow()}`}</h2>
<div className='Players'>
Expand Down
19 changes: 18 additions & 1 deletion client/src/styles/_helpers.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
// Custom focus styles
*:focus {
@mixin focusStyles {
outline: 0;
box-shadow: 0 0 0 0.2rem rgba($color-primary, 0.75);
transition: box-shadow 0.25s;
}

// Apply focus styles
*:focus {
@include focusStyles();
}

// Visually hide element
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}
7 changes: 7 additions & 0 deletions client/src/styles/_reset.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ textarea,
select {
font: inherit;
}

/* Remove ugly fieldset styles */
fieldset {
border: 0;
margin: 0;
padding: 0;
}
12 changes: 12 additions & 0 deletions client/src/styles/_typography.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ h1 {
font-size: $font-size-larger;
font-weight: $font-weight-bold;
}

h2 {
font-size: $font-size-large;
font-weight: $font-weight-medium;
}

label,
legend {
color: $color-black-70;
font-size: $font-size-regular;
font-weight: $font-weight-medium;
}
2 changes: 2 additions & 0 deletions client/src/styles/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $spacing-larger: $base-spacing * 8;
$color-primary: #cfcfcf;
$color-white: #ffffff;
$color-black: #050505;
$color-black-70: rgba($color-black, 0.7);

$radius-regular: 0.3125rem;

Expand All @@ -22,4 +23,5 @@ $font-size-large: 1.75rem;
$font-size-larger: 2.25rem;

$font-weight-bold: 700;
$font-weight-medium: 500;
$font-weight-regular: 400;
5 changes: 5 additions & 0 deletions client/src/types/Difficulty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum Difficulty {
EASY = 'Easy',
MEDIUM = 'Medium',
HARD = 'Hard'
}
7 changes: 7 additions & 0 deletions client/src/types/Settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Difficulty } from './Difficulty'

export interface Settings {
difficulty: Difficulty
roundsToWin: number
cardSpeed: number
}