-
Notifications
You must be signed in to change notification settings - Fork 684
Expand file tree
/
Copy pathStreamBoss.tsx
More file actions
315 lines (295 loc) · 9.82 KB
/
StreamBoss.tsx
File metadata and controls
315 lines (295 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import React, { useState, useEffect } from 'react';
import { inject } from 'slap';
import { Menu, Button, message } from 'antd';
import { useWidget, WidgetModule } from './common/useWidget';
import { metadata, TInputMetadata } from 'components-react/shared/inputs/metadata';
import { $t } from 'services/i18n';
import { UserService } from 'app-services';
import { TPlatform } from 'services/platforms';
import { WidgetLayout } from './common/WidgetLayout';
import FormFactory, { TInputValue } from 'components-react/shared/inputs/FormFactory';
import Form from 'components-react/shared/inputs/Form';
import { authorizedHeaders, jfetch } from 'util/requests';
import { assertIsDefined } from 'util/properties-type-guards';
import styles from './GenericGoal.m.less';
interface IStreamBossState {
data: {
goal: {
boss_img: string;
boss_name: string;
current_health: number;
mode: 'fixed' | 'overkill' | 'increment';
multiplier: number;
percent: number;
total_health: number;
} | null;
settings: {
background_color: string;
bar_bg_color: string;
bar_color: string;
bar_text_color: string;
bg_transparent: boolean;
bit_multiplier: number;
boss_heal: boolean;
donation_multiplier: boolean;
fade_time: number;
follow_multiplier: boolean;
font: string;
incr_amount: string;
kill_animation: string;
overkill_min: number;
overkill_multiplier: number;
skin: string;
sub_multiplier: number;
superchat_multiplier: number;
text_color: string;
};
};
}
export function Streamboss() {
const {
setSelectedTab,
selectedTab,
isLoading,
settings,
goalSettings,
updateSetting,
resetGoal,
saveGoal,
visualMeta,
goalMeta,
battleMeta,
} = useStreamboss();
const hasGoal = !!goalSettings;
const [goalCreateValues, setGoalCreateValues] = useState<Dictionary<TInputValue>>({
total_health: 0,
mode: 'fixed',
overkill_multiplier: 0,
overkill_min: 0,
incr_amount: 0,
});
useEffect(() => {
message.config({ top: 270 });
}, []);
function updateGoalCreate(key: string) {
return (val: TInputValue) => {
setGoalCreateValues({ ...goalCreateValues, [key]: val });
};
}
return (
<WidgetLayout>
<Menu onClick={e => setSelectedTab(e.key)} selectedKeys={[selectedTab]}>
<Menu.Item key="goal">{$t('Goal')}</Menu.Item>
<Menu.Item key="battle">{$t('Manage Battle')}</Menu.Item>
<Menu.Item key="visual">{$t('Visual Settings')}</Menu.Item>
</Menu>
<Form>
{!isLoading && selectedTab === 'goal' && !hasGoal && (
<>
<FormFactory
metadata={goalMeta}
values={goalCreateValues}
onChange={updateGoalCreate}
/>
<Button
className="button button--action"
onClick={() => saveGoal(goalCreateValues)}
style={{ marginBottom: 16 }}
>
{$t('Set Stream Boss Health')}
</Button>
</>
)}
{!isLoading && selectedTab === 'goal' && hasGoal && (
<DisplayGoal goal={goalSettings} resetGoal={resetGoal} />
)}
{!isLoading && selectedTab === 'battle' && (
<FormFactory metadata={battleMeta} values={settings} onChange={updateSetting} />
)}
{!isLoading && selectedTab === 'visual' && (
<FormFactory metadata={visualMeta} values={settings} onChange={updateSetting} />
)}
</Form>
</WidgetLayout>
);
}
function DisplayGoal(p: { goal: IStreamBossState['data']['goal']; resetGoal: () => void }) {
if (!p.goal) return <></>;
return (
<div className="section__body">
<div className={styles.goalRow}>
<span>{$t('Current Boss Name')}</span>
<span>{p.goal.boss_name}</span>
</div>
<div className={styles.goalRow}>
<span>{$t('Total Health')}</span>
<span>{p.goal.total_health}</span>
</div>
<div className={styles.goalRow}>
<span>{$t('Current Health')}</span>
<span>{p.goal.current_health}</span>
</div>
<div className={styles.goalRow}>
<span>{$t('Mode')}</span>
<span>{p.goal.mode}</span>
</div>
<Button
className="button button--soft-warning"
onClick={p.resetGoal}
style={{ marginBottom: 16 }}
>
{$t('Reset Stream Boss')}
</Button>
</div>
);
}
export class StreambossModule extends WidgetModule<IStreamBossState> {
userService = inject(UserService);
get visualMeta() {
return {
skin: metadata.list({
label: $t('Theme'),
options: [
{ value: 'default', label: 'Default' },
{ value: 'future', label: 'Future' },
{ value: 'noimg', label: 'No Image' },
{ value: 'pill', label: 'Slim' },
{ value: 'future-curve', label: 'Curved' },
],
}),
kill_animation: metadata.animation({ label: $t('Kill Animation') }),
bg_transparent: metadata.bool({ label: $t('Transparent Background') }),
background_color: metadata.color({ label: $t('Background Color') }),
text_color: metadata.color({ label: $t('Text Color') }),
bar_text_color: metadata.color({ label: $t('Health Text Color') }),
bar_color: metadata.color({ label: $t('Health Bar Color') }),
bar_bg_color: metadata.color({ label: $t('Health Bar Background Color') }),
font: metadata.fontFamily({ label: $t('Font') }),
};
}
get battleMeta() {
return {
fade_time: metadata.slider({
label: $t('Fade Time (s)'),
min: 0,
max: 20,
tooltip: $t('Set to 0 to always appear on screen'),
}),
boss_heal: metadata.bool({ label: $t('Damage From Boss Heals') }),
...this.multipliersByPlatform(),
};
}
get goalMeta() {
return {
total_health: metadata.number({ label: $t('Starting Health'), required: true, min: 0 }),
mode: metadata.list({
label: $t('Mode'),
options: [
{
label: $t('Fixed'),
value: 'fixed',
description: $t('The boss will spawn with the set amount of health everytime.'),
},
{
label: $t('Incremental'),
value: 'incremental',
description: $t(
'The boss will have additional health each time he is defeated. The amount is set below.',
),
},
{
label: $t('Overkill'),
value: 'overkill',
description: $t(
"The boss' health will change depending on how much damage is dealt on the killing blow. Excess damage multiplied by the multiplier will be the boss' new health. I.e. 150 damage with 100 health remaining and a set multiplier of 3 would result in the new boss having 150 health on spawn. \n Set your multiplier below.",
),
},
],
children: {
overkill_multiplier: metadata.number({
label: $t('Overkill Multiplier'),
displayed: this.widgetData.goal?.mode === 'overkill',
}),
overkill_min: metadata.number({
label: $t('Overkill Min Health'),
displayed: this.widgetData.goal?.mode === 'overkill',
}),
incr_amount: metadata.number({
label: $t('Increment Amount'),
displayed: this.widgetData.goal?.mode === 'increment',
}),
},
}),
};
}
multipliersByPlatform(): Dictionary<TInputMetadata> {
const platform = this.userService.platform?.type as Exclude<
TPlatform,
'tiktok' | 'twitter' | 'instagram' | 'kick'
>;
const multipliers = {
twitch: {
bit_multiplier: metadata.number({ label: $t('Damage Per Bit') }),
sub_multiplier: metadata.number({ label: $t('Damage Per Subscriber') }),
follow_multiplier: metadata.number({ label: $t('Damage Per Follower') }),
},
facebook: {
follow_multiplier: metadata.number({ label: $t('Damage Per Follower') }),
sub_multiplier: metadata.number({ label: $t('Damage Per Subscriber') }),
},
youtube: {
sub_multiplier: metadata.number({ label: $t('Damage Per Membership') }),
superchat_multiplier: metadata.number({ label: $t('Damage Per Superchat Dollar') }),
follow_multiplier: metadata.number({ label: $t('Damage Per Subscriber') }),
},
trovo: {
sub_multiplier: metadata.number({ label: $t('Damage Per Subscriber') }),
follow_multiplier: metadata.number({ label: $t('Damage Per Follower') }),
},
};
return {
...multipliers[platform],
donation_multiplier: metadata.number({ label: $t('Damage Per Dollar Donation') }),
};
}
get goalSettings() {
return this.widgetData.goal;
}
get headers() {
return authorizedHeaders(
this.userService.apiToken,
new Headers({ 'Content-Type': 'application/json' }),
);
}
resetGoal() {
const url = this.config.goalUrl;
if (!url) return;
jfetch(new Request(url, { method: 'DELETE', headers: this.headers }));
this.setGoalData(null);
}
async saveGoal(options: Dictionary<TInputValue>) {
const url = this.config.goalUrl;
if (!url) return;
try {
const resp: IStreamBossState['data'] = await jfetch(
new Request(url, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(options),
}),
);
this.setGoalData(resp.goal);
} catch (e: unknown) {
message.error({ content: (e as any).result.message, duration: 2 });
}
}
private setGoalData(goal: IStreamBossState['data']['goal']) {
assertIsDefined(this.state.widgetData.data);
this.state.mutate(state => {
state.widgetData.data.goal = goal;
});
}
}
function useStreamboss() {
return useWidget<StreambossModule>();
}