示例#1
0
def stock_stores(conn, some_bar, window, some_label, some_queue):
    # TODO: This is a seam
    canceled = False
    count = 0
    time_to_install = time.time()
    store_dict = stores.stores()    # {'some_store':[1,2,3,4,5]} Used to tell which items in which stores - ints are ids
    url = api.construct_api_url('equipment')
    s = api.create_session()
    response = api.call_api(url, s)
    response_dict = api.get_api_all(response)
    usable_dict = api.get_nested_api_dict(response_dict, 'results')  # [{'name': 'some_name', 'url': 'some_url'}]
    # TODO: This is a seam
    for dic in usable_dict:
        temp = {}
        # TODO: Make sure to do with conn: around phases instead of each sql statement. Will require need execute_sql.
        for key, value in dic.items():
            # TODO: First thing will always be to slice ID off url via regex and add to dictionary as 'index'
            # TODO: Then add item name, url and index to temp dictionary for DB insertion
            if not temp:    # If temp dictionary is empty
                temp['item'] = value    # add value with key 'item'
            else:
                temp['api'] = value     # add value with key 'api'

            # TODO: This will be a pool of API calls, post phase1 DB insertion
            item_value = api.get_item_value(temp['item'], character.Character.list_of_item_dicts, s)
            temp['currency'] = item_value

            # TODO: This should be done prior to phase1 DB insertion.
            if value[0:37] == url:  # if one of those values beings with a url like string
                num = api.regex(value, 'equipment/')    # slices number off url and captures as variable

                # This logic compares the captured number to the numbers in the dict imported earlier
                # Then it adds a 'store':'some_store' key:value to the temp dictionary
                if num in store_dict['GS']:
                    temp['store'] = 'General Store'
                elif num in store_dict['BS']:
                    temp['store'] = 'Blacksmith'
                elif num in store_dict['Ship']:
                    temp['store'] = 'Shipyard'
                elif num in store_dict['Stables']:
                    temp['store'] = 'Stables'
                else:
                    temp['store'] = 'No Store'

        # TODO: This will be phase2 DB insertion. Inserting the item values where index
        # TODO: add_store_item will need to be reworked, and, most likely, split into multiple functions
        # adds an item to a store table based on information stored in dictionary
        database.add_store_item(conn, sql.add_store_item(), temp)
        count += 1
        print(count)
        # TODO: Change this to a Daemon thread to avoid having to do this cancel workaround
        if not some_queue.empty():
            canceled = some_queue.get()
        update_mainloop(some_bar, count, some_label, window, canceled)

    print('done in: ', time.time() - time_to_install)
    print('Done with everything.')
示例#2
0
 def test_main_dictionaries(self):
     equip_count = api.get_nested_api_dict(
         api.get_api_all(api.call_api(api.construct_api_url('equipment'))),
         'count')
     spells_count = api.get_nested_api_dict(
         api.get_api_all(api.call_api(api.construct_api_url('spells'))),
         'count')
     classes_count = api.get_nested_api_dict(
         api.get_api_all(api.call_api(api.construct_api_url('classes'))),
         'count')
     features_count = api.get_nested_api_dict(
         api.get_api_all(api.call_api(api.construct_api_url('features'))),
         'count')
     monsters_count = api.get_nested_api_dict(
         api.get_api_all(api.call_api(api.construct_api_url('monsters'))),
         'count')
     self.assertEqual(equip_count, 256)
     self.assertEqual(spells_count, 319)
     self.assertEqual(classes_count, 12)
     self.assertEqual(features_count, 414)
     self.assertEqual(monsters_count, 325)
示例#3
0
class Character:

    # TODO: unit_integration_test: FIXTURE REQUIRED
    # Main equipment dictionary returned from DnD 5e API
    # [{'name': 'some name', 'url': 'some url'}, {'name': 'some name', 'url': 'some url'}]
    list_of_item_dicts = api.get_nested_api_dict(
        api.get_api_all(api.call_api(api.construct_api_url('equipment'))),
        'results')

    def __init__(self, char_id, name, currency, inventories_list):
        self.id = char_id
        self.currency = currency  # All currency held in cp. cp is then formatted via convert_currency() before view.
        self.name = name
        self.inventories = inventories_list  # List of player inventory names in string format. Not currently used.

    # TODO: Not tested
    # TODO: This makes more sense as an inventory method. Change it once multiple inventories.
    # Adds an item under character id. Can specify a specific inventory via inv_id
    def add_item_db(self, conn, item, acc_id, inv_id):
        url = sql.execute_fetchone_sql(conn, sql.query_store_item_url(), item)
        item_value = sql.execute_fetchone_sql(conn,
                                              sql.query_store_item_value(),
                                              item)

        item_info = {
            'acc_id': acc_id,
            'char_id': self.id,
            'inv_id': inv_id,
            'item': item,
            'api': url[0],
            'value': item_value[0],
            'quantity': 1
        }

        database.add_item_row(conn, sql.add_item_row(), item_info)

    # TODO: Test
    # Updates currency column in DB based on character ID and currency.
    def update_currency_db(self, conn):
        sql.execute_sql(conn, sql.update_currency(), self.currency, self.id)

    # TODO: action= and conn= aren't exactly optional and should not be treated as such.
    # Gets item price info and adds/subtracts it to/from currency based on [unit] key.
    # See convert_price_info() in api.py for more information on conversion.
    # Also updates currency in DB via self.update_currency_db
    # Returns True if item is affordable. Else returns False.
    def buy_sell(self, item, action=None, conn=None):
        url = api.get_item_url(item, Character.list_of_item_dicts)
        item_info = api.get_api_all(api.call_api(url))
        item_cost = api.get_nested_api_dict(item_info, 'cost')
        item_value = api.convert_price_info(item_cost)
        if action == 'buy':
            if item_value > self.currency:
                return False
            else:
                print('---item bought---')  # TODO remove later
                self.currency -= item_value
                self.update_currency_db(conn)
                return True
        elif action == 'sell':
            print('---item sold---')  # TODO remove later
            self.currency += item_value
            self.update_currency_db(conn)
示例#4
0
 def setUp(self):
     self.list_of_dics = api.get_nested_api_dict(
         api.get_api_all(api.call_api(api.construct_api_url('equipment'))),
         'results')
示例#5
0
 def test_make_api_url(self):
     self.assertEqual(construct_api_url('equipment'),
                      'http://www.dnd5eapi.co/api/equipment/')