class UserTask(object): run = True def __init__(self, username, password, openBoxType=[True, True, True, True], smeltEquipType=[True, True, True, False], useItemType=[True, True, True, True, True, True], sellItemType=[True, True, True, True, True, True], buyExp=True, log=None): self.username = username self.password = password self.openBoxType = openBoxType self.smeltEquipType = smeltEquipType self.useItemType = useItemType self.sellItemType = sellItemType if log is None or len(logfilePath) == 0: self.log = Logging(self.username) else: self.log = log self.buyExp = buyExp self.statistic = {"username": self.username} def init(self): self.user = User(self.username, self.password) self.user.login() self.attacker = Attacker(self.user, log=self.log) self.box = Box(self.user, self.openBoxType) self.equip = Equip(self.user, self.smeltEquipType) self.item = Item(self.user, self.useItemType, self.sellItemType) self.shop = Shop(self.user) self.growup = Growup(self.user) self.card = Card(self.user) def task(self): stage = self.attacker.autoAttack() self.statistic['stage'] = stage self.box.autoOpenBox() self.equip.autoSmelt() self.item.autoItemCommand() # if self.buyExp is True: # self.shop.autoBuy() self.growup.growup() # self.card.getCard() self.log.info('============= END USER TASK ================')
class Shop(object): __SHOP_PAGE = constants.DOMAIN + '/kf_fw_ig_shop.php' __SHOP_ITEM = {'101': '等级经验药丸', '102': '等级经验药丸(蛋)'} def __init__(self, user, log=None): self.safeid = user.safeid self.req = user.req if log is None: self.log = Logging(user.username) else: self.log = log def getCurMoney(self): res = self.req.get(self.__SHOP_PAGE) htmlTree = etree.HTML(res) kfbStr = (''.join( htmlTree.xpath( '//*[@id="alldiv"]/div[3]/div[1]/div[1]/a[2]/text()'))) return int(re.search('^(\d*)KFB', kfbStr).group(1)) def buy(self, id): payload = dict(buy=id, safeid=self.safeid) res = self.req.post(self.__SHOP_PAGE, data=payload) return res def autoBuy(self): self.log.info('--开始自动购买经验--') kfb = self.getCurMoney() self.log.info('当前KFB: ' + str(kfb)) while True: buyId = -1 used = 0 if kfb >= 10000: buyId = '102' used = 10000 elif kfb >= 5000: buyId = '101' used = 5000 else: break self.buy(buyId) self.log.info( str(kfb) + '\t' + self.__SHOP_ITEM[buyId] + '\tUSED' + str(used) + '\t' + str(kfb - used)) kfb = kfb - used self.log.info('--自动购买经验结束--') self.log.info()
class Growup(object): __GROWUP_PAGE = constants.DOMAIN + '/kf_growup.php' def __init__(self, user, log=None): self.user = user self.req = user.req if log is None: self.log = Logging(user.username) else: self.log = log def growup(self): self.user.reloadSafeid() self.safeid = self.user.safeid self.log.info('--开始领取登录奖励--') growupUrl = self.__GROWUP_PAGE + '?ok=3&safeid=' + self.safeid res = self.req.get(growupUrl) self.log.info('--领取登录奖励结束--') self.log.info()
class User: __LOGIN_PAGE = constants.DOMAIN + '/login.php' __INDEX_PAGE = constants.DOMAIN + '/' __SAFEID_PAGE = constants.DOMAIN + '/kf_fw_ig_index.php' __SM_PAGE = constants.DOMAIN + '/kf_growup.php' def __init__(self, username, password, log=None): self.loginStatus = False self._safeid = None self.username = username self.password = password self.req = KfReq() if log is None: self.log = Logging(username) else: self.log = log def login(self): self.log.info('--开始登录--') self.log.info('username:'******'password:'******'GBK'), pwpwd=self.password, submit='登录'.encode('GBK')) res = self.req.post(self.__LOGIN_PAGE, data=payload) self._safeid = None if '您已经顺利登' in res: self.log.info('登录成功') self.log.info('Cookies: ') self.log.info(str(self.req.cookies)) self.log.info('') self.loginStatus = True return True else: self.log.info('登录失败') self.loginStatus = False return False def getSafeid(self): self.log.info('--开始获取safeid--') res = self.req.get(self.__SAFEID_PAGE) htmlTree = etree.HTML(res) jsCode = htmlTree.xpath( '//*[@id="alldiv"]/div[3]/div[2]/script[2]/text()')[0] _safeid = re.search("\"safeid=(.*)\"", jsCode).group(1) self.log.info('成功获取safeid: ' + _safeid) return _safeid def reloadSafeid(self): self._safeid = self.getSafeid() def getSM(self): self.log.info('--开始获取神秘系数--') res = self.req.get(self.__SM_PAGE) htmlTree = etree.HTML(res) sm = ''.join( htmlTree.xpath( '//*[@id="alldiv"]/div[3]/div[2]/div[1]/div[1]/text()')) sm = re.search('.*神秘系数\s(\d*).*', sm).group(1) self.log.info('--成功获取神秘系数:' + sm + '--') return int(sm) @property def safeid(self, force=False): if self.loginStatus: if self._safeid == None or force: self._safeid = self.getSafeid() return self._safeid else: self.log.error('未登录账号')
class Box(object): __BOX_PAGE = constants.DOMAIN + '/kf_fw_ig_mybp.php' __BOX_OPEN_PAGE = constants.DOMAIN + '/kf_fw_ig_mybpdt.php' def __init__(self, user, boxType, log=None): self.safeid = user.safeid self.req = user.req self.boxType = boxType if log is None: self.log = Logging(user.username) else: self.log = log def getBoxQuantity(self): res = self.req.get(self.__BOX_PAGE) htmlTree = etree.HTML(res) print() return [ int(''.join(htmlTree.xpath('//*[@id="alldiv"]/div[3]/div[2]/div[3]/table/tr[2]/td[1]/span[2]/text()'))), int(''.join(htmlTree.xpath('//*[@id="alldiv"]/div[3]/div[2]/div[3]/table/tr[2]/td[2]/span[2]/text()'))), int(''.join(htmlTree.xpath('//*[@id="alldiv"]/div[3]/div[2]/div[3]/table/tr[2]/td[3]/span[2]/text()'))), int(''.join(htmlTree.xpath('//*[@id="alldiv"]/div[3]/div[2]/div[3]/table/tr[2]/td[4]/span[2]/text()'))) ] def openSingleBox(self, openBoxType): payload = dict( do=3, id=openBoxType, safeid=self.safeid ) openRes = self.req.post(self.__BOX_OPEN_PAGE, data=payload) return openRes def openBox(self): boxQuantity = self.getBoxQuantity() for i in range(0, 4): if self.boxType[i]: for n in range(0, boxQuantity[i]): openRes = self.openSingleBox(i+1) self.log.info(str(i+1)+':\t'+openRes) time.sleep(1) def autoOpenBox(self): self.log.info('--开始自动开盒--') self.log.info('获取盒子数:') boxQuantity = self.getBoxQuantity() self.log.info(' '.join(map(str, boxQuantity))) self.log.info('开始打开盒子') self.openBox() self.log.info('--自动开盒结束--') self.log.info()
class Card(object): __CARD_PAGE = constants.DOMAIN + '/kf_fw_ig_mycard.php' def __init__(self, user, log=None): self.safeid = user.safeid self.req = user.req self.user = user if log is None: self.log = Logging(user.username) else: self.log = log def getNowCard(self): self.log.info('--开始获取现有卡片信息--') res = self.req.get(self.__CARD_PAGE) cardHtml = etree.HTML(res) cardTable = cardHtml.xpath( '//*[@id="alldiv"]/div[3]/div[2]/div[3]/div[2]/table/tr') cardList = [] for card in cardTable: level = ''.join(card.xpath('td[1]/div/text()')) if len(level) == 0: continue level = re.search(".*等级上限\s(\d*).*", level).group(1) temp = ''.join(card.xpath('td[2]/text()')) skill = re.search('.*技能位:(\d*).*卡片品质:(\d*)', temp).group(1) quality = re.search('.*技能位:(\d*).*卡片品质:(\d*)', temp).group(2) cardId = ''.join(card.xpath('td[1]/div/a/@href')) cardId = re.search('.*cardid=(.*)&.*', cardId).group(1) cardList.append({ "level": int(level), "skill": int(skill), "quality": int(quality), "cardid": cardId }) self.log.info('--获取现有卡片信息结束--') return cardList def deleteCard(self, cardId): deleteUrl = self.__CARD_PAGE + '?card=del&safeid=' + self.safeid + '&cardid=' + cardId res = self.req.get(deleteUrl) def getCard(self): self.log.info('--开始领取卡片--') cardList = self.getNowCard() sm = self.user.getSM() if len(cardList) == 3: minCardId = None minCardVal = sys.maxsize hopeLevel = sm * 2 + 30 hopeSkill = 4 hopeQuality = 8 for card in cardList: val = int( (card['level'] / hopeLevel + card['skill'] / hopeLevel + card['quality'] / hopeQuality) * 1000) if val < minCardVal: minCardVal = val minCardId = card['cardid'] self.deleteCard(minCardId) growupUrl = self.__CARD_PAGE + '?card=new&safeid=' + self.safeid res = self.req.get(growupUrl) self.log.info('--领取卡片结束--') self.log.info()
class Attacker(object): __LOOT_PAGE = constants.DOMAIN + '/kf_fw_ig_index.php' __ATTACK_PAGE = constants.DOMAIN + '/kf_fw_ig_intel.php' def __init__(self, user, log=None): self.safeid = user.safeid self.req = user.req if log is None: self.log = Logging(user.username) else: self.log = log self.attackTime = None def checkOnlineBattleIsOver(self, htmlTree=None): if htmlTree is None: res = self.req.get(self.__LOOT_PAGE) htmlTree = etree.HTML(res) pk_log = ''.join(htmlTree.xpath('//*[@id="pk_text"]/li/text()')) attacked = (pk_log == '今日战斗已完成,请重置后再战。') return attacked def checkOffLineBattleIsOver(self): if self.attackTime is None: return None attacked = (datetime.datetime.now().day - self.attackTime.day) == 0 return attacked def checkAttacked(self): offLineBattleIsOver = self.checkOffLineBattleIsOver() self.log.info('检查线下争夺记录: ' + str(offLineBattleIsOver)) if offLineBattleIsOver is None: onLineBattleIsOver = self.checkOnlineBattleIsOver() self.log.info('检查线上争夺记录: ' + str(onLineBattleIsOver)) return onLineBattleIsOver else: return offLineBattleIsOver def getServerStatus(self, htmlTree): status = ''.join( htmlTree.xpath( '//*[@id="alldiv"]/div[3]/div[2]/div[3]/table/tr[1]/td/span/text()' )) return status[-2:] def attack(self): res = self.req.post(self.__ATTACK_PAGE, data=dict(safeid=self.safeid)) return res def attackToEnd(self): stage = '0' while True: self.log.info('查询服务器状态') lootPage = self.req.get(self.__LOOT_PAGE) lootPageTree = etree.HTML(lootPage) serverStatus = self.getServerStatus(lootPageTree) self.log.info(serverStatus) if serverStatus in ['空闲', '正常']: self.log.info('开始争夺') attackRes = self.attack() #self.log.info(attackRes) if attackRes == 'no': break m = re.finditer(r'<li((?!</li>).)*</li>', attackRes) for s in m: tmp = ''.join(etree.HTML(s.group()).xpath('//text()')) if re.search('.*\s(\d*)\s层.*', tmp) != None: stage = re.search('.*\s(\d*)\s层.*', tmp).group(1) self.log.info(tmp) self.log.info() time.sleep(1) else: self.log.info('等待服务器状态变化...') time.sleep(60 * 5) return stage def autoAttack(self): self.log.info('--开始自动争夺--') self.log.info('检查是否已争夺') attacked = self.checkAttacked() stage = '0' if attacked: self.log.info('今日已进行争夺') else: stage = self.attackToEnd() self.log.info('--自动争夺结束--') self.log.info() self.attackTime = datetime.datetime.now() return stage
class Item(object): __ITEM_NAME_LIST = ['蕾米莉亚同人漫画','十六夜同人漫画','档案室钥匙','傲娇LOLI娇蛮音CD','消逝之药','整形优惠卷'] __ITEM_PAGE = constants.DOMAIN + '/kf_fw_ig_mybp.php' __ITEM_USE_SELL_PAGE = constants.DOMAIN + '/kf_fw_ig_mybpdt.php' def __init__(self, user, useType, sellType, log = None): self.safeid = user.safeid self.req = user.req self.useType = useType self.sellType = sellType if log is None: self.log = Logging(user.username) else: self.log = log def getItemTypeByName(self, name): for i in range(0, len(self.__ITEM_NAME_LIST)): if name == self.__ITEM_NAME_LIST[i]: return i def getItemList(self): res = self.req.get(self.__ITEM_PAGE) htmlTree = etree.HTML(res) itemIds = htmlTree.xpath('//*[@id="alldiv"]/div[3]/div[2]/div[7]/table/tr/@id') itemNames = htmlTree.xpath('//*[@id="alldiv"]/div[3]/div[2]/div[7]/table/tr[(@id)]/td[3]/text()') itemList = [] for i in range(0, len(itemIds)): itemList.append(dict(id=re.search('(\d*)$', itemIds[i]).group(1), name=itemNames[i], type=self.getItemTypeByName(itemNames[i]))) return itemList def useItem(self, id): payload = dict( do=1, id=id, safeid=self.safeid ) useRes = self.req.post(self.__ITEM_USE_SELL_PAGE, data=payload) return useRes def sellItem(self, id): payload = dict( do=2, id=id, safeid=self.safeid ) sellRes = self.req.post(self.__ITEM_USE_SELL_PAGE, data=payload) return sellRes def autoItemCommand(self): self.log.info('--开始自动处理道具--') itemList = self.getItemList() self.log.info('获取道具列表,共有 ' + str(len(itemList))) for item in itemList: if self.useType[item['type']]: ret = self.useItem(item['id']) self.log.info(ret) self.log.info(str(item['name']) + '\t' + item['id'] + '\t USE') elif self.sellType[item['type']]: ret = self.sellItem(item['id']) self.log.info(ret) self.log.info(str(item['name']) + '\t' + item['id'] + '\t SELL') time.sleep(2) self.log.info('--自动处理道具结束--') self.log.info()
class Equip(object): __EQUIP_PAGE = constants.DOMAIN + '/kf_fw_ig_mybp.php' __EQUIP_SMELT_PAGE = constants.DOMAIN + '/kf_fw_ig_mybpdt.php' __EQUIP_NAME = ['普通', '幸运', '稀有', '传奇'] abilityAbs = { '增加攻击力': 'ATK', '增加暴击伤害': 'CRT', '增加技能伤害': 'SKL', '穿透对方意志': 'BRC', '生命夺取': 'LCH', '增加速度': 'SPD', '攻击': 'ATK', '暴击': 'CRT', '技能': 'SKL', '穿透': 'BRC', '吸血': 'LCH', '速度': 'SPD', '被攻击回血100+': 'HEL', '获得无护甲魔法盾500+': 'SLD', '每减少5%生命值获得额外意志': 'AMR', '反弹对方实际伤害15%+': 'RFL', '减少来自暴击的伤害10%+': 'CRD', '减少来自技能的伤害10%+': 'SRD', '回血': 'HEL', '护盾': 'SLD', '加防': 'AMR', '反伤': 'RFL', '暴减': 'CRD', '技减': 'SRD' } def __init__(self, user, smeltType, log = None): self.safeid = user.safeid self.smeltType = smeltType self.req = user.req if log is None: self.log = Logging(user.username) else: self.log = log def getEquipList(self): equipListRes = self.req.get(self.__EQUIP_PAGE) htmlTree = etree.HTML(equipListRes) equipTable = htmlTree.xpath('//*[@id="alldiv"]/div[3]/div[2]/div[5]/table/tr') equipList = [] for equip in equipTable: equipId = ''.join(equip.xpath('td[1]/@id')) equipId = re.search('(\d*)$', equipId).group(1) if len(equipId) == 0: continue equidName = ''.join(equip.xpath('td[3]/span[1]/text()')) info = equip.xpath('td[3]/text()') # 是否存在神秘 subAbList = [] if info[1] == '。': smAbility = ''.join(equip.xpath('td[3]/span[2]/text()')) mainAbility = info[2] subAbList = info[3:] else: smAbility = '' mainAbility = info[1] subAbList = info[2:] subAbility = ''.join(subAbList) mainAbility = mainAbility.replace('主属性:', '').split('。') subAbility = subAbility.replace('从属性:', '').split('。') mainAbilityAb = [] for ability in mainAbility: if len(ability) == 0: continue ab = re.search('(.*)\(.*\)', ability).group(1) mainAbilityAb.append(self.abilityAbs[ab]) subAbilityAb = [] for ability in subAbility: if len(ability) == 0: continue ab = re.search('([^(]*)\(.*\)', ability).group(1) subAbilityAb.append(self.abilityAbs[ab]) subMissCount = 0 for ab in subAbilityAb: if ab not in mainAbilityAb: subMissCount = subMissCount + 1 equipList.append({ "id": equipId, "name": equidName, "smAbility": smAbility, "hasSmAbility": smAbility != '', "mainAbility": mainAbilityAb, "subAbility": subAbilityAb, "subMiss": subMissCount }) # equipIds = htmlTree.xpath('//*[@id="alldiv"]/div[4]/div[2]/div[5]/table/tr/td[1]/@id') # equipNames = htmlTree.xpath('//*[@id="alldiv"]/div[4]/div[2]/div[5]/table/tr/td[3]/span[1]/text()') # equipList = [] # for i in range(0, len(equipIds)): # equipList.append(dict(id=re.search('(\d*)$', equipIds[i]).group(1), name=equipNames[i])) return equipList def genMatchRegex(self, matchType=None): if matchType is None: matchType=self.smeltType regexStr = '^(' for i in range(0, len(matchType)): if matchType[i]: regexStr += (self.__EQUIP_NAME[i] + '|') if len(regexStr) != 2: regexStr = regexStr[0: len(regexStr)-1] regexStr += ')' return regexStr def smeltEquip(self, id): payload = dict( do=5, id=id, safeid=self.safeid ) smeltRes = self.req.post(self.__EQUIP_SMELT_PAGE, data=payload) return smeltRes def autoSmelt(self): self.log.info('--开始自动熔炼--') matchRegex = self.genMatchRegex() while True: equipList = self.getEquipList() beforeSize = len(equipList) print(equipList) for equip in equipList: if equip['hasSmAbility']: continue if re.match(matchRegex, equip['name']) and equip['subMiss'] >= 2: self.smeltEquip(equip['id']) equipList.remove(equip) self.log.info('熔炼:\t' + equip['name'] + '\t' + equip['id']) if beforeSize == len(equipList): break self.log.info('--自动熔炼结束--') self.log.info()