<commit_before># -*- coding: utf-8 -*-

"""
The MIT License (MIT)

Copyright (c) 2017 SML

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""

import discord
from discord.ext import commands
from .utils import checks
from .utils.dataIO import dataIO
from .general import General
from __main__ import send_cmd_help
import os
import datetime
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import io
import string
import itertools

settings_path = "data/deck/settings.json"
crdata_path = "data/deck/clashroyale.json"
max_deck_per_user = 5

class Deck:
    """
    Saves a Clash Royale deck for a user and display them
    """

    def __init__(self, bot):
        self.bot = bot
        self.file_path = settings_path
        self.crdata_path = crdata_path

        self.settings = dataIO.load_json(self.file_path)
        self.crdata = dataIO.load_json(self.crdata_path)

        # init card data
        self.cards = []
        self.cards_abbrev = {}

        for card_key, card_value in self.crdata["Cards"].items():
            self.cards.append(card_key)

            aka_list = card_value["aka"]
            for aka in aka_list:
                self.cards_abbrev[aka] = card_key


        self.card_w = 302
        self.card_h = 363
        self.card_ratio = self.card_w / self.card_h
        self.card_thumb_scale = 0.5
        self.card_thumb_w = int(self.card_w * self.card_thumb_scale)
        self.card_thumb_h = int(self.card_h * self.card_thumb_scale)

        # deck validation hack
        self.deck_is_valid = False

    def grouper(self, n, iterable, fillvalue=None):
        """
        Helper function to split lists

        Example:
        grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
        """
        args = [iter(iterable)] * n
        return ([e for e in t if e != None] for t in itertools.zip_longest(*args))


    @commands.group(pass_context=True, no_pm=True)
    async def deck(self, ctx):
        """
        Clash Royale Decks
        
        Example usage:
        !deck get 3m mm ig is fs pump horde knight

        !deck get dark-prince dart-goblin electro-wizard elite-barbarians elixir-collector executioner fire-spirits fireball 

        Card list
        !deck cards
        """
        if ctx.invoked_subcommand is None:
            await send_cmd_help(ctx)

    @deck.command(name="get", pass_context=True, no_pm=True)
    async def deck_get(self, ctx,
                       card1=None, card2=None, card3=None, card4=None, 
                       card5=None, card6=None, card7=None, card8=None, 
                       deck_name=None):
        """
        Display a deck with cards and average elixir by entering 8 cards,
        followed by a name. The deck will be named “unnamed deck”
        if no name is entered

        Example: !deck get bbd mm loon bt is fs gs lh

        For the full list of acceptable card names, type !deck cards
        """
        if deck_name is None:
            deck_name = 'Deck'
        author = ctx.message.author

        member_deck = [card1, card2, card3, card4, card5, card6, card7, card8]
        if not all(member_deck):
            await self.bot.say("Please enter 8 cards.")
            await send_cmd_help(ctx)
        else:
            await self.deck_show(ctx, member_deck, deck_name, author)

    @deck.command(name="add", pass_context=True, no_pm=True)
    async def _deck_add(self, ctx, *member_deck:str):
        """
        Add a deck to a personal decklist 

        Example: !deck add bbd mm loon bt is fs gs lh

        For the full list of acceptable card names, type !deck cards
        """
        author = ctx.message.author
        server = ctx.message.server

        deck_name = ""

        # convert arguments to deck list and name
        if (len(member_deck) > 8):
            deck_name = ' '.join(member_deck[8:])
            member_deck = member_deck[:8]


        member_deck = self.normalize_deck_data(member_deck)

        await self.deck_show(ctx, member_deck, deck_name)

        decks = self.settings["Servers"][server.id]["Members"][author.id]["Decks"]

        if self.deck_is_valid:

            # creates sets with decks.values
            # decks_sets = [set(d) for d in decks.values()]

            # if  set(member_deck) in decks_sets:
            #     # existing deck
            #     await self.bot.say("Deck exists already")
            # else:
            # new deck
            await self.bot.say("Deck added.")
            deck_key = str(datetime.datetime.utcnow())
            decks[deck_key] = {
                "Deck": member_deck,
                "DeckName": deck_name
            }

            self.save_settings()

            # If user has more than allowed by max, remove older decks
            timestamp = decks.keys()
            timestamp = sorted(timestamp)

            while len(decks) > max_deck_per_user:
                t = timestamp.pop(0)
                decks.pop(t, None)

            self.save_settings()
            

    @deck.command(name="list", pass_context=True, no_pm=True)
    async def deck_list(self, ctx, member:discord.Member=None):
        """
        List the decks of a user

        """

        author = ctx.message.author
        server = ctx.message.server

        member_is_author = False<commit_msg>Deck: changed *args params to card1, card2, card3, … for deck add subcommand<commit_after># -*- coding: utf-8 -*-

"""
The MIT License (MIT)

Copyright (c) 2017 SML

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""

