Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ktane-solver/process.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
213 lines (183 sloc)
7.09 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ktane.button | |
import ktane.compWires | |
import ktane.keypad | |
import ktane.knobs | |
import ktane.maze | |
import ktane.memory | |
import ktane.morse | |
import ktane.password | |
import ktane.simonsays | |
import ktane.wires | |
import ktane.wireSeq | |
import ktane.words | |
from word2number import w2n | |
modules = ["button","complicated","keypad","knobs","maze","memory","morse","password","simon","wires","wire sequence","words"] | |
###############################START OF DATA##################################### | |
def write_empty(sessionID): # Returns true if not exsist already, else false | |
data_file = open("data", "a+") | |
if sessionID not in data_file: | |
data_file.write("\n"+sessionID+","+"None,None") # abcdefghi,None,None | |
data_file.close() | |
return True | |
else: | |
data_file.close() | |
return False | |
def del_ID(sessionID): # Delete an entry in the file if it matches the current ID | |
with open("data", "r+") as f: | |
d = f.readlines() | |
f.seek(0) | |
for i in d: | |
if sessionID not in i: | |
f.write(i) | |
f.truncate() | |
def check_ID(sessionID): # Returns true if the sessionID exsists | |
data_file = open("data", "r") | |
for entry in data_file: | |
if sessionID in entry: | |
return True | |
return False | |
def read_data(sessionID): # Returns sessionID, battNo and serial | |
with open("data", "r+") as f: | |
for line in f: | |
if sessionID in line: | |
battNo = line.split(",")[1] | |
serial = line.split(",")[2] | |
return battNo, serial | |
def write_data(sessionID, battNo=None, serial=None): | |
data_file = open("data", "a") # write either data based on which != None | |
if check_ID(sessionID): | |
del_ID(sessionID) # delete old data | |
data_file.write(sessionID+","+str(battNo)+","+str(serial)) | |
else: | |
write_empty(sessionID) # session did not exsist but adding session here | |
write_data(sessionID, battNo, serial) | |
################################END OF DATA#################################### | |
def checkMeta(sessionID): | |
if not check_ID(sessionID): | |
write_empty(sessionID) | |
return read_data(sessionID) | |
else: | |
return read_data(sessionID) | |
def module_handle(modName, modData, battNo, serial): | |
if modName == "button": | |
colour = modData[0] | |
label = modData[1] | |
if len(modData) > 2: | |
led = modData[2] | |
tempBat = battNo | |
if not tempBat.isdigit(): | |
tempBat = w2n.word_to_num(battNo) | |
if len(modData) > 2: | |
action = ktane.button.start(tempBat, colour, label, led) | |
else: | |
action = ktane.button.start(tempBat, colour, label) | |
print(action) | |
if action == "hold": | |
print("should return") | |
return {"fulfillmentText": "Hold the button. Release as follows. Blue strip. 4 in the timer. Yellow strip. 5 in the timer. Anything else, 1 in the timer."} | |
elif action == "click": | |
return {"fulfillmentText": "Press and quickly release the button"} | |
else: | |
return action # raise error | |
elif modName == "complicated": | |
pass | |
elif modName == "keypad": | |
ktane.keypad.start(modData) | |
elif modName == "knobs": | |
return ktane.knobs.start(modData) | |
elif modName == "maze": | |
new_data = [] | |
for d in data: | |
if d == "to" or d == "two": | |
d = "two" | |
new_data.append(d) | |
data = new_data | |
indi_one = str(w2n.word_to_num(data[0])) + "," + str(w2n.word_to_num(data[1])) | |
indi_two = str(w2n.word_to_num(data[2])) + "," + str(w2n.word_to_num(data[3])) | |
pos = str(w2n.word_to_num(data[4])) + "," + str(w2n.word_to_num(data[5])) | |
goal = str(w2n.word_to_num(data[6])) + "," + str(w2n.word_to_num(data[7])) | |
return ktane.maze.start(indi_one, indi_two, pos, goal) | |
elif modName == "memory": | |
pass | |
elif modName == "morse": | |
sequence = [] | |
words = data.split(" ") | |
for word in words: | |
if word == "dot": | |
sequence.append(".") | |
elif word == "dash": | |
sequence.append("-") | |
elif word == "next": | |
sequence.append(" ") | |
sequence = ["-"] * (len(sequence) * 2 - 1) | |
ktane.morse.start("".join(sequence)) | |
elif modName == "password": | |
return ktane.password.start(data) | |
elif modName == "simon": | |
if len(modData) > 1: | |
strikes = modData[1] # says should be second word | |
return ktane.simonsays.start(modData, strikes) | |
else: | |
return {"fulfillmentText": "Make sure to say Simon Says followed by the number of strikes"} | |
elif modName == "wires": | |
return ktane.wires.start(serial, data) | |
elif modName == "sequence": | |
pass # cant be implamented | |
elif modName == "words": | |
pass # cant be implamented | |
else: | |
return {"fulfillmentText": "Not a module. Try another?"} | |
def start_proc(req, text, sessionID): | |
text = text.lower() | |
if " " not in text: | |
command = text | |
data = [] | |
else: | |
text = text.split(" ") | |
command = text[0] | |
data = text[1:] | |
# Check for batt and serial in file - checkMeta() | |
battNo, serial = checkMeta(sessionID) | |
# Tell user the current saved bomb details | |
if command == "echo": | |
if "batteries" in data: | |
if battNo == "None": | |
return {"fulfillmentText": "You have not set the amount of batteries"} | |
else: | |
return {"fulfillmentText": "You have " + str(battNo) + " batteries"} | |
elif "serial" in data or "cereal" in data or "label" in data: | |
if serial == "None": | |
return {"fulfillmentText": "You have not set the serial"} | |
else: | |
temp = " ".join(serial.split()) | |
return {"fulfillmentText": "Your serial is " + str(temp)} | |
else: | |
return {"fulfillmentText": "What do you want to hear?"} | |
# Let the user set the bomb details | |
elif command == "batteries": | |
if data != []: | |
battNo = data[0] | |
if battNo == "to" or battNo == "too": | |
battNo = "two" | |
if battNo == "for": | |
battNo = "four" | |
if battNo == "ate": | |
battNo = "eight" | |
write_data(sessionID, battNo, serial) | |
return {"fulfillmentText": "Okay, stored " + str(battNo) + " batteries"} | |
else: | |
return {"fulfillmentText": "You didn't say an amount of batteries"} | |
elif command == "serial" or command == "cereal" or command == "label": | |
if data != []: | |
serial = data[0:] | |
serial = "".join(serial) | |
serial = serial.strip() | |
serial = serial.upper() | |
write_data(sessionID, battNo, serial) | |
return {"fulfillmentText": "Okay, stored the serial " + str(serial)} | |
else: | |
return {"fulfillmentText": "You didn't specify a serial"} | |
elif command in modules: | |
return module_handle(command, data, battNo, serial) | |
else: | |
return {"fulfillmentText": "Sorry, I didn't get that"} |