import { HeroModel } from '../db/Hero'; import { ItemModel } from '../db/Item'; import { EquipModel, RandSe, Holes } from './../db/Equip'; import { BagInter, EquipInter } from './interface'; import { gameData } from './data'; import { RANDOM_SE_COUNT, FIX_ATTRIBUTES_RAN, ITID } from '../consts'; import { getRandomByLen } from './util'; const _ = require('underscore'); export async function addSkins(roleId: string, id: number) { let skinInfo = gameData.fashion.get(id); if (!skinInfo) return false; let hero = await HeroModel.findByHidAndRole(skinInfo.actorId, roleId, false); if (!hero) return false; if (!!_.findWhere(hero.skins, { id })) return false; hero.skins.push({ id, enable: false }); await HeroModel.updateHeroInfo(roleId, hero.hid, hero); return { skins: hero.skins, hid: hero.hid }; } export async function addBags(roleId: string, roleName: string, data: BagInter) { let { id, count, itemName, type, hid } = data; let item = await ItemModel.increaseItem(roleId, id, count, { roleId, roleName, itemName, id, type, hid }); return { id: item.id, count: item.count }; } export async function addEquips(roleId: string, roleName: string, weapon: EquipInter) { let { id, name, quality, suitId, hole, randomEffect, itid, hid } = weapon; let {type} = ITID.get(itid); let randomNum = RANDOM_SE_COUNT.get(quality); let pool = randomEffect.map(cur => gameData.randomEffectPool.get(cur)); let chosen = new Array(); let randSe = new Array(); for(let i = 0; i < randomNum; i++) { pool = pool.filter(cur => !chosen.includes(cur.id)); let random = getRandomByLen(pool); if(!random) break; chosen.push(random.id); let rand = 0; if(random.id > 0) rand = Math.floor(Math.random() * (random.Max - random.Min) + random.Min); randSe.push({ id: i + 1, seid: random.id, rand, locked: false }); } let randRange = Math.floor(Math.random() * FIX_ATTRIBUTES_RAN * 2) - FIX_ATTRIBUTES_RAN; let holes = new Array(); for(let i = 0; i < hole; i++) { holes.push({id: i+1, isOpen: false, jewel: 0}) } const equip = await EquipModel.createEquip({roleId, roleName, id, name, quality, suitId, randRange, ePlaceId: type, randSe, holes, hid}); return equip }