models.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 Region(Base):
  8. """Model for region"""
  9. __tablename__ = 'region'
  10. id = Column(Integer, primary_key=True)
  11. name = Column(String)
  12. gold_limit = Column(SmallInteger)
  13. oil_limit = Column(SmallInteger)
  14. ore_limit = Column(SmallInteger)
  15. uranium_limit = Column(SmallInteger)
  16. diamond_limit = Column(SmallInteger)
  17. class DeepExploration(Base):
  18. """Model for deep exploration"""
  19. __tablename__ = 'deep_exploration'
  20. id = Column(Integer, primary_key=True)
  21. date_time_end = Column(DateTime)
  22. region_id = Column(Integer)
  23. resource_type = Column(SmallInteger)
  24. region_id = Column(Integer, ForeignKey('region.id'))
  25. region_track = relationship(
  26. "Region",
  27. backref=backref("deep_explorations", lazy="dynamic")
  28. )
  29. class ResourceTrack(Base):
  30. """Model for resource track"""
  31. __tablename__ = 'resource_track'
  32. id = Column(Integer, primary_key=True)
  33. resource_type = Column(SmallInteger)
  34. date_time = Column(DateTime, default=datetime.datetime.now)
  35. state_id = Column(Integer, ForeignKey('state.id'))
  36. resource_track = relationship(
  37. "State",
  38. backref=backref("resource_tracks", lazy="dynamic")
  39. )
  40. class ResourceStat(Base):
  41. """Model for resource stat"""
  42. __tablename__ = 'resource_stat'
  43. id = Column(Integer, primary_key=True)
  44. explored = Column(SmallInteger)
  45. deep_exploration = Column(SmallInteger)
  46. limit_left = Column(SmallInteger)
  47. resource_track_id = Column(Integer, ForeignKey('resource_track.id'))
  48. resource_track = relationship(
  49. "ResourceTrack",
  50. backref=backref("resource_stats", lazy="dynamic")
  51. )
  52. region_id = Column(Integer, ForeignKey('region.id'))
  53. region = relationship(
  54. "Region",
  55. backref=backref("resource_stats", lazy="dynamic")
  56. )
  57. def total(self):
  58. """total left"""
  59. return self.limit_left + self.explored
  60. class State(Base):
  61. """Model for state"""
  62. __tablename__ = 'state'
  63. id = Column(Integer, primary_key=True)
  64. name = Column(String)