Created
December 25, 2023 21:14
-
-
Save graylan0/0bbf28cf5e6a8a62a40410ca04fdddb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiosqlite | |
import chess | |
from beem import Hive | |
from beem.comment import Comment | |
from beem.account import Account | |
from beem.discussions import Query, Discussions_by_created | |
class ChessGame: | |
def __init__(self, board=None): | |
self.board = board if board else chess.Board() | |
def make_move(self, move): | |
try: | |
self.board.push_san(move) | |
return True, str(self.board) | |
except ValueError: | |
return False, "Invalid move" | |
def game_over(self): | |
return self.board.is_game_over() | |
def get_fen(self): | |
return self.board.fen() | |
@staticmethod | |
def from_fen(fen): | |
board = chess.Board(fen) | |
return ChessGame(board) | |
class HiveChess: | |
def __init__(self, config, db_path): | |
self.config = config | |
self.hive = Hive(nodes=config["hive_nodes"], keys=[config["posting_key"]]) | |
self.account = config["account_name"] | |
self.db_path = db_path | |
async def initialize_db(self): | |
async with aiosqlite.connect(self.db_path) as db: | |
await db.execute('''CREATE TABLE IF NOT EXISTS games ( | |
game_id TEXT PRIMARY KEY, | |
state TEXT, | |
player_white TEXT, | |
player_black TEXT, | |
status TEXT | |
)''') | |
await db.commit() | |
async def post_game(self, game_id, title, body, tags): | |
try: | |
post = self.hive.post(title=title, body=body, author=self.account, tags=tags) | |
return post | |
except Exception as e: | |
print(f"Error posting game: {e}") | |
return None | |
async def post_move(self, game_id, move): | |
try: | |
game_post = Comment(f"@{self.account}/{game_id}", blockchain_instance=self.hive) | |
move_comment = game_post.reply(body=move, author=self.account) | |
return move_comment | |
except Exception as e: | |
print(f"Error posting move: {e}") | |
return None | |
async def challenge_player(self, opponent, game_id, title, body, tags): | |
challenge_body = f"{body}\n\n---\n\nChallenge to: @{opponent}" | |
try: | |
post = self.hive.post(title=title, body=challenge_body, author=self.account, tags=tags) | |
return post | |
except Exception as e: | |
print(f"Error challenging player: {e}") | |
return None | |
async def fetch_updates(self): | |
try: | |
query = Query(limit=10, tag=self.config["chess_tag"], start_author=self.account) | |
discussions = Discussions_by_created(query, blockchain_instance=self.hive) | |
for post in discussions: | |
await self.process_post(post) | |
except Exception as e: | |
print(f"Error fetching updates: {e}") | |
async def process_post(self, post): | |
try: | |
game_id = post["permlink"] | |
move = post["body"] | |
game_state = await self.load_game_state(game_id) | |
if game_state: | |
chess_game = ChessGame.from_fen(game_state) | |
valid_move, _ = chess_game.make_move(move) | |
if valid_move: | |
new_state = chess_game.get_fen() | |
await self.save_game_state(game_id, new_state) | |
except Exception as e: | |
print(f"Error processing post: {e}") | |
async def save_game_state(self, game_id, state): | |
async with aiosqlite.connect(self.db_path) as db: | |
await db.execute("INSERT INTO games (game_id, state) VALUES (?, ?)", (game_id, state)) | |
await db.commit() | |
async def load_game_state(self, game_id): | |
async with aiosqlite.connect(self.db_path) as db: | |
cursor = await db.execute("SELECT state FROM games WHERE game_id = ?", (game_id,)) | |
row = await cursor.fetchone() | |
return row[0] if row else None | |
async def main(): | |
config = { | |
"hive_nodes": ["https://api.hive.blog"], | |
"account_name": "your_account", | |
"posting_key": "your_posting_key", | |
"chess_tag": "chess_game" | |
} | |
hive_chess = HiveChess(config, "chess.db") | |
await hive_chess.initialize_db() | |
while True: | |
await hive_chess.fetch_updates() | |
await asyncio.sleep(60) | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment