Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- from random import randrange
- # CONFIG
- LOOT = """
- <item id="2148" chance="30000" countmax="30"/><!-- 30 30% gold coin -->
- <item id="2148" chance="40000" countmax="25"/><!-- 25 40% gold coin -->
- <item id="2148" chance="40000" countmax="40"/><!-- 40 40% gold coin -->
- <item id="2489" chance="7000"/><!-- 5% dark armor -->
- <item id="2647" chance="1500"/><!-- 1.5% plate legs -->
- <item id="2497" chance="500"/><!-- 0.5% crusader helmet -->
- <item id="1987" chance="90000"><!-- 90% bag -->
- <inside>
- <item id="2044" chance="8000"/><!-- 8% lamp -->
- <item id="2377" chance="5000"/><!-- 5% two handed sword -->
- <item id="2209" chance="900"/><!-- 0.9% club ring -->
- <item id="2425" chance="1200"/><!-- 1,2% obsidian lance -->
- <item id="2509" chance="3000"/><!-- 3% steel shield -->
- <item id="2149" chance="1000" countmax="2"/> <!-- 2 1% small emerald -->
- """.splitlines()
- REGEXP_RULES = {'chance': 'chance=\"(\d*)\"',
- 'id': 'id=\"(\d*)\"',
- 'countmax': 'countmax=\"(\d*)\"'}
- VALUES = {
- # itemid: price_per_item
- 2148: 1,
- }
- def collect_values(LOOT):
- global POSSIBLE_ITEMS
- for item in LOOT:
- if item is not None and item != "":
- try:
- item_id = int(re.search(REGEXP_RULES['id'], item).group(1))
- except:
- continue
- try:
- loot_countmax = int(re.search(REGEXP_RULES['countmax'], item).group(1))
- except:
- loot_countmax = 1
- POSSIBLE_ITEMS.append({'id': item_id,
- 'chance': int(re.search(REGEXP_RULES['chance'], item).group(1)) / 1000,
- 'countmax': loot_countmax})
- LOOTED = {}
- POSSIBLE_ITEMS = []
- def pretend_loot():
- global LOOTED
- global POSSIBLE_ITEMS
- for item in POSSIBLE_ITEMS:
- rand = randrange(101) # from <0-100> included
- if rand <= item['chance']:
- count = 1
- if int(item['countmax']) > 1:
- count = randrange(int(item['countmax']) + 1)
- try:
- LOOTED[item['id']] = LOOTED[item['id']] + count
- except:
- LOOTED[item['id']] = count
- def calculate_value():
- global VALUES
- global LOOTED
- value = 0
- for item in LOOTED:
- if item in VALUES:
- value += LOOTED[item] * VALUES[item]
- else:
- print("Couldn't find {} in VALUES".format(item))
- print("End value is: {}".format(value))
- TRIES = 1000
- collect_values(LOOT)
- for i in range(0, TRIES):
- pretend_loot()
- print(LOOTED)
- calculate_value()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement