云环境和本地调试 添加
This commit is contained in:
@@ -0,0 +1,370 @@
|
||||
// 出战英雄操作模块
|
||||
const { getOrCreaterUser } = require('./auth');
|
||||
|
||||
const user_db_name = "cocos_users";
|
||||
|
||||
/**
|
||||
* 获取出战英雄配置
|
||||
* @param {Object} db 数据库实例
|
||||
* @param {string} openid 用户openid
|
||||
* @returns {Object} 操作结果
|
||||
*/
|
||||
async function getFightHeros(db, openid) {
|
||||
try {
|
||||
let user = await getOrCreaterUser(db, openid);
|
||||
if (!user) {
|
||||
return {
|
||||
code: -4,
|
||||
msg: "User not found"
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
data: user.fight_heros,
|
||||
msg: "Fight heros retrieved successfully"
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Get fight heros error:", error);
|
||||
return {
|
||||
code: -5,
|
||||
msg: `Get fight heros error: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置出战英雄
|
||||
* @param {Object} db 数据库实例
|
||||
* @param {string} openid 用户openid
|
||||
* @param {number} position 出战位置 (0-4)
|
||||
* @param {number} heroId 英雄ID,0表示移除
|
||||
* @returns {Object} 操作结果
|
||||
*/
|
||||
async function setFightHero(db, openid, position, heroId) {
|
||||
try {
|
||||
const _ = db.command;
|
||||
let user = await getOrCreaterUser(db, openid);
|
||||
if (!user) {
|
||||
return {
|
||||
code: -4,
|
||||
msg: "User not found"
|
||||
};
|
||||
}
|
||||
|
||||
// 验证位置参数
|
||||
if (position < 0 || position > 4) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: "Invalid position, must be 0-4"
|
||||
};
|
||||
}
|
||||
|
||||
// 验证英雄ID
|
||||
if (typeof heroId !== 'number' || heroId < 0) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: "Invalid hero ID"
|
||||
};
|
||||
}
|
||||
|
||||
// 如果不是移除操作,检查英雄是否存在
|
||||
if (heroId > 0 && !user.heros[heroId]) {
|
||||
return {
|
||||
code: -6,
|
||||
msg: "Hero not owned"
|
||||
};
|
||||
}
|
||||
|
||||
const oldHeroId = user.fight_heros[position];
|
||||
|
||||
let updateRes = await db.collection(user_db_name).doc(user._id).update({
|
||||
data: {
|
||||
[`fight_heros.${position}`]: _.set(heroId),
|
||||
last_save_time: _.set(Date.now())
|
||||
}
|
||||
});
|
||||
|
||||
if (updateRes?.stats?.updated >= 1) {
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
position: position,
|
||||
old_hero_id: oldHeroId,
|
||||
new_hero_id: heroId
|
||||
},
|
||||
msg: `Fight hero position ${position} updated successfully`
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: -1,
|
||||
msg: `Set fight hero fail`
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Set fight hero error:", error);
|
||||
return {
|
||||
code: -5,
|
||||
msg: `Set fight hero error: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量设置出战英雄
|
||||
* @param {Object} db 数据库实例
|
||||
* @param {string} openid 用户openid
|
||||
* @param {Object} fightHeros 出战英雄配置对象
|
||||
* @returns {Object} 操作结果
|
||||
*/
|
||||
async function updateFightHeros(db, openid, fightHeros) {
|
||||
try {
|
||||
const _ = db.command;
|
||||
let user = await getOrCreaterUser(db, openid);
|
||||
if (!user) {
|
||||
return {
|
||||
code: -4,
|
||||
msg: "User not found"
|
||||
};
|
||||
}
|
||||
|
||||
// 验证数据格式
|
||||
if (!fightHeros || typeof fightHeros !== 'object') {
|
||||
return {
|
||||
code: -3,
|
||||
msg: "Invalid fight heros data format"
|
||||
};
|
||||
}
|
||||
|
||||
// 验证每个位置和英雄ID
|
||||
for (const pos in fightHeros) {
|
||||
const position = parseInt(pos);
|
||||
const heroId = fightHeros[pos];
|
||||
|
||||
if (isNaN(position) || position < 0 || position > 4) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: `Invalid position: ${pos}`
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof heroId !== 'number' || heroId < 0) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: `Invalid hero ID for position ${pos}: ${heroId}`
|
||||
};
|
||||
}
|
||||
|
||||
// 如果不是移除操作,检查英雄是否存在
|
||||
if (heroId > 0 && !user.heros[heroId]) {
|
||||
return {
|
||||
code: -6,
|
||||
msg: `Hero ${heroId} not owned for position ${pos}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const newFightHeros = { ...user.fight_heros, ...fightHeros };
|
||||
|
||||
let updateRes = await db.collection(user_db_name).doc(user._id).update({
|
||||
data: {
|
||||
fight_heros: _.set(newFightHeros),
|
||||
last_save_time: _.set(Date.now())
|
||||
}
|
||||
});
|
||||
|
||||
if (updateRes?.stats?.updated >= 1) {
|
||||
return {
|
||||
code: 200,
|
||||
data: newFightHeros,
|
||||
msg: "Fight heros updated successfully"
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: -1,
|
||||
msg: `Update fight heros fail`
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Update fight heros error:", error);
|
||||
return {
|
||||
code: -5,
|
||||
msg: `Update fight heros error: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前出战英雄列表(不包含空位)
|
||||
* @param {Object} db 数据库实例
|
||||
* @param {string} openid 用户openid
|
||||
* @returns {Object} 操作结果
|
||||
*/
|
||||
async function getActiveFightHeros(db, openid) {
|
||||
try {
|
||||
let user = await getOrCreaterUser(db, openid);
|
||||
if (!user) {
|
||||
return {
|
||||
code: -4,
|
||||
msg: "User not found"
|
||||
};
|
||||
}
|
||||
|
||||
const activeHeros = [];
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const heroId = user.fight_heros[i];
|
||||
if (heroId && heroId > 0) {
|
||||
activeHeros.push({
|
||||
position: i,
|
||||
hero_id: heroId,
|
||||
hero_data: user.heros[heroId] || null
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
active_heros: activeHeros,
|
||||
total_count: activeHeros.length
|
||||
},
|
||||
msg: "Active fight heros retrieved successfully"
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Get active fight heros error:", error);
|
||||
return {
|
||||
code: -5,
|
||||
msg: `Get active fight heros error: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 交换两个出战位置的英雄
|
||||
* @param {Object} db 数据库实例
|
||||
* @param {string} openid 用户openid
|
||||
* @param {number} position1 位置1 (0-4)
|
||||
* @param {number} position2 位置2 (0-4)
|
||||
* @returns {Object} 操作结果
|
||||
*/
|
||||
async function swapFightHeros(db, openid, position1, position2) {
|
||||
try {
|
||||
const _ = db.command;
|
||||
let user = await getOrCreaterUser(db, openid);
|
||||
if (!user) {
|
||||
return {
|
||||
code: -4,
|
||||
msg: "User not found"
|
||||
};
|
||||
}
|
||||
|
||||
// 验证位置参数
|
||||
if (position1 < 0 || position1 > 4 || position2 < 0 || position2 > 4) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: "Invalid positions, must be 0-4"
|
||||
};
|
||||
}
|
||||
|
||||
if (position1 === position2) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: "Cannot swap same position"
|
||||
};
|
||||
}
|
||||
|
||||
const hero1 = user.fight_heros[position1];
|
||||
const hero2 = user.fight_heros[position2];
|
||||
|
||||
let updateRes = await db.collection(user_db_name).doc(user._id).update({
|
||||
data: {
|
||||
[`fight_heros.${position1}`]: _.set(hero2),
|
||||
[`fight_heros.${position2}`]: _.set(hero1),
|
||||
last_save_time: _.set(Date.now())
|
||||
}
|
||||
});
|
||||
|
||||
if (updateRes?.stats?.updated >= 1) {
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
position1: position1,
|
||||
position2: position2,
|
||||
hero1_moved_to: hero1,
|
||||
hero2_moved_to: hero2
|
||||
},
|
||||
msg: `Fight heros swapped successfully`
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: -1,
|
||||
msg: `Swap fight heros fail`
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Swap fight heros error:", error);
|
||||
return {
|
||||
code: -5,
|
||||
msg: `Swap fight heros error: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置出战英雄配置
|
||||
* @param {Object} db 数据库实例
|
||||
* @param {string} openid 用户openid
|
||||
* @returns {Object} 操作结果
|
||||
*/
|
||||
async function resetFightHeros(db, openid) {
|
||||
try {
|
||||
const _ = db.command;
|
||||
const { getNewUserInitData } = require('../user_init_data');
|
||||
let user = await getOrCreaterUser(db, openid);
|
||||
if (!user) {
|
||||
return {
|
||||
code: -4,
|
||||
msg: "User not found"
|
||||
};
|
||||
}
|
||||
|
||||
const defaultData = getNewUserInitData();
|
||||
|
||||
let resetRes = await db.collection(user_db_name).doc(user._id).update({
|
||||
data: {
|
||||
fight_heros: _.set(defaultData.fight_heros),
|
||||
last_save_time: _.set(Date.now()),
|
||||
reset_time: _.set(Date.now())
|
||||
}
|
||||
});
|
||||
|
||||
if (resetRes?.stats?.updated >= 1) {
|
||||
return {
|
||||
code: 200,
|
||||
data: defaultData.fight_heros,
|
||||
msg: "Fight heros reset successfully"
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: -1,
|
||||
msg: `Reset fail, ${JSON.stringify(resetRes)}`
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Reset fight heros error:", error);
|
||||
return {
|
||||
code: -5,
|
||||
msg: `Reset fight heros error: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFightHeros,
|
||||
setFightHero,
|
||||
updateFightHeros,
|
||||
getActiveFightHeros,
|
||||
swapFightHeros,
|
||||
resetFightHeros
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user