-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.py
56 lines (49 loc) · 1.83 KB
/
Log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from datetime import datetime
from time import time
import numpy as np
class Log:
def __init__(self):
self.file_name = 'log.csv'
self.header = 'date,' \
'time,' \
'clicks,' \
'flags,' \
'correct_flags,' \
'incorrect_flags,'\
'doubt,' \
'player,' \
'win,' \
'lose,' \
'restart,' \
'quit,' \
'lines,' \
'columns,' \
'difficulty,' \
'GUI'
self.log = np.zeros(self.header.count(',') + 1, dtype=object)
self.log[0] = datetime.now()
self.generate_header()
self.beginning = 0
def start(self) -> None:
"""marca o horario que o jogo iniciou"""
self.beginning = time()
def save(self, new=False) -> None:
"""salva a partida atual no arquivo de log ja existente ou cria um novo"""
if new:
with open(self.file_name, mode='w') as file:
file.write(self.header + '\n')
else:
if self.beginning:
with open(self.file_name, mode='a+') as file:
self.log[1] = round(time() - self.beginning, 5)
content = ','.join([str(x) for x in self.log])
file.write(content + '\n')
def generate_header(self):
"""cria o cabecalho do arquivo de log"""
try:
with open(self.file_name):
pass
except FileNotFoundError:
self.save(new=True)
if __name__ == '__main__':
Log().save()