Skip to content

Commit

Permalink
Merge pull request #1857 from jas14/1844-tournament-time-options
Browse files Browse the repository at this point in the history
Update tournament time options
  • Loading branch information
veloce authored Oct 16, 2021
2 parents 2f8fb4b + 7ff5744 commit 9b3293f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 26 deletions.
17 changes: 14 additions & 3 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncStorage from './asyncStorage'
import { getAtPath, setAtPath } from './utils/object'
import { ClockType } from './ui/shared/clock/interfaces'
import { increments, incrementTuples } from './utils/increments'

const STORAGE_KEY = 'settings'

Expand Down Expand Up @@ -352,9 +353,19 @@ export default {
['Casual', '0'],
['Rated', '1']
],
availableTimes: [['0', '0'], ['½', '0.5'], ['¾', '0.75'], ['1', '1'], ['1.5', '1.5'], ['2', '2'], ['3', '3'], ['4', '4'], ['5', '5'], ['6', '6'], ['7', '7']],
availableIncrements: ['0', '1', '2'],
availableDurations: ['20', '25', '30', '35', '40', '45', '50', '55', '60', '70', '80', '90', '100', '110', '120'],
availableTimes: [
['0', '0'], ['½', '0.5'], ['¾', '0.75'], ['1', '1'], ['1.5', '1.5'],
].concat(incrementTuples(2, 7, 1))
.concat(incrementTuples(10, 25, 5))
.concat(incrementTuples(30, 60, 10)),
availableIncrements: increments(0, 7, 1)
.concat(increments(10, 25, 5))
.concat(increments(30, 60, 10)),
availableDurations: increments(20, 55, 5)
.concat(increments(60, 110, 10))
.concat(increments(120, 360, 30))
.concat(increments(420, 540, 60))
.concat(['600', '720']),
availableTimesToStart: ['1', '2', '3', '5', '10', '15', '20', '30', '45', '60'],
variant: prop('tournament.variant', '1'),
mode: prop('tournament.mode', '0'),
Expand Down
5 changes: 3 additions & 2 deletions src/ui/challengeForm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import h from 'mithril/hyperscript'
import { parseFen, makeFen } from 'chessops/fen'
import * as utils from '../utils'
import tupleOf from '../utils/tupleOf'
import router from '../router'
import redraw from '../utils/redraw'
import i18n from '../i18n'
Expand Down Expand Up @@ -154,7 +155,7 @@ function renderForm() {
),
h('div.select_input.inline',
formWidgets.renderSelect('increment', formName + 'increment',
settings.gameSetup.availableIncrements.map(utils.tupleOf), settingsObj.increment, false)
settings.gameSetup.availableIncrements.map(tupleOf), settingsObj.increment, false)
)
)
}
Expand All @@ -163,7 +164,7 @@ function renderForm() {
timeFieldset.push(
h('div.select_input.large_label',
formWidgets.renderSelect('daysPerTurn', formName + 'days',
settings.gameSetup.availableDays.map(utils.tupleOf), settingsObj.days, false)
settings.gameSetup.availableDays.map(tupleOf), settingsObj.days, false)
))
}

Expand Down
5 changes: 3 additions & 2 deletions src/ui/newGameForm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import h from 'mithril/hyperscript'
import * as utils from '../utils'
import tupleOf from '../utils/tupleOf'
import i18n from '../i18n'
import router from '../router'
import { humanSetupFromSettings } from '../lichess/setup'
Expand Down Expand Up @@ -255,15 +256,15 @@ function renderCustomSetup(formName: string, settingsObj: HumanSettings, variant
),
h('div.select_input.inline',
formWidgets.renderSelect('increment', formName + 'increment',
settings.gameSetup.availableIncrements.map(utils.tupleOf), settingsObj.increment, false)
settings.gameSetup.availableIncrements.map(tupleOf), settingsObj.increment, false)
)
)
}

if (hasDays) {
timeFieldset.push(
h('div.select_input.large_label', formWidgets.renderSelect('daysPerTurn', formName + 'days',
settings.gameSetup.availableDays.map(utils.tupleOf), settingsObj.days!, false)
settings.gameSetup.availableDays.map(tupleOf), settingsObj.days, false)
))
}

Expand Down
5 changes: 3 additions & 2 deletions src/ui/playMachineForm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as utils from '../utils'
import tupleOf from '../utils/tupleOf'
import { parseFen, makeFen } from 'chessops/fen'
import router from '../router'
import * as xhr from '../xhr'
Expand Down Expand Up @@ -126,7 +127,7 @@ function renderForm(formName: string, settingsObj: AiSettings, variants: string[
h('div.select_input', [
formWidgets.renderSelect('level', 'ailevel', [
'1', '2', '3', '4', '5', '6', '7', '8'
].map(utils.tupleOf), settingsObj.level)
].map(tupleOf), settingsObj.level)
])
]

Expand All @@ -144,7 +145,7 @@ function renderForm(formName: string, settingsObj: AiSettings, variants: string[
]),
h('div.select_input.inline', [
formWidgets.renderSelect('increment', formName + 'increment',
settings.gameSetup.availableIncrements.map(utils.tupleOf), settingsObj.increment, false)
settings.gameSetup.availableIncrements.map(tupleOf), settingsObj.increment, false)
])
)
}
Expand Down
16 changes: 8 additions & 8 deletions src/ui/shared/clock/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ClockType, IChessClock, IStageClock, StageSetting } from './interfaces'
import settings, { Prop } from '../../../settings'
import redraw from '../../../utils/redraw'
import * as helper from '../../helper'
import * as utils from '../../../utils'
import tupleOf from '../../../utils/tupleOf'
import formWidgets from '../../shared/form'

export const MILLIS = 1000
Expand Down Expand Up @@ -61,7 +61,7 @@ export function clockSettingsView (clockType: ClockType | 'none', onChange: () =
{formWidgets.renderSelect('Time', 'time', settings.clock.availableTimes, settings.clock.increment.time, false, onChange)}
</div>
<div className="select_input">
{formWidgets.renderSelect('Increment', 'increment', settings.clock.availableIncrements.map(utils.tupleOf), settings.clock.increment.increment, false, onChange)}
{formWidgets.renderSelect('Increment', 'increment', settings.clock.availableIncrements.map(tupleOf), settings.clock.increment.increment, false, onChange)}
</div>
</div>
)
Expand All @@ -75,7 +75,7 @@ export function clockSettingsView (clockType: ClockType | 'none', onChange: () =
{formWidgets.renderSelect('Time', 'topTime', settings.clock.availableTimes, settings.clock.handicapInc.topTime, false, onChange)}
</div>
<div className="select_input inline handicapRowMember">
{formWidgets.renderSelect('Increment', 'topIncrement', settings.clock.availableIncrements.map(utils.tupleOf), settings.clock.handicapInc.topIncrement, false, onChange)}
{formWidgets.renderSelect('Increment', 'topIncrement', settings.clock.availableIncrements.map(tupleOf), settings.clock.handicapInc.topIncrement, false, onChange)}
</div>
</div>
<div className="handicapRow">
Expand All @@ -84,7 +84,7 @@ export function clockSettingsView (clockType: ClockType | 'none', onChange: () =
{formWidgets.renderSelect('Time', 'bottomTime', settings.clock.availableTimes, settings.clock.handicapInc.bottomTime, false, onChange)}
</div>
<div className="select_input inline handicapRowMember">
{formWidgets.renderSelect('Increment', 'bottomIncrement', settings.clock.availableIncrements.map(utils.tupleOf), settings.clock.handicapInc.bottomIncrement, false, onChange)}
{formWidgets.renderSelect('Increment', 'bottomIncrement', settings.clock.availableIncrements.map(tupleOf), settings.clock.handicapInc.bottomIncrement, false, onChange)}
</div>
</div>
</div>
Expand All @@ -97,7 +97,7 @@ export function clockSettingsView (clockType: ClockType | 'none', onChange: () =
{formWidgets.renderSelect('Time', 'time', settings.clock.availableTimes, settings.clock.delay.time, false, onChange)}
</div>
<div className="select_input">
{formWidgets.renderSelect('Increment', 'increment', settings.clock.availableIncrements.map(utils.tupleOf), settings.clock.delay.increment, false, onChange)}
{formWidgets.renderSelect('Increment', 'increment', settings.clock.availableIncrements.map(tupleOf), settings.clock.delay.increment, false, onChange)}
</div>
</div>
)
Expand All @@ -109,7 +109,7 @@ export function clockSettingsView (clockType: ClockType | 'none', onChange: () =
{formWidgets.renderSelect('Time', 'time', settings.clock.availableTimes, settings.clock.bronstein.time, false, onChange)}
</div>
<div className="select_input">
{formWidgets.renderSelect('Increment', 'increment', settings.clock.availableIncrements.map(utils.tupleOf), settings.clock.bronstein.increment, false, onChange)}
{formWidgets.renderSelect('Increment', 'increment', settings.clock.availableIncrements.map(tupleOf), settings.clock.bronstein.increment, false, onChange)}
</div>
</div>
)
Expand All @@ -128,7 +128,7 @@ export function clockSettingsView (clockType: ClockType | 'none', onChange: () =
<div className="clockSettingParameters">
{ settings.clock.stage.stages().map((_, index) => renderStage(onChange, index)) }
<div className="select_input">
{formWidgets.renderSelect('Increment', 'increment', settings.clock.availableIncrements.map(utils.tupleOf), settings.clock.stage.increment, false, onChange)}
{formWidgets.renderSelect('Increment', 'increment', settings.clock.availableIncrements.map(tupleOf), settings.clock.stage.increment, false, onChange)}
</div>
</div>
)
Expand All @@ -150,7 +150,7 @@ function renderStage(onChange: () => void, index: number) {
{formWidgets.renderSelect('Time', 'time', settings.clock.availableTimes, timeProp, false, onChange)}
</div>
<div className={'select_input inline stage stageRowMember' + ((index === settings.clock.stage.stages().length - 1 ) ? ' lastStage' : '')}>
{formWidgets.renderSelect('Moves', 'moves', settings.clock.availableMoves.map(utils.tupleOf), moves, false, onChange)}
{formWidgets.renderSelect('Moves', 'moves', settings.clock.availableMoves.map(tupleOf), moves, false, onChange)}
</div>
<div className={'stageRowMember addSubtractStage' + ((index === settings.clock.stage.stages().length - 1 ) ? ' lastStage' : '')}>
<span className={'fa fa-plus-square-o' + (hidePlus ? ' hiddenButton' : '')} oncreate={helper.ontap(() => addStage(settings.clock.stage.stages))}/> <span className={'fa fa-minus-square-o' + (hideMinus ? ' hiddenButton' : '')} oncreate={helper.ontap(() => removeStage(settings.clock.stage.stages))}/>
Expand Down
9 changes: 5 additions & 4 deletions src/ui/tournament/newTournamentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import settings from '../../settings'
import i18n from '../../i18n'
import router from '../../router'
import * as utils from '../../utils'
import tupleOf from '../../utils/tupleOf'
import { standardFen } from '../../lichess/variant'
import { TournamentCreateResponse } from '../../lichess/interfaces/tournament'
import popupWidget from '../shared/popup'
Expand Down Expand Up @@ -91,13 +92,13 @@ function renderForm(ctrl: TournamentsListCtrl) {
{formWidgets.renderSelect(i18n('time'), 'time', settings.tournament.availableTimes, settings.tournament.time, false)}
</div>
<div className="select_input inline no-margin">
{formWidgets.renderSelect(i18n('increment'), 'increment', settings.tournament.availableIncrements.map((x: string) => utils.tupleOf(Number(x))), settings.tournament.increment, false)}
{formWidgets.renderSelect(i18n('increment'), 'increment', settings.tournament.availableIncrements.map((x: string) => tupleOf(Number(x))), settings.tournament.increment, false)}
</div>
<div className="select_input inline">
{formWidgets.renderSelect(i18n('duration'), 'duration', settings.tournament.availableDurations.map((x: string) => utils.tupleOf(Number(x))), settings.tournament.duration, false)}
{formWidgets.renderSelect(i18n('duration'), 'duration', settings.tournament.availableDurations.map((x: string) => tupleOf(Number(x))), settings.tournament.duration, false)}
</div>
<div className="select_input inline no-margin">
{formWidgets.renderSelect('Time to Start', 'timeToStart', settings.tournament.availableTimesToStart.map((x: string) => utils.tupleOf(Number(x))), settings.tournament.timeToStart, false)}
{formWidgets.renderSelect('Time to Start', 'timeToStart', settings.tournament.availableTimesToStart.map((x: string) => tupleOf(Number(x))), settings.tournament.timeToStart, false)}
</div>
<div className="select_input">
{formWidgets.renderCheckbox(i18n('isPrivate'), 'private', settings.tournament.private)}
Expand Down Expand Up @@ -127,7 +128,7 @@ function renderForm(ctrl: TournamentsListCtrl) {
}

function create(form: HTMLFormElement) {
const elements: HTMLCollection = (form[0] as HTMLFieldSetElement).elements as HTMLCollection
const elements: HTMLCollection = (form[0] as HTMLFieldSetElement).elements
const name = (elements[0] as HTMLInputElement).value
const variant = (elements[1] as HTMLInputElement).value
const position = settings.tournament.variant() === '1' ? (elements[2] as HTMLInputElement).value : '---'
Expand Down
13 changes: 13 additions & 0 deletions src/utils/increments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import tupleOf from './tupleOf'

export function increments(min: number, max: number, step = 1): string[] {
const array = []
for (let i = min; i <= max; i += step) {
array.push(i.toString())
}
return array
}

export function incrementTuples(min: number, max: number, step = 1): [string, string][] {
return increments(min, max, step).map(tupleOf)
}
6 changes: 1 addition & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ export function secondsToMinutes(sec: number): number {
return sec === 0 ? sec : sec / 60
}

export function tupleOf(x: number | string): [string, string] {
return [x.toString(), x.toString()]
}

export function oppositeColor(color: Color): Color {
return color === 'white' ? 'black' : 'white'
}
Expand Down Expand Up @@ -307,7 +303,7 @@ function charToInt(char: string) {

export function base62ToNumber(id?: string): number | undefined {
// Server idSize is 5 at the time of writing, but we'll be lenient
if (id === undefined || id.length > 7 || id.length === 0 || id.match(/[^a-zA-Z0-9]/)) {
if (id === undefined || id.length > 7 || id.length === 0 || (/[^a-zA-Z0-9]/.exec(id))) {
return undefined
}

Expand Down
3 changes: 3 additions & 0 deletions src/utils/tupleOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function tupleOf(x: number | string): [string, string] {
return [x.toString(), x.toString()]
}

0 comments on commit 9b3293f

Please sign in to comment.