<commit_before>import asyncio
import websockets
import json
import time


class Player():
    players = {}
    next_id = 0

    @staticmethod
    def by_id(player_id):
        return Player.players[player_id]

    @staticmethod
    def remove(player):
        del Player.players[player.id]

    def __init__(self, ws):
        self.id = Player.next_id
        self.socket = ws
        self.room = None
        self.number = 0
        self.name = "Неизвестный"
        self.wants_replay = False
        self.last_activity = time.time()
        self.disconnect = False
        Player.players[self.id] = self
        Player.next_id += 1

    def join(self, room):
        self.room = room
        self.number = self.room.add_player(self)


class Field():
    def __init__(self):
        self.matrix = [[0, 0, 0] for i in range(3)]
        self.win_event = lambda who, how: None

    def at(self, x, y):
        return self.matrix[x][y]

    def set(self, x, y, pl):
        if self.at(x, y) == 0:
            self.matrix[x][y] = pl
            return True
        else:
            return False

    def check_for_win(self):
        how = []

        for x in range(0, 3):
            if self.at(x, 0) == self.at(x, 1) == self.at(x, 2) != 0:
                for y in range(0, 3):
                    how.append((x, y))
                return how

        for y in range(0, 3):
            if self.at(0, y) == self.at(1, y) == self.at(2, y) != 0:
                for x in range(0, 3):
                    how.append((x, y))
                return how

        if self.at(0, 0) == self.at(1, 1) == self.at(2, 2) != 0:
            return [(0, 0), (1, 1), (2, 2)]

        if self.at(2, 0) == self.at(1, 1) == self.at(0, 2) != 0:
            return [(2, 0), (1, 1), (0, 2)]

        return False


