-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
139 lines (128 loc) · 4.69 KB
/
player.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
from turtle import update
Game1='Game1'
Game2='Game2'
Game3='Game3'
Game4='Game4'
Game5='Game5'
# from play_game import Game1
# from play_game import Player
class player:
def GetName(self):
self.name=input("Enter your name:")
if(os.path.isfile(f'{self.name}.txt')):
pass
else:
f=open('all_players.txt','a')
f.write(f'\n{self.name}')
f.close
def createOrUpdateFile(self,score,num):
if(os.path.isfile(f'{self.name}.txt')):
self.score=score
self.game=num
if self.game==1:
Update=self.update(self.game)
if Update != 0:
f=open(f'{self.name}.txt','r')
all_lines=f.readlines()
line=all_lines[Update-1]
lt=line.split()
prev_score=int(lt[3])
if(prev_score<int(self.score)):
self.Replace(lt[3],str(self.score))
f.close()
else:
f=open(f'{self.name}.txt','a+')
f.write(f'\nGame1 highest score {score}')
f.close()
elif self.game==2:
Update=self.update(self.game)
if Update != 0:
f=open(f'{self.name}.txt','r')
all_lines=f.readlines()
line=all_lines[Update-1]
lt=line.split()
prev_score=int(lt[3])
if(prev_score<int(self.score)):
self.Replace(lt[3],str(self.score))
f.close()
else:
f=open(f'{self.name}.txt','a+')
f.write(f'\nGame2 highest score {score}')
f.close()
elif self.game==3:
Update=self.update(self.game)
if Update != 0:
f=open(f'{self.name}.txt','r')
all_lines=f.readlines()
line=all_lines[Update-1]
lt=line.split()
prev_score=int(lt[3])
if(prev_score<int(self.score)):
self.Replace(lt[3],str(self.score))
f.close()
else:
f=open(f'{self.name}.txt','a+')
f.write(f'\nGame3 highest score {score}')
f.close()
elif self.game==4:
Update=self.update(self.game)
if Update != 0:
f=open(f'{self.name}.txt','r')
all_lines=f.readlines()
line=all_lines[Update-1]
lt=line.split()
prev_score=int(lt[3])
if(prev_score<int(self.score)):
self.Replace(lt[3],str(self.score))
f.close()
else:
f=open(f'{self.name}.txt','a+')
f.write(f'\nGame4 highest score {score}')
f.close()
else:
self.score=score
self.game=num
f=open(f'{self.name}.txt','a')
f.write(self.name)
if self.game == 1:
f.write("\nGame1 Highest Score ")
f.write(str(self.score))
f.close()
elif self.game == 2:
f.write("\nGame2 Highest Score ")
f.write(str(self.score))
f.close()
elif self.game == 3:
f.write("\nGame3 Highest Score ")
f.write(str(self.score))
f.close()
elif self.game == 4:
f.write("\nGame4 Highest Score ")
f.write(str(self.score))
f.close()
def Replace(self,search_text,replace_text):
self.search_text=search_text
self.replace_text=replace_text
with open(f'{self.name}.txt','r') as file:
data=file.read()
data=data.replace(self.search_text,self.replace_text)
with open(f'{self.name}.txt','w') as file:
file.write(data)
def update(self,num):
string1=f'Game{num}'
f=open(f'{self.name}.txt','r')
flag=0
index=0
for line in f:
index+=1
if string1 in line:
flag=1
break
if flag==0:
return 0
else:
return index
# Player=player()
# Player.GetName()
# print(Player.update(1))