Feldherren

Fate/Extra Pattern Recorder/Matcher

Mar 31st, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. import os
  2. import re
  3.  
  4. # A - Attack
  5. # B - Break
  6. # G - Guard
  7. # S - Skill
  8. # - - Wait
  9. # ? - Unknown
  10. # 6 commands
  11.  
  12. # Allow user to input enemy name/identifier. Check if a f exists with that name in the directory the Python f was opened from; if there is a f of that name, open it. If there isn't, create it.
  13. # From there, allow user to add new patterns, attempt to match patterns, view all patterns, or save and quit that f.
  14. # Show how many sequences are recorded for the selected enemy.
  15.  
  16. # When adding a pattern, show valid codes above text entry bit. '?' is not a valid code at this point. There are 6 commands in a sequence.
  17. # Check string for validity - each character should match '(?ABGS-)'
  18. # Confirm adding.
  19.  
  20. # When matching, allow 'Unknown' code in addition to the rest above.
  21. # Use regex. Construct a string: '?' should become '.'
  22.  
  23. exit = False
  24. input = "0"
  25. inputMatch = re.compile("(A|B|G|S|-)(A|B|G|S|-)(A|B|G|S|-)(A|B|G|S|-)(A|B|G|S|-)(A|B|G|S|-)")
  26.  
  27. while True:
  28.     print "Feldherren's Useful Fate/Extra Enemy Sequence Matching Tool\n"
  29.     print "1. Enter enemy name"
  30.     print "2. Quit"
  31.     input = raw_input("> ")
  32.    
  33.     if input == "1":
  34.         input = "0"
  35.         print ""
  36.         filename = raw_input("Enemy name: ")
  37.         if os.path.exists(os.path.realpath(__file__)[:-28] + filename + ".txt"):
  38.             print "Opening " + filename + ".txt"
  39.             f = open(filename+'.txt', 'r+')
  40.         else:
  41.             print "Enemy f " + filename + ".txt not found - creating new file"
  42.             f = open(filename+'.txt', 'a+')
  43.             f.close()
  44.             f = open(filename+'.txt', 'r+')
  45.  
  46.         while True:
  47.             print "Enemy file: " + filename + ".txt\n"
  48.             print "1. Add new sequence"
  49.             print "2. Match sequence"
  50.             print "3. Output all sequences"
  51. #           print "4. Remove sequence"
  52.             print "4. Return to main menu"
  53.             input = raw_input("> ")
  54.             if input == "1":
  55.                 input = "0"
  56.                 print "Enter new enemy action sequence. Valid characters:"
  57.                 print "A: Attack"
  58.                 print "B: Break"
  59.                 print "G: Guard"
  60.                 print "S: Skill"
  61.                 print "-: Wait"
  62.                 print ""
  63.                 s = raw_input("Sequence: ")
  64.                 if inputMatch.match(s) is not None:
  65.                     # checking for duplicates
  66.                     f = open(filename+'.txt', 'r+')
  67.                     l = f.readline()
  68.                     f.close
  69.                     l = l.split(",")
  70.                     duplicate = False
  71.                     for sequence in l:
  72.                         if s == sequence:
  73.                             duplicate = True
  74.                     if duplicate != True:
  75.                         f = open(filename+'.txt', 'a+')
  76.                         f.write(s + "," )
  77.                         f.close()
  78.                     else:
  79.                         print "Sequence already exists."
  80.                 else:
  81.                     print "Error: entered sequence is not valid pattern.\n"
  82.             elif input == "2":
  83.                 input = "0"
  84.                 print "Enter enemy action sequence to match. Valid characters:"
  85.                 print "A: Attack"
  86.                 print "B: Break"
  87.                 print "G: Guard"
  88.                 print "S: Skill"
  89.                 print "-: Wait"
  90.                 print "?: Unknown"
  91.                 print ""
  92.                 input = raw_input("Sequence: ")
  93.                
  94.                 matchSequence = input.replace("?", ".")
  95.                 matchSequence = re.compile(matchSequence)
  96.                
  97.                 f = open(filename+'.txt', 'r+')
  98.                 l = f.readline()
  99.                 f.close
  100.                 l = l.split(",")
  101.                 for sequence in l:
  102.                     if matchSequence.match(sequence) is not None:
  103.                         print sequence
  104.                     input = "0"
  105.             elif input == "3":
  106.                 input = "0"
  107.                 print "All sequences for " + filename + ":"
  108.                 f = open(filename+'.txt', 'r+')
  109.                 l = f.readline()
  110.                 f.close
  111.                 l = l.split(",")
  112.                 for sequence in l:
  113.                     print sequence
  114. #           elif input == "4": # If you get curious, this was my attempt at removing
  115. #               input = "0"
  116. #               print "Enter enemy action sequence to remove. Valid characters:"
  117. #               print "A: Attack"
  118. #               print "B: Break"
  119. #               print "G: Guard"
  120. #               print "S: Skill"
  121. #               print "-: Wait"
  122. #               print ""
  123. #               s = raw_input("Sequence: ")
  124. #               if inputMatch.match(s) is not None:
  125. #                   f = open(filename+'.txt', 'r+')
  126. #                   l = f.readline()
  127. #                   f.close
  128. #                   l = l.split(",")
  129. #                   if s in l:
  130. #                       l.pop(l.index(s))
  131. #                  
  132. #                   for i in l: # here it starts petering out as I started wondering '...wait, does he really need this?' This would have made one comma-separated string from the list l again
  133. #                      
  134. #                   f = open(filename+'.txt', 'r+') # opening a file in mode 'r+' as opposed to 'a+' will simply have it write over all the contents. I basically went 'ehh' when I realised I'd have to make one string of the list for it to work.
  135. #                  
  136. #               else:
  137. #                   print "Error: entered sequence is not valid pattern.\n"
  138.                
  139.             elif input == "4":
  140.                 input = "0"
  141.                 break
  142.                
  143.     elif input == "2":
  144.         input = "0"
  145.         break
  146.  
  147. #print ""
Advertisement
Add Comment
Please, Sign In to add comment