card_svr/src/common/WebApi.ts

192 lines
4.8 KiB
TypeScript

import axios from 'axios'
import { debugRoom, error } from './Debug'
import { Service } from '../service/Service'
const isProd = process.env.NODE_ENV === 'production'
const SERVER_TOKEN = 'ibDbuTmpQn3f48uJr2mBMkGrqvIhSbIg'
/**
* 获取卡组详情
* @param {string} accountid
* @param {number} heroid
* @param {string} cardgroup
* @return {Promise<AxiosResponse<any>>}
*/
export async function getCardGroup(accountid: string, heroid: number, cardgroup: string) {
const infoHost = await new Service().getInfoSvr()
return axios.get(`${ infoHost }/${ accountid }/group_info/${ heroid }/${ cardgroup }`, {
params: {token: SERVER_TOKEN}
})
.then(function (response) {
let res = response.data
if (res.errcode) {
throw new Error(res.errmsg)
} else {
return res.data
}
})
}
export async function getUserInfo(accountid: string, isMatch: boolean) {
const infoHost = await new Service().getInfoSvr()
return axios.post(`${ infoHost }/${ accountid }/uinfo`, {isMatch, token: SERVER_TOKEN})
.then(function (response) {
let res = response.data
if (res.errcode) {
throw new Error(res.errmsg)
} else {
return res.data
}
})
}
export async function randomUserInfo(min: number, max: number, accounts: string[]) {
const data = {min, max, accounts, token: SERVER_TOKEN}
const infoHost = await new Service().getInfoSvr()
return axios.post(`${ infoHost }/randomrobot`, data)
.then(function (response) {
let res = response.data
if (res.errcode) {
throw new Error(res.errmsg)
} else {
return res.data
}
})
}
/**
* 解锁英雄
* @param {string} accountid
* @param {number | string} heroid
* @return {Promise<AxiosResponse<any>>}
*/
export async function requestUnlockHero(accountid: string, heroid: number | string) {
let data = { 'type': 0, token: SERVER_TOKEN }
const infoHost = await new Service().getInfoSvr()
return axios.post(`${ infoHost }/${ accountid }/hero/unlock/${ heroid }`, data)
}
/**
* 上报游戏结果
* @param data
*/
export async function reportGameResult(data: any) {
data.token = SERVER_TOKEN
let dataStr = JSON.stringify(data)
const infoHost = await new Service().getInfoSvr()
let reqConfig = {
method: 'post',
url: `${ infoHost }/record/save`,
headers: {
'Content-Type': 'application/json'
},
data: dataStr
}
// @ts-ignore
return axios(reqConfig)
}
/**
* 使用物品
* @param {string} accountid
* @param {number} itemid
* @param {number} count
*/
export async function useItem(accountid: string, itemid: number, count: number) {
const data = { itemid, count, token: SERVER_TOKEN }
let dataStr = JSON.stringify(data)
const infoHost = await new Service().getInfoSvr()
if (!infoHost) {
error('no info host found!!!')
}
let reqConfig = {
method: 'post',
url: `${ infoHost }/${ accountid }/useitem`,
headers: {
'Content-Type': 'application/json'
},
data: dataStr
}
// @ts-ignore
return axios(reqConfig)
}
/**
* 检查匹配所需的资源是否足够
* @param {string} accountid
* @param {string} matchid
* @return {Promise<any>}
*/
export async function checkMatchTicket(accountid: string, matchid: string) {
const data = { matchid, token: SERVER_TOKEN }
const infoHost = await new Service().getInfoSvr()
const url = `${ infoHost }/${ accountid }/beginmatch`
let reqConfig = {
method: 'post',
url,
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify(data)
}
// @ts-ignore
return axios(reqConfig).then(function (response) {
let res = response.data
if (res.errcode) {
error(`check match ticket error with code: ${res.errcode}, msg: ${res.errmsg}`)
throw new Error(res.errcode)
} else {
return {}
}
})
}
/**
* 创建机器人
* @param data
*/
export async function createRobot(data: any) {
const robotHost = await new Service().getRobotSvr()
if (!robotHost) {
error('no robot host found!!!')
}
axios.get(`${robotHost}/robot/create`, {
params: data
}).then((res) => {
debugRoom(`caeate robot result: `, res.data)
}).catch((err) => {
error(err)
})
}
/**
* 用户主动离开游戏
* @param {string} accountid
* @param {string} roomid
* @return {Promise<any>}
*/
export async function leftGame(accountid: string, roomid: string) {
debugRoom(`player dead and left game: ${roomid}, ${accountid}`)
const data = { roomid, token: SERVER_TOKEN }
let dataStr = JSON.stringify(data)
const infoHost = await new Service().getInfoSvr()
if (!infoHost) {
error('no info host found!!!')
}
let reqConfig = {
method: 'post',
url: `${ infoHost }/${ accountid }/leftgame`,
headers: {
'Content-Type': 'application/json'
},
data: dataStr
}
// @ts-ignore
return axios(reqConfig)
}