Exemple #1
0
    def post(self, event_id):
        add_form = AddBotForm(self.request.POST)

        if add_form.validate():
            bot_data = add_form.data
            bot_data['event_id'] = int(event_id)
            bot_data['bid'] = 0
            bot_data['registered_ind'] = 'Y'
            bot = Bot(**bot_data)
            bot.put()
            response = {
                'successful': True,
                'message': json.dumps(bot.to_dict())
            }
        else:
            response = {
                'successful': False,
                'message': json.dumps(add_form.errors)
            }

        context = {
            'data': json.dumps(response)
        }

        self.render_response('json.json', **context)
def get(current_user, workspaceId, projectId):
    """
    Get list of bots available in the catalog
    """

    filterObj = request.args.get("filter")
    bots = Bot.get_catalog(filterObj)
    payload = []

    if bots:
        for bot in bots:
            payload.append({
                'id':
                bot._id,
                'name':
                bot.name,
                'description':
                bot.description,
                'price':
                float(bot.price),
                'tags':
                bot.tags,
                'marketplaceCardMediaUrl':
                bot.marketplaceCardMediaUrl
            })
            print(payload)

    return response_with_obj("success", "Successfully retrieved catalog.",
                             payload, 200)
Exemple #3
0
 def get(self, psid):
     customer = Customer.find_by_psid(psid)
     vendor = customer.vendor
     bot = Bot(access_token=vendor.page_access_token)
     order = helper.get_order_from_customer(customer)
     output = OrderSchema.dump(order)
     return jsonify(output), 200
def search(current_user, workspaceId, projectId):
    """
        Search Marketplace
    """
    query = request.json.get("query", None)
    filter_obj = request.json.get("filter", None)
    pageNum = int(request.args.get('pageNum', 1))
    itemsPerPage = int(request.args.get('itemsPerPage', 10))

    print("Query and Filter:", query, filter_obj)

    bots_list = Bot.search_bots(query, filter_obj, pageNum, itemsPerPage,
                                projectId)
    # totalItems = Bot.get_total(projectId, query=query, filter_obj=filter_obj)
    payload = []
    print(len(bots_list))

    for bot in bots_list:
        payload.append({
            'id': bot._id,
            'name': bot.name,
            'description': bot.description,
            'price': float(bot.price),
            'tags': bot.tags,
            'marketplaceCardMediaUrl': bot.marketplaceCardMediaUrl
        })

    return response_with_obj("success", "Successfully retrieved catalog.",
                             payload, 200)
Exemple #5
0
def reset(current_user, workspaceId, projectId):
    """
    Publish a playground
    Playground ID Is mandatory
    :return: Http Json response
    """
    if request.content_type == 'application/json':
        playgroundId = request.get_json(force=True).get('playgroundId')
        if playgroundId:
            playground = Playground.get_by_id(playgroundId)
            if playground:
                bot = Bot.get_by_id(playground.parentMarketplaceBot)
                playgroundMeta = bot.botMeta
                playgroundMeta["mediaUrl"] = bot.marketplaceCardMediaUrl
                playgroundMeta["name"] = playground.playgroundMeta.get("name")
                playground.playgroundMeta = playgroundMeta
                playground.save()
                return response('success', 'playground reset successful', 200)
            else:
                return response('failed', 'playground not found', 404)
        else:
            return response(
                'failed', 'Playground ID is required in the request payload.',
                402)
    else:
        return response('failed', 'Content-type must be json', 402)
Exemple #6
0
    def post(self, event_id, bot_id):
        update_form = AddBotForm(self.request.POST)

        if update_form.validate():
            bot_data = update_form.data
            del bot_data['id']  # don't want to update this, EVER
            bot_data['multibot_ind'] = 'Y' if bot_data['multibot_ind'] else 'N'
            bot = Bot.get_by_id(bot_id)
            bot.__dict__.update(bot_data)
            bot.put()
            response = {
                'successful': True,
                'message': json.dumps(bot.to_dict())
            }
        else:
            response = {
                'successful': False,
                'message': json.dumps(update_form.errors)
            }

        context = {
            'data': json.dumps(response)
        }

        self.render_response('json.json', **context)
Exemple #7
0
    def get(self, event_id):
        event = Event.get_by_id(event_id)
        if not event:
            self.redirect(uri_for('home'))

        weightclasses = Weightclass.get_by_event(event_id)
        generate_form = GenerateBracketForm()
        weightclass_choices = []
        brackets = {}
        for weightclass in weightclasses:
            bots = Bot.get_by_weightclass_registered(weightclass.code,
                                                     event.id)
            if len(bots) > 1:
                weightclass_choices.append(
                    (weightclass.code,
                     '%s (%d bots)' % (weightclass.name, len(bots))))
            brackets[weightclass.name] = []
            weightclass_brackets = Bracket.get_by_event_and_class(
                event_id, weightclass.code)
            for bracket in weightclass_brackets:
                matches = Match.get_by_bracket(bracket.id)
                completed_matches = [
                    match for match in matches if match.winning_bot_id
                ]
                bots = Bot.get_by_bracket(bracket.id)

                brackets[weightclass.name].append({
                    'total_matches':
                    len(matches) if matches else 0,
                    'completed_matches':
                    len(completed_matches) if completed_matches else 0,
                    'total_bots':
                    len(bots) if bots else 0,
                    'bracket':
                    bracket,
                    'format':
                    FORMATS.get(bracket.format_code)
                })
        generate_form.weightclass.choices = weightclass_choices

        context = {
            'event_brackets': brackets,
            'event': event,
            'generate_form': generate_form,
            'weightclasses': weightclasses
        }
        self.render_response('brackets.html', **context)
Exemple #8
0
    def post(self, sender_id, item_id):
        customer = Customer.find_by_psid(sender_id)
        vendor = customer.vendor
        catalog = Catalog.find_by_page_id(vendor.page_id)
        item = catalog.items[item_id]
        knowledge = catalog.knowledge
        bot = Bot(access_token=vendor.page_access_token)
        order = helper.get_order_from_customer(customer)
        print(order)
        # if not vendor.is_open():
        #     bot.send_text_message(sender_id,
        #                           knowledge['browse']['values']['Business_Closed_Message'])
        #     return 'Vendor is Closed', 200
        if order is None or order.is_confirmed:
            order = Order(sender_id, vendor.page_id)
        order_item = {}
        order_item['quantity'] = request.form.get('quantity')
        if request.form.get('notes') is not None:
            order_item['notes'] = request.form.get('notes')
        order_item['category'] = item['category_title']
        order_item['name'] = item['title']
        order_item['price'] = item['price']
        order.add_item(order_item)
        confirm_block = ButtonTemplate()
        confirm_block.add_web_url(
            **{
                knowledge['buttons']['values']['Confirm_Order']:
                'https://rest-bot-dev.herokuapp.com/confirm_order'
            })
        confirm_block.add_postback(
            **{knowledge['buttons']['values']['Add_to_Order']: 'main_menu'})
        confirm_block.add_web_url(
            **{
                knowledge['buttons']['values']['Edit_Order']:
                'https://rest-bot-dev.herokuapp.com/edit_order'
            })

        text = '{} * {} تمت اضافته للأوردو الخاص بك'.format(
            order_item['quantity'], order_item['name'])
        confirm_block.set_text(text)
        bot.send_template_message(sender_id,
                                  {'payload': confirm_block.get_template()})
        return 'Item added to Order', 200

        def put(self, sender_id):
            pass
Exemple #9
0
 def post(self):
     data = request.get_json()
     print(data)
     vendor = Vendor.find_by_uid(data['uid'])
     if vendor is None:
         return 'Vendor Not Found'
     bot = Bot(access_token=vendor.page_access_token)
     request_endpoint = 'https://graph.facebook.com/v6.0/{}/subscribed_apps?access_token={}&subscribed_fields=messages,messaging_postbacks,feed'.format(
         data['page']['id'], data['page']['access_token'])
     response = requests.post(request_endpoint)
     print(response.json())
     vendor.page_access_token = data['page']['access_token']
     vendor.page_id = data['page']['id']
     white_listed_domains = ['https://rest-bot-dev.herokuapp.com/']
     print(bot.set_white_listed_domains(white_listed_domains))
     vendor.save()
     return 'Page Connected'
Exemple #10
0
    def put(self, psid):
        customer = Customer.find_by_psid(psid)
        order = Order.query.filter_by(psid=psid, is_confirmed=False).first()
        vendor = Vendor.find_by_page_id(customer.page_id)
        catalog = Catalog.find_by_page_id(vendor.page_id)
        knowledge = catalog.knowledge
        bot = Bot(access_token=vendor.page_access_token)
        data = request.get_json()
        print(data)
        if not data['items']:
            bot.send_text_message(psid, 'انت لم تطلب شيء بعد!')
            return 'Order is empty', 200
        order.item = data['items']
        order.save()
        confirm_block = ButtonTemplate()
        confirm_block.add_web_url(
            **{
                knowledge['buttons']['values']['Confirm_Order']:
                'https://rest-bot-dev.herokuapp.com/confirm_order'
            })
        confirm_block.add_postback(
            **{knowledge['buttons']['values']['Add_to_Order']: 'main_menu'})
        confirm_block.add_web_url(
            **{
                knowledge['buttons']['values']['Edit_Order']:
                'https://rest-bot-dev.herokuapp.com/edit_order'
            })

        confirm_block.set_text('تم تعديل الأوردر الخاص بك')
        bot.send_template_message(psid,
                                  {'payload': confirm_block.get_template()})
        return 'Order was Edited', 200
Exemple #11
0
    def populate_bot_entities(self):

        from app.models.bot import Bot
        # add bot1 and bot2 objects
        if self.bot1_id:
            self.bot1 = Bot.get_by_id(self.bot1_id)
        elif self.bot1_id == 0:
            self.bot1 = Bot.bye()
        else:
            self.bot1 = None

        if self.bot2_id:
            self.bot2 = Bot.get_by_id(self.bot2_id)
        elif self.bot2_id == 0:
            self.bot2 = Bot.bye()
        else:
            self.bot2 = None

        if self.winning_bot_id:
            self.winning_bot = Bot.get_by_id(self.winning_bot_id)
        else:
            self.winning_bot = None
Exemple #12
0
    def populate_bot_entities(self):

        from app.models.bot import Bot
        # add bot1 and bot2 objects
        if self.bot1_id:
            self.bot1 = Bot.get_by_id(self.bot1_id)
        elif self.bot1_id == 0:
            self.bot1 = Bot.bye()
        else:
            self.bot1 = None

        if self.bot2_id:
            self.bot2 = Bot.get_by_id(self.bot2_id)
        elif self.bot2_id == 0:
            self.bot2 = Bot.bye()
        else:
            self.bot2 = None

        if self.winning_bot_id:
            self.winning_bot = Bot.get_by_id(self.winning_bot_id)
        else:
            self.winning_bot = None
