|
2 | 2 | from cell import Cell |
3 | 3 | from abc import ABC, abstractmethod |
4 | 4 | import json |
| 5 | +import csv |
5 | 6 |
|
6 | 7 |
|
7 | 8 | class Model: |
@@ -319,18 +320,55 @@ def create_file(self, model): |
319 | 320 | json_dict["Random"] = rand_dict |
320 | 321 |
|
321 | 322 | json_obj = json.dumps(json_dict, indent=4) |
322 | | - with open("historico.json", "a") as outfile: |
| 323 | + with open("historico.json", "w") as outfile: |
323 | 324 | outfile.write(json_obj) |
324 | 325 |
|
325 | 326 |
|
326 | 327 | class To_csv(TypeFile): |
327 | 328 | def create_file(self, model): |
328 | | - pass |
| 329 | + field_names = ['Nome', 'Tempo de Jogo'] |
| 330 | + results = [] |
| 331 | + if len(model.playersEasy) != 0: |
| 332 | + easy_dict = {} |
| 333 | + for player in model.playersEasy: |
| 334 | + easy_dict[player.get_nome()] = player.get_time() |
| 335 | + results.append(easy_dict) |
| 336 | + if len(model.playersRandom) != 0: |
| 337 | + rand_dict = {} |
| 338 | + for player in model.playersRandom: |
| 339 | + rand_dict[player.get_nome()] = player.get_time() |
| 340 | + results.append(rand_dict) |
| 341 | + with open('historico.csv', 'w') as csvfile: |
| 342 | + writer = csv.DictWriter(csvfile, fieldnames=field_names) |
| 343 | + writer.writeheader() |
| 344 | + writer.writerows(results) |
329 | 345 |
|
330 | 346 |
|
331 | 347 | class To_txt(TypeFile): |
332 | 348 | def create_file(self, model): |
333 | | - pass |
| 349 | + with open('historico.txt', 'w') as arquivo: |
| 350 | + arquivo.write('Histórico dos Jogadores da Sessão\n') |
| 351 | + arquivo.write('Nome - Tempo de Jogo\n') |
| 352 | + if len(model.playersEasy) != 0: |
| 353 | + arquivo.write('EASY:\n') |
| 354 | + for player in model.playersEasy: |
| 355 | + arquivo.write(str(player.get_nome()) + ' - ' + str(player.get_time()) + '\n') |
| 356 | + |
| 357 | + if len(model.playersMid) != 0: |
| 358 | + arquivo.write('MID:\n') |
| 359 | + for player in model.playersMid: |
| 360 | + arquivo.write(str(player.get_nome()) + ' - ' + str(player.get_time()) + '\n') |
| 361 | + |
| 362 | + if len(model.playersHard) != 0: |
| 363 | + arquivo.write('HARD:\n') |
| 364 | + for player in model.playersHard: |
| 365 | + arquivo.write(str(player.get_nome()) + ' - ' + str(player.get_time()) + '\n') |
| 366 | + |
| 367 | + if len(model.playersRandom) != 0: |
| 368 | + arquivo.write('RANDOM:\n') |
| 369 | + for player in model.playersRandom: |
| 370 | + arquivo.write(str(player.get_nome()) + ' - ' + str(player.get_time()) + '\n') |
| 371 | + arquivo.close() |
334 | 372 |
|
335 | 373 |
|
336 | 374 | """Context to choose option""" |
|
0 commit comments