-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamesaker.py
40 lines (35 loc) · 1.35 KB
/
namesaker.py
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
import logging
import threading
import copy
import time
from database import Database as db
class NameSaker(threading.Thread):
"""
Class responsible for finding out the bot's name.
"""
def __init__(self, bot, server):
threading.Thread.__init__(self)
self.__server = server
self.__bot = bot
def run(self):
named = False
deadline = time.time() + 5
while not named and time.time() < deadline:
data = self.__server.getData()
data_copy = copy.copy(data)
for peer_address, name in data_copy.items():
try:
if self.__bot.connection().getpeername() == peer_address:
self.__bot.putName(name)
logging.info(f'Namesaker: Bot introduced himself as {name}')
del data[peer_address]
if not db().botExist(name):
logging.info(f'Namesaker: This bot is a new challenger!')
db().insertBot(name)
elo = db().getBot(name)[-1]
logging.info(f'Namesaker: This bot ELO rating is {elo}!')
named = True
except OSError:
named = True
if not named:
logging.info('Bot stay without name...')