Skip to content

Commit

Permalink
Support combo
Browse files Browse the repository at this point in the history
Close #282
  • Loading branch information
mantou132 committed Oct 14, 2023
1 parent e7ec5bd commit ae3a6b5
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 19 deletions.
3 changes: 3 additions & 0 deletions packages/webapp/src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const defaultUISettings = {
};

export type Cheat = { code: string; enabled: boolean; toggleKey: string; comment: string };
export type Combo = { code: string; enabled: boolean; binding: string; comment: string };

export type Settings = {
keybinding: typeof defaultKeybinding;
Expand All @@ -117,6 +118,7 @@ export type Settings = {
video: typeof defaultVideoSettings;
ui: typeof defaultUISettings;
cheat: Record<number, Cheat[] | undefined>;
combo: Record<number, Combo[] | undefined>;
tourIndex: number;
};

Expand All @@ -140,6 +142,7 @@ export const parseAccount = (account: GetAccountQuery['account']): User => {
video: mergeSettings(defaultVideoSettings, settings.video),
ui: mergeSettings(defaultUISettings, settings.ui),
cheat: settings.cheat || {},
combo: settings.combo || {},
tourIndex: settings.tourIndex || 0,
},
};
Expand Down
3 changes: 3 additions & 0 deletions packages/webapp/src/locales/en/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@
"settings.cheat.enable": "Enable",
"settings.cheat.key": "Toggle Key",
"settings.cheat.title": "Cheat Settings ($1)",
"settings.combo.code": "Combo Code",
"settings.combo.title": "Combo ($1)",
"settings.keybinding.title": "Joypad",
"settings.shortcut.global": "Global",
"settings.shortcut.inGame": "In Game",
"settings.shortcut.openCheat": "Cheat Settings",
"settings.shortcut.openCombo": "Combo",
"settings.shortcut.openHelp": "Help",
"settings.shortcut.openRam": "RAM Viewer",
"settings.shortcut.readMsg": "Read Message",
Expand Down
3 changes: 3 additions & 0 deletions packages/webapp/src/locales/ja/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@
"settings.cheat.enable": "開いてください",
"settings.cheat.key": "キーバインディング",
"settings.cheat.title": "Cheat Settings ($1)",
"settings.combo.code": "コンボコード",
"settings.combo.title": "コンボ($1)",
"settings.keybinding.title": "ジョイパッド",
"settings.shortcut.global": "グローバル",
"settings.shortcut.inGame": "ゲームで",
"settings.shortcut.openCheat": "チート設定",
"settings.shortcut.openCombo": "コンボ",
"settings.shortcut.openHelp": "ヘルプ",
"settings.shortcut.openRam": "RAM ビューア",
"settings.shortcut.readMsg": "読む情報",
Expand Down
3 changes: 3 additions & 0 deletions packages/webapp/src/locales/zh-CN/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@
"settings.cheat.enable": "启用",
"settings.cheat.key": "键绑定",
"settings.cheat.title": "金手指($1)",
"settings.combo.code": "连招代码",
"settings.combo.title": "连招($1)",
"settings.keybinding.title": "手柄",
"settings.shortcut.global": "全局",
"settings.shortcut.inGame": "游戏内",
"settings.shortcut.openCheat": "金手指",
"settings.shortcut.openCombo": "连招",
"settings.shortcut.openHelp": "帮助",
"settings.shortcut.openRam": "RAM 查看器",
"settings.shortcut.readMsg": "阅读消息",
Expand Down
3 changes: 3 additions & 0 deletions packages/webapp/src/locales/zh-TW/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@
"settings.cheat.enable": "啟用",
"settings.cheat.key": "鍵綁定",
"settings.cheat.title": "金手指($1)",
"settings.combo.code": "連招代碼",
"settings.combo.title": "連招($1)",
"settings.keybinding.title": "控制器",
"settings.shortcut.global": "全局",
"settings.shortcut.inGame": "遊戲內",
"settings.shortcut.openCheat": "金手指",
"settings.shortcut.openCombo": "連招",
"settings.shortcut.openHelp": "幫助",
"settings.shortcut.openRam": "RAM 查看器",
"settings.shortcut.readMsg": "閱讀消息",
Expand Down
223 changes: 223 additions & 0 deletions packages/webapp/src/modules/combo-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import {
GemElement,
html,
adoptedStyle,
customElement,
createCSSSheet,
css,
connectStore,
numattribute,
} from '@mantou/gem';
import { Toast } from 'duoyun-ui/elements/toast';

import { Combo, configure } from 'src/configure';
import { icons } from 'src/icons';
import { updateAccount } from 'src/services/api';
import { i18n } from 'src/i18n/basic';
import { theme } from 'src/theme';

import type { Columns } from 'duoyun-ui/elements/table';

import 'duoyun-ui/elements/table';
import 'duoyun-ui/elements/input';
import 'duoyun-ui/elements/space';
import 'duoyun-ui/elements/switch';
import 'duoyun-ui/elements/shortcut-record';
import 'duoyun-ui/elements/button';

