import axios from 'axios' const apiBase = 'http://127.0.0.1:2668/api' /** * 发送私信给玩家 * @param roomId * @param clientId * @param type * @param msg * @return {Promise>} */ export async function sendMsg(roomId, clientId, type, msg) { const url = `${apiBase}/room/call` const args = [clientId, type, msg] const data = { roomId, method: 'smsg', args: JSON.stringify(args) } return axios.post(url, data) .then(res => { return res.data }) } /** * 发送广播消息 * @param roomId * @param type * @param msg * @return {Promise>} */ export async function broadcast(roomId, type, msg) { const url = `${apiBase}/broadcast` const data = { roomId, type, msg } return axios.post(url, data) .then(res => { return res.data }) } /** * 从房间踢掉玩家 * @param roomId * @param clientId * @return {Promise>} */ export async function kickClient(roomId, clientId) { const url = `${apiBase}/room/call` const args = [clientId] const data = { roomId, method: '_forceClientDisconnect', args: JSON.stringify(args) } return axios.post(url, data) .then(res => { return res.data }) } /** * 开始游戏 * @param roomId * @param params * @return {Promise>} */ export async function beginGame(roomId, params) { console.log(`begin game: ${roomId}, ${params}`) const url = `${apiBase}/room/call` const args = [params] const data = { roomId, method: 'beginGame', args: JSON.stringify(args) } return axios.post(url, data) .then(res => { return res.data }) } export async function endGame(roomId, params) { console.log(`end game: ${roomId}, ${params}`) const url = `${apiBase}/room/call` const args = [params] const data = { roomId, method: 'endGame', args: JSON.stringify(args) } return axios.post(url, data) .then(res => { return res.data }) } /** * 创建房间 * @param data * @return {Promise>} */ export async function createRoom(data) { const url = `${apiBase}/matchmake/joinOrCreate/puzzle_room` return axios.post(url, data) } /** * 加入房间 * @param data * @return {Promise>} */ export async function joinRoom(data) { const url = `${apiBase}/matchmake/joinById/puzzle_room` return axios.post(url, data) } /** * 发送一条问题 * @param {string} roomId * @param data * @return {Promise>} */ export async function sendQuestion(roomId: string, data: any) { console.log(`sendOneQuestion to ${roomId}, ${JSON.stringify(data)}`) return broadcast(roomId, 'question', data) } /** * 更新玩家的分数 * @param {string} roomId * @param data * @return {Promise>} */ export async function updateScore(roomId: string, data: any) { console.log(`updateScore: ${roomId}, ${JSON.stringify(data)}`) const url = `${apiBase}/room/call` const args = [data] const params = { roomId, method: 'updateScore', args: JSON.stringify(args) } return axios.post(url, params) .then(res => { return res.data }) } /** * 更新游戏轮数 * @param {string} roomId * @param {number} round * @return {Promise>} */ export async function updateRound(roomId: string, round: number) { console.log(`updateRound: ${roomId}, ${round}`) const url = `${apiBase}/room/call` const args = [round] const params = { roomId, method: 'updateRound', args: JSON.stringify(args) } return axios.post(url, params) .then(res => { return res.data }) }