models.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 FactoryOwner(Base):
  270. """Model for factory owner"""
  271. __tablename__ = 'factory_owner'
  272. factory_id = Column(Integer, ForeignKey('factory.id'), primary_key=True)
  273. owner = relationship(
  274. 'Factory',
  275. backref=backref('owners', lazy='dynamic')
  276. )
  277. owner_id = Column(Integer, ForeignKey('player.id'), primary_key=True)
  278. owner = relationship(
  279. 'Player',
  280. backref=backref('owned_factories', lazy='dynamic')
  281. )
  282. from_date_time = Column(DateTime, primary_key=True)
  283. until_date_time = Column(DateTime)
  284. class Auction(Base):
  285. """Model for auction"""
  286. __tablename__ = 'auction'
  287. id = Column(Integer, primary_key=True)
  288. lot = Column(String)
  289. factory_id = Column(Integer, ForeignKey('factory.id'))
  290. player = relationship(
  291. 'Factory',
  292. backref=backref('won_auctions', lazy='dynamic')
  293. )
  294. end_date_time = Column(DateTime)
  295. bet = Column(BigInteger)
  296. money = Column(Boolean, server_default='f', default=False)
  297. class MarketTrack(Base):
  298. """Model for market track"""
  299. __tablename__ = 'market_track'
  300. id = Column(Integer, primary_key=True)
  301. date_time = Column(DateTime)
  302. player_resources = Column(Boolean, server_default='f', default=False)
  303. state_resources = Column(Boolean, server_default='f', default=False)
  304. items = Column(Boolean, server_default='f', default=False)
  305. class PlayerMarketStat(Base):
  306. """Model for market stat"""
  307. __tablename__ = 'player_market_stat'
  308. id = Column(Integer, primary_key=True)
  309. item_type = Column(SmallInteger)
  310. price = Column(Integer)
  311. amount = Column(BigInteger)
  312. half_t_average = Column(Integer)
  313. one_t_average = Column(Integer)
  314. two_t_average = Column(Integer)
  315. five_t_average = Column(Integer)
  316. total_offers = Column(Integer)
  317. player_id = Column(BigInteger, ForeignKey('player.id'))
  318. player = relationship(
  319. 'Player',
  320. backref=backref('player_market_stats', lazy='dynamic')
  321. )
  322. market_track_id = Column(Integer, ForeignKey('market_track.id'))
  323. market_track = relationship(
  324. 'MarketTrack',
  325. backref=backref('player_market_stats', lazy='dynamic')
  326. )
  327. class StateMarketStat(Base):
  328. """Model for market stat"""
  329. __tablename__ = 'state_market_stat'
  330. id = Column(Integer, primary_key=True)
  331. item_type = Column(SmallInteger)
  332. price = Column(Integer)
  333. amount = Column(BigInteger)
  334. region_id = Column(Integer, ForeignKey('region.id'))
  335. region = relationship(
  336. 'Region',
  337. backref=backref('region_market_stats', lazy='dynamic')
  338. )
  339. market_track_id = Column(Integer, ForeignKey('market_track.id'))
  340. market_track = relationship(
  341. 'MarketTrack',
  342. backref=backref('state_market_stats', lazy='dynamic')
  343. )
  344. class TelegramAccount(Base):
  345. """Model for Telegram account"""
  346. __tablename__ = 'telegram_account'
  347. id = Column(BigInteger, primary_key=True)
  348. name = Column(String)
  349. registration_date = Column(DateTime)
  350. class TelegramHandle(Base):
  351. """Model for Telegram handle"""
  352. __tablename__ = 'telegram_handle'
  353. id = Column(Integer, primary_key=True)
  354. handle = Column(String)
  355. registration_date = Column(DateTime)
  356. telegram_account_id = Column(BigInteger, ForeignKey('telegram_account.id'))
  357. telegram_account = relationship(
  358. 'TelegramAccount',
  359. backref=backref('account_handles', lazy='dynamic')
  360. )
  361. class PlayerTelegram(Base):
  362. """Model for belongs to"""
  363. __tablename__ = 'player_telegram'
  364. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  365. telegram_id = Column(BigInteger, ForeignKey('telegram_account.id'), primary_key=True)
  366. from_date_time = Column(DateTime, primary_key=True)
  367. until_date_time = Column(DateTime)
  368. class TelegramVerification(Base):
  369. """Model for Telegram verification"""
  370. __tablename__ = 'telegram_verification'
  371. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  372. telegram_id = Column(BigInteger, ForeignKey('telegram_account.id'), primary_key=True)
  373. code = Column(String)
  374. date_time = Column(DateTime)
  375. confirmed = Column(Boolean, server_default='f', default=False)
  376. class Donation(Base):
  377. """Model for donation"""
  378. __tablename__ = 'donation'
  379. id = Column(Integer, primary_key=True)
  380. amount = Column(BigInteger)
  381. resources = Column(SmallInteger)
  382. date_time = Column(DateTime)
  383. player_id = Column(BigInteger, ForeignKey('player.id'))
  384. player = relationship(
  385. 'Player',
  386. backref=backref('donations', lazy='dynamic')
  387. )
  388. fund_id = Column(BigInteger, ForeignKey('fund.id'))
  389. fund = relationship(
  390. 'Fund',
  391. backref=backref('donations', lazy='dynamic')
  392. )
  393. class DonationTrigger(Base):
  394. """Donation trigger"""
  395. __tablename__ = 'table_name'
  396. id = Column(Integer, primary_key=True)
  397. resource_type = Column(Integer)
  398. from_date_time = Column(DateTime)
  399. until_date_time = Column(DateTime)
  400. class Fund(Base):
  401. """Model for fund"""
  402. __tablename__ = 'fund'
  403. id = Column(Integer, primary_key=True)
  404. name = Column(String)
  405. player_id = Column(BigInteger, ForeignKey('player.id'))
  406. player = relationship(
  407. 'Player',
  408. backref=backref('funds', lazy='dynamic')
  409. )
  410. region_id = Column(Integer, ForeignKey('region.id'))
  411. region_track = relationship(
  412. 'Region',
  413. backref=backref('funds', lazy='dynamic')
  414. )
  415. class Balance(Base):
  416. """Model for balance"""
  417. __tablename__ = 'balance'
  418. fund_id = Column(Integer, ForeignKey('fund.id'))
  419. fund_track = relationship(
  420. 'Fund',
  421. backref=backref('funds', lazy='dynamic')
  422. )
  423. amount = Column(Integer)
  424. resource = Column(SmallInteger)
  425. class Transfer(Base):
  426. """Model for transfer"""
  427. __tablename__ = 'transfer'
  428. from_region_id = Column(Integer, ForeignKey('region.id'))
  429. from_region_track = relationship(
  430. 'Region',
  431. backref=backref('export_transfer', lazy='dynamic')
  432. )
  433. to_region_id = Column(Integer, ForeignKey('region.id'))
  434. to_region_track = relationship(
  435. 'Region',
  436. backref=backref('import_transfer', lazy='dynamic')
  437. )