-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKill_From_Pool.py
120 lines (103 loc) · 3.59 KB
/
Kill_From_Pool.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
import sys
import os
def pool_reader(poolname): # reading the existing pools
out = []
lines = open(poolname).readlines()[:] #Reading the current condition
backup = open('.pool.bk','w') #A backup file
for line in lines:
words = line.split()
name = words[0] #The name of the monster
n = int(words[2]) #How many of that monster are there
toput = [name,n] + words[3:] #Don't touch the rest
out.append(toput)
backup.write(line)
backup.flush()
backup.close()
return out
def asker(poolname): #This is the class
pool = pool_reader(poolname)
while True: #Printg the use option
for i in range (len(pool)):
index = i + 1
name = pool[i][0]
n = pool[i][1]
line = '{index}){name} ({n})'.format(index = index,
name=name,
n=n)
print line
print '0)Done'
inp = raw_input('pick one> ') #Waiting for user input
try:
inp = int(inp) #Getting the index
except ValueError: #The user gave a non number input
print 'Use integer in the range 0, {r}'.format(r=len(pool))
continue
if inp <0 or inp > len(pool): #The user gave an out of range input
print 'Use integer in the range 0, {r}'.format(r=len(pool))
continue
if inp == 0: #End the loop, return the new pool
return pool
else: #kill one monster
pool[inp-1][1] -= 1
pool[inp-1][1] = max(pool[inp-1][1],0)
def store_pool(pool,poolname):
out = open (poolname,'w')
for p in pool:
name = p[0]
n = p[1]
line = '{name} = {n}'.format(name=name,n=n)
for w in p[2:]:
line += ' {w}'.format(w=w)
line += '\n'
out.write(line)
out.flush()
out.close()
#We have written the new pool file, done
return
def ask_store_pool(pool,poolname):
oldpool = pool_reader(poolname)
lines = []
for i in range (len(oldpool)):
name = oldpool[i][0]
old_num = oldpool[i][1]
new_num = pool[i][1]
if old_num == new_num: #I wish to print only the new lines
continue
line = '{name}: {old} -> {new}'.format(name=name,
old = old_num,
new = new_num)
lines.append(line)
if len(lines) == 0:
print
print '\033[1mNothing has changed, leaving\033[0m'
print
exit()
print
print ('\033[1m')
print ('Changes:')
print ('\033[0m')
for line in lines:
print line
while True:
inp = raw_input('1)Save\n2)Cancel')
if not inp in ['1','2']:
print 'Please pick 1 or 2\n'
continue
if inp == '1':
return True
else:
return False
if __name__ == '__main__':
if len (sys.argv) == 1: #Checking for pool name in the input arguments
poolname ='pool.txt' #Use the default if there are none
else:
poolname = '{}.txt'.format(sys.argv[1])
if not os.path.isfile(poolname): #In the pool file inputed doesn't need txt extension
poolname = str(sys.argv[1])
if not os.path.isfile(poolname):
print 'couldn\'t find pool file named {}'.format(poolname)
exit()
pool = asker(poolname)
store = ask_store_pool(pool,poolname)
if store:
store_pool(pool,poolname)