Example #1
0
def add_spell_to_db(db_cursor, spell):
    existing_spell_flag = False

    parsed_spell = parse_spell(spell)

    # First, test if spell already exists.
    # Spit out error and exit if it does.
    # This may be subject to change for now
    db_cursor.execute("SELECT id FROM spell WHERE name = ?", (spell['Name'], ))
    matched_id = db_cursor.fetchone()
    if matched_id:
        existing_spell_flag = True
        print "%sError: Spell already exists: Spell with matching name found. Please resolve in import file." \
          % (colorz.RED, )
        print "Found spell:"
        full_spell_description(db_cursor, matched_id[0])

    # If spell doesn't exist, let's put it in.
    db_cursor.execute("SELECT spell_id FROM alt_spell WHERE name = ?",
                      (spell['Name'], ))
    matched_id = db_cursor.fetchone()
    if matched_id:
        existing_spell_flag = True
        print "%sError: Spell already exists: Spell with matching name found. Please resolve in import file." \
          % (colorz.RED, )
        print "Found spell:%s" % colorz.GREY
        print "    %s" % spell['Name']
        db_cursor.execute("SELECT name FROM spell WHERE id = ?",
                          (matched_id[0]))
        alt_spell_name = db_cursor.fetchone()[0]
        print "See \"%s\"" % alt_spell_name

    if existing_spell_flag:
        print "%sSpell to be imported:%s" % (colorz.RED, colorz.GREY)
        print "\n".join(spell)
        return

    if "alt_spell_name" in parsed_spell:
        insert_alt_spell_into_db(db_cursor, parsed_spell)
    else:
        insert_spell_into_db(db_cursor, parsed_spell)
def add_spell_to_db(db_cursor, spell):
  existing_spell_flag = False

  parsed_spell = parse_spell(spell)

  # First, test if spell already exists.
  # Spit out error and exit if it does.
  # This may be subject to change for now
  db_cursor.execute("SELECT id FROM spell WHERE name = ?", (spell['Name'], ))
  matched_id = db_cursor.fetchone()
  if matched_id:
    existing_spell_flag = True
    print "%sError: Spell already exists: Spell with matching name found. Please resolve in import file." \
      % (colorz.RED, )
    print "Found spell:"
    full_spell_description(db_cursor, matched_id[0])

  # If spell doesn't exist, let's put it in.
  db_cursor.execute("SELECT spell_id FROM alt_spell WHERE name = ?", (spell['Name'],))
  matched_id = db_cursor.fetchone()
  if matched_id:
    existing_spell_flag = True
    print "%sError: Spell already exists: Spell with matching name found. Please resolve in import file." \
      % (colorz.RED, )
    print "Found spell:%s" % colorz.GREY
    print "    %s" % spell['Name']
    db_cursor.execute("SELECT name FROM spell WHERE id = ?", (matched_id[0]))
    alt_spell_name =  db_cursor.fetchone()[0]
    print "See \"%s\"" % alt_spell_name

  if existing_spell_flag:
    print "%sSpell to be imported:%s" % (colorz.RED, colorz.GREY)
    print "\n".join(spell)
    return

  if "alt_spell_name" in parsed_spell:
    insert_alt_spell_into_db(db_cursor, parsed_spell)
  else:
    insert_spell_into_db(db_cursor, parsed_spell)