Exemple #13
0
    def post(self, psid):
        # look for customer
        customer = Customer.find_by_psid(psid)
        vendor = customer.vendor
        catalog = Catalog.find_by_page_id(vendor.page_id)
        knowledge = catalog.knowledge
        bot = Bot(access_token=vendor.page_access_token)
        order = helper.get_order_from_customer(customer)
        print(order)
        if order.is_confirmed:
            print('Order is Confirmed')
            return 'Order is Confirmed', 200
        # update customer info
        customer.name = request.form.get('name')
        customer.phone_number = request.form.get('phone_number')
        customer.address = request.form.get('address')
        customer.save()  # imp
        # make a receipt
        receipt = ReceiptTemplate(recipient_name=customer.name,
                                  order_number=order.number)

        for item in order.items:
            # fill receipt with order from database
            receipt.add_element(title=item['name'],
                                quantity=item['quantity'],
                                price=item['price'])
        receipt.set_summary(total_cost=order.price)
        print(receipt.get_receipt())
        print(
            bot.send_template_message(psid,
                                      {'payload': receipt.get_receipt()}))
        bot.send_text_message(
            psid, knowledge['browse']['values']['Order_Confirmation_Message'])
        order.confirm()  # imp
        msg_id = helper.send_order_to_vendor(order, vendor.fcm_token)
        print(msg_id)
        return 'Customer info was added', 200
def get_bot(current_user, workspaceId, projectId):
    """
    Get list of all tags
    """

    botId = request.args.get("id")
    botObj = Bot.get_by_id_no_template(botId)
    payload = []

    return make_response(
        jsonify({
            'status': "success",
            'message': "Retrieved Bot Object",
            'bot': botObj
        })), 200
def get_tags(current_user, workspaceId, projectId):
    """
    Get list of all tags
    """

    filterObj = request.args.get("filter")
    tags = Bot.get_tags(filterObj)
    payload = []

    return make_response(
        jsonify({
            'status': "success",
            'message': "Got the tags",
            'tags': tags
        })), 200
Exemple #16
0
    def get(self, event_id):
        event = Event.get_by_id(event_id)
        if not event:
            self.redirect(uri_for('home'))

        weightclasses = Weightclass.get_by_event(event_id)
        generate_form = GenerateBracketForm()
        weightclass_choices = []
        brackets = {}
        for weightclass in weightclasses:
            bots = Bot.get_by_weightclass_registered(weightclass.code, event.id)
            if len(bots) > 1:
                weightclass_choices.append((weightclass.code, '%s (%d bots)' % (weightclass.name, len(bots))))
            brackets[weightclass.name] = []
            weightclass_brackets = Bracket.get_by_event_and_class(event_id, weightclass.code)
            for bracket in weightclass_brackets:
                matches = Match.get_by_bracket(bracket.id)
                completed_matches = [match for match in matches if match.winning_bot_id]
                bots = Bot.get_by_bracket(bracket.id)

                brackets[weightclass.name].append({
                    'total_matches': len(matches) if matches else 0,
                    'completed_matches': len(completed_matches) if completed_matches else 0,
                    'total_bots': len(bots) if bots else 0,
                    'bracket': bracket,
                    'format': FORMATS.get(bracket.format_code)
                })
        generate_form.weightclass.choices = weightclass_choices

        context = {
            'event_brackets': brackets,
            'event': event,
            'generate_form': generate_form,
            'weightclasses': weightclasses
        }
        self.render_response('brackets.html', **context)
Exemple #17
0
def create(current_user, workspaceId, projectId):
    """
        Create a playground. Reuires login
    """
    if request.content_type == 'application/json':
        post_data = request.get_json(force=True)
        required_keys = ['botId', 'name']
        if all(name in post_data for name in required_keys):
            bot = Bot.get_by_id(
                post_data.get('botId')
            )  # bot id is fronteous generated. bot is is for a specific
            # version of agent from dialogflow
            if not bot:
                return response('failed', 'Bot not found in the marketplace',
                                404)

            playgroundMeta = bot.botMeta
            playgroundMeta["mediaUrl"] = bot.marketplaceCardMediaUrl
            playgroundMeta["name"] = post_data.get("name")
            playground = Playground(playgroundType='bot',
                                    playgroundMeta=playgroundMeta,
                                    projectId=projectId,
                                    parentMarketplaceBot=bot._id,
                                    publishedServiceId=post_data.get(
                                        "name", None),
                                    createdBy=current_user.email_id)

            playground.create()

            # add to project
            # project = Project.get_by_id(projectId)
            # project.services.append(playground._id)
            # project.save()

            # Replcing _id with id
            playground_obj = json.loads(playground.to_json())
            playground_obj['id'] = playground_obj['_id']
            playground_obj.pop('_id', None)

            return response_with_obj('success',
                                     'Playground created successfully',
                                     playground_obj, 200)
        else:
            return response('failed', 'Required data not found in POST body.',
                            402)

    return response('failed', 'Content-type must be json', 402)
