You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.8 KiB

  1. import click
  2. from flask.cli import AppGroup
  3. from ..models import db
  4. npc_cli = AppGroup('npc')
  5. @npc_cli.command('populate')
  6. def populate_npc_database():
  7. import csv
  8. from .models import (
  9. CharacterClass,
  10. ClassLevelProgression,
  11. EquipmentArmour,
  12. EquipmentRangedWeapon,
  13. EquipmentMeleeWeapon,
  14. Spell
  15. )
  16. def load_csv_data(file_name, cls):
  17. rows = []
  18. with open('acks/npc/data/{}'.format(file_name), newline='') as data:
  19. reader = csv.DictReader(data)
  20. for row in reader:
  21. rows.append(cls(**row))
  22. return rows
  23. # Character Classes
  24. db.session.bulk_save_objects(load_csv_data('default_classes.csv', CharacterClass))
  25. # Equipment Armour
  26. db.session.bulk_save_objects(load_csv_data('default_armours.csv', EquipmentArmour))
  27. # Ranged Weapons
  28. db.session.bulk_save_objects(load_csv_data('default_ranged.csv', EquipmentRangedWeapon))
  29. # Melee Weapons
  30. melee_weps = load_csv_data('default_melee.csv', EquipmentMeleeWeapon)
  31. for wep in melee_weps:
  32. wep.two_handed = (wep.two_handed == 'True')
  33. db.session.bulk_save_objects(melee_weps)
  34. # Level Progressions
  35. progressions = load_csv_data('default_progression.csv', ClassLevelProgression)
  36. classes = {c.name: c.id for c in CharacterClass.query.all()}
  37. for prog in progressions:
  38. prog.guild_id = classes[prog.guild_id]
  39. db.session.bulk_save_objects(progressions)
  40. # Spells
  41. spells = load_csv_data('default_spells.csv', Spell)
  42. for spell in spells:
  43. if spell.arcane == '':
  44. spell.arcane = 0
  45. if spell.divine == '':
  46. spell.divine = 0
  47. spell.description = spell.description.strip()
  48. db.session.bulk_save_objects(spells)
  49. db.session.commit()