Commit fe02e6f3 authored by Chunchi Che's avatar Chunchi Che

add playerSlice

parent 6d6df46d
Pipeline #17245 passed with stage
in 1 minute and 6 seconds
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RootState } from "../store";
export type Player = {
name?: string;
state?: string;
isHost?: boolean;
};
export interface playerState {
player0: Player;
player1: Player;
observerCount: number;
}
const initialState: playerState = {
player0: {},
player1: {},
observerCount: 0,
};
const playerSlice = createSlice({
name: "player",
initialState,
reducers: {
player0Enter: (state, action: PayloadAction<string>) => {
state.player0.name = action.payload;
},
player1Enter: (state, action: PayloadAction<string>) => {
state.player1.name = action.payload;
},
player0Update: (state, action: PayloadAction<string>) => {
state.player0.state = action.payload;
},
player1Update: (state, action: PayloadAction<string>) => {
state.player1.state = action.payload;
},
hostChange: (state, action: PayloadAction<number>) => {
const i = action.payload;
if (i === 0) {
state.player0.isHost = true;
state.player1.isHost = false;
} else {
state.player1.isHost = true;
state.player0.isHost = false;
}
},
observerChange: (state, action: PayloadAction<number>) => {
state.observerCount += action.payload;
},
},
});
export const {
player0Enter,
player1Enter,
player0Update,
player1Update,
hostChange,
observerChange,
} = playerSlice.actions;
export const selectPlayer0 = (state: RootState) => state.player.player0;
export const selectPlayer1 = (state: RootState) => state.player.player1;
export default playerSlice.reducer;
import { configureStore } from "@reduxjs/toolkit";
import joinedReducer from "./reducers/joinSlice";
import chatReducer from "./reducers/chatSlice";
import playerReducer from "./reducers/playerSlice";
export const store = configureStore({
reducer: {
join: joinedReducer,
chat: chatReducer,
player: playerReducer,
},
});
......
......@@ -6,6 +6,14 @@ import "../css/WaitRoom.css";
import { useAppDispatch, useAppSelector } from "../hook";
import { setJoined, selectJoined } from "../reducers/joinSlice";
import { postChat, selectChat } from "../reducers/chatSlice";
import {
player0Enter,
player1Enter,
player0Update,
player1Update,
hostChange,
observerChange,
} from "../reducers/playerSlice";
type Player = {
name?: string;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment