GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BotLi/event_handler.py at main · Torom/BotLi

Expand file tree

/

event_handler.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
81 lines (69 loc) · 3.61 KB
/

event_handler.py

Copy path

File metadata and controls

executable file
·
81 lines (69 loc) · 3.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import asyncio
from typing import Any
from api import API
from botli_dataclasses import Challenge
from challenge_validator import ChallengeValidator
from config import Config
from game_manager import GameManager
class EventHandler:
def __init__(self, api: API, config: Config, username: str, game_manager: GameManager) -> None:
self.api = api
self.username = username
self.game_manager = game_manager
self.challenge_validator = ChallengeValidator(config, game_manager)
self.last_challenge_event: dict[str, Any] | None = None
async def run(self) -> None:
event_queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue()
self._task = asyncio.create_task(self.api.get_event_stream(event_queue))
while event := await event_queue.get():
match event["type"]:
case "challenge":
if event["challenge"]["challenger"]["name"] == self.username:
continue
self.last_challenge_event = event["challenge"]
self._print_challenge_event(event["challenge"])
if decline_reason := self.challenge_validator.get_decline_reason(event["challenge"]):
print(123 * "‾")
await self.api.decline_challenge(event["challenge"]["id"], decline_reason)
continue
self.game_manager.add_challenge(Challenge.from_challenge_event(event["challenge"]))
print("Challenge added to queue.")
print(123 * "‾")
case "gameStart":
self.game_manager.on_game_started(event["game"])
case "gameFinish":
continue
case "challengeDeclined":
opponent_name = event["challenge"]["destUser"]["name"]
if opponent_name == self.username:
continue
print(f"{opponent_name} declined challenge: {event['challenge']['declineReason']}")
case "challengeCanceled":
if event["challenge"]["challenger"]["name"] == self.username:
continue
self.game_manager.remove_challenge(Challenge.from_challenge_event(event["challenge"]))
self._print_challenge_event(event["challenge"])
print("Challenge has been canceled.")
print(123 * "‾")
case _:
print(event)
@staticmethod
def _print_challenge_event(challenge_event: dict[str, Any]) -> None:
id_str = f"ID: {challenge_event['id']}"
title = challenge_event["challenger"].get("title") or ""
name = challenge_event["challenger"]["name"]
rating = challenge_event["challenger"]["rating"]
provisional = "?" if challenge_event["challenger"].get("provisional") else ""
challenger_str = f"Challenger: {title}{' ' if title else ''}{name} ({rating}{provisional})"
time_control = challenge_event["timeControl"]
if "show" in time_control:
tc_str = f"TC: {time_control['show']}"
elif "daysPerTurn" in time_control:
tc_str = f"TC: {time_control['daysPerTurn']}d"
else:
tc_str = "TC: Unlimited"
rated_str = "Rated" if challenge_event["rated"] else "Casual"
color_str = f"Color: {challenge_event['color'].capitalize()}"
variant_str = f"Variant: {challenge_event['variant']['name']}"
print(123 * "_")
print(" • ".join([id_str, challenger_str, tc_str, rated_str, color_str, variant_str]))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.