LeagueStats/client/src/store/modules/summoner.js

197 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-09-08 20:08:49 +00:00
import { axios } from '@/plugins/axios'
2020-01-12 00:31:28 +00:00
import { createMatchData, createBasicSummonerData, createRecordsData } from '@/helpers/summoner'
2019-09-08 20:08:49 +00:00
export const namespaced = true
export const state = {
basic: {
account: {},
2020-02-01 19:17:14 +00:00
currentSeason: null,
matchList: [],
ranked: {},
seasons: [],
status: '',
},
overview: {
matchIndex: 0,
matches: [],
stats: {},
loaded: false,
matchesLoading: false,
},
champions: {
list: [],
championsLoaded: false
},
2020-01-12 00:31:28 +00:00
records: {
list: [],
recordsLoaded: false
},
2020-01-14 21:04:45 +00:00
live: {
match: {},
liveLoaded: false,
playing: false,
2020-01-14 21:04:45 +00:00
},
2019-09-08 20:08:49 +00:00
}
export const mutations = {
BASIC_REQUEST(state) {
state.basic.status = 'loading'
2020-02-01 19:17:14 +00:00
state.basic.currentSeason = null
2019-12-27 21:09:24 +00:00
state.champions.championsLoaded = false
2020-01-12 00:31:28 +00:00
state.records.recordsLoaded = false
state.overview.loaded = false
state.live.liveLoaded = false
},
CHAMPIONS_NOT_FOUND(state) {
state.champions.championsLoaded = false
},
2019-12-21 16:56:31 +00:00
CHAMPIONS_FOUND(state, { champions }) {
state.champions.list = champions
state.champions.championsLoaded = true
2019-12-21 16:56:31 +00:00
},
2020-01-14 21:04:45 +00:00
LIVE_FOUND(state, { live }) {
state.live.match = live
state.live.liveLoaded = true
},
LIVE_NOT_FOUND(state) {
state.live.liveLoaded = false
},
MATCHES_LOADING(state) {
state.overview.matchesLoading = true
},
MATCHES_FOUND(state, { newMatches, stats }) {
state.overview.matchesLoading = false
2020-01-02 09:52:52 +00:00
state.overview.matches = [...state.overview.matches, ...newMatches]
state.overview.matchIndex += newMatches.length
state.overview.stats = stats
state.champions.championsLoaded = false
2020-01-12 00:31:28 +00:00
state.records.recordsLoaded = false
},
OVERVIEW_FOUND(state, infos) {
state.overview.matches = infos.matches
state.overview.matchIndex = infos.matches.length
state.overview.stats = infos.stats
state.overview.loaded = true
2019-09-08 20:08:49 +00:00
},
2020-01-12 00:31:28 +00:00
RECORDS_FOUND(state, { records }) {
state.records.list = records
state.records.recordsLoaded = true
},
2019-09-08 20:08:49 +00:00
SUMMONER_FOUND(state, infos) {
state.basic.account = infos.account
state.basic.matchList = infos.matchList
state.basic.ranked = infos.ranked
state.basic.seasons = infos.seasons
state.basic.status = 'found'
state.live.match = infos.current
state.live.playing = infos.playing
2019-09-08 20:08:49 +00:00
},
SUMMONER_NOT_FOUND(state) {
state.basic.status = 'error'
},
SUMMONER_NOT_PLAYING(state) {
state.live.match = {}
state.live.playing = false
state.live.liveLoaded = false
2020-02-01 19:17:14 +00:00
},
UPDATE_SEASON(state, { season }) {
state.basic.currentSeason = season
state.overview.loaded = false
state.champions.championsLoaded = false
state.records.recordsLoaded = false
},
2019-09-08 20:08:49 +00:00
}
export const actions = {
async basicRequest({ commit, dispatch, rootState }, { summoner, region }) {
region = rootState.regionsList[region]
commit('BASIC_REQUEST')
2019-09-08 20:08:49 +00:00
try {
const resp = await axios(({ url: 'summoner-basic', data: { summoner, region }, method: 'POST' }))
2019-09-08 20:08:49 +00:00
if (resp.data) {
console.log(`---SUMMONER INFOS ${resp.data.account.name}---`)
console.log(resp.data)
const infos = createBasicSummonerData(resp.data)
2019-09-08 20:08:49 +00:00
commit('SUMMONER_FOUND', infos)
} else {
commit('SUMMONER_NOT_FOUND')
dispatch('notification/add', {
type: 'error',
message: 'Summoner not found.'
}, { root: true })
console.log('Summoner not found - store')
}
} catch (error) {
2020-01-12 00:31:28 +00:00
if (error.message !== 'Summoner changed') {
commit('SUMMONER_NOT_FOUND')
}
2019-09-08 20:08:49 +00:00
console.log(error)
}
},
championsNotLoaded({ commit }) {
commit('CHAMPIONS_NOT_FOUND')
},
async championsRequest({ commit }, queue = null) {
const resp = await axios(({ url: 'summoner-champions', data: { puuid: state.basic.account.puuid, queue: queue }, method: 'POST' })).catch(() => { })
console.log('---CHAMPIONS---')
console.log(resp.data)
commit('CHAMPIONS_FOUND', { champions: resp.data })
},
2020-01-14 21:04:45 +00:00
async liveMatchRequest({ commit, rootState }) {
commit('LIVE_NOT_FOUND')
2020-01-14 21:04:45 +00:00
const resp = await axios(({ url: 'summoner-live', data: { account: state.basic.account, region: rootState.currentRegion }, method: 'POST' })).catch(() => { })
console.log('---LIVE---')
console.log(resp.data)
if (resp.data) {
commit('LIVE_FOUND', { live: resp.data })
} else {
commit('SUMMONER_NOT_PLAYING')
}
2020-01-14 21:04:45 +00:00
},
async moreMatches({ commit }) {
commit('MATCHES_LOADING')
const account = state.basic.account
const gameIds = state.basic.matchList.slice(state.overview.matchIndex, state.overview.matchIndex + 10).map(({ gameId }) => gameId)
const resp = await axios(({ url: 'match', data: { account, gameIds }, method: 'POST' })).catch(() => { })
console.log('---MATCHES INFOS---')
console.log(resp.data)
const newMatches = createMatchData(resp.data.matches)
commit('MATCHES_FOUND', { newMatches, stats: resp.data.stats })
},
async overviewRequest({ commit }) {
const resp = await axios(({ url: 'summoner-overview', data: { account: state.basic.account }, method: 'POST' })).catch(() => { })
console.log('---OVERVIEW---')
console.log(resp.data)
resp.data.matches = createMatchData(resp.data.matchesDetails)
commit('OVERVIEW_FOUND', resp.data)
2020-01-12 00:31:28 +00:00
},
async recordsRequest({ commit }) {
const resp = await axios(({ url: 'summoner-records', data: { puuid: state.basic.account.puuid }, method: 'POST' })).catch(() => { })
console.log('---RECORDS---')
console.log(resp.data)
const records = resp.data ? createRecordsData(resp.data) : {}
commit('RECORDS_FOUND', { records })
2020-02-01 19:17:14 +00:00
},
updateSeason({ commit }, season) {
commit('UPDATE_SEASON', { season })
2019-09-08 20:08:49 +00:00
}
}
2019-09-11 20:02:05 +00:00
export const getters = {
matchesLoading: state => state.overview.matchesLoading,
moreMatchesToFetch: state => state.overview.matchIndex < state.basic.matchList.length,
overviewLoaded: state => state.overview.loaded,
playing: state => state.live.playing,
summonerFound: state => state.basic.status === 'found',
summonerNotFound: state => state.basic.status === 'error',
summonerLoading: state => state.basic.status === 'loading',
2019-09-11 20:02:05 +00:00
}