1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-20 02:00:49 +00:00

Split reducer in two files

This commit is contained in:
Ash Keel 2021-09-30 12:26:59 +02:00
parent bc12772959
commit 69bef12d44
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E
2 changed files with 176 additions and 175 deletions

View file

@ -1,4 +1,3 @@
/* eslint-disable camelcase */
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
import { import {
AsyncThunk, AsyncThunk,
@ -9,160 +8,12 @@ import {
PayloadAction, PayloadAction,
} from '@reduxjs/toolkit'; } from '@reduxjs/toolkit';
import KilovoltWS from '@strimertul/kilovolt-client'; import KilovoltWS from '@strimertul/kilovolt-client';
import {
// Storage APIState,
const moduleConfigKey = 'stul-meta/modules'; LoyaltyPointsEntry,
const httpConfigKey = 'http/config'; LoyaltyRedeem,
const twitchConfigKey = 'twitch/config'; LoyaltyStorage,
const twitchBotConfigKey = 'twitch/bot-config'; } from './types';
const twitchBotCommandsKey = 'twitch/bot-custom-commands';
const stulbeConfigKey = 'stulbe/config';
const loyaltyConfigKey = 'loyalty/config';
const loyaltyPointsPrefix = 'loyalty/points/';
const loyaltyRewardsKey = 'loyalty/rewards';
const loyaltyGoalsKey = 'loyalty/goals';
const loyaltyRedeemQueueKey = 'loyalty/redeem-queue';
// RPCs
const loyaltyCreateRedeemKey = 'loyalty/@create-redeem';
const loyaltyRemoveRedeemKey = 'loyalty/@remove-redeem';
interface ModuleConfig {
configured: boolean;
kv: boolean;
static: boolean;
twitch: boolean;
stulbe: boolean;
loyalty: boolean;
}
interface HTTPConfig {
bind: string;
path: string;
}
interface TwitchConfig {
enable_bot: boolean;
api_client_id: string;
api_client_secret: string;
}
interface TwitchBotConfig {
username: string;
oauth: string;
channel: string;
chat_keys: boolean;
chat_history: number;
}
type AccessLevelType = 'everyone' | 'vip' | 'moderators' | 'streamer';
export interface TwitchBotCustomCommand {
description: string;
access_level: AccessLevelType;
response: string;
enabled: boolean;
}
type TwitchBotCustomCommands = Record<string, TwitchBotCustomCommand>;
interface StulbeConfig {
endpoint: string;
username: string;
auth_key: string;
}
interface LoyaltyConfig {
currency: string;
points: {
interval: number;
amount: number;
activity_bonus: number;
};
banlist: string[];
}
export interface LoyaltyPointsEntry {
points: number;
}
export type LoyaltyStorage = Record<string, LoyaltyPointsEntry>;
export interface LoyaltyReward {
enabled: boolean;
id: string;
name: string;
description: string;
image: string;
price: number;
required_info?: string;
cooldown: number;
}
export interface LoyaltyGoal {
enabled: boolean;
id: string;
name: string;
description: string;
image: string;
total: number;
contributed: number;
contributors: Record<string, number>;
}
export interface LoyaltyRedeem {
username: string;
display_name: string;
when: Date;
reward: LoyaltyReward;
request_text: string;
}
export interface APIState {
client: KilovoltWS;
connected: boolean;
initialLoadComplete: boolean;
loyalty: {
users: LoyaltyStorage;
rewards: LoyaltyReward[];
goals: LoyaltyGoal[];
redeemQueue: LoyaltyRedeem[];
};
twitchBot: {
commands: TwitchBotCustomCommands;
};
moduleConfigs: {
moduleConfig: ModuleConfig;
httpConfig: HTTPConfig;
twitchConfig: TwitchConfig;
twitchBotConfig: TwitchBotConfig;
stulbeConfig: StulbeConfig;
loyaltyConfig: LoyaltyConfig;
};
}
const initialState: APIState = {
client: null,
connected: false,
initialLoadComplete: false,
loyalty: {
users: null,
rewards: null,
goals: null,
redeemQueue: null,
},
twitchBot: {
commands: null,
},
moduleConfigs: {
moduleConfig: null,
httpConfig: null,
twitchConfig: null,
twitchBotConfig: null,
stulbeConfig: null,
loyaltyConfig: null,
},
};
function makeGetterThunk<T>(key: string) { function makeGetterThunk<T>(key: string) {
return async (_: void, { getState }) => { return async (_: void, { getState }) => {
@ -215,6 +66,14 @@ function makeModule<T>(
// eslint-disable-next-line import/no-mutable-exports, @typescript-eslint/ban-types // eslint-disable-next-line import/no-mutable-exports, @typescript-eslint/ban-types
export let setupClientReconnect: AsyncThunk<void, KilovoltWS, {}>; export let setupClientReconnect: AsyncThunk<void, KilovoltWS, {}>;
// Storage
const loyaltyPointsPrefix = 'loyalty/points/';
const loyaltyRewardsKey = 'loyalty/rewards';
// RPCs
const loyaltyCreateRedeemKey = 'loyalty/@create-redeem';
const loyaltyRemoveRedeemKey = 'loyalty/@remove-redeem';
export const createWSClient = createAsyncThunk( export const createWSClient = createAsyncThunk(
'api/createClient', 'api/createClient',
async (address: string, { dispatch }) => { async (address: string, { dispatch }) => {
@ -232,7 +91,9 @@ export const getUserPoints = createAsyncThunk(
const keys = await api.client.getKeysByPrefix(loyaltyPointsPrefix); const keys = await api.client.getKeysByPrefix(loyaltyPointsPrefix);
const userpoints: LoyaltyStorage = {}; const userpoints: LoyaltyStorage = {};
Object.entries(keys).forEach(([k, v]) => { Object.entries(keys).forEach(([k, v]) => {
userpoints[k.substr(loyaltyPointsPrefix.length)] = JSON.parse(v); userpoints[k.substr(loyaltyPointsPrefix.length)] = JSON.parse(
v as string,
);
}); });
return userpoints; return userpoints;
}, },
@ -258,71 +119,71 @@ export const setUserPoints = createAsyncThunk(
); );
export const modules = { export const modules = {
moduleConfig: makeModule<ModuleConfig>( moduleConfig: makeModule(
moduleConfigKey, 'stul-meta/modules',
(state) => state.moduleConfigs?.moduleConfig, (state) => state.moduleConfigs?.moduleConfig,
(state, { payload }) => { (state, { payload }) => {
state.moduleConfigs.moduleConfig = payload; state.moduleConfigs.moduleConfig = payload;
}, },
), ),
httpConfig: makeModule<HTTPConfig>( httpConfig: makeModule(
httpConfigKey, 'http/config',
(state) => state.moduleConfigs?.httpConfig, (state) => state.moduleConfigs?.httpConfig,
(state, { payload }) => { (state, { payload }) => {
state.moduleConfigs.httpConfig = payload; state.moduleConfigs.httpConfig = payload;
}, },
), ),
twitchConfig: makeModule<TwitchConfig>( twitchConfig: makeModule(
twitchConfigKey, 'twitch/config',
(state) => state.moduleConfigs?.twitchConfig, (state) => state.moduleConfigs?.twitchConfig,
(state, { payload }) => { (state, { payload }) => {
state.moduleConfigs.twitchConfig = payload; state.moduleConfigs.twitchConfig = payload;
}, },
), ),
twitchBotConfig: makeModule<TwitchBotConfig>( twitchBotConfig: makeModule(
twitchBotConfigKey, 'twitch/bot-config',
(state) => state.moduleConfigs?.twitchBotConfig, (state) => state.moduleConfigs?.twitchBotConfig,
(state, { payload }) => { (state, { payload }) => {
state.moduleConfigs.twitchBotConfig = payload; state.moduleConfigs.twitchBotConfig = payload;
}, },
), ),
twitchBotCommands: makeModule<TwitchBotCustomCommands>( twitchBotCommands: makeModule(
twitchBotCommandsKey, 'twitch/bot-custom-commands',
(state) => state.twitchBot?.commands, (state) => state.twitchBot?.commands,
(state, { payload }) => { (state, { payload }) => {
state.twitchBot.commands = payload; state.twitchBot.commands = payload;
}, },
), ),
stulbeConfig: makeModule<StulbeConfig>( stulbeConfig: makeModule(
stulbeConfigKey, 'stulbe/config',
(state) => state.moduleConfigs?.stulbeConfig, (state) => state.moduleConfigs?.stulbeConfig,
(state, { payload }) => { (state, { payload }) => {
state.moduleConfigs.stulbeConfig = payload; state.moduleConfigs.stulbeConfig = payload;
}, },
), ),
loyaltyConfig: makeModule<LoyaltyConfig>( loyaltyConfig: makeModule(
loyaltyConfigKey, 'loyalty/config',
(state) => state.moduleConfigs?.loyaltyConfig, (state) => state.moduleConfigs?.loyaltyConfig,
(state, { payload }) => { (state, { payload }) => {
state.moduleConfigs.loyaltyConfig = payload; state.moduleConfigs.loyaltyConfig = payload;
}, },
), ),
loyaltyRewards: makeModule<LoyaltyReward[]>( loyaltyRewards: makeModule(
loyaltyRewardsKey, loyaltyRewardsKey,
(state) => state.loyalty.rewards, (state) => state.loyalty.rewards,
(state, { payload }) => { (state, { payload }) => {
state.loyalty.rewards = payload; state.loyalty.rewards = payload;
}, },
), ),
loyaltyGoals: makeModule<LoyaltyGoal[]>( loyaltyGoals: makeModule(
loyaltyGoalsKey, 'loyalty/goals',
(state) => state.loyalty.goals, (state) => state.loyalty.goals,
(state, { payload }) => { (state, { payload }) => {
state.loyalty.goals = payload; state.loyalty.goals = payload;
}, },
), ),
loyaltyRedeemQueue: makeModule<LoyaltyRedeem[]>( loyaltyRedeemQueue: makeModule(
loyaltyRedeemQueueKey, 'loyalty/redeem-queue',
(state) => state.loyalty.redeemQueue, (state) => state.loyalty.redeemQueue,
(state, { payload }) => { (state, { payload }) => {
state.loyalty.redeemQueue = payload; state.loyalty.redeemQueue = payload;
@ -356,6 +217,29 @@ const moduleChangeReducers = Object.fromEntries(
(state: APIState, action: PayloadAction<unknown>) => never (state: APIState, action: PayloadAction<unknown>) => never
>; >;
const initialState: APIState = {
client: null,
connected: false,
initialLoadComplete: false,
loyalty: {
users: null,
rewards: null,
goals: null,
redeemQueue: null,
},
twitchBot: {
commands: null,
},
moduleConfigs: {
moduleConfig: null,
httpConfig: null,
twitchConfig: null,
twitchBotConfig: null,
stulbeConfig: null,
loyaltyConfig: null,
},
};
const apiReducer = createSlice({ const apiReducer = createSlice({
name: 'api', name: 'api',
initialState, initialState,

View file

@ -0,0 +1,117 @@
/* eslint-disable camelcase */
import KilovoltWS from '@strimertul/kilovolt-client';
interface ModuleConfig {
configured: boolean;
kv: boolean;
static: boolean;
twitch: boolean;
stulbe: boolean;
loyalty: boolean;
}
interface HTTPConfig {
bind: string;
path: string;
}
interface TwitchConfig {
enable_bot: boolean;
api_client_id: string;
api_client_secret: string;
}
interface TwitchBotConfig {
username: string;
oauth: string;
channel: string;
chat_keys: boolean;
chat_history: number;
}
type AccessLevelType = 'everyone' | 'vip' | 'moderators' | 'streamer';
export interface TwitchBotCustomCommand {
description: string;
access_level: AccessLevelType;
response: string;
enabled: boolean;
}
type TwitchBotCustomCommands = Record<string, TwitchBotCustomCommand>;
interface StulbeConfig {
endpoint: string;
username: string;
auth_key: string;
}
interface LoyaltyConfig {
currency: string;
points: {
interval: number;
amount: number;
activity_bonus: number;
};
banlist: string[];
}
export interface LoyaltyPointsEntry {
points: number;
}
export type LoyaltyStorage = Record<string, LoyaltyPointsEntry>;
export interface LoyaltyReward {
enabled: boolean;
id: string;
name: string;
description: string;
image: string;
price: number;
required_info?: string;
cooldown: number;
}
export interface LoyaltyGoal {
enabled: boolean;
id: string;
name: string;
description: string;
image: string;
total: number;
contributed: number;
contributors: Record<string, number>;
}
export interface LoyaltyRedeem {
username: string;
display_name: string;
when: Date;
reward: LoyaltyReward;
request_text: string;
}
export interface APIState {
client: KilovoltWS;
connected: boolean;
initialLoadComplete: boolean;
loyalty: {
users: LoyaltyStorage;
rewards: LoyaltyReward[];
goals: LoyaltyGoal[];
redeemQueue: LoyaltyRedeem[];
};
twitchBot: {
commands: TwitchBotCustomCommands;
};
moduleConfigs: {
moduleConfig: ModuleConfig;
httpConfig: HTTPConfig;
twitchConfig: TwitchConfig;
twitchBotConfig: TwitchBotConfig;
stulbeConfig: StulbeConfig;
loyaltyConfig: LoyaltyConfig;
};
}