damastodon.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/python3
  2. from mastodon import Mastodon
  3. import draughts_engine as dama
  4. import login
  5. import pickle
  6. import random
  7. import os
  8. import time
  9. import re
  10. import sys
  11. api_url = sys.argv[1]
  12. save_position = "/tmp"
  13. CLEANR = re.compile('<.*?>')
  14. def cleanHTML(raw):
  15. cleanText = re.sub(CLEANR, '',raw)
  16. return cleanText
  17. def check_message(notification):
  18. account = notification["account"]["acct"]
  19. content = cleanHTML(notification["status"]["content"])[6:]
  20. saves = os.listdir(save_position)
  21. #print(content) #DEBUG only
  22. if content.lower() == "help":
  23. mastodon.status_post("Hello @"+account+",\nchallenge an user by writing to me\nCHALL <USERNAME>\nEx. \"CHALL @someone@mastdn.inst.wxyz\"\nThe challenger takes WHITE and begins the match.\nFor movements and jumps, write the coords separated by spaces.\nEx. \"A4 B5\" (normal movement) or \"A4 C6 D8\" (double jump)\nQUIT ends the match.\nCommands are NOT case sensitive..",visibility="direct")
  24. return
  25. if not os.path.exists(save_position+account):
  26. if content[:5].lower() == "sfida":
  27. file_save_white = [sv for sv in saves if account in sv]
  28. file_save_black = [sv for sv in saves if content[:6] in sv]
  29. if len(file_save_white) > 0:
  30. mastodon.status_post("Hello @"+account+",\n you're already playing a match")
  31. return
  32. elif len(file_save_black):
  33. mastodon.status_post("Hello @"+content[7:]+",\n is already playing a match")
  34. return
  35. else:
  36. open(save_position+account,"w").close()
  37. ident = mastodon.status_post("Hello @"+content[7:]+",\n@"+account+" challenged you to a match of draughts! Anzwer \n@"+account+" OK\n to accept the chellenge or \n@"+account+" NO\n to cancel.",visibility="direct")
  38. return
  39. elif content.split(" ")[1].lower() == "ok":
  40. os.symlink(content.split(" ")[0][1:],save_position+account)
  41. board = dama.init_board()
  42. with open(account,"wb") as f:
  43. pickle.dump("@"+account,f)
  44. pickle.dump(content.split(" ")[0],f)
  45. pickle.dump(False,f)
  46. pickle.dump(board,f)
  47. mastodon.status_post("◾: @"+account+" ◽: "+content.split(" ")[0]+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct")
  48. return
  49. elif content.split(" ")[1].lower() == "no":
  50. os.remove(save_position+content.split(" ")[0][1:])
  51. mastodon.status_post(account+" you cancelled the challenge from "+content.split(" ")[0],visibility="direct")
  52. return
  53. else:
  54. mastodon.status_post("Hello @"+account+" I can't understand your commend or you're not in a match.\nWrite HELP to see the list of available commands.",visibility="direct")
  55. return
  56. else:
  57. with open(save_position+account,"rb") as f:
  58. black = pickle.load(f)
  59. white = pickle.load(f)
  60. turn = pickle.load(f)
  61. board = pickle.load(f)
  62. if content.lower() == "quit":
  63. os.remove(save_position+black[1:])
  64. os.remove(save_position+white[1:])
  65. mastodon.status_post(black+" "+white+" the match was cancelled.")
  66. return
  67. if (black == "@"+account and turn == 1) or (white == "@"+account and turn == 0):
  68. board = dama.valid_move(content.lower(),turn,board)
  69. if board == -1:
  70. mastodon.status_post("@"+account+"\nInvalid move.",visibility="direct")
  71. return
  72. else:
  73. with open(save_position+account,"wb") as f:
  74. turn = not turn
  75. pickle.dump(black,f)
  76. pickle.dump(white,f)
  77. pickle.dump(turn,f)
  78. pickle.dump(board,f)
  79. if turn == 0:
  80. colour = "◽"
  81. else:
  82. colour = "◾"
  83. winner = dama.checkWin(board)
  84. if winner == (False,False):
  85. mastodon.status_post("◾: "+black+" ◽: "+white+" turn "+colour+"\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct")
  86. return
  87. else:
  88. if winner == (True,False):
  89. winner_t = "WHITE"
  90. else:
  91. winner_t = "BLACK"
  92. os.remove(save_position+black[1:])
  93. os.remove(save_position+white[1:])
  94. mastodon.status_post("◾: "+black+" ◽: "+white+"\n"+winner_t+" WINS!\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct")
  95. return
  96. else:
  97. mastodon.status_post("@"+account+"\nIt's not your turn.",visibility="direct")
  98. return
  99. if __name__ == "__main__":
  100. if api_url[:4] != "http":
  101. print("Invalid address.")
  102. quit()
  103. mastodon = login.login(api_url)
  104. while True:
  105. time.sleep(10)
  106. for x in mastodon.notifications():
  107. check_message(x)
  108. mastodon.notifications_dismiss(x["id"])