noImplicitAny for ui/lobby

pull/9280/head
Benedikt Werner 2021-06-28 12:29:02 +02:00
parent 82d8761dce
commit 2c7acf5ced
No known key found for this signature in database
GPG Key ID: 1DBFF0F8E9E121EB
11 changed files with 54 additions and 36 deletions

View File

@ -20,7 +20,7 @@ export const toFormObject = (lines: FormLines): FormObject =>
const i = k.indexOf('[');
const fk = i > 0 ? k.slice(0, i) : k;
return i > 0 ? { ...o, [fk]: [...(o[fk] || []), lines[k]] } : { ...o, [fk]: lines[k] };
}, {});
}, {} as FormObject);
export const makeStore = (storage: LichessStorage): FormStore => ({
get: () => JSON.parse(storage.get() || 'null') as FormLines,

View File

@ -1,11 +1,11 @@
import LobbyController from './ctrl';
import { Hook } from './interfaces';
function ratingOrder(a, b) {
function ratingOrder(a: Hook, b: Hook) {
return (a.rating || 0) > (b.rating || 0) ? -1 : 1;
}
function timeOrder(a, b) {
function timeOrder(a: Hook, b: Hook) {
return a.t < b.t ? -1 : 1;
}
@ -13,16 +13,17 @@ export function sort(ctrl: LobbyController, hooks: Hook[]) {
hooks.sort(ctrl.sort === 'time' ? timeOrder : ratingOrder);
}
export function init(hook: Hook) {
export function init(hook: Hook): Hook {
hook.action = hook.sri === lichess.sri ? 'cancel' : 'join';
hook.variant = hook.variant || 'standard';
return hook;
}
export function initAll(ctrl: LobbyController) {
ctrl.data.hooks.forEach(init);
}
export function add(ctrl: LobbyController, hook) {
export function add(ctrl: LobbyController, hook: Hook) {
init(hook);
ctrl.data.hooks.push(hook);
}
@ -30,15 +31,15 @@ export function setAll(ctrl: LobbyController, hooks: Hook[]) {
ctrl.data.hooks = hooks;
initAll(ctrl);
}
export function remove(ctrl: LobbyController, id) {
export function remove(ctrl: LobbyController, id: string) {
ctrl.data.hooks = ctrl.data.hooks.filter(h => h.id !== id);
ctrl.stepHooks.forEach(h => {
if (h.id === id) h.disabled = true;
});
}
export function syncIds(ctrl: LobbyController, ids) {
export function syncIds(ctrl: LobbyController, ids: string[]) {
ctrl.data.hooks = ctrl.data.hooks.filter(h => ids.includes(h.id));
}
export function find(ctrl: LobbyController, id) {
export function find(ctrl: LobbyController, id: string) {
return ctrl.data.hooks.find(h => h.id === id);
}

View File

@ -11,7 +11,23 @@ interface Untyped {
[key: string]: any;
}
export interface Hook extends Untyped {}
export interface Hook {
id: string;
sri: string;
clock: string;
t: number; // time
s: number; // speed
i: number; // increment
variant: VariantKey;
perf: Exclude<Perf, 'fromPosition'>;
prov?: true; // is rating provisional
u?: string; // username
rating?: number;
ra?: 1; // rated
c?: Color;
action: 'cancel' | 'join';
disabled?: boolean;
}
export interface Seek {
id: string;
@ -21,7 +37,7 @@ export interface Seek {
days?: number;
color: string;
perf: {
key: string;
key: Exclude<Perf, 'fromPosition'>;
};
provisional?: boolean;
variant?: string;

View File

@ -1,13 +1,13 @@
function makeKey(poolId) {
function makeKey(poolId: string) {
return 'lobby-pool-range-' + poolId;
}
export function set(poolId, range) {
export function set(poolId: string, range?: string) {
const key = makeKey(poolId);
if (range) lichess.storage.set(key, range);
else lichess.storage.remove(key);
}
export function get(poolId) {
export function get(poolId: string) {
return lichess.storage.get(makeKey(poolId));
}

View File

@ -1,6 +1,7 @@
import LobbyController from './ctrl';
import { Seek } from './interfaces';
function order(a, b) {
function order(a: Seek, b: Seek) {
return a.rating > b.rating ? -1 : 1;
}

View File

@ -4,12 +4,14 @@ import debounce from 'common/debounce';
import * as xhr from 'common/xhr';
import LobbyController from './ctrl';
type Stores = {
hook: FormStore;
friend: FormStore;
ai: FormStore;
};
export default class Setup {
stores: {
hook: FormStore;
friend: FormStore;
ai: FormStore;
};
stores: Stores;
constructor(readonly makeStorage: (name: string) => LichessStorage, readonly root: LobbyController) {
this.stores = {
@ -19,7 +21,8 @@ export default class Setup {
};
}
private save = (form: HTMLFormElement) => this.stores[form.getAttribute('data-type')!].set(toFormLines(form));
private save = (form: HTMLFormElement) =>
this.stores[form.getAttribute('data-type') as keyof Stores].set(toFormLines(form));
private sliderTimes = [
0,
@ -174,7 +177,7 @@ export default class Setup {
},
save = () => this.save($form[0] as HTMLFormElement);
const c = this.stores[typ].get();
const c = this.stores[typ as keyof Stores].get();
if (c) {
Object.keys(c).forEach(k => {
$form.find(`[name="${k}"]`).each(function (this: HTMLInputElement) {

View File

@ -1,7 +1,7 @@
import * as xhr from './xhr';
import * as hookRepo from './hookRepo';
import LobbyController from './ctrl';
import { Hook, PoolMember } from './interfaces';
import { PoolMember, Hook } from './interfaces';
interface Handlers {
[key: string]: (data: any) => void;
@ -31,7 +31,7 @@ export default class LobbySocket {
ctrl.redraw();
},
hli(ids: string) {
hookRepo.syncIds(ctrl, ids.match(/.{8}/g));
hookRepo.syncIds(ctrl, ids.match(/.{8}/g) || []);
ctrl.redraw();
},
reload_seeks() {

View File

@ -13,12 +13,12 @@ const variantConfirms = {
'This is a Crazyhouse game!\n\nEvery time a piece is captured, the capturing player gets a piece of the same type and of their color in their pocket.',
};
const storageKey = key => 'lobby.variant.' + key;
const storageKey = (key: string) => 'lobby.variant.' + key;
export default function (variant?: string) {
return (
!variant ||
Object.keys(variantConfirms).every(function (key) {
Object.keys(variantConfirms).every(function (key: keyof typeof variantConfirms) {
if (variant === key && !lichess.storage.get(storageKey(key))) {
const c = confirm(variantConfirms[key]);
if (c) lichess.storage.set(storageKey(key), '1');

View File

@ -8,7 +8,7 @@ const percents = (v: number) => v + '%';
const ratingLog = (a: number) => Math.log(a / 150 + 1);
function ratingY(e: number) {
function ratingY(e?: number) {
const rating = Math.max(1000, Math.min(2200, e || 1500));
let ratio: number;
const mid = 2 / 5;
@ -42,7 +42,7 @@ function renderPlot(ctrl: LobbyController, hook: Hook) {
hook: {
insert(vnode) {
$(vnode.elm as HTMLElement).powerTip({
placement: hook.rating > 1800 ? 'se' : 'ne',
placement: hook.rating && hook.rating > 1800 ? 'se' : 'ne',
closeDelay: 200,
popupId: 'hook',
preRender() {

View File

@ -11,7 +11,7 @@ function renderHook(ctrl: LobbyController, hook: Hook) {
'tr.hook.' + hook.action,
{
key: hook.id,
class: { disabled: hook.disabled },
class: { disabled: !!hook.disabled },
attrs: {
title: hook.disabled
? ''
@ -45,17 +45,17 @@ function renderHook(ctrl: LobbyController, hook: Hook) {
);
}
function isStandard(value) {
return function (hook) {
function isStandard(value: boolean) {
return function (hook: Hook) {
return (hook.variant === 'standard') === value;
};
}
function isMine(hook) {
function isMine(hook: Hook) {
return hook.action === 'cancel';
}
function isNotMine(hook) {
function isNotMine(hook: Hook) {
return !isMine(hook);
}

View File

@ -1,6 +1,3 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"noImplicitAny": false
}
"extends": "../tsconfig.base.json"
}