db.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. ########################################################################
  2. # Hello Worlds - Libre 3D RPG game.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Hello Worlds.
  6. #
  7. # Hello Worlds is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Hello Worlds is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import os
  22. import json
  23. global AssetsPath
  24. global Spells
  25. global NPCs
  26. global Maps
  27. global Players
  28. global Classes
  29. class GenericData: # dict
  30. empty = {}
  31. def __init__(self, _id=-1, data=None):
  32. self._id = _id
  33. if data: self._load(_id, data)
  34. else: self.setEmpty()
  35. def _load(self, _id, data):
  36. self._id = _id
  37. self._data = data
  38. @property
  39. def id(self): return self._id
  40. @id.setter
  41. def id(self, value): self._id = value
  42. def data(self): return self._data
  43. def columns(self): return len(self.data())
  44. def setEmpty(self):
  45. self._data = self.empty.copy()
  46. class GenericDataList(GenericData): # list
  47. empty = []
  48. def __init__(self, _id=-1, data=None):
  49. GenericData.__init__(self, _id=_id, data=data)
  50. """Classes
  51. """
  52. class ClassData(GenericDataList):
  53. empty = ['new',[]]
  54. def __init__(self, _id=-1, data=None):
  55. GenericData.__init__(self, _id, data=data)
  56. """
  57. 0 name
  58. 1 default spells/actions
  59. """
  60. @property
  61. def name(self): return self._data[0]
  62. @property
  63. def spells(self): return self._data[1] #[Spells[spellId] for spellId in self._data[1]]
  64. @name.setter
  65. def name(self, value): self._data[0] = value
  66. @spells.setter
  67. def spells(self, value):
  68. self._data[1] = value
  69. """CharacterData - base for PlayerData and NPCData
  70. """
  71. class StatData:
  72. def __init__(self, data):
  73. self._data = data
  74. def data(self): return self._data
  75. @property
  76. def max(self): return self._data[0]
  77. @property
  78. def value(self): return self._data[1]
  79. @max.setter
  80. def max(self, value): self._data[0] = value
  81. @value.setter
  82. def value(self, value): self._data[1] = value
  83. class StatsData(GenericData):
  84. empty = {
  85. 'level' : (10, 0),
  86. 'health' : (80, 80), # max, value
  87. 'energy' : (50, 50),
  88. 'strength' : (10, 1),
  89. 'stamina' : (10, 1),
  90. 'resistance': (25, 2),
  91. 'reflexes' : (10, 0.5),
  92. 'spitit' : (10, 0)
  93. }
  94. def __init__(self, _id=-1, data=None):
  95. GenericData.__init__(self, _id, data=data)
  96. """
  97. 0 level
  98. 1 health
  99. 2 energy
  100. 3 strength
  101. 4 stamina
  102. 5 resistance
  103. 6 reflexes
  104. 7 spitit
  105. """
  106. @property
  107. def level(self): return StatData(self._data['level'])
  108. @property
  109. def health(self): return StatData(self._data['health'])
  110. @property
  111. def energy(self): return StatData(self._data['energy'])
  112. @property
  113. def strength(self): return StatData(self._data['strength'])
  114. @property
  115. def stamina(self): return StatData(self._data['stamina'])
  116. @property
  117. def resistance(self): return StatData(self._data['resistance'])
  118. @property
  119. def reflexes(self): return StatData(self._data['reflexes'])
  120. @property
  121. def spitit(self): return StatData(self._data['spirit'])
  122. @level.setter
  123. def level(self, value): self._data['level'] = value
  124. @health.setter
  125. def health(self, value): self._data['health'] = value
  126. @energy.setter
  127. def energy(self, value): self._data['energy'] = value
  128. @strength.setter
  129. def strength(self, value): self._data['strength'] = value
  130. @stamina.setter
  131. def stamina(self, value): self._data['stamina'] = value
  132. @resistance.setter
  133. def resistance(self, value): self._data['resistance'] = value
  134. @reflexes.setter
  135. def reflexes(self, value): self._data['reflexes'] = value
  136. @spitit.setter
  137. def spitit(self, value): self._data['spirit'] = value
  138. class PlayerStatsData(StatsData):
  139. empty = {
  140. 'level' : (10, 0),
  141. 'health' : (80, 80), # max, value
  142. 'energy' : (50, 50),
  143. 'strength' : (10, 1),
  144. 'stamina' : (10, 1),
  145. 'resistance': (25, 2),
  146. 'reflexes' : (10, 0.5),
  147. 'spitit' : (10, 0),
  148. 'experience': (0,100)
  149. }
  150. def __init__(self, _id=-1, data=None):
  151. StatsData.__init__(self, _id, data)
  152. """
  153. 8 experience
  154. """
  155. @property
  156. def experience(self): return StatData(self._data['experience'])
  157. @experience.setter
  158. def experience(self, value): self._data['experience'] = value
  159. class CharacterData(GenericDataList):
  160. """
  161. 0 name
  162. 1 dirName
  163. 2 file
  164. 3 animations {}
  165. 4 stats {}
  166. 5 collisionShape {}
  167. 6 speciesId
  168. """
  169. empty = [
  170. 'new',
  171. 'dir-name',
  172. '.egg',
  173. {'idle':'.egg', 'walk':'.egg', 'run':'.egg', 'jump':'.egg'},
  174. StatsData.empty,
  175. [1,1],
  176. 1
  177. ]
  178. def __init__(self, _id=-1, data=None):
  179. GenericDataList.__init__(self, _id, data=data)
  180. @property
  181. def name(self): return self._data[0]
  182. @property
  183. def dirName(self): return self._data[1]
  184. @property
  185. def file(self): return self._data[2]
  186. @property
  187. def animations(self): return self._data[3]
  188. @property
  189. def stats(self): return StatsData(data = self._data[4])
  190. @property
  191. def collisionShape(self): return self._data[5]
  192. @property
  193. def speciesId(self): return self._data[6]
  194. @name.setter
  195. def name(self, value): self._data[0] = value
  196. @dirName.setter
  197. def dirName(self, value): self._data[1] = value
  198. @file.setter
  199. def file(self, value): self._data[2] = value
  200. @animations.setter
  201. def animations(self, value): self._data[3] = value
  202. @stats.setter
  203. def stats(self, value): self._data[4] = value
  204. @collisionShape.setter
  205. def collisionShape(self, value): self._data[5] = value
  206. @speciesId.setter
  207. def speciesId(self, value): self._data[6] = value
  208. """Players
  209. """
  210. class PlayerData(CharacterData):
  211. empty = [
  212. 'new',
  213. 'dir-name',
  214. '.egg',
  215. {'idle':'.egg', 'walk':'.egg', 'run':'.egg', 'jump':'.egg'},
  216. PlayerStatsData.empty,
  217. [1,1],
  218. 1
  219. ]
  220. def __init__(self, _id=-1, data=None):
  221. CharacterData.__init__(self, _id, data)
  222. @property
  223. def filePath(self): return os.path.join(AssetsPath.players, self.dirName, self.file)
  224. @property
  225. def stats(self):
  226. return PlayerStatsData(data = self._data[4])
  227. """Maps
  228. """
  229. class MapData(GenericDataList):
  230. empty = ['New map','map','.egg','.egg']
  231. def __init__(self, _id=-1, data=None):
  232. GenericData.__init__(self, _id, data)
  233. """
  234. 0 name
  235. 1 dir-name
  236. 2 file-name.egg
  237. 3 ortho-file-name.egg
  238. """
  239. @property
  240. def name(self): return self._data[0]
  241. @property
  242. def dirName(self): return self._data[1]
  243. @property
  244. def file(self): return self._data[2]
  245. @property
  246. def filePath(self): return os.path.join(AssetsPath.maps, self.dirName, self.file)
  247. @property
  248. def orthoFile(self): return self._data[3]
  249. @property
  250. def orthoFilePath(self): return os.path.join(AssetsPath.maps, self.dirName, self.orthoFile)
  251. @property
  252. def spawns(self): return Spawns(self.dirName)
  253. @name.setter
  254. def name(self, value): self._data[0] = value
  255. @dirName.setter
  256. def dirName(self, value): self._data[1] = value
  257. @file.setter
  258. def file(self, value): self._data[2] = value
  259. @orthoFile.setter
  260. def orthoFile(self, value): self._data[3] = value
  261. """Spawn data
  262. """
  263. class GenericSpawnData(GenericDataList):
  264. empty = [1,0,0,0,0]
  265. def __init__(self, _id=-1, data=None):
  266. GenericDataList.__init__(self, _id, data=data)
  267. """
  268. 0 characterId (npc or player-character)
  269. 1 x
  270. 2 y
  271. 3 z
  272. 4 o
  273. """
  274. #if data: self._load(_id, data)
  275. @property
  276. def characterId(self): return self._data[0]
  277. @property
  278. def x(self): return self._data[1]
  279. @property
  280. def y(self): return self._data[2]
  281. @property
  282. def z(self): return self._data[3]
  283. @property
  284. def orientation(self): return self._data[4]
  285. @property
  286. def pos(self): return (self.x, self.y, self.z)
  287. @characterId.setter
  288. def characterId(self, value): self._data[0] = value
  289. @x.setter
  290. def x(self, value): self._data[1] = value
  291. @y.setter
  292. def y(self, value): self._data[2] = value
  293. @z.setter
  294. def z(self, value): self._data[3] = value
  295. @orientation.setter
  296. def orientation(self, value): self._data[4] = value
  297. class NPCSpawnData(GenericSpawnData):
  298. empty = GenericSpawnData.empty + [10]
  299. def __init__(self, _id=-1, data=None):
  300. GenericSpawnData.__init__(self, _id, data)
  301. """
  302. 5 respawnTime - in seconds
  303. """
  304. @property
  305. def respawnTime(self): return self._data[5]
  306. @respawnTime.setter
  307. def respawnTime(self, value): self._data[5] = value
  308. """NPCs
  309. """
  310. class NPCData(CharacterData):
  311. def __init__(self, _id=-1, data=None):
  312. CharacterData.__init__(self, _id, data)
  313. @property
  314. def filePath(self): return os.path.join(AssetsPath.npcs, self.dirName, self.file)
  315. """Spells
  316. """
  317. class SpellData(GenericDataList):
  318. empty = [1,0,1,1,"new","unknown.png",""]
  319. def __init__(self, _id=-1, data=None):
  320. GenericDataList.__init__(self, _id, data=data)
  321. """
  322. 0 castTime
  323. 1 coolDown
  324. 2 damage
  325. 3 energy
  326. 4 name
  327. 5 icon
  328. 6 desc
  329. """
  330. def iconPath(self):
  331. # return full icon path
  332. return os.path.join(str(AssetsPath), 'icons/', self.icon)
  333. @property
  334. def castTime(self): return self._data[0]
  335. @property
  336. def coolDown(self): return self._data[1]
  337. @property
  338. def damage(self): return self._data[2]
  339. @property
  340. def energy(self): return self._data[3]
  341. @property
  342. def name(self): return self._data[4]
  343. @property
  344. def icon(self): return self._data[5]
  345. @property
  346. def desc(self): return self._data[6]
  347. @castTime.setter
  348. def castTime(self, value): self._data[0] = value
  349. @coolDown.setter
  350. def coolDown(self, value): self._data[1] = value
  351. @damage.setter
  352. def damage(self, value): self._data[2] = value
  353. @energy.setter
  354. def energy(self, value): self._data[3] = value
  355. @name.setter
  356. def name(self, value): self._data[4] = value
  357. @icon.setter
  358. def icon(self, value): self._data[5] = value
  359. @desc.setter
  360. def desc(self, value): self._data[6] = value
  361. class _AssetsPath:
  362. def __init__(self, path='assets/'):
  363. self._path = path
  364. self._callbacks = []
  365. def __str__(self): return self._path
  366. def __repr__(self): return self._path
  367. @property
  368. def icons(self): return os.path.join(str(self), 'icons/')
  369. @property
  370. def npcs(self): return os.path.join(str(self), 'creatures/')
  371. @property
  372. def maps(self): return os.path.join(str(self), 'maps/')
  373. @property
  374. def players(self): return os.path.join(str(self), 'characters/')
  375. @property
  376. def widgets(self): return os.path.join(str(self), 'widgets/')
  377. def joinPath(self, other):
  378. return os.path.join(str(self), other)
  379. def set(self, path):
  380. self._path = path
  381. for cb in self._callbacks: cb()
  382. def addReloadCallback(self, cb):
  383. self._callbacks.append(cb)
  384. class Table:
  385. def __init__(self, _file, _type):
  386. self._file = _file
  387. self._filePath = self.filePath()
  388. self._fileValid = False
  389. self._data = {}
  390. self._type = _type
  391. AssetsPath.addReloadCallback(self.__load)
  392. def columns(self): return len(self._type.empty) + 1 # +1 for id
  393. def __iter__(self):
  394. for _id, item in self._data.items(): yield item
  395. def __getitem__(self, key):
  396. return self._data.get(str(key))
  397. def reload(self): self.__load()
  398. def __load(self):
  399. self._data = {}
  400. if os.path.isfile(self.filePath()):
  401. print(" [OK] Found {0}".format(self._file))
  402. with open(self.filePath()) as f:
  403. try:
  404. data = json.load(f)
  405. for _id, d in data.items():
  406. self._data.update({_id : self._type(_id, d)})
  407. except json.decoder.JSONDecodeError as err:
  408. print("\t! Empty or corrupt file! Error: {0}".format(err))
  409. else:
  410. print(" [XX] Not found {0}".format(self._file))
  411. def __save(self):
  412. data = {}
  413. for item in self:
  414. data.update({item.id : item.data()})
  415. with open(self.filePath(), 'w') as fp:
  416. json.dump(data, fp)
  417. def _getNewId(self):
  418. for i in range(1, 1024):
  419. if str(i) not in self._data: return str(i)
  420. def filePath(self): return os.path.join(str(AssetsPath), self._file)
  421. def new(self, data=[]):
  422. if not data: data = self._type.empty
  423. _id = self._getNewId()
  424. self.edit(_id, data)
  425. return _id
  426. def edit(self, _id, newData):
  427. self._data.update({_id : self._type(_id, newData)})
  428. def remove(self, _id):
  429. self._data.pop(_id)
  430. def save(self): self.__save()
  431. class _Spells(Table):
  432. def __init__(self):
  433. Table.__init__(self, _file='spells.json', _type=SpellData)
  434. class _NPCs(Table):
  435. def __init__(self):
  436. Table.__init__(self, _file='npcs.json', _type=NPCData)
  437. class _Maps(Table):
  438. def __init__(self):
  439. Table.__init__(self, _file='maps.json', _type=MapData)
  440. class _Players(Table):
  441. def __init__(self):
  442. Table.__init__(self, _file='players.json', _type=PlayerData)
  443. class _Classes(Table):
  444. def __init__(self):
  445. Table.__init__(self, _file='classes.json', _type=ClassData)
  446. class Spawns(Table):
  447. def __init__(self, mapDir):
  448. _file = 'spawns.json'
  449. self._subPath = os.path.join(mapDir, _file)
  450. Table.__init__(self, _file=_file, _type=NPCSpawnData)
  451. self.reload()
  452. def filePath(self): return os.path.join(AssetsPath.maps, self._subPath)
  453. AssetsPath = _AssetsPath()
  454. Spells = _Spells()
  455. NPCs = _NPCs()
  456. Maps = _Maps()
  457. Players = _Players()
  458. Classes = _Classes()