damastodon.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. botname = "@damastodon "
  15. firow = "🔠1️⃣ 2️⃣ 3️⃣ 4️⃣ 5️⃣ 6️⃣ 7️⃣ 8️⃣\n"
  16. colsx = "🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 "
  17. def cleanHTML(raw):
  18. cleanText = re.sub(CLEANR, '',raw)
  19. return cleanText
  20. def check_message(notification):
  21. account = notification["account"]["acct"]
  22. try:
  23. content = cleanHTML(notification["status"]["content"])
  24. except KeyError:
  25. return
  26. content = content[len(botname):]
  27. saves = os.listdir(save_position)
  28. if content.lower() == "help":
  29. 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")
  30. return
  31. if not os.path.exists(save_position+account):
  32. try:
  33. challenged = notification["status"]["mentions"][1]["acct"]
  34. except:
  35. mastodon.status_post("Hello @"+account+" \n your request is not valid")
  36. return
  37. if content[:5].lower() == "chall":
  38. file_save_white = [sv for sv in saves if account in sv]
  39. file_save_black = [sv for sv in saves if challenged in sv]
  40. if len(file_save_white) > 0:
  41. mastodon.status_post("Hello @"+account+" \n you're already playing a match")
  42. return
  43. elif len(file_save_black):
  44. mastodon.status_post("Hello @"+account+" \n the user you challenged is already playing a match")
  45. return
  46. else:
  47. open(save_position+account,"w").close()
  48. ident = mastodon.status_post("Hello @"+challenged+" \n@"+account+" challenged you to a match of draughts! Answer \n@"+account+" OK\n to accept the chellenge or \n@"+account+" NO\n to cancel.",visibility="direct")
  49. return
  50. elif content.split(" ")[1].lower() == "ok":
  51. try:
  52. challenger = notification["status"]["mentions"][1]["acct"]
  53. except:
  54. mastodon.status_post("Hello @"+account+" \n your request is not valid")
  55. return
  56. os.symlink(save_position+challenger,save_position+account)
  57. board = dama.init_board()
  58. with open(save_position+account,"wb") as f:
  59. pickle.dump("@"+account,f)
  60. pickle.dump("@"+challenger,f)
  61. pickle.dump(False,f)
  62. pickle.dump(board,f)
  63. mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow=firow, column=colsx),visibility="direct")
  64. return
  65. elif content.split(" ")[1].lower() == "no":
  66. os.remove(save_position+content.split(" ")[0][1:])
  67. mastodon.status_post(account+" you cancelled the challenge from "+content.split(" ")[0],visibility="direct")
  68. return
  69. else:
  70. mastodon.status_post("Hello @"+account+" \nI can't understand your command or you're not in a match.\nWrite HELP to see the list of available commands.",visibility="direct")
  71. return
  72. else:
  73. with open(save_position+account,"rb") as f:
  74. black = pickle.load(f)
  75. white = pickle.load(f)
  76. turn = pickle.load(f)
  77. board = pickle.load(f)
  78. if content.lower() == "quit":
  79. os.remove(save_position+black[1:])
  80. os.remove(save_position+white[1:])
  81. mastodon.status_post(black+" "+white+" the match was cancelled.")
  82. return
  83. if (black == "@"+account and turn == 1) or (white == "@"+account and turn == 0):
  84. board = dama.valid_move(content.lower(),turn,board,inversion=True)
  85. if board == -1:
  86. mastodon.status_post("@"+account+" \nInvalid move.",visibility="direct")
  87. return
  88. else:
  89. with open(save_position+account,"wb") as f:
  90. turn = not turn
  91. pickle.dump(black,f)
  92. pickle.dump(white,f)
  93. pickle.dump(turn,f)
  94. pickle.dump(board,f)
  95. if turn == 0:
  96. colour = "◽"
  97. else:
  98. colour = "◾"
  99. winner = dama.checkWin(board)
  100. if winner == (False,False):
  101. mastodon.status_post("◾: "+black+" ◽: "+white+" turn "+colour+"\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow=firow, column=colsx),visibility="direct")
  102. return
  103. else:
  104. if winner == (True,False):
  105. winner_t = "WHITE"
  106. else:
  107. winner_t = "BLACK"
  108. os.remove(save_position+black[1:])
  109. os.remove(save_position+white[1:])
  110. 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")
  111. return
  112. else:
  113. mastodon.status_post("@"+account+" \nIt's not your turn.",visibility="direct")
  114. return
  115. if __name__ == "__main__":
  116. if api_url[:4] != "http":
  117. print("Invalid address.")
  118. quit()
  119. mastodon = login.login(api_url)
  120. while True:
  121. time.sleep(10)
  122. for x in mastodon.notifications():
  123. check_message(x)
  124. mastodon.notifications_dismiss(x["id"])