<commit_before>import { createStore, StoreCreator } from 'redux';
//import AsyncStorage from '@react-native-community/async-storage';
import { AsyncStorage } from 'react-native';
import { persistStore, persistReducer } from 'redux-persist';
import _ from 'lodash';

const persistConfig = {
  key: 'root',
  storage: AsyncStorage
}

export type Player = {
  name: string,
  active: boolean
}

export type Round = {
  game: string,
  teams: [Array<Player>, Array<Player>],
  winner: -1 | 0 | 1 | null
}

type State = {
    players: Array<Player>,
    games: Array<String>,
    rounds: Array<Round>,
    matchId: number,
    lastChange: number,
}

const INITIAL_STATE: State = {
  players: [
    {name: "Markus", active: true},
    {name: "Hannah", active: true},
    {name: "Tobias", active: true},
    {name: "Moritz", active: true},
  ],
  games: ["Snake 🐍","Tron 🏍","Darts 🎯"],
  rounds: [],
  matchId: -1,
  lastChange: -1,
};

// TODO can I check payload types?
const matchReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case 'START_MATCH':
        // Start new game with players and games (replacing old state)
        return {
            players: action.payload.players,
            games: action.payload.games,
            rounds: [],
            matchId: Date.now(),
            lastChange: Date.now(),
        };
    case 'LOAD_MATCH':
        // Load match with state from payload (replacing old state)
        return action.payload;
    case 'GAME_RESULT':
        // Set result of this game
        let rounds = _.cloneDeep(state.rounds);
        rounds[rounds.length-1].winner = action.payload;
        return {...state, 
            rounds: rounds,
            lastChange: Date.now(),
        };
    case 'START_NEXT_GAME':
        // Add result of this game to previous rounds
        return {...state,
            rounds: [...state.rounds, {
              teams: action.payload,
              game: state.games[state.rounds.length],
              winner: -1
            }],
            lastChange: Date.now(),
        };
    case 'TOGGLE_PLAYER':
        // Toggle active/inactive status of player with payload name
        let newPlayers = state.players.map(p => {
            let newPlayer = p;
            if(p.name == action.payload){
                newPlayer.active = !newPlayer.active;
            }
            return newPlayer;
        })
        return {...state,
            players: newPlayers,
        };
    case 'TOGGLE_GAME_RESULT':
        // Swap winner of a finished game
        let newRounds = _.cloneDeep(state.rounds);
        let winnerBefore = newRounds[action.payload].winner
        if(winnerBefore>=0)
          newRounds[action.payload].winner = winnerBefore == 0 ? 1 : 0
        return {...state, rounds: newRounds, lastChange: Date.now()}
    case 'REMOVE_GAME':
        // Remove a game (can be played or unplayed)
        let newState = _.cloneDeep(state);
        newState.games.splice(action.payload,1)
        if(newState.rounds.length > action.payload){
          newState.rounds.splice(action.payload,1)
        }
        newState.lastChange = Date.now()
        return newState
    default:
      return state
  }
};

const persistedReducer = persistReducer(persistConfig, matchReducer)

export const store = createStore(persistedReducer);
export const persistor = persistStore(store);<commit_msg>Remove players and games from initial state<commit_after>import { createStore, StoreCreator } from 'redux';
//import AsyncStorage from '@react-native-community/async-storage';
import { AsyncStorage } from 'react-native';
import { persistStore, persistReducer } from 'redux-persist';
import _ from 'lodash';

const persistConfig = {
  key: 'root',
  storage: AsyncStorage
}

export type Player = {
  name: string,
  active: boolean
}

export type Round = {
  game: string,
  teams: [Array<Player>, Array<Player>],
  winner: -1 | 0 | 1 | null
}

type State = {
    players: Array<Player>,
    games: Array<String>,
    rounds: Array<Round>,
    matchId: number,
    lastChange: number,
}

const INITIAL_STATE: State = {
  players: [],
  games: [],
  rounds: [],
  matchId: -1,
  lastChange: -1,
};

// TODO can I check payload types?
const matchReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case 'START_MATCH':
        // Start new game with players and games (replacing old state)
        return {
            players: action.payload.players,
            games: action.payload.games,
            rounds: [],
            matchId: Date.now(),
            lastChange: Date.now(),
        };
    case 'LOAD_MATCH':
        // Load match with state from payload (replacing old state)
        return action.payload;
    case 'GAME_RESULT':
        // Set result of this game
        let rounds = _.cloneDeep(state.rounds);
        rounds[rounds.length-1].winner = action.payload;
        return {...state, 
            rounds: rounds,
            lastChange: Date.now(),
        };
    case 'START_NEXT_GAME':
        // Add result of this game to previous rounds
        return {...state,
            rounds: [...state.rounds, {
              teams: action.payload,
              game: state.games[state.rounds.length],
              winner: -1
            }],
            lastChange: Date.now(),
        };
    case 'TOGGLE_PLAYER':
        // Toggle active/inactive status of player with payload name
        let newPlayers = state.players.map(p => {
            let newPlayer = p;
            if(p.name == action.payload){
                newPlayer.active = !newPlayer.active;
            }
            return newPlayer;
        })
        return {...state,
            players: newPlayers,
        };
    case 'TOGGLE_GAME_RESULT':
        // Swap winner of a finished game
        let newRounds = _.cloneDeep(state.rounds);
        let winnerBefore = newRounds[action.payload].winner
        if(winnerBefore>=0)
          newRounds[action.payload].winner = winnerBefore == 0 ? 1 : 0
        return {...state, rounds: newRounds, lastChange: Date.now()}
    case 'REMOVE_GAME':
        // Remove a game (can be played or unplayed)
        let newState = _.cloneDeep(state);
        newState.games.splice(action.payload,1)
        if(newState.rounds.length > action.payload){
          newState.rounds.splice(action.payload,1)
        }
        newState.lastChange = Date.now()
        return newState
    default:
      return state
  }
};

const persistedReducer = persistReducer(persistConfig, matchReducer)

export const store = createStore(persistedReducer);
export const persistor = persistStore(store);
{'fraction_non_alphanumeric': 0.11927437641723355, 'fraction_numerical': 0.003930461073318216, 'mean_word_length': 2.232046897899365, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '570566', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1936, 'n_words': 515}