|
| 1 | +import '@ui5/webcomponents/dist/Button' |
| 2 | +import '@ui5/webcomponents/dist/Option' |
| 3 | +import '@ui5/webcomponents/dist/Select' |
| 4 | +import { fromEvent } from 'rxjs' |
| 5 | +import { h } from './lib/hyper' |
| 6 | + |
| 7 | +export interface ButtonSetProp { |
| 8 | + location: Location |
| 9 | + seed: string |
| 10 | +} |
| 11 | + |
| 12 | +let removeSeedParameter = (url: string): string => { |
| 13 | + return url.replace(/\?seed=[^?]*/g, '') |
| 14 | +} |
| 15 | + |
| 16 | +let removeModeParameter = (url: string): string => { |
| 17 | + let regexList = [ |
| 18 | + /\?hide(=[^?]*)?/g, |
| 19 | + /\?hideDelay(=[^?]*)?/g, |
| 20 | + /\?justSmooze(=[^?]*)?/g, |
| 21 | + /\?smooze(=[^?]*)?/g, |
| 22 | + /\?smoozeDelay(=[^?]*)?/g, |
| 23 | + ] |
| 24 | + regexList.forEach((regex) => { |
| 25 | + url = url.replace(regex, '') |
| 26 | + }) |
| 27 | + return url |
| 28 | +} |
| 29 | + |
| 30 | +let urlWithSeed = (prop: ButtonSetProp) => { |
| 31 | + let urlWithoutSeed = removeSeedParameter(prop.location.href) |
| 32 | + let destination = `${urlWithoutSeed}?seed=${prop.seed}` |
| 33 | + return destination |
| 34 | +} |
| 35 | + |
| 36 | +let willMatch = (regex: RegExp) => (text: string) => !!text.match(regex) |
| 37 | + |
| 38 | +let testLabyrinthMode = (url: string) => { |
| 39 | + return removeModeParameter(url) === url |
| 40 | +} |
| 41 | + |
| 42 | +let BUTTON = 'ui5-button' as 'button' |
| 43 | +let SELECT = 'ui5-select' |
| 44 | +let OPTION = 'ui5-option' as 'option' |
| 45 | + |
| 46 | +/** |
| 47 | + * The GameModeDropdown allows to select which mode the game should run in |
| 48 | + * |
| 49 | + * This reloads the page. |
| 50 | + */ |
| 51 | +let GameModeDropdown = (prop: ButtonSetProp) => { |
| 52 | + let { location } = prop |
| 53 | + |
| 54 | + let gameModeList: [string, string, (url: string) => boolean][] = [ |
| 55 | + ['labyrinth', '', testLabyrinthMode], |
| 56 | + ['just smooze', '?justSmooze', willMatch(/\?justSmooze(\?|$)/)], |
| 57 | + ['hidden walls', '?hide', willMatch(/\?hide(\?|$)/)], |
| 58 | + ['smooze', '?smooze', willMatch(/\?smooze(\?|$)/)], |
| 59 | + ] |
| 60 | + |
| 61 | + let elem = h<HTMLSelectElement>( |
| 62 | + SELECT, |
| 63 | + {}, |
| 64 | + gameModeList.map(([name, parameter, test]) => { |
| 65 | + let optionElem = h(OPTION, { textContent: name }) |
| 66 | + if (test(location.href)) { |
| 67 | + optionElem.selected = true |
| 68 | + } |
| 69 | + optionElem.dataset.parameter = parameter |
| 70 | + return optionElem |
| 71 | + }), |
| 72 | + ) |
| 73 | + |
| 74 | + elem.addEventListener( |
| 75 | + 'change', |
| 76 | + (ev) => { |
| 77 | + console.log('ev', ev) |
| 78 | + let modeParameter = (ev as any).detail.selectedOption.dataset.parameter |
| 79 | + let urlWithSeedButNotMode = removeModeParameter(urlWithSeed(prop)) |
| 80 | + location.assign(urlWithSeedButNotMode + modeParameter) |
| 81 | + }, |
| 82 | + true, |
| 83 | + ) |
| 84 | + |
| 85 | + return { elem } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * The NewGameButton allows to start a new game. |
| 90 | + * |
| 91 | + * This reloads the page. |
| 92 | + */ |
| 93 | +let NewGameButton = (prop: ButtonSetProp) => { |
| 94 | + let { location } = prop |
| 95 | + |
| 96 | + let onclick = () => { |
| 97 | + let destination = removeSeedParameter(location.href) |
| 98 | + location.assign(destination) |
| 99 | + } |
| 100 | + |
| 101 | + return { elem: h(BUTTON, { onclick, textContent: 'New Game' }) } |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * The RestartSameButton allows to replay the same game. |
| 106 | + * |
| 107 | + * This reloads the page. |
| 108 | + */ |
| 109 | +let RestartSameButton = (prop: ButtonSetProp) => { |
| 110 | + let { location } = prop |
| 111 | + |
| 112 | + let onclick = () => { |
| 113 | + let destination = urlWithSeed(prop) |
| 114 | + location.assign(destination) |
| 115 | + } |
| 116 | + |
| 117 | + return { elem: h(BUTTON, { onclick, textContent: 'Restart Same' }) } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * This button is used to reveal the labyrinthe when it's hidden. |
| 122 | + */ |
| 123 | +let RevealLabyrintheButton = () => { |
| 124 | + let elem = h(BUTTON, { innerText: 'Reveal' }) |
| 125 | + |
| 126 | + return { |
| 127 | + elem, |
| 128 | + click$: fromEvent(elem, 'click'), |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export let ButtonSet = (prop: ButtonSetProp) => { |
| 133 | + let revealButtonVisibility = false |
| 134 | + |
| 135 | + let gameModeDropDown = GameModeDropdown(prop) |
| 136 | + let newGameButton = NewGameButton(prop) |
| 137 | + let restartGameButton = RestartSameButton(prop) |
| 138 | + let revealLabyrinthButton = RevealLabyrintheButton() |
| 139 | + |
| 140 | + let elem = h( |
| 141 | + 'span', |
| 142 | + { |
| 143 | + className: 'buttonSet', |
| 144 | + }, |
| 145 | + [ |
| 146 | + gameModeDropDown.elem, |
| 147 | + restartGameButton.elem, |
| 148 | + newGameButton.elem, |
| 149 | + revealLabyrinthButton.elem, |
| 150 | + ], |
| 151 | + ) |
| 152 | + |
| 153 | + return { |
| 154 | + elem, |
| 155 | + revealLabyrinth$: revealLabyrinthButton.click$, |
| 156 | + setRevealButtonVisibility: (visibility: boolean) => { |
| 157 | + if (visibility === revealButtonVisibility) { |
| 158 | + return false |
| 159 | + } |
| 160 | + if (visibility === true) { |
| 161 | + elem.append(revealLabyrinthButton.elem) |
| 162 | + } else { |
| 163 | + revealLabyrinthButton.elem.remove() |
| 164 | + } |
| 165 | + }, |
| 166 | + } |
| 167 | +} |
0 commit comments