Exemple #18
0
    def get(self, event_id, bracket_id):
        event = Event.get_by_id(event_id)
        if not event:
            self.redirect(uri_for('home'))

        bracket = Bracket.get_by_id(bracket_id)
        if not bracket:
            self.redirect(uri_for('brackets', event_id=event_id))

        bots = Bot.get_by_weightclass_registered(bracket.weightclass_code, event.id)

        context = {
            'event': event,
            'bracket': bracket,
            'bots': json.dumps([{'id': bot.id, 'name': bot.name} for bot in bots])
        }
        self.render_response('seed.html', **context)
Exemple #19
0
    def get(self, event_id):
        event = Event.get_by_id(event_id)
        if not event:
            logging.warning("Tried to look up bots for event %s, redirecting to home", event_id)
            return redirect(uri_for('home'))

        bots = Bot.get_by_event(event.id, 'weightclass asc, name asc')
        import_form = ImportBotsForm()
        add_form = AddBotForm()

        context = {
            'event': event,
            'bots': json.dumps([bot.to_dict() for bot in bots]),
            'import_form': import_form,
            'add_form': add_form
        }

        self.render_response('bots.html', **context)
Exemple #20
0
    def post(self, event_id, bot_id):
        bot = Bot.get_by_id(bot_id)

        if bot:
            result = bot.register()
            response = {
                'successful': True if result == None else False,
                'message': result
            }
        else:
            response = {
                'successful': False,
                'message': 'Invalid bot id %d' % bot_id
            }

        context = {
            'data': json.dumps(response)
        }

        self.render_response('json.json', **context)
Exemple #21
0
    def get(self, event_id):
        if isinstance(event_id, basestring):
            event_id = int(event_id)

        event = Event.get_by_id(event_id)
        if not event:
            return redirect(uri_for('home'))

        bots = Bot.get_by_event(event_id)
        registered_bots = [bot for bot in bots if bot.registered_ind == 'Y']

        brackets = Bracket.get_by_event(event.id)

        context = {
            'event': event,
            'bots_registered': len(registered_bots),
            'bots_total': len(bots),
            'brackets': brackets
        }

        self.render_response('event.html', **context)
Exemple #22
0
    def get(self, event_id, bracket_id):
        event = Event.get_by_id(event_id)
        if not event:
            self.redirect(uri_for('home'))

        bracket = Bracket.get_by_id(bracket_id)
        if not bracket:
            self.redirect(uri_for('brackets', event_id=event_id))

        bots = Bot.get_by_weightclass_registered(bracket.weightclass_code,
                                                 event.id)

        context = {
            'event': event,
            'bracket': bracket,
            'bots': json.dumps([{
                'id': bot.id,
                'name': bot.name
            } for bot in bots])
        }
        self.render_response('seed.html', **context)
Exemple #23
0
    def post(self, event_id):
        bots = Bot.get_by_event(int(event_id))

        try:
            for bot in bots:
                bot.register()

            response = {
                'successful': True,
                'message': None
            }

        except Exception as e:
            response = {
                'successful': False,
                'message': e.message
            }

        context = {
            'data': json.dumps(response)
        }

        self.render_response('json.json', **context)
def handle_first_time_vendor(page_id):
    new_vendor = Vendor.find_by_page_id(page_id)
    bot = Bot(new_vendor.page_access_token)
    catalog = Catalog(page_id)
    catalog.save()
    knowledge = catalog.knowledge
    bot.set_get_started({
        'get_started': {
            'payload': 'get_started'
        }
    })
    bot.set_persistent_menu({
        'persistent_menu': [
            {
                'locale': 'default',
                'composer_input_disabled': False,
                'call_to_actions': [
                    {
                        'type': 'postback',
                        'title': knowledge['persistant_menut']['values'][0]['value']
                    },
                    {
                        'type': 'postback',
                        'title': knowledge['persistant_menut']['values'][1]['value']
                    },
                    {
                        'type': 'web_url',
                        'title': 'Powered By Sentri',
                        'url': 'https://www.sentri.io/',
                    }
                ]
            }
        ]
    })
    new_vendor.is_setup = True
    new_vendor.save()
    return new_vendor, catalog
