Maguro

sendGameMessage

Jun 10th, 2020
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. def sendGameMessage(ptype, message, user):
  2.     global CommandNumber
  3.     global UnconfirmedCommands
  4.  
  5.     try:
  6.         tree = ET.parse(BANKFILE) #reload to account for new changes
  7.         root = tree.getroot()
  8.  
  9.         #reset unconfirmed commands if it's a new game
  10.         for child in root:
  11.             if child.attrib['name'] == "NewGame":
  12.                 UnconfirmedCommands = dict()
  13.                 root.remove(child)
  14.                 break
  15.  
  16.        
  17.         #update unconfirmed commands with those that were executed
  18.         for child in root:
  19.             if child.attrib['name'] == "ExecutedCommands":
  20.                 for command in child:
  21.                     com_number = command.attrib['name']
  22.                     if com_number in UnconfirmedCommands:
  23.                         lock.acquire()
  24.                         del UnconfirmedCommands[com_number]
  25.                         lock.release()
  26.  
  27.                 root.remove(child) #removes the section
  28.                 break
  29.  
  30.  
  31.         #delete old commands if there are any
  32.         for child in root:
  33.             if child.attrib['name'] == 'Commands':
  34.                 root.remove(child)
  35.                 break
  36.  
  37.         #get unconfirmed commands
  38.         new_command_string = ""
  39.         for command in UnconfirmedCommands:
  40.             new_command_string = new_command_string + UnconfirmedCommands[command]
  41.        
  42.         #get new commmand
  43.         lock.acquire()
  44.         CommandNumber += 1
  45.         lock.release()
  46.         msg = message.replace('"',"''")
  47.         msg = html.escape(msg) # convert & → &
  48.         command_string = f'<Key name="{ptype} {str(CommandNumber)} #{user}"><Value string="{msg}"/></Key>'  
  49.         new_command_string = new_command_string + command_string
  50.         lock.acquire()      
  51.         UnconfirmedCommands[str(CommandNumber)] = command_string
  52.         lock.release()
  53.  
  54.         #create command section
  55.         new_command_string = f'<Section name="Commands">{new_command_string}</Section>'
  56.         root.append((ET.fromstring(new_command_string)))
  57.         tree.write(BANKFILE)
  58.  
  59.         #log
  60.         tree = ET.parse(BANKFILE)
  61.         root = tree.getroot()
  62.         for child in root:
  63.             if child.attrib['name'] == 'Commands':
  64.                 with open('XLM_debugging.txt', 'ab') as file:
  65.                     file.write((f"{UnconfirmedCommands}\n{ET.tostring(child, encoding='unicode')}\n-------\n").replace('<Section name="Commands">','').replace('</Section>','').encode('utf-8'))
  66.                 break      
  67.        
  68.     except Exception as e:
  69.         print(e,'ERROR – bank not loaded properly, message not sent')
  70.         with open('XLM_debugging.txt', 'a') as file:
  71.             file.write(f'\nERROR, bank not loaded properly\n',traceback.format_exc(),'\n')
Advertisement
Add Comment
Please, Sign In to add comment