DataManagment.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import omdb
  2. import os
  3. import pickle
  4. import random
  5. import json
  6. import copy
  7. from django.conf import settings
  8. class DataManagment:
  9. save_file = None
  10. def __init__(self):
  11. # Should check if the api is up
  12. self.load()
  13. def load(self):
  14. # Query omdb and create gamefile
  15. try:
  16. if __name__ == '__main__':
  17. f = open("/tmp/test", "rb")
  18. else:
  19. f = open(settings.SAVE_FILE, "rb")
  20. except Exception:
  21. self.save_file = self.init_save()
  22. else:
  23. self.save_file = pickle.load(f)
  24. f.close()
  25. def dump(self, save=False):
  26. try:
  27. if __name__ == '__main__':
  28. f = open("/tmp/test", "wb")
  29. else:
  30. f = open(settings.SAVE_FILE, "wb")
  31. except Exception:
  32. # TODO : Custom exeption for saving error
  33. raise Exception("Error while saving : data may be unavailable")
  34. if self.save_file == None:
  35. self.save_file = self.init_save()
  36. pickle.dump(self.save_file, f)
  37. f.close()
  38. f = open('/tmp/bidule', 'w')
  39. f.write(json.dumps(self.save_file))
  40. f.close()
  41. def load_default_settings(self):
  42. # Load the settings into the game object
  43. pass
  44. def init_save(self, name = ''):
  45. if __name__ == '__main__':
  46. c = [5, 5]
  47. else:
  48. c = [settings.START_X, settings.START_Y]
  49. save_file = {
  50. 'player' : {
  51. 'strength' : 1,
  52. 'dir' : 0,
  53. 'ball' : 5000,
  54. 'coord' : c,
  55. },
  56. 'movie' : {},
  57. }
  58. return {
  59. 'active' : -1,
  60. 'current' : save_file,
  61. 'curser' : 0,
  62. 0: None,
  63. 1: None,
  64. 2: None,
  65. }
  66. ### Getter
  67. def get_strength(self):
  68. return self.save_file['current']['player']['strength']
  69. def get_movie(self, id):
  70. # Return movie based on id
  71. save = self.save_file['current']
  72. try:
  73. return save['movie'][id]
  74. except Exception:
  75. return None
  76. def get_all_movie(self):
  77. return self.save_file['current']['movie']
  78. def get_movies_array(self):
  79. tab = []
  80. movies = self.save_file['current']['movie']
  81. for index, movie in enumerate(self.save_file['current']['movie']):
  82. tab.append(movie)
  83. return tab
  84. def get_movie_rand(self):
  85. tab = []
  86. for index, movie in enumerate(self.save_file['current']['movie']):
  87. if movie['catched'] == False:
  88. tab.append(index)
  89. if (len(tab) == 0):
  90. return None
  91. return random.choice(tab)
  92. def get_coord(self):
  93. save = self.save_file['current']
  94. return save['player']['coord']
  95. #def get_coord_percent(self):
  96. # coord = self.save_file['current']['player']['coord']
  97. # coord = [coord[0]/ settings.SIZE_X, coord[1] / settings.SIZE_Y]
  98. def get_coord_percentY(self):
  99. coord = self.save_file['current']['player']['coord']
  100. return coord[1] / settings.SIZE_Y * 87
  101. def get_coord_percentX(self):
  102. coord = self.save_file['current']['player']['coord']
  103. return coord[0] / settings.SIZE_X * 91
  104. def get_balls(self):
  105. return self.save_file['current']['player']['ball']
  106. def get_strength(self):
  107. return self.save_file['current']['player']['strength']
  108. def add_ball(self):
  109. if self.save_file['current']['player']['ball'] < 9:
  110. self.save_file['current']['player']['ball'] += 1
  111. def get_save_file(self):
  112. return self.save_file
  113. def get_movie_catched(self, id):
  114. n = 0
  115. for movie in self.save_file[id]['movie']:
  116. if movie['catched']:
  117. n += 1
  118. return n
  119. def get_movie_count(self, id):
  120. return len(self.save_file[id]['movie'])
  121. def del_ball(self):
  122. if self.save_file['current']['player']['ball'] > 0:
  123. self.save_file['current']['player']['ball'] -= 1
  124. # Setter
  125. def set_active_file(self, num):
  126. print("******* LOADING NEW SAVE *******")
  127. self.save_file['active'] = num
  128. self.save_file['current'] = self.save_file[num]
  129. def set_save(self, num):
  130. print("******* SAVING *******")
  131. #self.save_file['active'] = num
  132. #print(self.save_file[num]['player']['dir'])
  133. self.save_file[num] = copy.deepcopy(self.save_file['current'])
  134. #print(self.save_file[num]['player']['dir'])
  135. ### Model action
  136. def new_file(self, num):
  137. if __name__ == '__main__':
  138. c = [5, 5]
  139. else:
  140. c = [settings.START_X, settings.START_Y]
  141. self.save_file['current'] = {
  142. 'player' : {
  143. 'strength' : 1,
  144. 'dir' : 0,
  145. 'ball' : 5000,
  146. 'coord' : c,
  147. },
  148. 'movie' : {},
  149. }
  150. omdb.set_default('apikey', settings.OMDBAPI_KEY)
  151. movies = []
  152. for movie in settings.MOVIE_LIST:
  153. data = omdb.imdbid(movie)
  154. movies.append({
  155. 'name': data['title'],
  156. 'strength': int(float(data['imdb_rating'])),
  157. 'rating': data['imdb_rating'],
  158. 'actors': data['actors'],
  159. 'year': data['released'].split(' ')[2],
  160. 'image': data['poster'],
  161. 'director': data['director'],
  162. 'catched': False,
  163. })
  164. self.save_file['current']['movie'] = movies
  165. def strength_up(self):
  166. self.save_file['current']['player']['strength'] += 1
  167. def getdir(self):
  168. return self.save_file['current']['player']['dir']
  169. def setdir(self, dir):
  170. self.save_file['current']['player']['dir'] = dir
  171. def catch_movie(self, id):
  172. self.save_file['current']['movie'][id]['catched'] = True
  173. # Movement
  174. def go_up(self):
  175. if self.save_file['current']['player']['coord'][1] > 0:
  176. self.save_file['current']['player']['coord'][1] -= 1
  177. def go_down(self):
  178. if self.save_file['current']['player']['coord'][1] < settings.SIZE_Y:
  179. self.save_file['current']['player']['coord'][1] += 1
  180. def go_left(self):
  181. if self.save_file['current']['player']['coord'][0] > 0:
  182. self.save_file['current']['player']['coord'][0] -= 1
  183. def go_right(self):
  184. if self.save_file['current']['player']['coord'][0] < settings.SIZE_X:
  185. self.save_file['current']['player']['coord'][0] += 1
  186. def find_random(self):
  187. return (5 == random.randint(2, 8))
  188. if __name__ == '__main__':
  189. c = DataManagment()
  190. settings.configure()
  191. # Load without file
  192. c.load()
  193. c.set_active_file(0)
  194. print("Coord :", c.get_coord())
  195. print("Strength :", c.get_strength())
  196. c.strength_up()
  197. print("Strength up :", c.get_strength())
  198. c.dump()
  199. c.load()
  200. print("\nRound 2:\nCoord :", c.get_coord())
  201. print("Strength :", c.get_strength())
  202. c.strength_up()
  203. print("Strength up :", c.get_strength())
  204. c.go_up()
  205. print("Coord :", c.get_coord())
  206. c.go_up()
  207. c.go_up()
  208. c.go_up()
  209. c.go_up()
  210. print("Coord :", c.get_coord())
  211. c.go_up()
  212. c.go_up()
  213. c.go_up()
  214. print("Coord :", c.get_coord())
  215. print(c.get_movie_rand())
  216. #c.new_file(0)
  217. #m = c.get_movie(0)
  218. #print(m['name'], m['strength'], m['catched'])
  219. #c.catch_movie(0)
  220. #m = c.get_movie(0)
  221. #print(m['name'], m['strength'], m['catched'])