Exemple #25
0
    def generate(self, seeding=None):
        """
        generate the bracket
        """

        # find all registered bots in the given class
        bots = Bot.get_by_weightclass_registered(self.weightclass_code,
                                                 self.event_id,
                                                 order="id")

        # defines the order for matches to spread out the byes better, probably a formula for this but didn't take time to figure it out
        match_ordering = {
            2: [1, 2],
            4: [1, 3, 2, 4],
            8: [1, 8, 5, 4, 3, 6, 7, 2],
            16: [1, 16, 9, 8, 5, 12, 13, 4, 3, 14, 11, 6, 7, 10, 15, 2]
        }

        # need at least 2 bots to generate a chart
        if (len(bots) < 2):
            return False

        # manual seeding, if passed in (is dumb, assumes # of seeds matches # of bots)
        if seeding:
            for i, bot_id in enumerate(seeding):
                bot = Bot.get_by_id(bot_id)
                bot.seed_number = i
                bot.bracket_id = self.id
                bot.put()
        else:
            # generate a random array for the seeding
            seeds = range(0, len(bots))
            shuffle(seeds)

            # assign the seeds
            for i, bot in enumerate(bots):
                bot.seed_number = seeds[i]
                bot.bracket_id = self.id
                bot.put()

        # generate matches
        if self.format_code.upper() != 'ROUNDROBIN':
            chart_size = 2

            # find the first power of 2 higher than the number of bots in the bracket, this is our chart size
            num_rounds = 1
            while (len(bots) > chart_size):
                chart_size *= 2
                num_rounds += 1

            # create first round matches, do our best to avoid a team fighting itself first round
            # will regenerate up to 5 times until it gives up on avoiding first round team fighting self
            for _ in xrange(0, 5):
                matches = []
                for i in xrange(0, chart_size / 2):
                    bot1_seed = i
                    bot2_seed = chart_size - 1 - i

                    bot1 = Bot.get_by_bracket_seed(self.event_id, self.id,
                                                   bot1_seed)
                    bot2 = Bot.get_by_bracket_seed(self.event_id, self.id,
                                                   bot2_seed)
                    bot1_id = bot1.id
                    bot2_id = bot2.id if bot2 else 0

                    match = Match(number=match_ordering[chart_size / 2][i],
                                  bracket_id=self.id,
                                  bracket_side="A",
                                  round="A",
                                  bot1_id=bot1_id,
                                  bot2_id=bot2_id)
                    matches.append(match)

                conflict = False
                for match in matches:
                    if match.bot1_id > 0 and match.bot2_id > 0:
                        bot1 = Bot.get_by_id(match.bot1_id)
                        bot2 = Bot.get_by_id(match.bot2_id)
                        if bot1.team_name == bot2.team_name:
                            conflict = True
                            break

                if not conflict:
                    break

            [match.put() for match in matches]

            # create the rest of the A side matches, one round at a time
            for i in xrange(2, num_rounds + 1):
                round_letter = chr(63 + (2 * i))  #A,C,E etc
                for j in xrange(1, chart_size / pow(2, i) + 1):
                    bot1_source = 'W%s%d' % (chr(63 + (2 * i) - 2), (j * 2 - 1)
                                             )  # ie WA1 (Winner of A1)
                    bot2_source = 'W%s%d' % (chr(63 + (2 * i) - 2), (j * 2))
                    match = Match(number=j,
                                  bracket_id=self.id,
                                  bracket_side="A",
                                  round=round_letter,
                                  bot1_source_match=bot1_source,
                                  bot2_source_match=bot2_source)
                    match.put()

            # generate B side matches, if necessary
            if self.format_code.upper(
            ) == 'DOUBLEELIM' or self.format_code.upper() == 'DOUBLETRUE':
                num_b_rounds = num_rounds * 2 - 1
                for i in xrange(2, num_b_rounds + 1):
                    round_letter = chr(62 + (2 * i))
                    round_size = int(chart_size / pow(2, math.floor(
                        (i + 2) / 2)))
                    for j in xrange(1, round_size + 1):
                        if i == 2:  # only case where a loser moves into bot1 spot
                            bot1_source = 'LA%d' % (j * 2 - 1)
                            bot2_source = 'LA%d' % (j * 2)
                        else:
                            if i % 2 == 1:  # means this round's bot2 is sourced from A side
                                # losing source bots need to be from opposite side of chart as to prevent rematches
                                bot1_source = 'W%s%d' % (chr(60 + (2 * i)), j)
                                bot2_source = 'L%s' % (chr(64 + i))
                                # match order depends how far into B side we are
                                # 3 possibilities: normal, reverse, half shift
                                if i % 7 == 0:  # normal
                                    bot2_source = '%s%d' % (bot2_source, j)
                                elif i % 5 == 0:  # half shift
                                    if j < round_size / 2:
                                        bot2_source = '%s%d' % (
                                            bot2_source,
                                            math.ceil((round_size / 2) + j))
                                    else:
                                        bot2_source = '%s%d' % (
                                            bot2_source,
                                            math.ceil((0 -
                                                       (round_size / 2)) + j))
                                else:  # reverse
                                    bot2_source = '%s%d' % (bot2_source,
                                                            round_size + 1 - j)
                            else:
                                bot1_source = 'W%s%d' % (chr(60 + (2 * i)),
                                                         (j * 2 - 1))
                                bot2_source = 'W%s%d' % (chr(60 + (2 * 1)),
                                                         (j * 2))

                        match = Match(number=j,
                                      bracket_id=self.id,
                                      bracket_side="B",
                                      round=round_letter,
                                      bot1_source_match=bot1_source,
                                      bot2_source_match=bot2_source)
                        match.put()

                # insert final A-side match
                round_letter = chr(63 + (2 * (num_rounds + 1)))
                bot1_source = 'W%s1' % chr(63 + (2 * num_rounds))
                bot2_source = 'W%s1' % chr(60 + (2 * (num_b_rounds + 1)))
                match = Match(number=1,
                              bracket_id=self.id,
                              bracket_side="A",
                              round=round_letter,
                              bot1_source_match=bot1_source,
                              bot2_source_match=bot2_source)
                match.put()

            matches_to_update = Match.get_by_bracket_round(self.id, 'A')
            for match in matches_to_update:
                match.check()

        else:  #ROUNDROBIN
            bot_index = 1  # start at 1 because bot0 can't fight bot0 etc
            for bot in bots:
                match_index = 1
                for i in xrange(bot_index, len(bots)):
                    match = Match(number=match_index,
                                  round=chr(64 + bot_index),
                                  bracket_id=self.id,
                                  bracket_side="A",
                                  bot1_id=bot.id,
                                  bot2_id=bots[i].id)
                    match.put()
                    match_index += 1
                bot_index += 1

        return True
