示例#1
0
def processWeapon(j, context, is_sub, dry_run):
    status_map = j.get(u'subStatusMap' if is_sub else  u'statusMap', {}) or {}
    weapon_name = j.get(u'subWeaponName' if is_sub else u'weaponName', u'').strip()
    level = j.get(u'subWeaponLevel' if is_sub else u'weaponLevel', 0)
    if not status_map or not weapon_name or not level:
        print u'Weapon %s is missing critical params.' % weapon_name
        return

    page = PageEditor(context, weapon_name)
    body_pre = u''
    body_json = {}
    body_post = u''
    if page.isStandardPage():
        match = RE_WEAPON_PAGE.match(page.get_body())
        if match:
            body_pre = match.group(u'pre')
            body_post = match.group(u'post')
            try:
                body_json = json.loads(match.group(u'body'))
            except Error:
                pass

    diff_dst_json = statusMapToAttrs(status_map)
    if not is_sub and j.get(u'subStatusMap', {}):
        diff_dst_json[u'サブウェポン'] = {
            u'名称': j[u'subWeaponName'].strip(),
            u'レベル': int(j[u'subWeaponLevel']),
        }

    if not body_json:
        body_pre = WEAPON_PAGE_DEFAULT_BODY_PRE
        body_post = WEAPON_PAGE_DEFAULT_BODY_POST
        body_json = {
            u'名称': weapon_name,
            u'レベル': {},
            u'弾種': j.get(u'subBulletTypeName' if is_sub else u'bulletTypeName', u''),
        }

    levels_json = body_json.get(u'レベル', {})
    if not levels_json:
        levels_json = {}
        body_json[u'レベル'] = levels_json

    dst_json = levels_json.get(u'%d' % level, {})
    if not dst_json:
        dst_json = {}
        levels_json[u'%d' % level] = dst_json

    dst_json.update(diff_dst_json)
    # clenup legacy attrs
    if u'爆発半径' in dst_json:
        del dst_json[u'爆発半径']
    if u'発射準備時間' in dst_json:
        del dst_json[u'発射準備時間']

    output = (body_pre + u'\n'
              + json_printer.print_json(body_json, indent=2) + u'\n'
              + body_post)

    try:
        if not dry_run:
            page.saveText(output, page.current_rev())
            print u'Processed weapon %s correctly.' % weapon_name
        else:
            differ = difflib.Differ(charjunk=lambda c:c.isspace())
            l = map(lambda s: s.strip(), page.get_body().splitlines())
            r = map(lambda s: s.strip(), output.splitlines())   
            if l != r:
                d = u'\n'.join(filter(lambda s: s and s[0] in u'+-?', differ.compare(l, r)))
                print((u'#### Diff in %s ####' % weapon_name).encode('utf-8'))
                print(d.encode('utf-8'))
    except PageEditor.Unchanged:
        pass

    if not is_sub and j.get(u'subStatusMap', {}):
        processWeapon(j, context, True, dry_run)
示例#2
0
def processWeaponPack(j, context, dry_run):
    wp_name = j.get(u'weaponPackName', None)
    if not wp_name:
        return
    page = PageEditor(context, wp_name)
    body_pre = u''
    body_json = {}
    body_post = u''
    if page.isStandardPage():
        match = RE_WP_PAGE.match(page.get_body())
        if match:
            body_pre = match.group(u'pre')
            body_post = match.group(u'post')
            try:
                body_json = json.loads(match.group(u'body'))
            except Error:
                pass

    def getBriefWeaponJson(weapon):
        return {
            u'名称': weapon.get(u'weaponName').strip(),
            u'レベル': weapon.get(u'weaponLevel'),
        }

    w_map = j.get(u'weaponMap', {})
    json_diff = {
        u'コスト': j.get(u'cost', 0),
        u'耐久力': j.get(u'hitPoint', 0),
        u'格闘補正': j.get(u'grappleUp', 1.0),
        u'右手武器': getBriefWeaponJson(w_map.get(u'right', {})),
        u'左手武器': getBriefWeaponJson(w_map.get(u'left', {})),
        u'サイド武器': getBriefWeaponJson(w_map.get(u'side', {})),
        u'タンデム武器': getBriefWeaponJson(w_map.get(u'tandem', {})),
    }

    if not body_json:
        body_pre = WP_PAGE_DEFAULT_BODY_PRE
        body_json = {
            u'名称': wp_name,
            u'入手条件': u'',
            u'タイプ': u''
        }
        body_post = WP_PAGE_DEFAULT_BODY_POST

    body_json.update(json_diff)

    output = (body_pre + u'\n'
              + json_printer.print_json(body_json, indent=2) + u'\n'
              + body_post)

    try:
        if not dry_run:
            page.saveText(output, page.current_rev())
            print u'Processed %s correctly.' % wp_name
        else:
            differ = difflib.Differ()
            l = map(lambda s: s.strip(), page.get_body().splitlines())
            r = map(lambda s: s.strip(), output.splitlines())
            if l != r:
                d = u'\n'.join(filter(lambda s: s and s[0] in u'+-?', differ.compare(l, r)))
                print((u'#### Diff in %s ####' % wp_name).encode('utf-8'))
                print(d.encode('utf-8'))
    except PageEditor.Unchanged:
        pass