models.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Database models"""
  2. import datetime
  3. from sqlalchemy import Column, ForeignKey, Integer, String, SmallInteger, DateTime
  4. from sqlalchemy.orm import relationship, backref
  5. from sqlalchemy.ext.declarative import declarative_base
  6. Base = declarative_base()
  7. class State(Base):
  8. """Model for state"""
  9. __tablename__ = 'state'
  10. id = Column(Integer, primary_key=True)
  11. name = Column(String)
  12. class Region(Base):
  13. """Model for region"""
  14. __tablename__ = 'region'
  15. id = Column(Integer, primary_key=True)
  16. name = Column(String)
  17. gold_limit = Column(SmallInteger)
  18. oil_limit = Column(SmallInteger)
  19. ore_limit = Column(SmallInteger)
  20. uranium_limit = Column(SmallInteger)
  21. diamond_limit = Column(SmallInteger)
  22. def get_limit(self, resource_type):
  23. """get limit for resoruce type"""
  24. limit = {
  25. 0: self.gold_limit,
  26. 3: self.oil_limit,
  27. 4: self.ore_limit,
  28. 11: self.uranium_limit,
  29. 15: self.diamond_limit,
  30. }
  31. return limit[resource_type] if resource_type in limit else None
  32. class StateRegion(Base):
  33. """Model for state region"""
  34. __tablename__ = 'state_region'
  35. state_id = Column(Integer, ForeignKey('state.id'), primary_key=True)
  36. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  37. from_date_time = Column(DateTime, primary_key=True)
  38. until_date_time = Column(DateTime)
  39. region = relationship(
  40. 'Region',
  41. backref=backref('state_regions', lazy='dynamic')
  42. )
  43. state = relationship(
  44. 'State',
  45. backref=backref('state_regions', lazy='dynamic')
  46. )
  47. class DeepExploration(Base):
  48. """Model for deep exploration"""
  49. __tablename__ = 'deep_exploration'
  50. id = Column(Integer, primary_key=True)
  51. until_date_time = Column(DateTime)
  52. points = Column(Integer)
  53. resource_type = Column(SmallInteger)
  54. region_id = Column(Integer, ForeignKey('region.id'))
  55. region = relationship(
  56. 'Region',
  57. backref=backref('deep_explorations', lazy='dynamic')
  58. )
  59. class DeepExplorationOrder(Base):
  60. """Model for deep exploration order"""
  61. __tablename__ = 'deep_exploration_order'
  62. id = Column(Integer, primary_key=True)
  63. resource_type = Column(SmallInteger, nullable=False)
  64. order_type = Column(SmallInteger, nullable=False)
  65. amount = Column(Integer)
  66. from_date_time = Column(DateTime)
  67. until_date_time = Column(DateTime)
  68. region_id = Column(Integer, ForeignKey('region.id'))
  69. region = relationship(
  70. 'Region',
  71. backref=backref('resource_stats', lazy='dynamic')
  72. )
  73. order_types = {
  74. 0: 'max',
  75. 1: 'fixed',
  76. 2: 'percentage',
  77. 3: 'auto',
  78. }
  79. def order_type_name(self):
  80. """Type name"""
  81. if self.order_type in self.order_types:
  82. return self.order_types[self.type]
  83. return 'unknown'