Exemple #26
0
    def generate(self, seeding=None):
        """
        generate the bracket
        """

        # find all registered bots in the given class
        bots = Bot.get_by_weightclass_registered(self.weightclass_code, self.event_id, order="id")

        # defines the order for matches to spread out the byes better, probably a formula for this but didn't take time to figure it out
        match_ordering = {
            2: [1, 2],
            4: [1, 3, 2, 4],
            8: [1, 8, 5, 4, 3, 6, 7, 2],
            16: [1, 16, 9, 8, 5, 12, 13, 4, 3, 14, 11, 6, 7, 10, 15, 2]
        }

        # need at least 2 bots to generate a chart
        if(len(bots) < 2):
            return False

        # manual seeding, if passed in (is dumb, assumes # of seeds matches # of bots)
        if seeding:
            for i, bot_id in enumerate(seeding):
                bot = Bot.get_by_id(bot_id)
                bot.seed_number = i
                bot.bracket_id = self.id
                bot.put()
        else:
            # generate a random array for the seeding
            seeds = range(0, len(bots))
            shuffle(seeds)

            # assign the seeds
            for i, bot in enumerate(bots):
                bot.seed_number = seeds[i]
                bot.bracket_id = self.id
                bot.put()

        # generate matches
        if self.format_code.upper() != 'ROUNDROBIN':
            chart_size = 2

            # find the first power of 2 higher than the number of bots in the bracket, this is our chart size
            num_rounds = 1
            while(len(bots) > chart_size):
                chart_size *= 2
                num_rounds += 1

            # create first round matches, do our best to avoid a team fighting itself first round
            # will regenerate up to 5 times until it gives up on avoiding first round team fighting self
            for _ in xrange(0,5):
                matches = []
                for i in xrange(0, chart_size/2):
                    bot1_seed = i
                    bot2_seed = chart_size - 1 - i

                    bot1 = Bot.get_by_bracket_seed(self.event_id, self.id, bot1_seed)
                    bot2 = Bot.get_by_bracket_seed(self.event_id, self.id, bot2_seed)
                    bot1_id = bot1.id
                    bot2_id = bot2.id if bot2 else 0

                    match = Match(number=match_ordering[chart_size/2][i],
                                  bracket_id=self.id,
                                  bracket_side="A",
                                  round="A",
                                  bot1_id=bot1_id,
                                  bot2_id=bot2_id)
                    matches.append(match)

                conflict = False
                for match in matches:
                    if match.bot1_id > 0 and match.bot2_id > 0:
                        bot1 = Bot.get_by_id(match.bot1_id)
                        bot2 = Bot.get_by_id(match.bot2_id)
                        if bot1.team_name == bot2.team_name:
                            conflict = True
                            break

                if not conflict:
                    break

            [match.put() for match in matches]

            # create the rest of the A side matches, one round at a time
            for i in xrange(2, num_rounds+1):
                round_letter = chr(63+(2*i))  #A,C,E etc
                for j in xrange(1, chart_size/pow(2, i)+1):
                    bot1_source = 'W%s%d' % (chr(63+(2*i)-2), (j*2-1))  # ie WA1 (Winner of A1)
                    bot2_source = 'W%s%d' % (chr(63+(2*i)-2), (j*2))
                    match = Match(number=j,
                                  bracket_id=self.id,
                                  bracket_side="A",
                                  round=round_letter,
                                  bot1_source_match=bot1_source,
                                  bot2_source_match=bot2_source)
                    match.put()

            # generate B side matches, if necessary
            if self.format_code.upper() == 'DOUBLEELIM' or self.format_code.upper() == 'DOUBLETRUE':
                num_b_rounds = num_rounds * 2 - 1
                for i in xrange(2, num_b_rounds + 1):
                    round_letter = chr(62+(2*i))
                    round_size = int(chart_size/pow(2, math.floor((i+2)/2)))
                    for j in xrange(1, round_size+1):
                        if i==2:  # only case where a loser moves into bot1 spot
                            bot1_source = 'LA%d' % (j*2-1)
                            bot2_source = 'LA%d' % (j*2)
                        else:
                            if i%2 == 1:  # means this round's bot2 is sourced from A side
                                # losing source bots need to be from opposite side of chart as to prevent rematches
                                bot1_source = 'W%s%d' % (chr(60+(2*i)), j)
                                bot2_source = 'L%s' % (chr(64+i))
                                # match order depends how far into B side we are
                                # 3 possibilities: normal, reverse, half shift
                                if i%7 == 0:  # normal
                                    bot2_source = '%s%d' % (bot2_source, j)
                                elif i%5 == 0:  # half shift
                                    if j < round_size/2:
                                        bot2_source = '%s%d' % (bot2_source, math.ceil((round_size/2) + j))
                                    else:
                                        bot2_source = '%s%d' % (bot2_source, math.ceil((0-(round_size/2))+j))
                                else:  # reverse
                                    bot2_source = '%s%d' % (bot2_source, round_size + 1 - j)
                            else:
                                bot1_source = 'W%s%d' % (chr(60+(2*i)), (j*2-1))
                                bot2_source = 'W%s%d' % (chr(60+(2*1)), (j*2))

                        match = Match(number=j,
                                      bracket_id=self.id,
                                      bracket_side="B",
                                      round=round_letter,
                                      bot1_source_match=bot1_source,
                                      bot2_source_match=bot2_source)
                        match.put()


                # insert final A-side match
                round_letter = chr(63+(2*(num_rounds+1)))
                bot1_source = 'W%s1' % chr(63+(2*num_rounds))
                bot2_source = 'W%s1' % chr(60+(2*(num_b_rounds+1)))
                match = Match(number=1,
                              bracket_id=self.id,
                              bracket_side="A",
                              round=round_letter,
                              bot1_source_match=bot1_source,
                              bot2_source_match=bot2_source)
                match.put()

            matches_to_update = Match.get_by_bracket_round(self.id, 'A')
            for match in matches_to_update:
                match.check()

        else:  #ROUNDROBIN
            bot_index = 1  # start at 1 because bot0 can't fight bot0 etc
            for bot in bots:
                match_index = 1
                for i in xrange(bot_index, len(bots)):
                    match = Match(number=match_index,
                                  round=chr(64+bot_index),
                                  bracket_id=self.id,
                                  bracket_side="A",
                                  bot1_id=bot.id,
                                  bot2_id=bots[i].id)
                    match.put()
                    match_index += 1
                bot_index += 1

        return True
