Example #1
0
def pa_get(pcid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{username})',
            "payload": None
        }), 409

    try:
        payload = RedisPa(pc).get()
    except Exception as e:
        msg = f'PA Query KO (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        return jsonify({
            "success": True,
            "msg": f'PA Query OK (pcid:{pc.id})',
            "payload": payload
        }), 200
def instance_get(pcid, instanceid):
    creature = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if creature is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (creatureid:{creatureid})',
            "payload": None
        }), 200
    if creature.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (creatureid:{creature.id},username:{user.name})',
            "payload": None
        }), 409
    if creature.instance is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not in an instance (creatureid:{creature.id})',
            "payload": None
        }), 200
    if creature.instance != instanceid:
        return jsonify({
            "success": False,
            "msg":
            f'PC is not in this instance (creatureid:{creature.id},instanceid:{instanceid})',
            "payload": None
        }), 200

    # Check if the instance exists
    try:
        instance = instances.get_instance(instanceid)
    except Exception as e:
        msg = f'Instance Query KO (creatureid:{creature.id},instanceid:{instanceid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if instance is False:
            return jsonify({
                "success": False,
                "msg":
                f'Instance not found (creatureid:{creature.id},instanceid:{instanceid})',
                "payload": None
            }), 200
        else:
            return jsonify({
                "success": True,
                "msg":
                f"Instance found (creatureid:{creature.id},instanceid:{instance['id']})",
                "payload": instance
            }), 200
Example #3
0
def korp_get_one(pcid, korpid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.korp != korpid:
        return jsonify({
            "success": False,
            "msg":
            f'Korp request outside of your scope (pcid:{pc.id},korpid:{korpid})',
            "payload": None
        }), 200

    try:
        korp = fn_korp_get_one(korpid)
    except Exception as e:
        msg = f'Korp Query KO (pcid:{pc.id},korpid:{korpid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if korp:
            return jsonify({
                "success": True,
                "msg": f'Korp Query OK (pcid:{pc.id},korpid:{korpid})',
                "payload": korp
            }), 200
        elif korp is False:
            return jsonify({
                "success": False,
                "msg":
                f'Korp Query KO - Not Found (pcid:{pc.id},korpid:{korpid})',
                "payload": None
            }), 200
        else:
            return jsonify({
                "success": False,
                "msg":
                f'Korp Query KO - Failed (pcid:{pc.id},korpid:{korpid})',
                "payload": None
            }), 200
Example #4
0
def stats_get(pcid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{username})',
            "payload": None
        }), 409

    try:
        # We check if we have the data in redis
        cached_stats = RedisStats(pc).as_dict()
        if cached_stats:
            # Data was in Redis, so we return it
            pc_stats = cached_stats
        else:
            # Data was not in Redis, so we compute it
            generated_stats = RedisStats(pc).refresh().dict
            if generated_stats:
                # Data was computed, so we return it
                pc_stats = generated_stats
            else:
                msg = f'Stats computation KO (pcid:{pc.id})'
                logger.error(msg)
                return (200, False, msg, None)
    except Exception as e:
        msg = f'Stats Query KO (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        return jsonify({
            "success": True,
            "msg": f'Stats Query OK (pcid:{pc.id})',
            "payload": pc_stats
        }), 200
Example #5
0
def mp_add(pcid):
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if not request.is_json:
        return jsonify({"msg": "Missing JSON in request"}), 400
    pcsrcid = request.json.get('src', None)
    dsts = request.json.get('dst', None)
    subject = request.json.get('subject', None)
    body = request.json.get('body', None)
    pcsrc = fn_creature_get(None, pcsrcid)[3]

    if pcsrc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcsrc})',
            "payload": None
        }), 200
    if pcsrc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pcsrc.id},username:{user.name})',
            "payload": None
        }), 409

    try:
        success = fn_mp_add(pcsrc, pcsrcid, dsts, subject, body)
    except Exception as e:
        msg = f'MP creation KO (srcid:{pcsrc.id},dstid:{dsts}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if success:
            return jsonify({
                "success": True,
                "msg": f'MP creation OK (pcid:{pcsrc.id})',
                "payload": None
            }), 201
        else:
            return jsonify({
                "success": True,
                "msg": f'MP creation KO (pcid:{pcsrc.id})',
                "payload": None
            }), 200
Example #6
0
def item_get(pcid):
    creature = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if creature is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (creatureid:{creatureid})',
            "payload": None
        }), 200
    if creature.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (creatureid:{creature.id},username:{username})',
            "payload": None
        }), 409

    try:
        all_items_sql = fn_item_get_all(creature)
        all_items_json = json.loads(jsonify(all_items_sql).get_data())

        armor = [x for x in all_items_json if x['metatype'] == 'armor']
        slots = fn_slots_get_all(creature)
        cosmetic = fn_cosmetics_get_all(creature)
        wallet = [fn_wallet_get(creature)]
        weapon = [x for x in all_items_json if x['metatype'] == 'weapon']
    except Exception as e:
        msg = f'Items Query KO (pcid:{creature.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        return jsonify({
            "success": True,
            "msg": f'Equipment query successed (pcid:{creature.id})',
            "payload": {
                "weapon": weapon,
                "armor": armor,
                "equipment": slots,
                "cosmetic": cosmetic,
                "wallet": wallet
            }
        }), 200
Example #7
0
def addressbook_get(pcid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409

    try:
        addressbook = fn_mp_addressbook_get(pc)
    except Exception as e:
        msg = f'Addressbook query failed (pcid:{pc.id})'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if addressbook:
            return jsonify({
                "success":
                True,
                "msg":
                f'Addressbook Query OK (pcid:{pc.id})',
                "payload": [{
                    "id": row[0],
                    "name": row[1]
                } for row in addressbook]
            }), 200
        else:
            return jsonify({
                "success": False,
                "msg": f'Addressbook Query KO (pcid:{pc.id})',
                "payload": None
            }), 200
Example #8
0
def view_get(pcid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if user is None:
        return jsonify({
            "success": False,
            "msg": f'User not found (user:{user})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{username})',
            "payload": None
        }), 409

    try:
        if pc.squad is None:
            # PC is solo / not in a squad
            view_final = fn_creature_view_get(pc)
        else:
            # PC is in a squad
            view_final = fn_creature_squad_view_get(pc)
    except Exception as e:
        msg = f'View Query KO (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        return jsonify({
            "success": True,
            "msg": f'View Query OK (pcid:{pc.id})',
            "payload": view_final
        }), 200
