refactor seat.py
This commit is contained in:
parent
4c2d899a90
commit
d8734a7ab2
173
cli/seat.py
173
cli/seat.py
@ -5,55 +5,87 @@ from random import choice
|
|||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
|
||||||
def load():
|
class Seat():
|
||||||
with open('seats.txt','r',encoding="utf-8") as f:
|
def __init__(self, seats=[]):
|
||||||
seats = json.load(f)
|
if len(seats) == 0:
|
||||||
return seats
|
self.seats = self.load()
|
||||||
|
self.requirements = self.load_requirements()
|
||||||
|
else:
|
||||||
|
self.seats = seats
|
||||||
|
self.requirements = self.load_requirements()
|
||||||
|
|
||||||
def save(seats):
|
def load(self):
|
||||||
with open('seats.txt','w',encoding="utf-8") as n:
|
with open('seats.txt', 'r', encoding="utf-8") as f:
|
||||||
n.write(str(seats))
|
seats = json.load(f)
|
||||||
|
return seats
|
||||||
|
|
||||||
|
def load_requirements(self):
|
||||||
|
req = dict()
|
||||||
|
with open('requirements.txt', 'w', encoding="utf-8") as req_f:
|
||||||
|
for user in self.query():
|
||||||
|
if user['steps'].strip() != "":
|
||||||
|
req[str(user['no'])] = (user['no'], user['direction'], int(user['steps']))
|
||||||
|
req_f.write("{0} {1} {2}\n".format(user['no'], user['direction'], int(user['steps'])))
|
||||||
|
return req
|
||||||
|
|
||||||
def query():
|
def save(self):
|
||||||
client = MongoClient('127.0.0.1', 27017)
|
with open('seats.txt','w',encoding="utf-8") as n:
|
||||||
db = client['210-seats']
|
n.write(str(self.seats))
|
||||||
collection = db['users']
|
|
||||||
|
|
||||||
data = collection.find().sort("no", )
|
def query(self):
|
||||||
for user in data:
|
client = MongoClient('127.0.0.1', 27017)
|
||||||
yield user
|
db = client['210-seats']
|
||||||
|
collection = db['users']
|
||||||
|
|
||||||
def apply_requirements(seats, requirements):
|
data = collection.find().sort("no", )
|
||||||
funcs = {"f": shiftUp,"b": shiftDown, "l": shiftLeft, "r": shiftRight, "fr": shiftRightUp, "br": shiftRightDown, "fl": shiftLeftUp, "bl": shiftLeftDown} # f: forward, b: backward, l:left, r: right
|
for user in data:
|
||||||
name = {"f": "Forward","b": "Backward", "l": "Left", "r": "Right", "fr": "Right Forward", "br": "Right Backward", "fl": "Left Forward", "bl": "Left Backward"}
|
yield user
|
||||||
order = sorted([int(n) for n in requirements.keys()])
|
|
||||||
luckier = choice(order)
|
|
||||||
print("Luckier: " + str(luckier) + "\n")
|
|
||||||
order = order[order.index(luckier):] + order[:order.index(luckier)]
|
|
||||||
|
|
||||||
for req in order:
|
def find(self, no, raw=False):
|
||||||
no, direction, steps = requirements[str(req)]
|
if raw:
|
||||||
row,col = find(seats, no)
|
modifier = 0
|
||||||
if col != None and row != None:
|
else:
|
||||||
if direction in funcs.keys():
|
modifier = 1 # start from 0 -> start from 1
|
||||||
funcs[direction](row, col, steps, seats)
|
|
||||||
print(no, name[direction], steps)
|
|
||||||
printArr(seats)
|
|
||||||
print()
|
|
||||||
return seats
|
|
||||||
|
|
||||||
def find(seats, no, raw=False):
|
for y in range(len(self.seats)):
|
||||||
if raw:
|
if no in self.seats[y]:
|
||||||
modifier = 0
|
col = self.seats[y].index(no)
|
||||||
else:
|
row = y
|
||||||
modifier = 1 # start from 0 -> start from 1
|
return (row + modifier, col + modifier)
|
||||||
|
return (None,None)
|
||||||
|
|
||||||
|
def switch(self, one, two):
|
||||||
|
# one: the first person; two: the second person
|
||||||
|
pos_one = self.find(one, True)
|
||||||
|
pos_two = self.find(two, True)
|
||||||
|
self.seats[pos_one[0]][pos_one[1]], self.seats[pos_two[0]][pos_two[1]] = self.seats[pos_two[0]][pos_two[1]], self.seats[pos_one[0]][pos_one[1]]
|
||||||
|
|
||||||
|
# logging switch
|
||||||
|
with open('switch.txt','a',encoding='utf-8') as log:
|
||||||
|
log.write("{} {}\n".format(str(one), str(two)))
|
||||||
|
|
||||||
|
return ((one, pos_two), (two, pos_one)) # return the result
|
||||||
|
|
||||||
|
def apply_requirements(self):
|
||||||
|
funcs = {"f": shiftUp, "b": shiftDown, "l": shiftLeft, "r": shiftRight, "fr": shiftRightUp, "br": shiftRightDown, "fl": shiftLeftUp, "bl": shiftLeftDown} # f: forward, b: backward, l:left, r: right
|
||||||
|
name = {"f": "Forward", "b": "Backward", "l": "Left", "r": "Right", "fr": "Right Forward", "br": "Right Backward", "fl": "Left Forward", "bl": "Left Backward"}
|
||||||
|
|
||||||
|
order = sorted([int(n) for n in self.requirements.keys()])
|
||||||
|
luckier = choice(order)
|
||||||
|
print("Luckier: " + str(luckier) + "\n")
|
||||||
|
order = order[order.index(luckier):] + order[:order.index(luckier)]
|
||||||
|
|
||||||
|
for req in order:
|
||||||
|
no, direction, steps = self.requirements[str(req)]
|
||||||
|
row, col = self.find(no)
|
||||||
|
if col != None and row != None:
|
||||||
|
if direction in funcs.keys():
|
||||||
|
funcs[direction](row, col, steps, self.seats)
|
||||||
|
print(no, name[direction], steps)
|
||||||
|
printArr(self.seats)
|
||||||
|
print()
|
||||||
|
return seats
|
||||||
|
|
||||||
for y in range(len(seats)):
|
|
||||||
if no in seats[y]:
|
|
||||||
col = seats[y].index(no)
|
|
||||||
row = y
|
|
||||||
return (row + modifier, col + modifier)
|
|
||||||
return (None,None)
|
|
||||||
|
|
||||||
help_msg = '''Please provide mode!
|
help_msg = '''Please provide mode!
|
||||||
generate - generate a random seat
|
generate - generate a random seat
|
||||||
@ -70,61 +102,42 @@ if __name__ == "__main__":
|
|||||||
else:
|
else:
|
||||||
mode = sys.argv[1]
|
mode = sys.argv[1]
|
||||||
if mode == "generate":
|
if mode == "generate":
|
||||||
seats = generateSeats()
|
seats = Seat(generateSeats())
|
||||||
printArr(seats)
|
printArr(seats.seats)
|
||||||
save(seats)
|
seats.save()
|
||||||
print("Done!")
|
print("Done!")
|
||||||
elif mode == "run" or mode == "debug":
|
elif mode == "run" or mode == "debug":
|
||||||
seats = load()
|
seats = Seat()
|
||||||
print("Original:")
|
print("Original:")
|
||||||
printArr(seats)
|
printArr(seats.seats)
|
||||||
|
|
||||||
# load requirements
|
|
||||||
requirements = dict()
|
|
||||||
with open('requirements.txt', 'w', encoding="utf-8") as req_f:
|
|
||||||
for user in query():
|
|
||||||
if user['steps'].strip() != "":
|
|
||||||
requirements[str(user['no'])] = (user['no'], user['direction'], int(user['steps']))
|
|
||||||
req_f.write("{0} {1} {2}\n".format(user['no'], user['direction'], int(user['steps'])))
|
|
||||||
|
|
||||||
apply_requirements(seats, requirements)
|
seats.apply_requirements()
|
||||||
|
|
||||||
print("Result:")
|
print("Result:")
|
||||||
printArr(seats)
|
printArr(seats.seats)
|
||||||
|
|
||||||
# save
|
# save
|
||||||
if mode == "run":
|
if mode == "run":
|
||||||
with open('seats.txt','w',encoding="utf-8") as n:
|
seats.save()
|
||||||
n.write(str(seats))
|
|
||||||
|
|
||||||
print("Done!")
|
print("Done!")
|
||||||
elif mode == "switch":
|
elif mode == "switch":
|
||||||
seats = load()
|
seats = Seat()
|
||||||
|
|
||||||
one = int(sys.argv[2])
|
one = int(sys.argv[2])
|
||||||
two = int(sys.argv[3])
|
two = int(sys.argv[3])
|
||||||
pos_one = find(seats, one, True)
|
result = seats.switch(one, two)
|
||||||
pos_two = find(seats, two, True)
|
for i in result:
|
||||||
seats[pos_one[0]][pos_one[1]], seats[pos_two[0]][pos_two[1]] = seats[pos_two[0]][pos_two[1]], seats[pos_one[0]][pos_one[1]]
|
print("{} postion {}".format(i[0], i[1]))
|
||||||
|
|
||||||
printArr(seats)
|
printArr(seats.seats)
|
||||||
save(seats)
|
seats.save()
|
||||||
|
|
||||||
# logging switch
|
|
||||||
log = open('switch.txt','a',encoding='utf-8')
|
|
||||||
log.write("{} {}\n".format(str(one), str(two)))
|
|
||||||
log.close()
|
|
||||||
elif mode == "export":
|
elif mode == "export":
|
||||||
requirements = dict()
|
seats = Seat()
|
||||||
with open('requirements.txt', 'w', encoding="utf-8") as req_f:
|
print(seats.requirements)
|
||||||
for user in query():
|
|
||||||
if user['steps'].strip() != "":
|
|
||||||
requirements[str(user['no'])] = (user['no'], user['direction'], int(user['steps']))
|
|
||||||
req_f.write("{0} {1} {2}\n".format(user['no'], user['direction'], int(user['steps'])))
|
|
||||||
|
|
||||||
print(requirements)
|
|
||||||
elif mode == "print":
|
elif mode == "print":
|
||||||
printArr(load())
|
seats = Seat()
|
||||||
|
printArr(seats.seats)
|
||||||
else:
|
else:
|
||||||
print(help_msg)
|
print(help_msg)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
Loading…
x
Reference in New Issue
Block a user