Example #3
0
def random_spell(db_cursor, choice_weights, spell_level, spell_type):
    frequency_weights = choice_weights['frequency_weights']
    general_class_weights = choice_weights['general_class_weights']
    domain_class_weights = choice_weights['domain_class_weights']
    domain_feat_weights = choice_weights['domain_feat_weights']

    print "%sPicking random spell....%s" % (colorz.PURPLE, colorz.ENDC)

    #################################################
    # First select either Common, Uncommon, or Rare #
    #################################################

    select_freq = weighted_choice(frequency_weights)

    #################################################
    # Next, select class in that frequency category #
    # base on whether arcane or divine.             #
    #################################################

    if spell_type == 'arcane':
        db_cursor.execute(
            "SELECT id, more_common FROM class \
                           WHERE frequency = ? AND arcane = 1",
            (select_freq, ))
    else:
        db_cursor.execute(
            "SELECT id, more_common FROM class \
                           WHERE frequency = ? AND divine = 1",
            (select_freq, ))
    class_weights = {}
    for returned_class in db_cursor.fetchall():
        if returned_class[1] == 0:
            class_weights[returned_class[0]] = general_class_weights['regular']
        else:
            class_weights[
                returned_class[0]] = general_class_weights['more_common']
    select_class = weighted_choice(class_weights)
    db_cursor.execute("SELECT name FROM class WHERE id = ?", (select_class, ))
    class_name = db_cursor.fetchone()[0]
    print "%s%s Spell (%s)%s" % (colorz.YELLOW, class_name, spell_type,
                                 colorz.ENDC)

    #################################################
    # Then, select if normal spells or domain/feats #
    #################################################

    select_class_domain = weighted_choice(domain_class_weights)

    ###################################################
    # Finally, select the spell. Depending on whether #
    # it's a class spell or domain/feat spell         #
    ###################################################

    if select_class_domain == 'class':
        db_cursor.execute(
            "SELECT spell_id FROM spell_class\
                           WHERE class_id = ? AND level = ?",
            (select_class, spell_level))
        class_spells = db_cursor.fetchall()
        select_spell = random.choice(class_spells)[0]
    else:
        ######################################################
        # Class specific domains/feats should be more likely #
        # to show than general class domain/feats.           #
        ######################################################

        db_cursor.execute(
            "SELECT domain_feat_id FROM class_domain_feat\
                           WHERE class_id = ?", (select_class, ))
        any_domain_feat = db_cursor.fetchall()
        db_cursor.execute("SELECT domain_feat_id FROM class_domain_feat\
                           WHERE class_id = 0")
        class_domain_feat = db_cursor.fetchall()

        if weighted_choice(
                domain_feat_weights) == 'class' and class_domain_feat:
            select_domain_feat = random.choice(class_domain_feat)[0]
        else:
            select_domain_feat = random.choice(any_domain_feat)[0]

        ############################################
        # Now pick the domain/feat spell at random #
        ############################################

        db_cursor.execute(
            "SELECT spell_id FROM spell_domain_feat\
                           WHERE domain_feat_id = ? AND level = ?",
            (select_domain_feat, spell_level))
        domain_feat_spells = db_cursor.fetchall()
        select_spell = random.choice(domain_feat_spells)[0]

    ####################################
    # Print out full spell description #
    ####################################

    full_spell_description(db_cursor, select_spell)
def random_spell(db_cursor, choice_weights, spell_level, spell_type):
    frequency_weights = choice_weights['frequency_weights']
    general_class_weights = choice_weights['general_class_weights']
    domain_class_weights = choice_weights['domain_class_weights']
    domain_feat_weights = choice_weights['domain_feat_weights']

    print "%sPicking random spell....%s" % (colorz.PURPLE, colorz.ENDC)

    #################################################
    # First select either Common, Uncommon, or Rare #
    #################################################

    select_freq = weighted_choice(frequency_weights)

    #################################################
    # Next, select class in that frequency category #
    # base on whether arcane or divine.             #
    #################################################

    if spell_type == 'arcane':
        db_cursor.execute("SELECT id, more_common FROM class \
                           WHERE frequency = ? AND arcane = 1", (select_freq, ))
    else:
        db_cursor.execute("SELECT id, more_common FROM class \
                           WHERE frequency = ? AND divine = 1", (select_freq, ))
    class_weights = {}
    for returned_class in db_cursor.fetchall():
        if returned_class[1] == 0:
            class_weights[returned_class[0]] = general_class_weights['regular']
        else:
            class_weights[returned_class[0]] = general_class_weights['more_common']
    select_class = weighted_choice(class_weights)
    db_cursor.execute("SELECT name FROM class WHERE id = ?", (select_class, ))
    class_name = db_cursor.fetchone()[0]
    print "%s%s Spell (%s)%s" % (colorz.YELLOW, class_name, spell_type, colorz.ENDC)

    #################################################
    # Then, select if normal spells or domain/feats #
    #################################################

    select_class_domain = weighted_choice(domain_class_weights)

    ###################################################
    # Finally, select the spell. Depending on whether #
    # it's a class spell or domain/feat spell         #
    ###################################################

    if select_class_domain == 'class':
        db_cursor.execute("SELECT spell_id FROM spell_class\
                           WHERE class_id = ? AND level = ?", (select_class, spell_level))
        class_spells = db_cursor.fetchall()
        select_spell = random.choice(class_spells)[0]
    else:
        ######################################################
        # Class specific domains/feats should be more likely #
        # to show than general class domain/feats.           #
        ######################################################

        db_cursor.execute("SELECT domain_feat_id FROM class_domain_feat\
                           WHERE class_id = ?", (select_class, ))
        any_domain_feat = db_cursor.fetchall()
        db_cursor.execute("SELECT domain_feat_id FROM class_domain_feat\
                           WHERE class_id = 0")
        class_domain_feat = db_cursor.fetchall()

        if weighted_choice(domain_feat_weights) == 'class' and class_domain_feat:
            select_domain_feat = random.choice(class_domain_feat)[0]
        else:
            select_domain_feat = random.choice(any_domain_feat)[0]

        ############################################
        # Now pick the domain/feat spell at random #
        ############################################

        db_cursor.execute("SELECT spell_id FROM spell_domain_feat\
                           WHERE domain_feat_id = ? AND level = ?",
                          (select_domain_feat, spell_level))
        domain_feat_spells = db_cursor.fetchall()
        select_spell = random.choice(domain_feat_spells)[0]

    ####################################
    # Print out full spell description #
    ####################################

    full_spell_description(db_cursor, select_spell)