Example #9
0
def mp_del(pcid, mpid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409

    try:
        success = fn_mp_del_one(pc, mpid)
    except Exception as e:
        msg = f'MP deletion KO (pcid:{pc.id},mpid:{mpid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if success:
            return jsonify({
                "success": True,
                "msg": f'MP deletion OK (pcid:{pc.id})',
                "payload": None
            }), 200
        else:
            return jsonify({
                "success": True,
                "msg": f'MP deletion KO (pcid:{pc.id})',
                "payload": None
            }), 200
def instance_join(pcid, instanceid):
    creature = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if creature is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (creatureid:{creatureid})',
            "payload": None
        }), 200
    if creature.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (creatureid:{creature.id},username:{user.name})',
            "payload": None
        }), 409
    if creature.instance is not None:
        return jsonify({
            "success": False,
            "msg":
            f'Creature in an instance (creatureid:{creature.id},instanceid:{creature.instance})',
            "payload": None
        }), 200

    # Check if the instance exists
    try:
        instance = instances.get_instance(instanceid)
    except Exception as e:
        msg = f'Instance Query KO (creatureid:{creature.id},instanceid:{instanceid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if instance is False:
            return jsonify({
                "success": False,
                "msg":
                f'Instance not found (creatureid:{creature.id},instanceid:{instanceid})',
                "payload": None
            }), 200
        if instance['public'] is False:
            return jsonify({
                "success": False,
                "msg":
                f'Instance not public (creatureid:{creature.id},instanceid:{instanceid})',
                "payload": None
            }), 200

    # We add the Creature into the instance
    try:
        ret = fn_creature_instance_set(creature, instance['id'])
        if ret is None:
            return jsonify({
                "success": False,
                "msg":
                f"Instance join KO (creatureid:{creature.id},instanceid:{instance['id']})",
                "payload": None
            }), 200
    except Exception as e:
        msg = f'Instance Query KO (creatureid:{creature.id},instanceid:{instanceid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        # We put the info in queue for Discord
        scopes = []
        if ret.korp is not None: scopes.append(f'Korp-{ret.korp}')
        if ret.squad is not None: scopes.append(f'Squad-{ret.squad}')
        for scope in scopes:
            qmsg = {
                "ciphered": False,
                "payload":
                f':map: **[{ret.id}] {ret.name}** joined an Instance ({instanceid})',
                "embed": None,
                "scope": scope
            }
            try:
                queue.yqueue_put('yarqueue:discord', qmsg)
            except Exception as e:
                msg = f'Queue Query KO (Queue:yarqueue:discord,qmsg:{qmsg}) [{e}]'
                logger.error(msg)
            else:
                logger.debug(
                    f'Queue Query OK (Queue:yarqueue:discord,qmsg:{qmsg})')

        return jsonify({
            "success": True,
            "msg":
            f"Instance join OK (creatureid:{creature.id},instanceid:{instance['id']})",
            "payload": ret
        }), 200
def instance_add(pcid):
    creature = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    if not request.is_json:
        return jsonify({
            "success": False,
            "msg": "Missing JSON in request",
            "payload": None
        }), 400

    hardcore = request.json.get('hardcore', None)
    fast = request.json.get('fast', None)
    mapid = request.json.get('mapid', None)
    public = request.json.get('public', None)

    # Pre-flight checks
    if creature is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (creatureid:{creatureid})',
            "payload": None
        }), 200
    if creature.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (creatureid:{creature.id},username:{user.name})',
            "payload": None
        }), 409
    if creature.instance is not None:
        return jsonify({
            "success": False,
            "msg": f'Creature in an instance (creatureid:{creature.id})',
            "payload": None
        }), 200
    if not isinstance(mapid, int):
        return jsonify({
            "success": False,
            "msg": f'Map ID should be an integer (mapid:{mapid})',
            "payload": None
        }), 200
    if not isinstance(hardcore, bool):
        return jsonify({
            "success": False,
            "msg": f'Hardcore param should be a boolean (hardcore:{hardcore})',
            "payload": None
        }), 200
    if not isinstance(fast, bool):
        return jsonify({
            "success": False,
            "msg": f'Fast param should be a boolean (fast:{fast})',
            "payload": None
        }), 200
    if not isinstance(public, bool):
        return jsonify({
            "success": False,
            "msg": f'Public param should be a boolean (public:{public})',
            "payload": None
        }), 200

    # Check if map related to mapid exists
    try:
        map = maps.get_map(mapid)
    except Exception as e:
        msg = f'Map Query KO (mapid:{mapid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    # Create the new instance
    try:
        instance = instances.add_instance(creature, fast, hardcore, mapid,
                                          public)
    except Exception as e:
        msg = f"Instance Query KO (creatureid:{creature.id},instanceid:{instance['id']}) [{e}]"
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if instance:
            # Everything went well so far
            try:
                # Assign the PC into the instance
                ret = fn_creature_instance_set(creature, instance['id'])
            except Exception as e:
                msg = f"Instance Query KO (creatureid:{creature.id},instanceid:{instance['id']}) [{e}]"
                logger.error(msg)
                return jsonify({
                    "success": False,
                    "msg": msg,
                    "payload": None
                }), 200

            if ret is None:
                return jsonify({
                    "success": False,
                    "msg":
                    f"Instance create KO (creatureid:{creature.id},instanceid:{instance['id']})",
                    "payload": None
                }), 200

            # Everything went well, creation DONE
            # We put the info in queue for Discord
            scopes = []
            if ret.korp is not None: scopes.append(f'Korp-{ret.korp}')
            if ret.squad is not None: scopes.append(f'Squad-{ret.squad}')
            for scope in scopes:
                qmsg = {
                    "ciphered": False,
                    "payload":
                    f':map: **[{ret.id}] {ret.name}** opened an Instance ({instanceid})',
                    "embed": None,
                    "scope": scope
                }
                try:
                    queue.yqueue_put('yarqueue:discord', qmsg)
                except Exception as e:
                    msg = f'Queue Query KO (Queue:yarqueue:discord,qmsg:{qmsg}) [{e}]'
                    logger.error(msg)
                else:
                    logger.debug(
                        f'Queue Query OK (Queue:yarqueue:discord,qmsg:{qmsg})')
            # We put the info in queue for IA to populate the instance
            try:
                qmsg = {"action": 'create', "instance": instance}
                queue.yqueue_put('yarqueue:instances', qmsg)
            except Exception as e:
                msg = f'Queue Query KO (Queue:yarqueue:instances,qmsg:{qmsg}) [{e}]'
                logger.error(msg)
            else:
                logger.debug(
                    f'Queue Query OK (Queue:yarqueue:instances,qmsg:{qmsg})')
            # Finally everything is done
            return jsonify({
                "success": True,
                "msg": f"Instance create OK (creatureid:{ret.id})",
                "payload": instance
            }), 201
        else:
            return jsonify({
                "success": False,
                "msg": f'Instance create KO (creatureid:{ret.id})',
                "payload": None
            }), 200