Exemple #27
0
    def get(self, event_id, bracket_id):
        event = Event.get_by_id(event_id)
        bracket = Bracket.get_by_id(bracket_id)
        weightclass = Weightclass.get_by_code(bracket.weightclass_code)
        format = FORMATS.get(bracket.format_code)
        matches = Match.get_by_bracket(bracket_id)
        bots = Bot.get_by_bracket(bracket_id)
        bots.sort(key=lambda x: x.id)

        if bracket.manual_seed and not bracket.generated:
            self.redirect(
                uri_for('manual-seed',
                        event_id=event_id,
                        bracket_id=bracket.id))

        for match in matches:
            match.populate_bot_entities()

        ordered_matches = {'A': []}
        rounds = {'A': []}
        a_final_round = None
        b_final_round = None
        a_winner = None
        b_winner = None
        final_round = None
        margin_top = None

        if bracket.format_code != ROUND_ROBIN:
            if bracket.format_code == DOUBLE_ELIMINATION:
                ordered_matches['B'] = []
                rounds['B'] = []

            for match in matches:
                ordered_matches[match.bracket_side].append(match)

            # sort A side matches ascending by round, match number
            ordered_matches['A'] = sorted(ordered_matches['A'],
                                          key=attrgetter('round', 'number'))

            number_first_round_matches = sum(1 for m in ordered_matches['A']
                                             if m.round == 'A')
            if bracket.format_code == SINGLE_ELIMINATION:
                a_final_round = chr(65 + int(
                    (2 * math.log(number_first_round_matches, 2))))
                final_round = chr(67 + int(
                    (2 * math.log(number_first_round_matches, 2))))
            else:
                a_final_round = chr(67 + int(
                    (2 * math.log(number_first_round_matches, 2))))
                final_round = chr(69 + int(
                    (2 * math.log(number_first_round_matches, 2))))

            a_winner = ordered_matches['A'][-1].winning_bot_id
            a_winner = Bot.get_by_id(a_winner) if a_winner else None

            if ordered_matches.get('B'):
                # sort B side matches desc by round, match number
                ordered_matches['B'] = sorted(ordered_matches['B'],
                                              key=attrgetter(
                                                  'round', 'number'),
                                              reverse=True)

                b_final_round = chr(66 + int(
                    (4 * (math.log(number_first_round_matches, 2)))))

                for match in ordered_matches.get('B'):
                    if match.round not in rounds['B']:
                        rounds['B'].append(match.round)

                # determine b side winner, if applicable
                b_winner = ordered_matches['B'][0].winning_bot_id
                b_winner = Bot.get_by_id(b_winner) if b_winner else None

            for match in ordered_matches.get('A'):
                if match.round not in rounds['A']:
                    rounds['A'].append(match.round)

        else:
            # don't care for round robin about sort
            ordered_matches['A'] = matches
            number_first_round_matches = sum(1 for m in ordered_matches['A']
                                             if m.round == 'A')

        if bracket.format_code != SINGLE_ELIMINATION:
            if number_first_round_matches <= 4:
                margin_top = "0px"
            elif number_first_round_matches <= 8:
                margin_top = "-50px"
            elif number_first_round_matches <= 16:
                margin_top = "-150px"
            elif number_first_round_matches <= 32:
                margin_top = "-400px"
        else:
            margin_top = "0px"

        context = {
            'format': format,
            'bracket': bracket,
            'weightclass': weightclass,
            'matches': ordered_matches,
            'rounds': rounds,
            'a_final_round': a_final_round,
            'b_final_round': b_final_round,
            'final_round': final_round,
            'a_winner': a_winner,
            'b_winner': b_winner,
            'number_first_round_matches': number_first_round_matches,
            'margin_top': margin_top,
            'event': event,
            'bots': bots
        }

        self.render_response('single-bracket.html', **context)
Exemple #28
0
    def get(self, event_id, bracket_id):
        event = Event.get_by_id(event_id)
        bracket = Bracket.get_by_id(bracket_id)
        weightclass = Weightclass.get_by_code(bracket.weightclass_code)
        format = FORMATS.get(bracket.format_code)
        matches = Match.get_by_bracket(bracket_id)
        bots = Bot.get_by_bracket(bracket_id)
        bots.sort(key=lambda x: x.id)

        if bracket.manual_seed and not bracket.generated:
            self.redirect(uri_for('manual-seed', event_id=event_id, bracket_id=bracket.id))

        for match in matches:
            match.populate_bot_entities()

        ordered_matches = {'A': []}
        rounds = {'A': []}
        a_final_round = None
        b_final_round = None
        a_winner = None
        b_winner = None
        final_round = None
        margin_top = None

        if bracket.format_code != ROUND_ROBIN:
            if bracket.format_code == DOUBLE_ELIMINATION:
                ordered_matches['B'] = []
                rounds['B'] = []

            for match in matches:
                ordered_matches[match.bracket_side].append(match)

            # sort A side matches ascending by round, match number
            ordered_matches['A'] = sorted(ordered_matches['A'], key=attrgetter('round', 'number'))

            number_first_round_matches = sum(1 for m in ordered_matches['A'] if m.round == 'A')
            if bracket.format_code == SINGLE_ELIMINATION:
                a_final_round = chr(65+int((2*math.log(number_first_round_matches, 2))))
                final_round = chr(67+int((2*math.log(number_first_round_matches, 2))))
            else:
                a_final_round = chr(67+int((2*math.log(number_first_round_matches, 2))))
                final_round = chr(69+int((2*math.log(number_first_round_matches, 2))))

            a_winner = ordered_matches['A'][-1].winning_bot_id
            a_winner = Bot.get_by_id(a_winner) if a_winner else None

            if ordered_matches.get('B'):
                # sort B side matches desc by round, match number
                ordered_matches['B'] = sorted(ordered_matches['B'], key=attrgetter('round', 'number'), reverse=True)

                b_final_round = chr(66+int((4*(math.log(number_first_round_matches,2)))))

                for match in ordered_matches.get('B'):
                    if match.round not in rounds['B']:
                        rounds['B'].append(match.round)

                # determine b side winner, if applicable
                b_winner = ordered_matches['B'][0].winning_bot_id
                b_winner = Bot.get_by_id(b_winner) if b_winner else None

            for match in ordered_matches.get('A'):
                if match.round not in rounds['A']:
                    rounds['A'].append(match.round)

        else:
            # don't care for round robin about sort
            ordered_matches['A'] = matches
            number_first_round_matches = sum(1 for m in ordered_matches['A'] if m.round == 'A')

        if bracket.format_code != SINGLE_ELIMINATION:
            if number_first_round_matches <= 4:
                margin_top = "0px"
            elif number_first_round_matches <= 8:
                margin_top = "-50px"
            elif number_first_round_matches <= 16:
                margin_top = "-150px"
            elif number_first_round_matches <= 32:
                margin_top = "-400px"
        else:
            margin_top = "0px"

        context = {
            'format': format,
            'bracket': bracket,
            'weightclass': weightclass,
            'matches': ordered_matches,
            'rounds': rounds,
            'a_final_round': a_final_round,
            'b_final_round': b_final_round,
            'final_round': final_round,
            'a_winner': a_winner,
            'b_winner': b_winner,
            'number_first_round_matches': number_first_round_matches,
            'margin_top': margin_top,
            'event': event,
            'bots': bots
        }

        self.render_response('single-bracket.html', **context)
Exemple #29
0
def handle_messaging(data):
    message_type = helper.get_type_from_message(data)
    page_id = helper.get_vendor_from_message(data)
    vendor = helper.handle_vendor(page_id)[0]
    catalog = helper.handle_vendor(page_id)[1]
    blocks = catalog.blocks
    bot = Bot(access_token=vendor.page_access_token)
    sender_id = helper.get_customer_from_message(data)
    print(sender_id)
    print(message_type)
    if not vendor.check_customer_limit:
        bot.send_text_message(
            sender_id, 'You have reached the Maximum Customer limit for your tier.')
        return 'Customer Limit', 200
    # if not vendor.is_open():
    #     bot.send_text_message(
    #         sender_id, 'المطعم مغلق حاليا \nالرجاء المحاولة مرة أخرى خلال مواعيد العمل الرسمية من {} الى {}'.format(vendor.opening_hours.strftime('%H:%M'), vendor.closing_hours.strftime('%H:%M')))
    #     return 'Vendor is Closed', 200
    if message_type == "text":
        # HANDLE TEXT MESSAGES HERE
        bot.send_before_message(sender_id)

        block = blocks['get_started']
        print(bot.send_template_message(sender_id, block))
        return "text", 200

    elif message_type == "quick_reply":
        # HANDLE QUICK REPLIES HERE
        bot.send_before_message(sender_id)
        block_name = helper.quick_replies_events(data)
        if block_name in blocks:
            block = blocks[block_name]
            print(block)
            # bot.send_template_message(sender_id, block)
            print(bot.send_template_message(sender_id, block))

        return "quick_reply", 200

    elif message_type == "postback":
        # HANDLE POSTBACK HERE
        bot.send_before_message(sender_id)
        block_name = helper.postback_events(data)
        if block_name in blocks:

            block = blocks[block_name]
            print(block)
            # bot.send_template_message(sender_id, block)
            print(bot.send_template_message(sender_id, block))

        return "postback", 200
    else:
        return "ok", 200