models.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. """Database models"""
  2. import datetime
  3. from sqlalchemy import MetaData, Column, ForeignKey, Integer, String, \
  4. SmallInteger, DateTime, BigInteger, Date, Boolean
  5. from sqlalchemy.orm import relationship, backref
  6. from sqlalchemy.ext.declarative import declarative_base
  7. meta = MetaData(naming_convention={
  8. 'ix': 'ix_%(column_0_label)s',
  9. 'uq': 'uq_%(table_name)s_%(column_0_name)s',
  10. 'ck': 'ck_%(table_name)s_%(constraint_name)s',
  11. 'fk': 'fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s',
  12. 'pk': 'pk_%(table_name)s'
  13. })
  14. Base = declarative_base(metadata=meta)
  15. class Region(Base):
  16. """Model for region"""
  17. __tablename__ = 'region'
  18. id = Column(Integer, primary_key=True)
  19. name = Column(String)
  20. gold_limit = Column(SmallInteger)
  21. oil_limit = Column(SmallInteger)
  22. ore_limit = Column(SmallInteger)
  23. uranium_limit = Column(SmallInteger)
  24. diamond_limit = Column(SmallInteger)
  25. class DeepExploration(Base):
  26. """Model for deep exploration"""
  27. __tablename__ = 'deep_exploration'
  28. id = Column(Integer, primary_key=True)
  29. until_date_time = Column(DateTime)
  30. points = Column(Integer)
  31. resource_type = Column(SmallInteger)
  32. region_id = Column(Integer, ForeignKey('region.id'))
  33. region = relationship(
  34. 'Region',
  35. backref=backref('deep_explorations', lazy='dynamic')
  36. )
  37. class DeepExplorationOrder(Base):
  38. """Model for deep exploration order"""
  39. __tablename__ = 'deep_exploration_order'
  40. id = Column(Integer, primary_key=True)
  41. resource_type = Column(SmallInteger, nullable=False)
  42. order_type = Column(SmallInteger, nullable=False)
  43. amount = Column(Integer)
  44. from_date_time = Column(DateTime)
  45. until_date_time = Column(DateTime)
  46. region_id = Column(Integer, ForeignKey('region.id'))
  47. region = relationship(
  48. 'Region',
  49. backref=backref('resource_stats', lazy='dynamic')
  50. )
  51. order_types = {
  52. 0: 'max',
  53. 1: 'fixed',
  54. 2: 'percentage',
  55. 3: 'auto',
  56. }
  57. def order_type_name(self):
  58. """Type name"""
  59. if self.order_type in self.order_types:
  60. return self.order_types[self.type]
  61. return 'unknown'
  62. class ResourceTrack(Base):
  63. """Model for resource track"""
  64. __tablename__ = 'resource_track'
  65. id = Column(Integer, primary_key=True)
  66. resource_type = Column(SmallInteger)
  67. date_time = Column(DateTime)
  68. state_id = Column(Integer, ForeignKey('state.id'))
  69. state = relationship(
  70. 'State',
  71. backref=backref('resource_tracks', lazy='dynamic')
  72. )
  73. class ResourceStat(Base):
  74. """Model for resource stat"""
  75. __tablename__ = 'resource_stat'
  76. id = Column(Integer, primary_key=True)
  77. explored = Column(SmallInteger)
  78. deep_exploration = Column(SmallInteger)
  79. limit_left = Column(SmallInteger)
  80. resource_track_id = Column(Integer, ForeignKey('resource_track.id'))
  81. resource_track = relationship(
  82. 'ResourceTrack',
  83. backref=backref('resource_stats', lazy='dynamic')
  84. )
  85. region_id = Column(Integer, ForeignKey('region.id'))
  86. region = relationship(
  87. 'Region',
  88. backref=backref('resource_stats', lazy='dynamic')
  89. )
  90. class StateRegion(Base):
  91. """Model for state region"""
  92. __tablename__ = 'state_region'
  93. state_id = Column(Integer, ForeignKey('state.id'), primary_key=True)
  94. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  95. from_date_time = Column(DateTime, primary_key=True)
  96. until_date_time = Column(DateTime)
  97. class State(Base):
  98. """Model for state"""
  99. __tablename__ = 'state'
  100. id = Column(Integer, primary_key=True)
  101. name = Column(String)
  102. regions = relationship('Region', secondary='state_region')
  103. capital_id = Column(Integer, ForeignKey('region.id'))
  104. capital = relationship(
  105. 'Region',
  106. backref=backref('state_capital', lazy='dynamic')
  107. )
  108. class Department(Base):
  109. """Model for department"""
  110. __tablename__ = 'department'
  111. id = Column(Integer, primary_key=True)
  112. name = Column(String)
  113. department_type = Column(Integer)
  114. state_id = Column(Integer, ForeignKey('state.id'))
  115. state = relationship(
  116. 'State',
  117. backref=backref('elections', lazy='dynamic')
  118. )
  119. class DepartmentStat(Base):
  120. """Model for departent stat"""
  121. __tablename__ = 'department_stat'
  122. id = Column(Integer, primary_key=True)
  123. date_time = Column(DateTime)
  124. points = Column(SmallInteger)
  125. player_id = Column(BigInteger, ForeignKey('player.id'))
  126. player = relationship(
  127. 'Player',
  128. backref=backref('department_stats', lazy='dynamic')
  129. )
  130. department_id = Column(Integer, ForeignKey('department.id'))
  131. department = relationship(
  132. 'Department',
  133. backref=backref('stats', lazy='dynamic')
  134. )
  135. class PlayerParty(Base):
  136. """Model fro player party"""
  137. __tablename__ = 'player_party'
  138. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  139. party_id = Column(Integer, ForeignKey('party.id'), primary_key=True)
  140. from_date_time = Column(DateTime, primary_key=True)
  141. until_date_time = Column(DateTime)
  142. class PlayerLocation(Base):
  143. """Model for player location"""
  144. __tablename__ = 'player_location'
  145. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  146. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  147. from_date_time = Column(DateTime, primary_key=True)
  148. until_date_time = Column(DateTime)
  149. class PlayerResidency(Base):
  150. """Model for player residency"""
  151. __tablename__ = 'player_residency'
  152. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  153. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  154. from_date_time = Column(DateTime, primary_key=True)
  155. until_date_time = Column(DateTime)
  156. class StateWorkPermit(Base):
  157. """Model for state work permit"""
  158. __tablename__ = 'state_work_permit'
  159. state_id = Column(Integer, ForeignKey('state.id'), primary_key=True)
  160. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  161. from_date_time = Column(DateTime, primary_key=True)
  162. until_date_time = Column(DateTime)
  163. class Player(Base):
  164. """Model for player"""
  165. __tablename__ = 'player'
  166. id = Column(BigInteger, primary_key=True)
  167. name = Column(String)
  168. nation = Column(String)
  169. registration_date = Column(Date)
  170. residencies = relationship('Region', secondary='player_residency')
  171. locations = relationship('Region', secondary='player_location')
  172. parties = relationship('Region', secondary='player_party')
  173. state_work_permits = relationship('State', secondary='state_work_permit')
  174. class Party(Base):
  175. """Model for party"""
  176. __tablename__ = 'party'
  177. id = Column(Integer, primary_key=True)
  178. name = Column(String)
  179. description = Column(String(512))
  180. from_date_time = Column(DateTime)
  181. until_date_time = Column(DateTime)
  182. class Election(Base):
  183. """Model for election"""
  184. __tablename__ = 'election'
  185. id = Column(Integer, primary_key=True)
  186. convocation_date_time = Column(DateTime)
  187. state_id = Column(Integer, ForeignKey('state.id'))
  188. state = relationship(
  189. 'State',
  190. backref=backref('elections', lazy='dynamic')
  191. )
  192. class ElectionStat(Base):
  193. """Model for election stat"""
  194. __tablename__ = 'election_stat'
  195. id = Column(Integer, primary_key=True)
  196. percentage = Column(SmallInteger)
  197. seats = Column(SmallInteger)
  198. election_id = Column(Integer, ForeignKey('election.id'))
  199. election = relationship(
  200. 'Election',
  201. backref=backref('election_stats', lazy='dynamic')
  202. )
  203. party_id = Column(Integer, ForeignKey('party.id'))
  204. party = relationship(
  205. 'Party',
  206. backref=backref('election_stats', lazy='dynamic')
  207. )
  208. class MilitaryAcademy(Base):
  209. """Model for military academy"""
  210. __tablename__ = 'military_academy'
  211. id = Column(Integer, primary_key=True)
  212. date_time = Column(DateTime)
  213. player_id = Column(BigInteger, ForeignKey('player.id'))
  214. player = relationship(
  215. 'User',
  216. backref=backref('military_academies', lazy='dynamic')
  217. )
  218. region_id = Column(Integer, ForeignKey('region.id'))
  219. region = relationship(
  220. 'Region',
  221. backref=backref('military_academies', lazy='dynamic')
  222. )
  223. class Factory(Base):
  224. """Model for factory"""
  225. __tablename__ = 'factory'
  226. id = Column(Integer, primary_key=True)
  227. name = Column(String)
  228. resource_type = Column(SmallInteger)
  229. player_id = Column(BigInteger, ForeignKey('player.id'))
  230. player = relationship(
  231. 'User',
  232. backref=backref('factories', lazy='dynamic')
  233. )
  234. class FactoryTrack(Base):
  235. """Model for facctory track"""
  236. __tablename__ = 'factory_track'
  237. id = Column(Integer, primary_key=True)
  238. date_time = Column(DateTime)
  239. state_id = Column(Integer, ForeignKey('state.id'))
  240. state = relationship(
  241. 'State',
  242. backref=backref('factory_tracks', lazy='dynamic')
  243. )
  244. class FactoryLocation(Base):
  245. """Model for factory location"""
  246. __tablename__ = 'factory_location'
  247. factory_id = Column(Integer, ForeignKey('factory.id'), primary_key=True)
  248. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  249. from_date_time = Column(DateTime, primary_key=True)
  250. until_date_time = Column(DateTime)
  251. class FactoryStat(Base):
  252. """Model for factory"""
  253. __tablename__ = 'factory_stat'
  254. id = Column(Integer, primary_key=True)
  255. level = Column(SmallInteger)
  256. workers = Column(SmallInteger)
  257. experience = Column(Integer)
  258. wage = Column(Integer)
  259. factory_id = Column(Integer, ForeignKey('factory.id'))
  260. factory = relationship(
  261. 'FactoryTrack',
  262. backref=backref('factory_stats', lazy='dynamic')
  263. )
  264. factory_track_id = Column(Integer, ForeignKey('factory_track.id'))
  265. factory_track = relationship(
  266. 'FactoryTrack',
  267. backref=backref('factory_stats', lazy='dynamic')
  268. )
  269. class MarketTrack(Base):
  270. """Model for market track"""
  271. __tablename__ = 'market_track'
  272. id = Column(Integer, primary_key=True)
  273. date_time = Column(DateTime)
  274. player_resources = Column(Boolean, server_default='f', default=False)
  275. state_resources = Column(Boolean, server_default='f', default=False)
  276. items = Column(Boolean, server_default='f', default=False)
  277. class PlayerMarketStat(Base):
  278. """Model for market stat"""
  279. __tablename__ = 'player_market_stat'
  280. id = Column(Integer, primary_key=True)
  281. item_type = Column(SmallInteger)
  282. price = Column(Integer)
  283. amount = Column(BigInteger)
  284. base_average = Column(BigInteger)
  285. one_average = Column(Integer)
  286. two_average = Column(Integer)
  287. three_average = Column(Integer)
  288. four_average = Column(Integer)
  289. five_average = Column(Integer)
  290. six_average = Column(Integer)
  291. seven_average = Column(Integer)
  292. eight_average = Column(Integer)
  293. nine_average = Column(Integer)
  294. ten_average = Column(Integer)
  295. total_offers = Column(Integer)
  296. player_id = Column(BigInteger, ForeignKey('player.id'))
  297. player = relationship(
  298. 'Player',
  299. backref=backref('player_market_stats', lazy='dynamic')
  300. )
  301. market_track_id = Column(Integer, ForeignKey('market_track.id'))
  302. market_track = relationship(
  303. 'MarketTrack',
  304. backref=backref('player_market_stats', lazy='dynamic')
  305. )
  306. class StateMarketStat(Base):
  307. """Model for market stat"""
  308. __tablename__ = 'state_market_stat'
  309. id = Column(Integer, primary_key=True)
  310. item_type = Column(SmallInteger)
  311. price = Column(Integer)
  312. amount = Column(BigInteger)
  313. region_id = Column(Integer, ForeignKey('region.id'))
  314. region = relationship(
  315. 'Region',
  316. backref=backref('region_market_stats', lazy='dynamic')
  317. )
  318. market_track_id = Column(Integer, ForeignKey('market_track.id'))
  319. market_track = relationship(
  320. 'MarketTrack',
  321. backref=backref('state_market_stats', lazy='dynamic')
  322. )
  323. class TelegramAccount(Base):
  324. """Model for Telegram account"""
  325. __tablename__ = 'telegram_account'
  326. id = Column(BigInteger, primary_key=True)
  327. name = Column(String)
  328. registration_date = Column(DateTime)
  329. class TelegramHandle(Base):
  330. """Model for Telegram handle"""
  331. __tablename__ = 'telegram_handle'
  332. id = Column(Integer, primary_key=True)
  333. handle = Column(String)
  334. registration_date = Column(DateTime)
  335. telegram_account_id = Column(BigInteger, ForeignKey('telegram_account.id'))
  336. telegram_account = relationship(
  337. 'TelegramAccount',
  338. backref=backref('account_handles', lazy='dynamic')
  339. )
  340. class PlayerTelegram(Base):
  341. """Model for belongs to"""
  342. __tablename__ = 'player_telegram'
  343. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  344. telegram_id = Column(BigInteger, ForeignKey('telegram_account.id'), primary_key=True)
  345. from_date_time = Column(DateTime, primary_key=True)
  346. until_date_time = Column(DateTime)