Example #12
0
def squad_create(pcid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.squad is not None:
        return jsonify({
            "success": False,
            "msg": f'PC already in a Squad (pcid:{pc.id},squadid:{pc.squad})',
            "payload": None
        }), 200

    try:
        newsquad = fn_squad_add_one(pc)
    except Exception as e:
        msg = f'Squad Query KO (pcid:{pcid},squadname:{squadname}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    # Squad created, let's assign the team creator in the squad
    try:
        pc = fn_squad_set_rank(pc, newsquad.id, 'Leader')
    except Exception as e:
        msg = f'Squad Query KO (pcid:{pcid},squadname:{squadname}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        try:
            squad = fn_squad_get_one(newsquad.id)
        except Exception as e:
            msg = f'Squad Query KO (squadid:{newsquad.id}) [{e}]'
            logger.error(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200
        else:
            # We put the info in queue for ws
            qmsg = {
                "ciphered": False,
                "payload":
                f':information_source: **[{pc.id}] {pc.name}** created this Squad',
                "embed": None,
                "scope": f'Squad-{pc.squad}'
            }
            queue.yqueue_put('yarqueue:discord', qmsg)
            # We put the info in queue for ws Front
            qmsg = {
                "ciphered": False,
                "payload": squad,
                "route": 'mypc/{id1}/squad',
                "scope": 'squad'
            }
            queue.yqueue_put('broadcast', json.loads(jsonify(qmsg).get_data()))

            return jsonify({
                "success": True,
                "msg": f'Squad create OK (pcid:{pc.id},squadid:{pc.squad})',
                "payload": squad
            }), 201
def inventory_item_equip(pcid, type, slotname, itemid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{username})',
            "payload": None
        }), 409

    # Loading the Meta for later use
    try:
        metaWeapons = metas.get_meta('weapon')
        metaArmors = metas.get_meta('armor')
    except Exception as e:
        msg = f'Meta fectching: KO [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        logger.trace(f'Meta fectching: OK')

    try:
        creature_stats = RedisStats(pc).refresh().dict
    except Exception as e:
        msg = f'Stats Query KO - failed (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if creature_stats is None:
            msg = f'Stats Query KO - Not found (pcid:{pc.id})'
            logger.warning(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200

    try:
        item = fn_item_get_one(itemid)
    except Exception as e:
        msg = f'Item Query KO - failed (pcid:{pc.id},itemid:{itemid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if item is None:
            msg = f'Item Query KO - Not found (pcid:{pc.id},itemid:{itemid})'
            logger.warning(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200

    if type == 'weapon':
        itemmeta = dict(
            list(filter(lambda x: x["id"] == item.metaid,
                        metaWeapons))[0])  # GruikFix
    elif type == 'armor':
        itemmeta = dict(
            list(filter(lambda x: x["id"] == item.metaid,
                        metaArmors))[0])  # Gruikfix
    if itemmeta is None:
        msg = f'ItemMeta Query KO - Not found (pcid:{pc.id},itemid:{itemid})'
        logger.warning(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    sizex, sizey = itemmeta['size'].split("x")
    costpa = round(int(sizex) * int(sizey) / 2)
    if RedisPa(pc).get()['red']['pa'] < costpa:
        msg = f"Not enough PA (pcid:{pc.id},redpa:{RedisPa(pc).get()['red']['pa']},cost:{costpa})"
        logger.warning(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    # Pre-requisite checks
    if itemmeta['min_m'] > creature_stats['base']['m']:
        msg = f"M prequisites failed (m_min:{itemmeta['min_m']},m:{creature_stats['base']['m']})"
        logger.warning(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    elif itemmeta['min_r'] > creature_stats['base']['r']:
        msg = f"R prequisites failed (r_min:{itemmeta['min_r']},r:{creature_stats['base']['r']})"
        logger.warning(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    elif itemmeta['min_g'] > creature_stats['base']['g']:
        msg = f"G prequisites failed (g_min:{itemmeta['min_g']},g:{creature_stats['base']['g']})"
        logger.warning(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    elif itemmeta['min_v'] > creature_stats['base']['v']:
        msg = f"V prequisites failed (v_min:{itemmeta['min_v']},v:{creature_stats['base']['v']})"
        logger.warning(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    elif itemmeta['min_p'] > creature_stats['base']['p']:
        msg = f"P prequisites failed (p_min:{itemmeta['min_p']},p:{creature_stats['base']['p']})"
        logger.warning(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    elif itemmeta['min_b'] > creature_stats['base']['b']:
        msg = f"B prequisites failed (b_min:{itemmeta['min_b']},b:{creature_stats['base']['b']})"
        logger.warning(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    try:
        equipment = fn_slots_get_one(pc)
    except Exception as e:
        msg = f'Slots Query KO - failed (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if equipment is None:
            msg = f'Slots Query KO - Not found (pcid:{pc.id},itemid:{itemid})'
            logger.warning(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200

    try:
        # The item to equip exists, is owned by the PC, and we retrieved his equipment from DB
        if slotname == 'holster':
            if int(sizex) * int(sizey) <= 4:
                # It fits inside the holster
                fn_slots_set_one(pc, slotname, item.id)
            else:
                return jsonify({
                    "success": False,
                    "msg":
                    f"Item does not fit in holster (itemid:{item.id},size:{itemmeta['size']})",
                    "payload": None
                }), 200
        elif slotname == 'righthand':
            if equipment.righthand:
                # Something is already equipped in RH
                equipped = fn_item_get_one(equipment.righthand)
                # We equip a 1H weapon
                if int(sizex) * int(sizey) <= 6:
                    if metaWeapons[equipped.metaid - 1]['onehanded'] is True:
                        # A 1H weapons is in RH : we replace
                        fn_slots_set_one(pc, 'righthand', item.id)
                    if metaWeapons[equipped.metaid - 1]['onehanded'] is False:
                        # A 2H weapons is in RH & LH : we replace RH and clean LH
                        fn_slots_set_one(pc, 'righthand', item.id)
                        fn_slots_set_one(pc, 'lefthand', None)
                # We equip a 2H weapon
                if int(sizex) * int(sizey) > 6:
                    # It is a 2H weapon: it fits inside the RH & LH
                    fn_slots_set_one(pc, 'righthand', item.id)
                    fn_slots_set_one(pc, 'lefthand', item.id)
            else:
                # Nothing in RH
                # We equip a 1H weapon
                if int(sizex) * int(sizey) <= 6:
                    fn_slots_set_one(pc, 'righthand', item.id)
                # We equip a 2H weapon
                if int(sizex) * int(sizey) > 6:
                    # It is a 2H weapon: it fits inside the RH & LH
                    fn_slots_set_one(pc, 'righthand', item.id)
                    fn_slots_set_one(pc, 'lefthand', item.id)
        elif slotname == 'lefthand':
            if int(sizex) * int(sizey) <= 4:
                # It fits inside the left hand
                fn_slots_set_one(pc, slotname, item.id)
            else:
                return jsonify({
                    "success": False,
                    "msg":
                    f"Item does not fit in left hand (itemid:{item.id},size:{itemmeta['size']})",
                    "payload": None
                }), 200
        else:
            fn_slots_set_one(pc, slotname, item.id)
    except Exception as e:
        msg = f'Slots Query KO - failed (pcid:{pc.id},itemid:{itemid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    # Here everything should be OK with the equip
    try:
        # We consume the red PA (costpa) right now
        RedisPa(pc).set(costpa, 0)
    except Exception as e:
        msg = f'Redis Query KO (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        equipment = fn_slots_get_one(pc)

        # We put the info in queue for ws
        qmsg = {
            "ciphered": False,
            "payload": equipment,
            "route": "mypc/{id1}/inventory/item/{id2}/equip/{id3}/{id4}",
            "scope": {
                "id": None,
                "scope": 'broadcast'
            }
        }
        queue.yqueue_put('broadcast', json.loads(jsonify(qmsg).get_data()))

        # We create the Creature Event
        RedisEvent(pc).add(pc.id, None, 'item', f'Equipped something',
                           30 * 86400)
        # JOB IS DONE
        msg = f'Equip Query OK (pcid:{pc.id},itemid:{itemid})'
        logger.trace(msg)
        return jsonify({
            "success": True,
            "msg": msg,
            "payload": {
                "red": RedisPa(pc).get()['red'],
                "blue": RedisPa(pc).get()['blue'],
                "equipment": equipment
            }
        }), 200
def inventory_item_dismantle(pcid, itemid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{username})',
            "payload": None
        }), 409
    if RedisPa(pc).get()['blue']['pa'] < 1:
        return jsonify({
            "success": False,
            "msg": f'Not enough PA (pcid:{pcid})',
            "payload": None
        }), 200

    try:
        item = fn_item_get_one(itemid)
    except Exception as e:
        msg = f'Item Query KO - failed (pcid:{pc.id},itemid:{itemid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if item is None:
            msg = f'Item Query KO - Not found (pcid:{pc.id},itemid:{itemid})'
            logger.warning(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200

    if item.rarity == 'Broken':
        shards = [6, 0, 0, 0, 0, 0]
        incr.many(f'highscores:{pc.id}:action:dismantle:shards:{item.rarity}',
                  shards[0])
    elif item.rarity == 'Common':
        shards = [0, 5, 0, 0, 0, 0]
        incr.many(f'highscores:{pc.id}:action:dismantle:shards:{item.rarity}',
                  shards[1])
    elif item.rarity == 'Uncommon':
        shards = [0, 0, 4, 0, 0, 0]
        incr.many(f'highscores:{pc.id}:action:dismantle:shards:{item.rarity}',
                  shards[2])
    elif item.rarity == 'Rare':
        shards = [0, 0, 0, 3, 0, 0]
        incr.many(f'highscores:{pc.id}:action:dismantle:shards:{item.rarity}',
                  shards[3])
    elif item.rarity == 'Epic':
        shards = [0, 0, 0, 0, 2, 0]
        incr.many(f'highscores:{pc.id}:action:dismantle:shards:{item.rarity}',
                  shards[4])
    elif item.rarity == 'Legendary':
        shards = [0, 0, 0, 0, 0, 1]
        incr.many(f'highscores:{pc.id}:action:dismantle:shards:{item.rarity}',
                  shards[5])

    try:
        # We add the shards in the wallet
        wallet = fn_wallet_shards_add(pc, shards)
    except Exception as e:
        msg = f'Wallet/Shards Query KO (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    try:
        # We destroy the item
        fn_item_del(pc, item.id)
    except Exception as e:
        msg = f'Item Query KO (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    try:
        # We consume the blue PA (1)
        RedisPa(pc).set(0, 1)
        # We add HighScore
        incr.many(f'highscores:{pc.id}:action:dismantle:items', 1)
    except Exception as e:
        msg = f'Redis Query KO (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        # JOB IS DONE
        return jsonify({
            "success": True,
            "msg": f'Item dismantle OK (pcid:{pc.id})',
            "payload": {
                "shards": {
                    "Broken": shards[0],
                    "Common": shards[1],
                    "Uncommon": shards[2],
                    "Rare": shards[3],
                    "Epic": shards[4],
                    "Legendary": shards[5]
                },
                "wallet": wallet
            }
        }), 200
Example #15
0
def action_resolver_skill(pcid, skillmetaid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if not request.is_json:
        msg = f'Missing JSON in request'
        logger.warn(msg)
        return jsonify({"msg": msg, "success": False, "payload": None}), 400
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.instance is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not in an instance (pcid:{pcid})',
            "payload": None
        }), 200

    try:
        cd = cds.get_cd(pc, skillmetaid)
    except Exception as e:
        msg = f'CDs Query KO (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if cd:
            # The skill was already used, and still on CD
            msg = f'Skill already on CD (pcid:{pc.id},skillmetaid:{skillmetaid})'
            logger.debug(msg)
            return jsonify({"success": False, "msg": msg, "payload": cd}), 200

    try:
        fightEventname = request.json.get('name', None)
        fightEventtype = request.json.get('type', None)
        fightEventactor = request.json.get('actor', None)
        fightEventparams = request.json.get('params', None)
        map = instances.get_instance(pc.instance)['map']
        creatures = fn_creatures_in_instance(pc.instance)
        creatures_effects = effects.get_instance_effects(pc)
        creatures_statuses = statuses.get_instance_statuses(pc)
        creatures_cds = cds.get_instance_cds(pc)
        pas = RedisPa(pc).get()
    except Exception as e:
        msg = f'ResolverInfo Query KO [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    # Everythins if fine, we can build the payload
    # Supposedly got all infos
    payload = {
        "context": {
            "map": map,
            "instance": pc.instance,
            "creatures": creatures,
            "effects": creatures_effects,
            "status": creatures_statuses,
            "cd": creatures_cds,
            "pa": pas
        },
        "fightEvent": {
            "name": fightEventname,
            "type": fightEventtype,
            "actor": fightEventactor,
            "params": fightEventparams
        }
    }

    try:
        response = requests.post(f'{RESOLVER_URL}/', json=payload)
    except Exception as e:
        msg = f'Resolver Query KO (pcid:{pc.id},skillmetaid:{skillmetaid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        # We create the Creature Event
        RedisEvent(pc).add(pc.id, None, 'skill',
                           f'Used a Skill ({skillmetaid})', 30 * 86400)
        msg = f'Resolver Query OK (pcid:{pc.id})'
        logger.debug(msg)
        return jsonify({
            "success": True,
            "msg": msg,
            "payload": {
                "resolver": json.loads(response.text),
                "internal": payload
            }
        }), 200
Example #16
0
def korp_create(pcid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if not request.is_json:
        return jsonify({"msg": "Missing JSON in request"}), 400
    korpname = request.json.get('name', None)
    if korpname is None:
        return jsonify({
            "success": False,
            "msg": f'Korpname not found (korpname:{korpname})',
            "payload": None
        }), 200

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.korp is not None:
        return jsonify({
            "success": False,
            "msg": f'PC already in a Korp (pcid:{pc.id},korpid:{pc.korp})',
            "payload": None
        }), 200

    # Remove everything, except alphanumeric, space, squote
    korpname = ''.join([c for c in korpname if c.isalnum() or c in [" ", "'"]])
    # Size check
    if len(korpname) > 16:
        return jsonify({
            "success": False,
            "msg": f'Korp name too long (korpname:{korpname})',
            "payload": None
        }), 200
    # Unicity test
    if fn_korp_get_one_by_name(korpname):
        msg = f'Korp already exists (korpname:{korpname})'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 409

    try:
        newkorp = fn_korp_add_one(pc, korpname)
    except Exception as e:
        msg = f'Korp Query KO (pcid:{pcid},korpname:{korpname}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    # Korp created, let's assign the team creator in the korp
    try:
        pc = fn_korp_set_rank(pc, newkorp.id, 'Leader')
    except Exception as e:
        msg = f'Korp Query KO (pcid:{pcid},korpname:{korpname}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        try:
            korp = fn_korp_get_one(newkorp.id)
        except Exception as e:
            msg = f'Korp Query KO (korpid:{newkorp.id}) [{e}]'
            logger.error(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200
        else:
            # We put the info in queue for ws
            qmsg = {
                "ciphered": False,
                "payload":
                f':information_source: **[{pc.id}] {pc.name}** created this Korp',
                "embed": None,
                "scope": f'Korp-{pc.korp}'
            }
            queue.yqueue_put('yarqueue:discord', qmsg)
            # We put the info in queue for ws Front
            qmsg = {
                "ciphered": False,
                "payload": korp,
                "route": 'mypc/{id1}/korp',
                "scope": 'korp'
            }
            queue.yqueue_put('broadcast', json.loads(jsonify(qmsg).get_data()))

            return jsonify({
                "success": True,
                "msg": f'Korp create OK (pcid:{pc.id},korpid:{pc.korp})',
                "payload": korp
            }), 201
Example #17
0
def action_weapon_reload(pcid,weaponid):
    pc          = fn_creature_get(None,pcid)[3]
    user        = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({"success": False,
                        "msg": f'Creature not found (pcid:{pcid})',
                        "payload": None}), 200
    if pc.account != user.id:
        return jsonify({"success": False,
                        "msg": f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
                        "payload": None}), 409

    # Retrieving weapon stats
    try:
        item = fn_item_get_one(weaponid)
    except Exception as e:
        msg = f'Item Query KO (pcid:{pc.id},weaponid:{weaponid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False,
                        "msg": msg,
                        "payload": None}), 200
    else:
        if item is None:
            return jsonify({"success": False,
                            "msg": f'Item not found (pcid:{pc.id},weaponid:{weaponid})',
                            "payload": None}), 200

    itemmeta = dict(list(filter(lambda x:x["id"]==item.metaid,metaWeapons))[0]) # Gruikfix
    if itemmeta is None:
        return jsonify({"success": False,
                        "msg": f'ItemMeta not found (pcid:{pcid},weaponid:{item.id})',
                        "payload": None}), 200
    if itemmeta['pas_reload'] is None:
        return jsonify({"success": False,
                        "msg": f'Item is not reloadable (pcid:{pc.id},weaponid:{item.id})',
                        "payload": None}), 200
    if item.ammo == itemmeta['max_ammo']:
        return jsonify({"success": False,
                        "msg": f'Item is already loaded (pcid:{pc.id},weaponid:{item.id})',
                        "payload": None}), 200

    if RedisPa(pc).get()['red']['pa'] < itemmeta['pas_reload']:
        # Not enough PA to reload
        return jsonify({"success": False,
                        "msg": f'Not enough PA to reload (pcid:{pc.id})',
                        "payload": {"red": RedisPa(pc).get()['red'],
                                    "blue": RedisPa(pc).get()['blue'],
                                    "action": None}}), 200

    walletammo = fn_wallet_ammo_get(pc,item,itemmeta['caliber'])
    neededammo = itemmeta['max_ammo'] - item.ammo
    if walletammo < neededammo:
        # Not enough ammo to reload
        return jsonify({"success": False,
                        "msg": f'Not enough PA to reload (pcid:{pc.id})',
                        "payload": None}), 200

    try:
        # We reload the weapon
        fn_item_ammo_set(weaponid,neededammo)
        # We remove the ammo from wallet
        fn_wallet_ammo_set(pc,itemmeta['caliber'],neededammo * -1)
        # We consume the PA
        RedisPa(pc).set(itemmeta['pas_reload'],0)
        # Wa add HighScore
        incr.one(f'highscores:{pc.id}:action:reload')
        # We create the Creature Event
        RedisEvent(pc).add(pc.id,
                           None,
                           'action',
                           f'Reloaded a weapon',
                           30*86400)
    except Exception as e:
        msg = f'Weapon reload KO (pcid:{pc.id},weaponid:{item.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False,
                        "msg": msg,
                        "payload": None}), 200
    else:
        return jsonify({"success": True,
                        "msg": f'Weapon reload OK (pcid:{pc.id},weaponid:{item.id})',
                        "payload": {"red": RedisPa(pc).get()['red'],
                                    "blue": RedisPa(pc).get()['blue'],
                                    "weapon": fn_item_get_one(weaponid)}}), 200
Example #18
0
def squad_accept(pcid, squadid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.squad != squadid:
        return jsonify({
            "success": False,
            "msg":
            f'Squad request outside of your scope (pcid:{pc.id},squadid:{squadid})',
            "payload": None
        }), 200
    if pc.squad_rank != 'Pending':
        return jsonify({
            "success": False,
            "msg":
            f'PC is not pending in a Squad (pcid:{pc.id},squadid:{squadid})',
            "payload": None
        }), 200

    try:
        pc = fn_squad_set_rank(pc, squadid, 'Member')
    except Exception as e:
        msg = f'Squad Query KO (pcid:{pc.id},squadid:{squadid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        try:
            squad = fn_squad_get_one(squadid)
        except Exception as e:
            msg = f'Squad Query KO (squadid:{squadid}) [{e}]'
            logger.error(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200
        else:
            # We put the info in queue for ws
            qmsg = {
                "ciphered": False,
                "payload":
                f':information_source: **[{pc.id}] {pc.name}** joined this Squad',
                "embed": None,
                "scope": f'Squad-{pc.squad}'
            }
            queue.yqueue_put('yarqueue:discord', qmsg)
            # We put the info in queue for ws Front
            qmsg = {
                "ciphered": False,
                "payload": squad,
                "route": 'mypc/{id1}/squad',
                "scope": 'squad'
            }
            queue.yqueue_put('broadcast', json.loads(jsonify(qmsg).get_data()))

            return jsonify({
                "success": True,
                "msg": f'Squad accept OK (pcid:{pc.id},squadid:{squadid})',
                "payload": squad
            }), 200
Example #19
0
def korp_leave(pcid, korpid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.korp != korpid:
        return jsonify({
            "success": False,
            "msg":
            f'Korp request outside of your scope (pcid:{pc.id},korpid:{korpid})',
            "payload": None
        }), 200
    if pc.korp_rank == 'Leader':
        return jsonify({
            "success": False,
            "msg":
            f'PC cannot be the Korp Leader (pcid:{pc.id},korpid:{korpid})',
            "payload": None
        }), 200

    try:
        fn_korp_set_rank(pc, None, None)  # We empty Korp fields for the Member
    except Exception as e:
        msg = f'Korp Query KO (korpid:{korpid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        try:
            korp = fn_korp_get_one(korpid)
        except Exception as e:
            msg = f'Korp Query KO (korpid:{korpid}) [{e}]'
            logger.error(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200
        else:
            # We put the info in queue for ws
            qmsg = {
                "ciphered": False,
                "payload":
                f':information_source: **[{pc.id}] {pc.name}** left this Korp',
                "embed": None,
                "scope": f'Korp-{korpid}'
            }
            queue.yqueue_put('yarqueue:discord', qmsg)
            # We put the info in queue for ws Front
            qmsg = {
                "ciphered": False,
                "payload": korp,
                "route": 'mypc/{id1}/korp',
                "scope": 'korp'
            }
            queue.yqueue_put('broadcast', json.loads(jsonify(qmsg).get_data()))

            return jsonify({
                "success": True,
                "msg": f'Korp leave OK (pcid:{pc.id},korpid:{korpid})',
                "payload": None
            }), 200
Example #20
0
def korp_invite(pcid, korpid, targetid):
    target = fn_creature_get(None, targetid)[3]
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.korp != korpid:
        return jsonify({
            "success": False,
            "msg":
            f'Korp request outside of your scope (pcid:{pc.id},korpid:{korpid})',
            "payload": None
        }), 200
    if pc.korp_rank != 'Leader':
        return jsonify({
            "success": False,
            "msg":
            f'PC is not the korp Leader (pcid:{pc.id},korprank:{pc.korp_rank})',
            "payload": None
        }), 200
    if target is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (targetid:{targetid})',
            "payload": None
        }), 200
    if target.korp is not None:
        return jsonify({
            "success": False,
            "msg":
            f'PC invited is already in a Korp (pcid:{target.id},korpid:{target.korp})',
            "payload": None
        }), 200

    # Korp population check
    try:
        korp = fn_korp_get_one(korpid)
    except Exception as e:
        msg = f'Korp Query KO (pcid:{pc.id},korpid:{korpid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    count = len(korp['members']) + len(korp['pending'])
    maxmembers = 100
    if count == maxmembers:
        msg = f'Korp is already full (korpid:{korpid},members:{count}/{maxmembers})'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    try:
        fn_korp_set_rank(target, korpid,
                         'Pending')  # We set Korp fields for the Target
    except Exception as e:
        msg = f'Korp Query KO (korpid:{korpid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        try:
            korp = fn_korp_get_one(korpid)
        except Exception as e:
            msg = f'Korp Query KO (pcid:{pc.id},korpid:{korpid}) [{e}]'
            logger.error(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200
        else:
            # We put the info in queue for ws
            qmsg = {
                "ciphered": False,
                "payload":
                f':information_source: **[{pc.id}] {pc.name}** invited **[{target.id}] {target.name}** in this Korp',
                "embed": None,
                "scope": f'Korp-{pc.korp}'
            }
            queue.yqueue_put('yarqueue:discord', qmsg)
            # We put the info in queue for ws Front
            qmsg = {
                "ciphered": False,
                "payload": korp,
                "route": 'mypc/{id1}/korp',
                "scope": 'korp'
            }
            queue.yqueue_put('broadcast', json.loads(jsonify(qmsg).get_data()))

            return jsonify({
                "success": True,
                "msg": f'Korp invite OK (pcid:{pc.id},korpid:{korpid})',
                "payload": korp
            }), 200
def inventory_item_unequip(pcid, type, slotname, itemid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{username})',
            "payload": None
        }), 409

    try:
        equipment = fn_slots_get_one(pc)
    except Exception as e:
        msg = f'Slots Query KO - failed (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if equipment is None:
            msg = f'Slots Query KO - Not found (pcid:{pc.id},itemid:{itemid})'
            logger.warning(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200

    try:
        if slotname == 'righthand':
            if equipment.righthand == itemid:
                if equipment.righthand == equipment.lefthand:
                    # If the weapon equipped takes both hands
                    fn_slots_set_one(pc, 'righthand', None)
                    fn_slots_set_one(pc, 'lefthand', None)
                else:
                    fn_slots_set_one(pc, slotname, None)
        elif slotname == 'lefthand':
            if equipment.lefthand == itemid:
                if equipment.righthand == equipment.lefthand:
                    # If the weapon equipped takes both hands
                    fn_slots_set_one(pc, 'righthand', None)
                    fn_slots_set_one(pc, 'lefthand', None)
                else:
                    fn_slots_set_one(pc, slotname, None)
        else:
            fn_slots_set_one(pc, slotname, None)
    except Exception as e:
        msg = f'Slots Query KO - failed (pcid:{pc.id},itemid:{itemid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    # Here everything should be OK with the unequip
    else:
        equipment = fn_slots_get_one(pc)

        # We put the info in queue for ws
        qmsg = {
            "ciphered": False,
            "payload": equipment,
            "route": "mypc/{id1}/inventory/item/{id2}/unequip/{id3}/{id4}",
            "scope": {
                "id": None,
                "scope": 'broadcast'
            }
        }
        queue.yqueue_put('broadcast', json.loads(jsonify(qmsg).get_data()))

        # JOB IS DONE
        msg = f'Unequip Query OK (pcid:{pc.id},itemid:{itemid})'
        logger.trace(msg)
        return jsonify({
            "success": True,
            "msg": msg,
            "payload": {
                "red": RedisPa(pc).get()['red'],
                "blue": RedisPa(pc).get()['blue'],
                "equipment": equipment
            }
        }), 200
Example #22
0
def korp_delete(pcid, korpid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.korp != korpid:
        return jsonify({
            "success": False,
            "msg":
            f'Korp request outside of your scope (pcid:{pc.id},korpid:{korpid})',
            "payload": None
        }), 200
    if pc.korp_rank != 'Leader':
        return jsonify({
            "success": False,
            "msg":
            f'PC is not the korp Leader (pcid:{pc.id},korprank:{pc.korp_rank})',
            "payload": None
        }), 200

    try:
        korp = fn_korp_get_one(korpid)
    except Exception as e:
        msg = f'Korp Query KO (pcid:{pc.id},korpid:{korpid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    count = len(korp['members']) + len(korp['pending'])
    if count > 1:
        msg = f'Korp not empty (korpid:{korpid},members:{count})'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    try:
        fn_korp_delete_one(korp['korp'].id)  # We delete the Korp
        fn_korp_set_rank(pc, None, None)  # We empty Korp fields for the Leader
    except Exception as e:
        msg = f'Korp Query KO (korpid:{korpid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        # We put the info in queue for ws
        qmsg = {
            "ciphered": False,
            "payload":
            f':information_source: **[{pc.id}] {pc.name}** deleted this Korp',
            "embed": None,
            "scope": f'Korp-{korpid}'
        }
        queue.yqueue_put('yarqueue:discord', qmsg)
        # We put the info in queue for ws Front
        qmsg = {
            "ciphered": False,
            "payload": None,
            "route": 'mypc/{id1}/korp',
            "scope": 'korp'
        }
        queue.yqueue_put('broadcast', qmsg)

        return jsonify({
            "success": True,
            "msg": f'Korp delete OK (pcid:{pc.id},korpid:{korpid})',
            "payload": None
        }), 200
def instance_leave(pcid, instanceid):
    creature = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if creature is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (creatureid:{creatureid})',
            "payload": None
        }), 200
    if creature.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (creatureid:{creature.id},username:{user.name})',
            "payload": None
        }), 409
    if creature.instance is None:
        return jsonify({
            "success": False,
            "msg":
            f'Creature not in an instance (creatureid:{creature.id},instanceid:{creature.instance})',
            "payload": None
        }), 200
    if creature.instance != instanceid:
        return jsonify({
            "success": False,
            "msg":
            f'PC is not in this instance (creatureid:{creature.id},instanceid:{instanceid})',
            "payload": None
        }), 200

    # Check if the instance exists
    try:
        instance = instances.get_instance(instanceid)
    except Exception as e:
        msg = f'Instance Query KO (creatureid:{creature.id},instanceid:{instanceid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if instance is False:
            return jsonify({
                "success": False,
                "msg":
                f'Instance not found (creatureid:{creature.id},instanceid:{instanceid})',
                "payload": None
            }), 200

    # Check if PC is the last inside the instance
    try:
        pcs = fn_creatures_in_instance(instanceid)
    except Exception as e:
        msg = f'PCs query failed (instanceid:{instanceid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    if len(pcs) == 1:
        # The PC is the last inside: we delete the instance
        # SQL modifications
        try:
            # Start with Redis deletion
            count = instances.del_instance(instanceid)
            if count is None or count == 0:
                # Delete keys failes, or keys not found
                return jsonify({
                    "success": False,
                    "msg":
                    f'Instance cleaning KO (instanceid:{instanceid}) [{e}]',
                    "payload": None
                }), 200
            # SQL data deletion
            ret = fn_creature_instance_set(creature, None)
            if ret is None:
                return jsonify({
                    "success": False,
                    "msg": f"Instance leave KO (creatureid:{creature.id})",
                    "payload": None
                }), 200
        except Exception as e:
            msg = f'Instance cleaning KO (instanceid:{instanceid}) [{e}]'
            logger.error(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200
        else:
            # Everything went well, deletion DONE
            # We put the info in queue for Discord
            scopes = []
            if ret.korp is not None: scopes.append(f'Korp-{ret.korp}')
            if ret.squad is not None: scopes.append(f'Squad-{ret.squad}')
            for scope in scopes:
                qmsg = {
                    "ciphered": False,
                    "payload":
                    f':map: **[{ret.id}] {ret.name}** closed an Instance ({instanceid})',
                    "embed": None,
                    "scope": scope
                }
                try:
                    queue.yqueue_put('yarqueue:discord', qmsg)
                except Exception as e:
                    msg = f'Queue Query KO (Queue:yarqueue:discord,qmsg:{qmsg}) [{e}]'
                    logger.error(msg)
                else:
                    logger.debug(
                        f'Queue Query OK (Queue:yarqueue:discord,qmsg:{qmsg})')
            # We put the info in queue for IA to clean the instance
            try:
                qmsg = {"action": 'delete', "instance": instance}
                queue.yqueue_put('yarqueue:instances', qmsg)
            except Exception as e:
                msg = f'Queue Query KO (Queue:yarqueue:instances,qmsg:{qmsg}) [{e}]'
                logger.error(msg)
            else:
                logger.debug(
                    f'Queue Query OK (Queue:yarqueue:instances,qmsg:{qmsg})')
            # Finally everything is done
            return jsonify({
                "success": True,
                "msg":
                f"Instance leave OK (creatureid:{creature.id},instanceid:{instanceid})",
                "payload": ret
            }), 200
    else:
        # Other players are still in the instance
        try:
            ret = fn_creature_instance_set(creature, None)
            if ret is None:
                return jsonify({
                    "success": False,
                    "msg": f"Instance leave KO (creatureid:{creature.id})",
                    "payload": None
                }), 200
        except Exception as e:
            msg = f'Instance cleaning KO (instanceid:{instanceid}) [{e}]'
            logger.error(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200
        else:
            # We put the info in queue for Discord
            scopes = []
            if ret.korp is not None: scopes.append(f'Korp-{ret.korp}')
            if ret.squad is not None: scopes.append(f'Squad-{ret.squad}')
            for scope in scopes:
                qmsg = {
                    "ciphered": False,
                    "payload":
                    f':map: **[{ret.id}] {ret.name}** left an Instance ({instanceid})',
                    "embed": None,
                    "scope": scope
                }
                try:
                    queue.yqueue_put('yarqueue:discord', qmsg)
                except Exception as e:
                    msg = f'Queue Query KO (Queue:yarqueue:discord,qmsg:{qmsg}) [{e}]'
                    logger.error(msg)
                else:
                    logger.debug(
                        f'Queue Query OK (Queue:yarqueue:discord,qmsg:{qmsg})')
            return jsonify({
                "success": True,
                "msg":
                f"Instance leave OK (creatureid:{creature.id},instanceid:{instanceid})",
                "payload": ret
            }), 200
Example #24
0
def action_resolver_move(pcid):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if not request.is_json:
        msg = f'Missing JSON in request'
        logger.warn(msg)
        return jsonify({"msg": msg, "success": False, "payload": None}), 400
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.instance is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not in an instance (pcid:{pcid})',
            "payload": None
        }), 200

    try:
        path = request.json.get('path', None)
        map = instances.get_instance(pc.instance)['map']
        creatures = fn_creatures_in_instance(pc.instance)
        creatures_effects = effects.get_instance_effects(pc)
        creatures_statuses = statuses.get_instance_statuses(pc)
        creatures_cds = cds.get_instance_cds(pc)
        pas = RedisPa(pc).get()
    except Exception as e:
        msg = f'ResolverInfo Query KO [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200

    # Supposedly got all infos
    payload = {
        "context": {
            "map": map,
            "instance": pc.instance,
            "creatures": creatures,
            "effects": creatures_effects,
            "status": creatures_statuses,
            "cd": creatures_cds,
            "pa": pas
        },
        "fightEvent": {
            "name": "RegularMovesFightClass",
            "type": 3,
            "actor": 1,
            "params": {
                "type": "target",
                "destinationType": "tile",
                "destination": None,
                "options": {
                    "path": path
                }
            }
        }
    }

    try:
        response = requests.post(f'{RESOLVER_URL}/', json=payload)
    except Exception as e:
        msg = f'Resolver Query KO - Failed (pcid:{pc.id}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        # We create the Creature Event
        RedisEvent(pc).add(pc.id, None, 'action', 'Moved', 30 * 86400)
        msg = f'Resolver Query OK (pcid:{pc.id})'
        logger.debug(msg)
        return jsonify({
            "success": True,
            "msg": msg,
            "payload": {
                "resolver": json.loads(response.text),
                "internal": payload
            }
        }), 200
Example #25
0
def korp_kick(pcid, korpid, targetid):
    target = fn_creature_get(None, targetid)[3]
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{user.name})',
            "payload": None
        }), 409
    if pc.korp != korpid:
        return jsonify({
            "success": False,
            "msg":
            f'Korp request outside of your scope (pcid:{pc.id},korpid:{korpid})',
            "payload": None
        }), 200
    if pc.korp_rank != 'Leader':
        return jsonify({
            "success": False,
            "msg":
            f'PC is not the korp Leader (pcid:{pc.id},korprank:{pc.korp_rank})',
            "payload": None
        }), 200
    if target is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (targetid:{targetid})',
            "payload": None
        }), 200
    if target.korp is None:
        return jsonify({
            "success": False,
            "msg":
            f'PC kicked is not in a Korp (pcid:{target.id},korpid:{target.korp})',
            "payload": None
        }), 200
    if pc.id == targetid:
        return (
            200, False,
            f'PC target cannot be the Korp Leader (pcid:{pcid},targetid:{targetid})',
            None)

    try:
        fn_korp_set_rank(target, None,
                         None)  # We empty Korp fields for the Target
    except Exception as e:
        msg = f'Korp Query KO (korpid:{korpid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        try:
            korp = fn_korp_get_one(korpid)
        except Exception as e:
            msg = f'Korp Query KO (pcid:{pc.id},korpid:{korpid}) [{e}]'
            logger.error(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200
        else:
            # We put the info in queue for ws Discord
            qmsg = {
                "ciphered": False,
                "payload":
                f':information_source: **[{pc.id}] {pc.name}** kicked **[{target.id}] {target.name}** from this Korp',
                "embed": None,
                "scope": f'Korp-{pc.korp}'
            }
            queue.yqueue_put('yarqueue:discord', qmsg)
            # We put the info in queue for ws Front
            qmsg = {
                "ciphered": False,
                "payload": korp,
                "route": 'mypc/{id1}/korp',
                "scope": 'korp'
            }
            queue.yqueue_put('broadcast', json.loads(jsonify(qmsg).get_data()))

            return jsonify({
                "success": True,
                "msg": f'Korp kick OK (pcid:{pc.id},korpid:{korpid})',
                "payload": korp
            }), 200
def inventory_item_offset(pcid, itemid, offsetx=None, offsety=None):
    pc = fn_creature_get(None, pcid)[3]
    user = fn_user_get(get_jwt_identity())

    # Pre-flight checks
    if pc is None:
        return jsonify({
            "success": False,
            "msg": f'Creature not found (pcid:{pcid})',
            "payload": None
        }), 200
    if pc.account != user.id:
        return jsonify({
            "success": False,
            "msg":
            f'Token/username mismatch (pcid:{pc.id},username:{username})',
            "payload": None
        }), 409

    try:
        item = fn_item_get_one(itemid)
    except Exception as e:
        msg = f'Item Query KO - failed (pcid:{pc.id},itemid:{itemid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        if item is None:
            msg = f'Item Query KO - Not found (pcid:{pc.id},itemid:{itemid})'
            logger.warning(msg)
            return jsonify({
                "success": False,
                "msg": msg,
                "payload": None
            }), 200

    try:
        item = fn_item_offset_set(itemid, offsetx, offsety)
    except Exception as e:
        msg = f'Item Query KO - failed (pcid:{pc.id},itemid:{itemid}) [{e}]'
        logger.error(msg)
        return jsonify({"success": False, "msg": msg, "payload": None}), 200
    else:
        all_items_sql = fn_item_get_all(pc)
        all_items_json = json.loads(jsonify(all_items_sql).get_data())

        armor = [x for x in all_items_json if x['metatype'] == 'armor']
        weapon = [x for x in all_items_json if x['metatype'] == 'weapon']
        equipment = fn_slots_get_all(pc)

        # JOB IS DONE
        msg = f'Offset Query OK (pcid:{pc.id},itemid:{itemid})'
        logger.trace(msg)
        return jsonify({
            "success": True,
            "msg": msg,
            "payload": {
                "armor": armor,
                "equipment": equipment,
                "weapon": weapon
            }
        }), 200