Track items sold and graph profit for player shop inventory.
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.

97 lines
3.0 KiB

6 years ago
  1. import re
  2. from . import (
  3. db,
  4. models
  5. )
  6. def convert_lancre_to_brass(raw_price):
  7. coins = {
  8. 'penny': 0,
  9. 'shilling': 0,
  10. 'crown': 0,
  11. 'sovereign': 0,
  12. 'hedgehog': 0
  13. }
  14. def noz(num):
  15. if num and num is not '-':
  16. return int(num)
  17. return 0
  18. # Figure out which special notation we have
  19. if "LC" in raw_price:
  20. pattern = r'^LC (\d+)\|(\d+|-)\|(\d+|-)$'
  21. match = re.match(pattern, raw_price)
  22. groups = match.groups()
  23. coins['penny'] = noz(groups[2])
  24. coins['shilling'] = noz(groups[1])
  25. coins['crown'] = noz(groups[0])
  26. elif "LSov" in raw_price:
  27. pattern = r'^LSov (\d+)\|(\d+|-)\|(\d+|-)\|(\d+|-)$'
  28. match = re.match(pattern, raw_price)
  29. groups = match.groups()
  30. coins['penny'] = noz(groups[3])
  31. coins['shilling'] = noz(groups[2])
  32. coins['crown'] = noz(groups[1])
  33. coins['sovereign'] = noz(groups[0])
  34. elif "LH" in raw_price:
  35. pattern = r'^LH (\d+)\|(\d+|-)\|(\d+|-)\|(\d+|-)\|(\d+|-)$'
  36. match = re.match(pattern, raw_price)
  37. groups = match.groups()
  38. coins['penny'] = noz(groups[4])
  39. coins['shilling'] = noz(groups[3])
  40. coins['crown'] = noz(groups[2])
  41. coins['sovereign'] = noz(groups[1])
  42. coins['hedgehog'] = noz(groups[0])
  43. # Convert to brass
  44. brass_coins = (
  45. (coins['hedgehog'] * 248832) +
  46. (coins['sovereign'] * 20736) +
  47. (coins['crown'] * 1728) +
  48. (coins['shilling'] * 144) +
  49. (coins['penny'] * 12)
  50. )
  51. return brass_coins
  52. def convert_brass_to_am(brass_price):
  53. # 12 brass coins to am pennies
  54. pennies = brass_price / 4
  55. return (pennies/100)
  56. def parse_shop_output(data):
  57. pattern = r'^\s{3}?\w{2}\)\sAn*\s([\w\s-]+) for (L\w{1,3} [\d\-|]+);\s(\w+)\sleft\.$'
  58. matches = [m.groups() for m in re.finditer(pattern, data, re.MULTILINE)]
  59. if not matches:
  60. return
  61. # Iterate over each product line in the data
  62. seen_products = []
  63. for m in matches:
  64. product = models.ShopProduct.query.filter_by(name=m[0]).first()
  65. if not product:
  66. # If we didn't find a product, create it
  67. product = models.ShopProduct(name=m[0])
  68. db.session.add(product)
  69. # Add a ShopEntry for this row only if stock has changed
  70. if not product.latest_entry or product.latest_entry.raw_stock != m[2]:
  71. entry = models.ShopEntry(raw_price=m[1], raw_stock=m[2], product=product)
  72. db.session.add(entry)
  73. seen_products.append(product)
  74. # Check all products against seen, record sellouts
  75. products = models.ShopProduct.query.all()
  76. for product in products:
  77. if product not in seen_products and product.latest_entry.stock != 0:
  78. entry = models.ShopEntry(
  79. raw_price=product.latest_entry.raw_price,
  80. raw_stock='zero', product=product
  81. )
  82. db.session.add(entry)
  83. db.session.commit()