damastodon.py 5.2 KB

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