models.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 Bloc(Base):
  109. """Model for bloc"""
  110. __tablename__ = 'bloc'
  111. id = Column(Integer, primary_key=True)
  112. name = Column(String)
  113. state_id = Column(Integer, ForeignKey('state.id'))
  114. state = relationship(
  115. 'State',
  116. backref=backref('bloc_founders', lazy='dynamic')
  117. )
  118. class BlocStates(Base):
  119. """Model for bloc states"""
  120. __tablename__ = 'bloc_states'
  121. bloc_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  122. state_id = Column(Integer, ForeignKey('state.id'), primary_key=True)
  123. from_date_time = Column(DateTime, primary_key=True)
  124. until_date_time = Column(DateTime)
  125. class Department(Base):
  126. """Model for department"""
  127. __tablename__ = 'department'
  128. id = Column(Integer, primary_key=True)
  129. name = Column(String)
  130. department_type = Column(Integer)
  131. state_id = Column(Integer, ForeignKey('state.id'))
  132. state = relationship(
  133. 'State',
  134. backref=backref('elections', lazy='dynamic')
  135. )
  136. class DepartmentStat(Base):
  137. """Model for departent stat"""
  138. __tablename__ = 'department_stat'
  139. id = Column(Integer, primary_key=True)
  140. date_time = Column(DateTime)
  141. points = Column(SmallInteger)
  142. player_id = Column(BigInteger, ForeignKey('player.id'))
  143. player = relationship(
  144. 'Player',
  145. backref=backref('department_stats', lazy='dynamic')
  146. )
  147. department_id = Column(Integer, ForeignKey('department.id'))
  148. department = relationship(
  149. 'Department',
  150. backref=backref('stats', lazy='dynamic')
  151. )
  152. class PlayerParty(Base):
  153. """Model fro player party"""
  154. __tablename__ = 'player_party'
  155. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  156. party_id = Column(Integer, ForeignKey('party.id'), primary_key=True)
  157. from_date_time = Column(DateTime, primary_key=True)
  158. until_date_time = Column(DateTime)
  159. class PlayerLocation(Base):
  160. """Model for player location"""
  161. __tablename__ = 'player_location'
  162. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  163. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  164. from_date_time = Column(DateTime, primary_key=True)
  165. until_date_time = Column(DateTime)
  166. class PlayerResidency(Base):
  167. """Model for player residency"""
  168. __tablename__ = 'player_residency'
  169. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  170. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  171. from_date_time = Column(DateTime, primary_key=True)
  172. until_date_time = Column(DateTime)
  173. class StateWorkPermit(Base):
  174. """Model for state work permit"""
  175. __tablename__ = 'state_work_permit'
  176. state_id = Column(Integer, ForeignKey('state.id'), primary_key=True)
  177. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  178. from_date_time = Column(DateTime, primary_key=True)
  179. until_date_time = Column(DateTime)
  180. class Player(Base):
  181. """Model for player"""
  182. __tablename__ = 'player'
  183. id = Column(BigInteger, primary_key=True)
  184. name = Column(String)
  185. nation = Column(String)
  186. registration_date = Column(Date)
  187. residencies = relationship('Region', secondary='player_residency')
  188. locations = relationship('Region', secondary='player_location')
  189. parties = relationship('Region', secondary='player_party')
  190. state_work_permits = relationship('State', secondary='state_work_permit')
  191. class Party(Base):
  192. """Model for party"""
  193. __tablename__ = 'party'
  194. id = Column(Integer, primary_key=True)
  195. name = Column(String)
  196. description = Column(String(512))
  197. from_date_time = Column(DateTime)
  198. until_date_time = Column(DateTime)
  199. class Election(Base):
  200. """Model for election"""
  201. __tablename__ = 'election'
  202. id = Column(Integer, primary_key=True)
  203. convocation_date_time = Column(DateTime)
  204. state_id = Column(Integer, ForeignKey('state.id'))
  205. state = relationship(
  206. 'State',
  207. backref=backref('elections', lazy='dynamic')
  208. )
  209. class ElectionStat(Base):
  210. """Model for election stat"""
  211. __tablename__ = 'election_stat'
  212. id = Column(Integer, primary_key=True)
  213. percentage = Column(SmallInteger)
  214. seats = Column(SmallInteger)
  215. election_id = Column(Integer, ForeignKey('election.id'))
  216. election = relationship(
  217. 'Election',
  218. backref=backref('election_stats', lazy='dynamic')
  219. )
  220. party_id = Column(Integer, ForeignKey('party.id'))
  221. party = relationship(
  222. 'Party',
  223. backref=backref('election_stats', lazy='dynamic')
  224. )
  225. class MilitaryAcademy(Base):
  226. """Model for military academy"""
  227. __tablename__ = 'military_academy'
  228. id = Column(Integer, primary_key=True)
  229. date_time = Column(DateTime)
  230. player_id = Column(BigInteger, ForeignKey('player.id'))
  231. player = relationship(
  232. 'User',
  233. backref=backref('military_academies', lazy='dynamic')
  234. )
  235. region_id = Column(Integer, ForeignKey('region.id'))
  236. region = relationship(
  237. 'Region',
  238. backref=backref('military_academies', lazy='dynamic')
  239. )
  240. class Factory(Base):
  241. """Model for factory"""
  242. __tablename__ = 'factory'
  243. id = Column(Integer, primary_key=True)
  244. name = Column(String)
  245. resource_type = Column(SmallInteger)
  246. player_id = Column(BigInteger, ForeignKey('player.id'))
  247. player = relationship(
  248. 'User',
  249. backref=backref('factories', lazy='dynamic')
  250. )
  251. class FactoryTrack(Base):
  252. """Model for facctory track"""
  253. __tablename__ = 'factory_track'
  254. id = Column(Integer, primary_key=True)
  255. date_time = Column(DateTime)
  256. state_id = Column(Integer, ForeignKey('state.id'))
  257. state = relationship(
  258. 'State',
  259. backref=backref('factory_tracks', lazy='dynamic')
  260. )
  261. class FactoryLocation(Base):
  262. """Model for factory location"""
  263. __tablename__ = 'factory_location'
  264. factory_id = Column(Integer, ForeignKey('factory.id'), primary_key=True)
  265. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  266. from_date_time = Column(DateTime, primary_key=True)
  267. until_date_time = Column(DateTime)
  268. class FactoryStat(Base):
  269. """Model for factory"""
  270. __tablename__ = 'factory_stat'
  271. id = Column(Integer, primary_key=True)
  272. level = Column(SmallInteger)
  273. workers = Column(SmallInteger)
  274. experience = Column(Integer)
  275. wage = Column(Integer)
  276. factory_id = Column(Integer, ForeignKey('factory.id'))
  277. factory = relationship(
  278. 'FactoryTrack',
  279. backref=backref('factory_stats', lazy='dynamic')
  280. )
  281. factory_track_id = Column(Integer, ForeignKey('factory_track.id'))
  282. factory_track = relationship(
  283. 'FactoryTrack',
  284. backref=backref('factory_stats', lazy='dynamic')
  285. )
  286. class MarketTrack(Base):
  287. """Model for market track"""
  288. __tablename__ = 'market_track'
  289. id = Column(Integer, primary_key=True)
  290. date_time = Column(DateTime)
  291. player_resources = Column(Boolean, server_default='f', default=False)
  292. state_resources = Column(Boolean, server_default='f', default=False)
  293. items = Column(Boolean, server_default='f', default=False)
  294. class PlayerMarketStat(Base):
  295. """Model for market stat"""
  296. __tablename__ = 'player_market_stat'
  297. id = Column(Integer, primary_key=True)
  298. item_type = Column(SmallInteger)
  299. price = Column(Integer)
  300. amount = Column(BigInteger)
  301. half_t_average = Column(Integer)
  302. one_t_average = Column(Integer)
  303. two_t_average = Column(Integer)
  304. five_t_average = Column(Integer)
  305. total_offers = Column(Integer)
  306. player_id = Column(BigInteger, ForeignKey('player.id'))
  307. player = relationship(
  308. 'Player',
  309. backref=backref('player_market_stats', lazy='dynamic')
  310. )
  311. market_track_id = Column(Integer, ForeignKey('market_track.id'))
  312. market_track = relationship(
  313. 'MarketTrack',
  314. backref=backref('player_market_stats', lazy='dynamic')
  315. )
  316. class StateMarketStat(Base):
  317. """Model for market stat"""
  318. __tablename__ = 'state_market_stat'
  319. id = Column(Integer, primary_key=True)
  320. item_type = Column(SmallInteger)
  321. price = Column(Integer)
  322. amount = Column(BigInteger)
  323. region_id = Column(Integer, ForeignKey('region.id'))
  324. region = relationship(
  325. 'Region',
  326. backref=backref('region_market_stats', lazy='dynamic')
  327. )
  328. market_track_id = Column(Integer, ForeignKey('market_track.id'))
  329. market_track = relationship(
  330. 'MarketTrack',
  331. backref=backref('state_market_stats', lazy='dynamic')
  332. )
  333. class TelegramAccount(Base):
  334. """Model for Telegram account"""
  335. __tablename__ = 'telegram_account'
  336. id = Column(BigInteger, primary_key=True)
  337. name = Column(String)
  338. registration_date = Column(DateTime)
  339. class TelegramHandle(Base):
  340. """Model for Telegram handle"""
  341. __tablename__ = 'telegram_handle'
  342. id = Column(Integer, primary_key=True)
  343. handle = Column(String)
  344. registration_date = Column(DateTime)
  345. telegram_account_id = Column(BigInteger, ForeignKey('telegram_account.id'))
  346. telegram_account = relationship(
  347. 'TelegramAccount',
  348. backref=backref('account_handles', lazy='dynamic')
  349. )
  350. class PlayerTelegram(Base):
  351. """Model for belongs to"""
  352. __tablename__ = 'player_telegram'
  353. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  354. telegram_id = Column(BigInteger, ForeignKey('telegram_account.id'), primary_key=True)
  355. from_date_time = Column(DateTime, primary_key=True)
  356. until_date_time = Column(DateTime)
  357. class War(Base):
  358. """Model for war"""
  359. __tablename__ = 'war'
  360. id = Column(Integer, primary_key=True)
  361. until_date_time = Column(DateTime)
  362. war_type = Column(SmallInteger)
  363. priority = Column(SmallInteger)
  364. attacking_id = Column(Integer, ForeignKey('region.id'))
  365. attacking = relationship(
  366. 'Region',
  367. backref=backref('attacked_wars', lazy='dynamic')
  368. )
  369. defending_id = Column(Integer, ForeignKey('region.id'))
  370. defending = relationship(
  371. 'Region',
  372. backref=backref('defending_wars', lazy='dynamic')
  373. )
  374. class war_stat(Base):
  375. """Model for war stat"""
  376. __tablename__ = 'war_stat'
  377. id = Column(Integer, primary_key=True)
  378. war_id = Column(Integer, ForeignKey('war.id'))
  379. attack_damage = Column(BigInteger)
  380. attack_military_base = Column(Integer)
  381. attack_military_academy = Column(Integer)
  382. defend_damage = Column(BigInteger)
  383. war = relationship(
  384. 'War',
  385. backref=backref('defending_wars', lazy='dynamic')
  386. )