<commit_before># -*- coding: utf-8 -*-
from collections import defaultdict
from hackarena.player import Player
from hackarena.spell import Spell
from hackarena.team import Team
from hackarena.messages import AllMainBroadcast
from hackarena.messages import FEMessages
from hackarena.messages import WelcomeBroadcast
from hackarena.messages import SendChatBroadcast
from hackarena.utilities import Utilities
from hackarena.constants import MAP_TILES_WIDTH
from hackarena.constants import MAP_TILES_HEIGHT
from hackarena.constants import MAP_TILE_SIZE
from hackarena.constants import MAP_OBSTACLES
from hackarena.constants import Spell as SpellConstants
from sockjs.tornado import SockJSConnection
import hackarena.constants
import itertools
import json
import time


class WebSocketHandler(SockJSConnection):

    clients = defaultdict(dict)
    teams = {}
    players = {}
    spells = []

    @property
    def player(self):
        return self.players[self.session_string]

    @player.setter
    def player(self, player):
        self.players[self.session_string] = player

    @property
    def room(self):
        try:
            return self.player.room
        except:
            return hackarena.constants.DEFAULT_ROOM

    @room.setter
    def room(self, room):
        self.player.room = room

    def on_open(self, info):
        self.session_string = Utilities.get_session_string(str(self.session))

        self.clients[self.room][self.session_string] = self
        WelcomeBroadcast(message='Welcome to HackArena!').send(self)
        WelcomeBroadcast(message='Welcome to HackArena! (to all)').broadcast_to_all(self)

    def on_close(self):
        self.teams[self.room][self.player.team].remove_player(self.player)

        self.broadcast_game_state()

        del self.players[self.session_string]
        del self.clients[self.room][self.session_string]

    def on_message(self, message):
        try:
            data = json.loads(message)
        except:
            print 'Received unsupported message type'
            return

        if data['type'] == FEMessages.FE_JOIN_ROOM:
            new_room = data['content']['room']
            self.player = Player(
                username=data['content']['username'],
                character_class=data['content']['characterClass'],
                team=data['content']['team'],
            )

            self.change_room(new_room)

            if new_room not in self.teams:
                self.teams[new_room] = {
                    'red': Team('red'),
                    'blue': Team('blue'),
                }

            self.teams[new_room][self.player.team].add_player(self.player)
            self.broadcast_game_state()

        if data['type'] == FEMessages.FE_HERO_MOVE:
            char_pos_x = self.player.position['x']
            char_pos_y = self.player.position['y']
            move_allowed, new_pos_x, new_pos_y = self.move_request(char_pos_x, char_pos_y, data['content']['direction'])
            self.player.position['x'] = new_pos_x
            char_pos_y = self.player.position['y'] = new_pos_y
            self.broadcast_game_state()

        if data['type'] == FEMessages.FE_HERO_SPELL:
            content = data['content']
            self.spell_request(content['spell_type'], content['position_x'], content['position_y'], content['direction'])
            self.broadcast_game_state()
            del self.spells[:]
            if self.teams[self.room]['red'].building_hp <= 0 or self.teams[self.room]['blue'].building_hp <= 0:
                del self.teams[self.room]
                self.broadcast_game_state()

        if data['type'] == FEMessages.FE_SEND_CHAT:
            SendChatBroadcast(
                username=data['content']['username'],
                message=data['content']['message']
            ).broadcast_to_all(self)


    def spell_request(self, spell_type, position_x, position_y, direction):
        if spell_type in self.player.available_spells:
            spell = Spell.create_spell(spell_type, position_x * MAP_TILE_SIZE, position_y * MAP_TILE_SIZE, direction)
<commit_msg>Whitespace fix.<commit_after># -*- coding: utf-8 -*-
from collections import defaultdict
from hackarena.player import Player
from hackarena.spell import Spell
from hackarena.team import Team
from hackarena.messages import AllMainBroadcast
from hackarena.messages import FEMessages
from hackarena.messages import WelcomeBroadcast
from hackarena.messages import SendChatBroadcast
from hackarena.utilities import Utilities
from hackarena.constants import MAP_TILES_WIDTH
from hackarena.constants import MAP_TILES_HEIGHT
from hackarena.constants import MAP_TILE_SIZE
from hackarena.constants import MAP_OBSTACLES
from hackarena.constants import Spell as SpellConstants
from sockjs.tornado import SockJSConnection
import hackarena.constants
import itertools
import json
import time


class WebSocketHandler(SockJSConnection):

    clients = defaultdict(dict)
    teams = {}
    players = {}
    spells = []

    @property
    def player(self):
        return self.players[self.session_string]

    @player.setter
    def player(self, player):
        self.players[self.session_string] = player

    @property
    def room(self):
        try:
            return self.player.room
        except:
            return hackarena.constants.DEFAULT_ROOM

    @room.setter
    def room(self, room):
        self.player.room = room

    def on_open(self, info):
        self.session_string = Utilities.get_session_string(str(self.session))

        self.clients[self.room][self.session_string] = self
        WelcomeBroadcast(message='Welcome to HackArena!').send(self)
        WelcomeBroadcast(message='Welcome to HackArena! (to all)').broadcast_to_all(self)

    def on_close(self):
        self.teams[self.room][self.player.team].remove_player(self.player)

        self.broadcast_game_state()

        del self.players[self.session_string]
        del self.clients[self.room][self.session_string]

    def on_message(self, message):
        try:
            data = json.loads(message)
        except:
            print 'Received unsupported message type'
            return

        if data['type'] == FEMessages.FE_JOIN_ROOM:
            new_room = data['content']['room']
            self.player = Player(
                username=data['content']['username'],
                character_class=data['content']['characterClass'],
                team=data['content']['team'],
            )

            self.change_room(new_room)

            if new_room not in self.teams:
                self.teams[new_room] = {
                    'red': Team('red'),
                    'blue': Team('blue'),
                }

            self.teams[new_room][self.player.team].add_player(self.player)
            self.broadcast_game_state()

        if data['type'] == FEMessages.FE_HERO_MOVE:
            char_pos_x = self.player.position['x']
            char_pos_y = self.player.position['y']
            move_allowed, new_pos_x, new_pos_y = self.move_request(char_pos_x, char_pos_y, data['content']['direction'])
            self.player.position['x'] = new_pos_x
            char_pos_y = self.player.position['y'] = new_pos_y
            self.broadcast_game_state()

        if data['type'] == FEMessages.FE_HERO_SPELL:
            content = data['content']
            self.spell_request(content['spell_type'], content['position_x'], content['position_y'], content['direction'])
            self.broadcast_game_state()
            del self.spells[:]
            if self.teams[self.room]['red'].building_hp <= 0 or self.teams[self.room]['blue'].building_hp <= 0:
                del self.teams[self.room]
                self.broadcast_game_state()

        if data['type'] == FEMessages.FE_SEND_CHAT:
            SendChatBroadcast(
                username=data['content']['username'],
                message=data['content']['message']
            ).broadcast_to_all(self)

    def spell_request(self, spell_type, position_x, position_y, direction):
        if spell_type in self.player.available_spells:
            spell = Spell.create_spell(spell_type, position_x * MAP_TILE_SIZE, position_y * MAP_TILE_SIZE, direction)

{'fraction_non_alphanumeric': 0.10532083282600754, 'fraction_numerical': 0.0007305491294289541, 'mean_word_length': 2.7302452316076296, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '184634', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2389, 'n_words': 459}