class Room():
    rooms = {}

    @staticmethod
    def by_name(name):
        if name in Room.rooms:
            return Room.rooms[name]
        else:
            return None

    @staticmethod
    def remove(room):
        del Room.rooms[room.name]

    def __init__(self, name, password):
        self.name = name
        self.first_player = None
        self.second_player = None
        self.fields = [[Field(), Field(), Field()] for i in range(3)]
        self.main_field = Field()
        self.who_goes = 1
        self.started = False
        self.ended = False
        self.password = password
        Room.rooms[name] = self

        for player_id in Player.players:
            Packet.send(Packet9RoomsList({}, Player.players[player_id]))

    def add_player(self, player):
        if self.first_player is None:
            self.first_player = player
            return 1
        else:
            self.second_player = player
            Packet.send(Packet3GameStarted({}, self.first_player))
            Packet.send(Packet3GameStarted({}, player))
            self.started = True
            for player_id in Player.players:
                Packet.send(Packet9RoomsList({}, Player.players[player_id]))
            return 2

    def other(self, player):
        if player == self.first_player:
            return self.second_player
        else:
            return self.first_player

    def set(self, field_x, field_y, x, y, pl):
        if not self.started:
            return [0]
        if self.ended:
            return [0]
        if self.who_goes != pl.number:
            return [0]
        if self.main_field.at(field_x, field_y) != 0:
            return [0]
        if not self.fields[field_x][field_y].set(x, y, pl.number):
            return [0]

        if self.who_goes == 1:
            self.who_goes = 2
        else:
            self.who_goes = 1

        win = self.fields[field_x][field_y].check_for_win()
        if not win:
            return [1]
        else:
            self.main_field.set(field_x, field_y, pl.number)
            win_main = self.main_field.check_for_win()
            if win_main:
                self.ended = True
                return [3, win, win_main]
            else:
                return [2, win]

    def wants_replay(self, player):
        player.wants_replay = True
        if not (self.first_player.wants_replay and self.second_player.wants_replay):
            if player == self.first_player:
                Packet.send(Packet8OtherWantsReplay({}, self.second_player))
            else:
                Packet.send(Packet8OtherWantsReplay({}, self.first_player))<commit_msg>Server supports draws (client doesn't support it)<commit_after>import asyncio
import websockets
import json
import time


class Player():
    players = {}
    next_id = 0

    @staticmethod
    def by_id(player_id):
        return Player.players[player_id]

    @staticmethod
    def remove(player):
        del Player.players[player.id]

    def __init__(self, ws):
        self.id = Player.next_id
        self.socket = ws
        self.room = None
        self.number = 0
        self.name = "Неизвестный"
        self.wants_replay = False
        self.last_activity = time.time()
        self.disconnect = False
        Player.players[self.id] = self
        Player.next_id += 1

    def join(self, room):
        self.room = room
        self.number = self.room.add_player(self)


class Field():
    def __init__(self):
        self.matrix = [[0, 0, 0] for i in range(3)]
        self.win_event = lambda who, how: None

    def at(self, x, y):
        return self.matrix[x][y]

    def set(self, x, y, pl):
        if self.at(x, y) == 0:
            self.matrix[x][y] = pl
            return True
        else:
            return False

    def check_for_win(self):
        how = []

        for x in range(3):
            if self.at(x, 0) == self.at(x, 1) == self.at(x, 2) != 0:
                for y in range(3):
                    how.append((x, y))
                return how

        for y in range(3):
            if self.at(0, y) == self.at(1, y) == self.at(2, y) != 0:
                for x in range(3):
                    how.append((x, y))
                return how

        if self.at(0, 0) == self.at(1, 1) == self.at(2, 2) != 0:
            return [(0, 0), (1, 1), (2, 2)]

        if self.at(2, 0) == self.at(1, 1) == self.at(0, 2) != 0:
            return [(2, 0), (1, 1), (0, 2)]

        draw = False

        for x in range(3):
            for y in range(3):
                if self.at(x, y) == 0:
                    draw = True

        if draw:
            return -1

        return False


class Room():
    rooms = {}

    @staticmethod
    def by_name(name):
        if name in Room.rooms:
            return Room.rooms[name]
        else:
            return None

    @staticmethod
    def remove(room):
        del Room.rooms[room.name]

    def __init__(self, name, password):
        self.name = name
        self.first_player = None
        self.second_player = None
        self.fields = [[Field(), Field(), Field()] for i in range(3)]
        self.main_field = Field()
        self.who_goes = 1
        self.started = False
        self.ended = False
        self.password = password
        Room.rooms[name] = self

        for player_id in Player.players:
            Packet.send(Packet9RoomsList({}, Player.players[player_id]))

    def add_player(self, player):
        if self.first_player is None:
            self.first_player = player
            return 1
        else:
            self.second_player = player
            Packet.send(Packet3GameStarted({}, self.first_player))
            Packet.send(Packet3GameStarted({}, player))
            self.started = True
            for player_id in Player.players:
                Packet.send(Packet9RoomsList({}, Player.players[player_id]))
            return 2

    def other(self, player):
        if player == self.first_player:
            return self.second_player
        else:
            return self.first_player

    def set(self, field_x, field_y, x, y, pl):
        if not self.started:
            return [0]
        if self.ended:
            return [0]
        if self.who_goes != pl.number:
            return [0]
        if self.main_field.at(field_x, field_y) != 0:
            return [0]
        if not self.fields[field_x][field_y].set(x, y, pl.number):
            return [0]

        if self.who_goes == 1:
            self.who_goes = 2
        else:
            self.who_goes = 1

        win = self.fields[field_x][field_y].check_for_win()
        if not win:
            return [1]
        else:
            set_number = pl.number
            if win == -1:
                set_number = -1

            self.main_field.set(field_x, field_y, set_number)
            win_main = self.main_field.check_for_win()
            if win_main:
                self.ended = True
                if win_main == -1:
                    return [3, -1, -1]
                return [3, win, win_main]
            else:
                return [2, win]

    def wants_replay(self, player):
        player.wants_replay = True
        if not (self.first_player.wants_replay and self.second_player.wants_replay):
            if player == self.first_player:
                Packet.send(Packet8OtherWantsReplay({}, self.second_player))
            else:
                Packet.send(Packet8OtherWantsReplay({}, self.first_player))
{'fraction_non_alphanumeric': 0.11574624446125581, 'fraction_numerical': 0.01621095860801902, 'mean_word_length': 1.6001685866816522, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7071961', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2859, 'n_words': 737}