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.

95 lines
2.9 KiB

6 years ago
  1. from datetime import datetime
  2. from . import (
  3. db,
  4. shop
  5. )
  6. class ShopProduct(db.Model):
  7. id = db.Column(db.Integer, primary_key = True)
  8. name = db.Column(db.String(100), unique=True, nullable=False)
  9. total_listed = db.Column(db.Integer)
  10. total_sold = db.Column(db.Integer)
  11. entries = db.relationship('ShopEntry', backref='product', lazy=True,
  12. order_by="desc(ShopEntry.date)")
  13. def __repr__(self):
  14. return '<ShopProduct "{}">'.format(self.name)
  15. @property
  16. def latest_entry(self):
  17. return ShopEntry.query.filter_by(
  18. product_id=self.id).order_by(ShopEntry.date.desc()).first()
  19. @property
  20. def total_stocked(self):
  21. entries = ShopEntry.query.filter_by(product_id=self.id).order_by(
  22. ShopEntry.date.asc())
  23. last_stock = 0
  24. stocked_count = 0
  25. for entry in entries:
  26. diff = last_stock - entry.stock
  27. if diff < 0:
  28. stocked_count += abs(diff)
  29. last_stock = entry.stock
  30. return stocked_count
  31. @property
  32. def total_sold(self):
  33. entries = ShopEntry.query.filter_by(product_id=self.id).order_by(
  34. ShopEntry.date.asc())
  35. last_stock = 0
  36. sold_count = 0
  37. for entry in entries:
  38. diff = last_stock - entry.stock
  39. if diff > 0:
  40. sold_count += diff
  41. last_stock = entry.stock
  42. return sold_count
  43. @property
  44. def total_earned(self):
  45. entries = ShopEntry.query.filter_by(product_id=self.id).order_by(
  46. ShopEntry.date.asc())
  47. last_stock = 0
  48. earned_count = 0.00
  49. for entry in entries:
  50. diff = last_stock - entry.stock
  51. if diff > 0:
  52. earned_count += (entry.price * diff)
  53. last_stock = entry.stock
  54. return earned_count
  55. class ShopEntry(db.Model):
  56. id = db.Column(db.Integer, primary_key=True)
  57. stock = db.Column(db.Integer, unique=False, nullable=False)
  58. raw_price = db.Column(db.String(20), unique=False, nullable=False)
  59. raw_stock = db.Column(db.String(2), unique=False, nullable=False)
  60. date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
  61. product_id = db.Column(db.Integer, db.ForeignKey('shop_product.id'),
  62. nullable=False)
  63. def __repr__(self):
  64. return '<ShopEntry "{}" {}>'.format(
  65. self.product.name,
  66. self.id
  67. )
  68. @property
  69. def stock(self):
  70. stock_map = {
  71. 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5,
  72. 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10,
  73. 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14
  74. }
  75. return stock_map[self.raw_stock]
  76. @property
  77. def price(self):
  78. brass = shop.convert_lancre_to_brass(self.raw_price)
  79. return shop.convert_brass_to_am(brass)