import discord
from discord.ext import commands
from .utils import checks
from .utils.dataIO import dataIO
from .general import General
from __main__ import send_cmd_help
import os
import datetime
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import io
import string
import itertools

settings_path = "data/deck/settings.json"
crdata_path = "data/deck/clashroyale.json"
max_deck_per_user = 5

class Deck:
    """
    Saves a Clash Royale deck for a user and display them
    """

    def __init__(self, bot):
        self.bot = bot
        self.file_path = settings_path
        self.crdata_path = crdata_path

        self.settings = dataIO.load_json(self.file_path)
        self.crdata = dataIO.load_json(self.crdata_path)

        # init card data
        self.cards = []
        self.cards_abbrev = {}

        for card_key, card_value in self.crdata["Cards"].items():
            self.cards.append(card_key)

            aka_list = card_value["aka"]
            for aka in aka_list:
                self.cards_abbrev[aka] = card_key


        self.card_w = 302
        self.card_h = 363
        self.card_ratio = self.card_w / self.card_h
        self.card_thumb_scale = 0.5
        self.card_thumb_w = int(self.card_w * self.card_thumb_scale)
        self.card_thumb_h = int(self.card_h * self.card_thumb_scale)

        # deck validation hack
        self.deck_is_valid = False

    def grouper(self, n, iterable, fillvalue=None):
        """
        Helper function to split lists

        Example:
        grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
        """
        args = [iter(iterable)] * n
        return ([e for e in t if e != None] for t in itertools.zip_longest(*args))


    @commands.group(pass_context=True, no_pm=True)
    async def deck(self, ctx):
        """
        Clash Royale Decks
        
        Example usage:
        !deck get 3m mm ig is fs pump horde knight

        !deck get dark-prince dart-goblin electro-wizard elite-barbarians elixir-collector executioner fire-spirits fireball 

        Card list
        !deck cards
        """
        if ctx.invoked_subcommand is None:
            await send_cmd_help(ctx)

    @deck.command(name="get", pass_context=True, no_pm=True)
    async def deck_get(self, ctx,
                       card1=None, card2=None, card3=None, card4=None, 
                       card5=None, card6=None, card7=None, card8=None, 
                       deck_name=None):
        """
        Display a deck with cards and average elixir by entering 8 cards,
        followed by a name. The deck will be named “unnamed deck”
        if no name is entered

        Example: !deck get bbd mm loon bt is fs gs lh

        For the full list of acceptable card names, type !deck cards
        """
        if deck_name is None:
            deck_name = 'Deck'
        author = ctx.message.author

        member_deck = [card1, card2, card3, card4, card5, card6, card7, card8]
        if not all(member_deck):
            await self.bot.say("Please enter 8 cards.")
            await send_cmd_help(ctx)
        else:
            await self.deck_show(ctx, member_deck, deck_name, author)

    @deck.command(name="add", pass_context=True, no_pm=True)
    async def _deck_add(self, ctx,
                       card1=None, card2=None, card3=None, card4=None, 
                       card5=None, card6=None, card7=None, card8=None, 
                       deck_name=None):
        """
        Add a deck to a personal decklist 

        Example: !deck add bbd mm loon bt is fs gs lh

        For the full list of acceptable card names, type !deck cards
        """
        if deck_name is None:
            deck_name = 'Deck'
        author = ctx.message.author
        server = ctx.message.server

        # convert arguments to deck list and name
        member_deck = [card1, card2, card3, card4, card5, card6, card7, card8]
        if not all(member_deck):
            await self.bot.say("Please enter 8 cards.")
            await send_cmd_help(ctx)
        else:
            member_deck = self.normalize_deck_data(member_deck)

            await self.deck_show(ctx, member_deck, deck_name)

            decks = self.settings["Servers"][server.id]["Members"][author.id]["Decks"]

            if self.deck_is_valid:

                # creates sets with decks.values
                # decks_sets = [set(d) for d in decks.values()]

                # if  set(member_deck) in decks_sets:
                #     # existing deck
                #     await self.bot.say("Deck exists already")
                # else:
                # new deck
                await self.bot.say("Deck added.")
                deck_key = str(datetime.datetime.utcnow())
                decks[deck_key] = {
                    "Deck": member_deck,
                    "DeckName": deck_name
                }

                # If user has more than allowed by max, remove older decks
                timestamp = decks.keys()
                timestamp = sorted(timestamp)

                while len(decks) > max_deck_per_user:
                    t = timestamp.pop(0)
                    decks.pop(t, None)

                self.save_settings()
            

    @deck.command(name="list", pass_context=True, no_pm=True)
    async def deck_list(self, ctx, member:discord.Member=None):
        """
        List the decks of a user

        """

        author = ctx.message.author
        server = ctx.message.server

        member_is_author = False
{'fraction_non_alphanumeric': 0.08177263969171483, 'fraction_numerical': 0.007167630057803468, 'mean_word_length': 2.2134720158494305, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 8, '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': '574335', 'n_tokens_mistral': 3879, 'n_tokens_neox': 3685, 'n_words': 1244}