const style = createCSSSheet(css`
:host {
display: block;
}
.list {
width: 40em;
min-height: 20em;
margin-block-end: 1em;
}
.list::part(side) {
height: 10em;
}
.list::part(icon) {
width: 1.5em;
opacity: 0.8;
}
.list::part(close) {
color: ${theme.negativeColor};
}
.list::part(icon):hover {
opacity: 1;
}
`);

type State = {
newCombo?: Combo;
};

/**
* @customElement m-combo-settings
*/
@customElement('m-combo-settings')
@adoptedStyle(style)
@connectStore(configure)
@connectStore(i18n.store)
export class MComboSettingsElement extends GemElement<State> {
@numattribute gameId: number;

state: State = {};

get #data() {
return configure.user?.settings.combo[this.gameId] || [];
}

get #comboSettings() {
return configure.user?.settings.combo[this.gameId] || [];
}

#onChangeNewCombo = (detail: Partial<Combo>) => {
this.setState({ newCombo: Object.assign(this.state.newCombo!, detail) });
};

#onChangeSettings = (data: Combo[]) => {
return updateAccount({
settings: {
...configure.user!.settings,
combo: {
...configure.user?.settings.combo,
[this.gameId]: data,
},
},
});
};

#addNewCheat = () => {
this.setState({ newCombo: { code: '', comment: '', enabled: true, binding: '' } });
};

#addData = async (data: Combo) => {
if (this.#comboSettings.find((e) => e.code === data.code)) {
Toast.open('error', i18n.get('tip.cheat.exist'));
} else {
await this.#onChangeSettings([...this.#comboSettings, data]);
this.setState({ newCombo: undefined });
}
};

#removeData = (data: Combo) => {
if (data === this.state.newCombo) {
this.setState({ newCombo: undefined });
} else {
this.#onChangeSettings(this.#comboSettings.filter((e) => e !== data));
}
};

#changeToggleKey = (data: Combo, detail: string[]) => {
const key = detail.length > 1 || detail[0].length > 1 ? undefined : detail[0];
if (data === this.state.newCombo) {
this.#onChangeNewCombo({ binding: key });
} else {
this.#onChangeSettings(
this.#comboSettings.map((e) => (e === data ? Object.assign(data, { toggleKey: key }) : e)),
);
}
};

#toggle = (data: Combo) => {
if (data === this.state.newCombo) {
this.#onChangeNewCombo({ enabled: !data.enabled });
} else {
this.#onChangeSettings(
this.#comboSettings.map((e) => (e === data ? Object.assign(data, { enabled: !data.enabled }) : e)),
);
}
};

render = () => {
const data = this.#data;
const columns: Columns<Combo> = [
{
title: i18n.get('settings.combo.code'),
dataIndex: 'code',
render: (data) =>
data === this.state.newCombo
? html`
<dy-input
style="width: 100%"
@change=${({ detail }: CustomEvent<string>) => this.#onChangeNewCombo({ code: detail.toUpperCase() })}
.value=${data.code}
></dy-input>
`
: data.code,
},
{
title: i18n.get('settings.cheat.comment'),
dataIndex: 'comment',
render: (data) =>
data === this.state.newCombo
? html`
<dy-input
style="width: 100%"
@change=${({ detail }: CustomEvent<string>) => this.#onChangeNewCombo({ comment: detail })}
.value=${data.comment}
></dy-input>
`
: data.comment,
},
{
title: i18n.get('settings.cheat.key'),
dataIndex: 'binding',
width: '25%',
render: (data) =>
html`
<dy-shortcut-record
style="width: 100%"
@change=${({ detail }: CustomEvent<string[]>) => this.#changeToggleKey(data, detail)}
.value=${data.binding ? [data.binding] : undefined}
></dy-shortcut-record>
`,
},
{
title: i18n.get('settings.cheat.enable'),
dataIndex: 'enabled',
width: '100px',
render: (data) =>
html`
<dy-switch @change=${() => this.#toggle(data)} neutral="informative" .checked=${data.enabled}></dy-switch>
`,
style: {
textAlign: 'center',
},
},
{
title: '',
width: '80px',
style: {
textAlign: 'right',
},
render: (data) =>
html`
<dy-space>
<dy-use
part="icon"
?hidden=${data !== this.state.newCombo}
@click=${() => this.#addData(data)}
.element=${icons.check}
></dy-use>
<dy-use part="icon close" @click=${() => this.#removeData(data)} .element=${icons.close}></dy-use>
</dy-space>
`,
},
];

return html`
<dy-table
class="list"
.data=${this.state.newCombo ? [...data, this.state.newCombo] : data}
.columns=${columns}
.noData=${' '}
></dy-table>
<dy-button ?disabled=${!!this.state.newCombo} type="reverse" .icon=${icons.add} @click=${this.#addNewCheat}>
${i18n.get('settings.cheat.add')}
</dy-button>
`;
};
}
Loading

0 comments on commit ae3a6b5

Please sign in to comment.