11from random import randint
22from cell import Cell
33from abc import ABC , abstractmethod
4+ import json
45
56
67class Model :
@@ -25,7 +26,7 @@ def __init__(self):
2526 self .playersMid = []
2627 self .playersHard = []
2728 self .playersRandom = []
28- self .save = Strategy
29+ self .save = SaveGame ()
2930
3031 """Getter and setter"""
3132
@@ -245,7 +246,7 @@ def next_mark(self, x, y):
245246 self .flagged_cells -= 1
246247
247248 def store_played_games (self ):
248- if self .MINES_MAX == 3 :
249+ if self .MINES_MAX == 10 :
249250 nome = self .controller .get_text_input ("Noobie mode" , "Insert your name" )
250251 time = self .seconds_from_start
251252 self .playersEasy .append (Player (nome , time ))
@@ -262,6 +263,9 @@ def store_played_games(self):
262263 time = self .seconds_from_start
263264 self .playersRandom .append (Player (nome , time ))
264265
266+ def save_state (self ):
267+ self .save .start_save (self )
268+
265269
266270class Player :
267271 def __init__ (self , nome , time ):
@@ -275,22 +279,84 @@ def get_time(self):
275279 return self .time
276280
277281
278- class Strategy (ABC ):
282+ """Fake interface in python """
283+
284+
285+ class TypeFile (ABC ):
279286 @abstractmethod
280- def create_file (self ):
287+ def create_file (self , model ):
281288 pass
282289
283290
284- class to_json (Strategy ):
285- def create_file (self ):
291+ """Concrete strategies implements"""
292+
293+
294+ class To_json (TypeFile ):
295+ def create_file (self , model ):
296+ print ("OK" )
297+ if len (model .playersEasy ) != 0 :
298+ easy_dict = {
299+ "GameMode" : "Easy" ,
300+ "NamePlayer" : "PlayTime"
301+ }
302+ for player in model .playersEasy :
303+ easy_dict [player .get_nome ()] = player .get_time ()
304+ json_obj = json .dumps (easy_dict , indent = 4 )
305+ with open ("historico.json" , "a" ) as outfile :
306+ outfile .write (json_obj )
307+ if len (model .playersMid ) != 0 :
308+ mid_dict = {
309+ "GameMode" : "Mid" ,
310+ "NamePlayer" : "PlayTime"
311+ }
312+ for player in model .playersMid :
313+ mid_dict [player .get_nome ()] = player .get_time ()
314+ json_obj = json .dumps (mid_dict , indent = 4 )
315+ with open ("historico.json" , "a" ) as outfile :
316+ outfile .write (json_obj )
317+ if len (model .playersHard ) != 0 :
318+ hard_dict = {
319+ "GameMode" : "Hard" ,
320+ "NamePlayer" : "PlayTime"
321+ }
322+ for player in model .playersHard :
323+ hard_dict [player .get_nome ()] = player .get_time ()
324+ json_obj = json .dumps (hard_dict , indent = 4 )
325+ with open ("historico.json" , "a" ) as outfile :
326+ outfile .write (json_obj )
327+ if len (model .playersRandom ) != 0 :
328+ rand_dict = {
329+ "GameMode" : "Random" ,
330+ "NamePlayer" : "PlayTime"
331+ }
332+ for player in model .playersRandom :
333+ rand_dict [player .get_nome ()] = player .get_time ()
334+ json_obj = json .dumps (rand_dict , indent = 4 )
335+ with open ("historico.json" , "a" ) as outfile :
336+ outfile .write (json_obj )
337+
338+
339+ class To_csv (TypeFile ):
340+ def create_file (self , model ):
286341 pass
287342
288343
289- class to_csv ( Strategy ):
290- def create_file (self ):
344+ class To_xml ( TypeFile ):
345+ def create_file (self , model ):
291346 pass
292347
293348
294- class to_xml (Strategy ):
295- def create_file (self ):
296- pass
349+ """Context to choose option"""
350+
351+
352+ class SaveGame :
353+ strategy : TypeFile
354+
355+ def __init__ (self , strategy : TypeFile = None ):
356+ if strategy is not None :
357+ self .strategy = strategy
358+ else :
359+ self .strategy = To_json ()
360+
361+ def start_save (self , model ):
362+ self .strategy .create_file (model )
0 commit comments