176 lines
5.9 KiB
TypeScript
176 lines
5.9 KiB
TypeScript
import type {
|
|
ReqGetGuid,
|
|
ResGetGuid,
|
|
ReqGameCharacterList,
|
|
CharacterInfo,
|
|
ResGameCharacterList,
|
|
} from '#layers/types/api/gameLinkedData'
|
|
|
|
/**
|
|
* 게임 관련 항목
|
|
*/
|
|
const useGameLinkedData = () => {
|
|
const logPrefix = {
|
|
exception: '[Exception] /composables/useGameLinkedData',
|
|
failure: '[Failure] /composables/useGameLinkedData',
|
|
}
|
|
const hasGuid = ref(false) // GUID 존재 여부
|
|
const characterList = ref([] as CharacterInfo[]) // 보유 캐릭터 목록
|
|
const mainCharacter = ref({} as CharacterInfo) // 대표 캐릭터
|
|
const selectCharacter = ref(null) // (사용자가 )선택한 캐릭터
|
|
|
|
// [Setter] GUID 존재 여부 세팅
|
|
const setHasGuid = (newHasGuid: boolean) => {
|
|
hasGuid.value = newHasGuid
|
|
}
|
|
// [Setter] 보유 캐릭터 목록 세팅
|
|
const setCharacterList = (newCharacters: CharacterInfo[]) => {
|
|
characterList.value = newCharacters
|
|
}
|
|
// [Setter] 대표 캐릭터 세팅
|
|
const setMainCharacter = (newCharacter: CharacterInfo) => {
|
|
mainCharacter.value = newCharacter
|
|
}
|
|
// [Setter] 캐릭터 선택 세팅
|
|
const setSelectCharacter = (newCharacter: CharacterInfo | null) => {
|
|
selectCharacter.value = newCharacter as any
|
|
}
|
|
|
|
// [Computed] 캐릭터 목록 존재 여부
|
|
const hasNoCharacterList: ComputedRef<boolean> = computed(() => {
|
|
return characterList.value == null || characterList.value.length === 0
|
|
})
|
|
|
|
/**
|
|
* GUID 조회
|
|
*
|
|
* @param {ReqGetGuid} req
|
|
* @description https://developers-beta.onstove.com/ko/docs/web/member/api_member_game_id
|
|
*/
|
|
const getGuid = async (req: ReqGetGuid) => {
|
|
let res: ResGetGuid = {} as ResGetGuid
|
|
try {
|
|
const baseApiUrl = req.baseApiUrl || ''
|
|
|
|
const url = `${baseApiUrl}/member/v3.0/${req.game_id}`
|
|
const headers = {
|
|
Authorization: `Bearer ${req.accessToken}`,
|
|
}
|
|
|
|
res = (await commonFetch('GET', url, { headers })) as ResGetGuid
|
|
|
|
if (res != null) {
|
|
if (res.code === 0 && res.value != null && res.value?.guid != null) {
|
|
setHasGuid(res.value?.guid > 0)
|
|
} else {
|
|
res = { code: res.code, message: res.message || '' }
|
|
console.log(`${logPrefix.failure}.getGuid: `, res)
|
|
res.code = -99999 // else 알럿 띄우기 용 세팅
|
|
setHasGuid(false)
|
|
}
|
|
} else {
|
|
console.log(`${logPrefix.failure}.getGuid - res is null: `, res)
|
|
res = { code: -99999, message: '' }
|
|
setHasGuid(false)
|
|
}
|
|
} catch (e) {
|
|
console.error(`${logPrefix.exception}.getGuid: `, e)
|
|
res = { code: -99999, message: `${e}` }
|
|
setHasGuid(false)
|
|
}
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* 보유 캐릭터 목록 조회
|
|
*
|
|
* @param {ReqGameCharacterList} req
|
|
* @description https://wiki.smilegate.net/pages/viewpage.action?pageId=193659225
|
|
*/
|
|
const getCharacterList = async (req: ReqGameCharacterList) => {
|
|
let res: ResGameCharacterList = {} as ResGameCharacterList
|
|
try {
|
|
const baseApiUrl = req.baseApiUrl || ''
|
|
|
|
const url = `${baseApiUrl}/game/v2.1/${req.game_id}/character`
|
|
const headers = {
|
|
Authorization: `Bearer ${req.accessToken}`,
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
}
|
|
|
|
res = (await commonFetch('GET', url, { headers })) as ResGameCharacterList
|
|
|
|
if (res != null) {
|
|
if (res.code === 0) {
|
|
const characterInfos =
|
|
res.value?.character_infos || ([] as CharacterInfo[])
|
|
|
|
// '[서버명] 캐릭터명' 포맷의 라벨용 네이밍 세팅
|
|
characterInfos.forEach(characterInfo => {
|
|
const characterName = `${characterInfo.name}`
|
|
let formattedNm = `${characterName}`
|
|
|
|
// 넘어온 월드 목록이 있는 경우 해당 목록에서 다국어로 된 정보 조회
|
|
if (req.world_list != null && req.world_list.length > 0) {
|
|
const worldNm =
|
|
req.world_list.find(
|
|
world => world.world_id === characterInfo?.world_id
|
|
)?.world_nm || ''
|
|
if (worldNm != null && worldNm !== '') {
|
|
formattedNm = `[${worldNm}] ${characterName}`
|
|
}
|
|
}
|
|
characterInfo.formatted_nm = formattedNm
|
|
})
|
|
|
|
setCharacterList(characterInfos)
|
|
setMainCharacter(
|
|
res.value?.main_game_character || ({} as CharacterInfo)
|
|
)
|
|
} else if (res.code === 515) {
|
|
// [515] AccessToken이 유효하지 않을 경우
|
|
res = { code: res.code, message: res.message || '' }
|
|
console.log(`${logPrefix.failure}.getCharacterList: `, res)
|
|
setCharacterList([] as CharacterInfo[])
|
|
setMainCharacter({} as CharacterInfo)
|
|
} else {
|
|
// [501] 캐릭터가 존재하지 않을 경우(member_no, game_no, character_id 기준)
|
|
// [502] 유효하지 않거나 잘못된 파라미터로 호출할 경우
|
|
// [701] 존재하지 않은 게임일 경우(game_no 기준)
|
|
res = { code: res.code, message: res.message || '' }
|
|
console.log(`${logPrefix.failure}.getCharacterList: `, res)
|
|
res.code = -99999 // else 알럿 띄우기 용 세팅
|
|
setCharacterList([] as CharacterInfo[])
|
|
setMainCharacter({} as CharacterInfo)
|
|
}
|
|
} else {
|
|
res = { code: -99999, message: '' }
|
|
console.log(`${logPrefix.failure}.getCharacterList: `, res)
|
|
setCharacterList([] as CharacterInfo[])
|
|
setMainCharacter({} as CharacterInfo)
|
|
}
|
|
} catch (e) {
|
|
console.error(`${logPrefix.exception}.getCharacterList: `, e)
|
|
res = { code: -99999, message: `${e}` }
|
|
setCharacterList([] as CharacterInfo[])
|
|
setMainCharacter({} as CharacterInfo)
|
|
}
|
|
return res
|
|
}
|
|
|
|
return {
|
|
hasGuid,
|
|
characterList,
|
|
mainCharacter,
|
|
selectCharacter,
|
|
hasNoCharacterList,
|
|
getGuid,
|
|
setCharacterList,
|
|
setMainCharacter,
|
|
setSelectCharacter,
|
|
getCharacterList,
|
|
}
|
|
}
|
|
|
|
export { useGameLinkedData }
|