diff --git a/game-server/app/servers/role/handler/roleHandler.ts b/game-server/app/servers/role/handler/roleHandler.ts index d78f6eb0e..fc151bf34 100644 --- a/game-server/app/servers/role/handler/roleHandler.ts +++ b/game-server/app/servers/role/handler/roleHandler.ts @@ -1,10 +1,10 @@ import { STATUS } from '../../../consts/statusCode'; -import { RoleModel } from './../../../db/Role'; +import Role, { RoleModel } from './../../../db/Role'; import { HeroModel } from '../../../db/Hero'; import { resResult, decodeIdCntArrayStr, parseGoodStr } from '../../../pubUtils/util'; import { Application, BackendSession, pinus } from 'pinus'; -import { handleCost } from '../../../services/rewardService'; -import { getTitle, getTeraph, gameData, getScollByStar, getFriendLvByExp } from '../../../pubUtils/data'; +import { handleCost, addItems, createHeroes } from '../../../services/rewardService'; +import { getTitle, getTeraph, gameData, getScollByStar, getFriendLvByExp, getHeroExpByLv } from '../../../pubUtils/data'; import { SCHOOL, SCROLL, EXTERIOR } from '../../../pubUtils/dicParam'; import { getAtrrNameById } from '../../../consts/constModules/abilityConst' import { findIndex } from 'underscore'; @@ -12,12 +12,13 @@ import { SclResultInter, SclPosInter } from '../../../pubUtils/interface'; import { SchoolModel } from '../../../db/School'; import { checkTeraphMaterialEnough } from '../../../services/roleService' import { calPlayerCeAndSave, calAllHeroCe } from '../../../services/playerCeService'; -import { HERO_SYSTEM_TYPE, LINEUP_NUM, ROLE_SELECT, REDIS_KEY, TASK_TYPE } from '../../../consts'; +import { HERO_SYSTEM_TYPE, LINEUP_NUM, ROLE_SELECT, REDIS_KEY, TASK_TYPE, DEFAULT_HEROES, DEFAULT_HERO_LV, DEFAULT_ITEMS, DEFAULT_EQUIPS, DEFAULT_GOLD, DEFAULT_COIN } from '../../../consts'; import { checkBattleHeroesByHid } from '../../../services/normalBattleService'; import { Rank } from '../../../services/rankService'; import { updateUserInfo } from '../../../services/redisService'; import { checkTaskWithHero, checkTask, checkTaskWithArgs } from '../../../services/taskService'; import { accomplishTask } from '../../../pubUtils/taskUtil'; +import { getGoldObject, getCoinObject } from '../../../pubUtils/itemUtils'; export default function (app: Application) { return new RoleHandler(app); @@ -57,22 +58,35 @@ export class RoleHandler { await HeroModel.createHero(heroInfo); } - async initRole(msg: { content: string, target: string }, session: BackendSession) { + async initRole(msg: { roleName: string }, session: BackendSession) { + let roleId = session.get('roleId'); - let roleName = session.get('roleName'); - console.log('role in initRole: ', roleId, roleName); + let serverId = session.get('serverId'); + let sid: string = session.get('sid'); + let funcs: number[] = session.get('funcs'); + + let { roleName } = msg; + let role = await RoleModel.findByRoleId(roleId, 'roleName hasInit'); + if(role.hasInit) return resResult(STATUS.ROLE_HAS_INIT); - let heros = await HeroModel.findByRole(roleId); - if (!heros || heros.length === 0) { - await this.initHeros(roleId, roleName); + let heroInfos = []; + for (let hid of DEFAULT_HEROES) { + heroInfos.push({ + hid, lv: DEFAULT_HERO_LV, exp: getHeroExpByLv(DEFAULT_HERO_LV - 1) || 0 + }); } + let heroes = await createHeroes(roleId, roleName, sid, serverId, funcs, heroInfos); + session.set('roleName', roleName); + session.push('roleName', () => { }); - // let equips = await EquipModel.findbyRole(roleId); - // if (!equips || equips.length === 0) { - await this.initEquips(roleId, roleName); - // } + let items = [].concat(DEFAULT_ITEMS, DEFAULT_EQUIPS, [getGoldObject(DEFAULT_GOLD)], [getCoinObject(DEFAULT_COIN)]); + await addItems(roleId, roleName, sid, items); - console.log('initRole finish'); + role = await RoleModel.updateRoleInfo(roleId, { hasInit: true, roleName }) + + return resResult(STATUS.SUCCESS, { + roleId, roleName, heroes + }) } async getRoleInfo(msg: { targetRoleId: string }, session: BackendSession) { diff --git a/shared/consts/statusCode.ts b/shared/consts/statusCode.ts index 7f342bd5e..54cd3b816 100644 --- a/shared/consts/statusCode.ts +++ b/shared/consts/statusCode.ts @@ -25,6 +25,7 @@ export const STATUS = { AUTHEN_FAIL: { code: 10013, simStr: '实名失败' }, TEL_LEN_ERR: { code: 10014, simStr: '手机长度错误' }, NAME_HAS_USED: { code: 10015, simStr: '该名字已使用' }, + ROLE_HAS_INIT: { code: 10016, simStr: '该角色已初始过' }, // 战斗相关状态 20000 - 29999 // 战斗通用 20000 - 20099 BATTLE_MISS_INFO: { code: 20001, simStr: '缺少关卡信息' }, diff --git a/shared/db/Role.ts b/shared/db/Role.ts index 588654ae0..98c37aabf 100644 --- a/shared/db/Role.ts +++ b/shared/db/Role.ts @@ -120,6 +120,8 @@ export default class Role extends BaseModel { blocked: boolean; // 是否屏蔽 @prop({ required: true }) code: string; // 邀请码 + @prop({ required: true, default: false }) + hasInit: boolean; // 是否初始化过 @prop({ required: true, type: Number }) showLineup: number[]; // 展示阵容 diff --git a/shared/pubUtils/itemUtils.ts b/shared/pubUtils/itemUtils.ts index 1ca041b64..849afd6e3 100644 --- a/shared/pubUtils/itemUtils.ts +++ b/shared/pubUtils/itemUtils.ts @@ -78,6 +78,14 @@ export function getGoldObject(count: number) { return { id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count }; } +/** + * @description 获取金币物品 { id, count } + * @param count 元宝数量 + */ +export function getCoinObject(count: number) { + return { id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN), count }; +} + /** * @description 获取友情点物品 { id, count } * @param count 友情点数量 diff --git a/web-server/app/controller/account.ts b/web-server/app/controller/account.ts index 034881af9..62d1340ef 100644 --- a/web-server/app/controller/account.ts +++ b/web-server/app/controller/account.ts @@ -46,8 +46,8 @@ export default class AccountController extends Controller { public async createRole() { const { ctx } = this; - const { serverId, roleName } = ctx.request.body; - ctx.body = await ctx.service.auth.createRole(serverId, roleName); + const { serverId } = ctx.request.body; + ctx.body = await ctx.service.auth.createRole(serverId); } public async bind() { diff --git a/web-server/app/service/Auth.ts b/web-server/app/service/Auth.ts index 584ac5819..00b892f95 100644 --- a/web-server/app/service/Auth.ts +++ b/web-server/app/service/Auth.ts @@ -1,20 +1,16 @@ -import { COUNTER, DEFAULT_LV, DEFAULT_ITEMS, ITID, DEFAULT_GOLD, DEFAULT_HERO_LV, DEFAULT_EQUIPS, DEFAULT_COIN, ADULT_AGE, GUEST_MAX_TIME, TASK_TYPE } from '@consts'; -import { DEFAULT_HEROES } from '@consts'; -import { HeroModel } from '@db/Hero'; +import { COUNTER, DEFAULT_LV, ADULT_AGE, GUEST_MAX_TIME, TASK_TYPE } from '@consts'; import { RoleModel } from '@db/Role'; import { UserModel, UserType } from '@db/User'; import { STATUS, GET_SMS_TYPE, ADDICTION_PREVENTION_CODE } from '@consts'; import { smsModel } from '@db/Sms'; import { Service } from 'egg'; import Counter from '@db/Counter'; -import { getHeroInfoById } from '../pubUtils/gamedata'; -import { getExpByLv, getHeroExpByLv, gameData } from '../pubUtils/data'; +import { getExpByLv } from '../pubUtils/data'; import { isString } from 'underscore'; import { getAge } from '../pubUtils/timeUtil'; import { shouldRefresh, resResult } from '../pubUtils/util'; import { authenticate } from '../pubUtils/httpUtil'; -import { createHeroes } from '../pubUtils/itemUtils'; import { checkTask, getCreateUserFuncs } from 'app/pubUtils/taskUtil'; /** @@ -289,7 +285,7 @@ export default class Auth extends Service { return ctx.service.utils.resResult(STATUS.ROLE_NOT_FOUND); } - public async createRole(serverId: number, roleName: string) { + public async createRole(serverId: number) { console.log('enter Auth createRole'); const ctx = this.ctx; const { uid } = ctx; @@ -297,44 +293,9 @@ export default class Auth extends Service { const code = ctx.service.utils.genCode(6); const seqId = await Counter.getNewCounter(COUNTER.ROLE) || -1; - let checkName = await RoleModel.checkName(roleName, serverId); - if(checkName) return ctx.service.utils.resResult(STATUS.NAME_HAS_USED); - - const role = await RoleModel.createRole(uid, serverId, { roleId, code, roleName, seqId, lv: DEFAULT_LV, exp: (getExpByLv(DEFAULT_LV - 1) || { sum: 0 }).sum || 0 }); + const role = await RoleModel.createRole(uid, serverId, { roleId, code, roleName: roleId, seqId, lv: DEFAULT_LV, exp: (getExpByLv(DEFAULT_LV - 1) || { sum: 0 }).sum || 0 }); if (role) { let funcs = await getCreateUserFuncs(role.funcs, role.lv); - let heroInfos = []; - for (let hid of DEFAULT_HEROES) { - let hero = await HeroModel.findByHidAndRole(hid, roleId); - if (hero) continue - - let dicHero = getHeroInfoById(hid); - if (!dicHero) break; - - heroInfos.push({ - hid, lv: DEFAULT_HERO_LV, exp: getHeroExpByLv(DEFAULT_HERO_LV - 1) || 0 - }); - } - await createHeroes(roleId, role.roleName, serverId, heroInfos, funcs); - - for (let { id, count } of DEFAULT_ITEMS) { - let dicGoods = gameData.goods.get(id); - if (!dicGoods) continue; - let itidObj = ITID.get(dicGoods.itid); - if (!itidObj) continue; - await ctx.service.utils.addBags(roleId, role.roleName, { id, itemName: dicGoods.name, count, type: itidObj.type, hid: 0 }); - } - - for (let { id, count } of DEFAULT_EQUIPS) { - for (let j = 0; j < count; j++) { - let dicGoods = gameData.goods.get(id); - if (!dicGoods) continue; - - await ctx.service.utils.addEquips(roleId, role.roleName, { id, ...dicGoods, hid: 0 }); - } - } - await RoleModel.addGoldFree(roleId, DEFAULT_GOLD); - await RoleModel.addCoin(roleId, DEFAULT_COIN); // 任务 await checkTask(roleId, TASK_TYPE.ROLE_LV, role.lv, false, {}, funcs);