File size: 2,195 Bytes
f01cd90
 
 
cd58f92
 
5de9e57
71114f0
9ed81f1
8e8100e
9ed81f1
0102145
71114f0
9ed81f1
8e8100e
 
aaa3dd0
17d9988
 
aaa3dd0
9ed81f1
aaa3dd0
039909f
892f0ea
5de9e57
17d9988
aaa3dd0
4b80d2e
03b8c54
80666a1
4b80d2e
236d051
19e5579
5de9e57
 
19e5579
 
71114f0
 
17d9988
 
71114f0
 
8e8100e
 
 
9ed81f1
9241e12
 
 
 
9ed81f1
5de9e57
9ed81f1
 
 
892f0ea
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
import logging

logger = logging.getLogger(__name__)


class Conversation:
    def __init__(self, game, engine, xhr, version, challenge_queue):
        self.game = game
        self.engine = engine
        self.xhr = xhr
        self.version = version
        self.challengers = challenge_queue

    command_prefix = "!"

    def react(self, line, game):
        logger.info(f'*** {self.game.url()} [{line.room}] {line.username}: {line.text.encode("utf-8")}')
        if line.text[0] == self.command_prefix:
            self.command(line, game, line.text[1:].lower())

    def command(self, line, game, cmd):
        if cmd == "commands" or cmd == "help":
            self.send_reply(line, "Supported commands: !wait (only applicable at the start of the game), !name, !howto, !eval, !queue")
        elif cmd == "wait" and game.is_abortable():
            game.ping(60, 120, 120)
            self.send_reply(line, "Waiting 60 seconds...")
        elif cmd == "name":
            name = game.me.name
            self.send_reply(line, f"{name} running Fairy-Owen (Lishogi-Bot v{self.version})")
        elif cmd == "howto":
            self.send_reply(line, "How to run: https://github.com/TheYoBots/Lishogi-Bot")
        elif cmd == "eval" and line.room == "spectator":
            stats = self.engine.get_stats()
            self.send_reply(line, ", ".join(stats))
        elif cmd == "eval":
            self.send_reply(line, "I don't tell that to my opponent, sorry.")
        elif cmd == "queue":
            if self.challengers:
                challengers = ", ".join([f"@{challenger.challenger_name}" for challenger in reversed(self.challengers)])
                self.send_reply(line, f"Challenge queue: {challengers}")
            else:
                self.send_reply(line, "No challenges queued.")

    def send_reply(self, line, reply):
        self.xhr.chat(self.game.id, line.room, reply)

    def send_message(self, room, message):
        if message:
            self.send_reply(ChatLine({"room": room}), message)


class ChatLine:
    def __init__(self, json):
        self.room = json.get("room")
        self.username = json.get("username")
        self.text = json.get("text")