~mohab/fightlike

e11612eec4272a707933c5906b015f36d982c813 — mohab 11 months ago 9a79d1e
BREAKING CHANGE(feat): resolve same name conflicts
add game qualifier for mains
add response to character not found
4 files changed, 104 insertions(+), 19 deletions(-)

M db.py
M test_db.py
M test_util.py
M util.py
M db.py => db.py +29 -8
@@ 32,9 32,30 @@ def getReplyFromDB(charName: str, flags: [str]):
    # Get collection.
    collection = db["character"]

    # Get character archetype.
    charObj = collection.find_one({"name": charName})
    archetype: str = charObj["archetype"]
    # Formulate archetype query.
    archetypeQuery = {
        "name": charName
    }

    # Check for and add game flag, if present.
    if (len(flags) == 3):
        archetypeQuery["game.title"] = flags.pop(0)

    # Get character document.
    charObj = collection.find_one(archetypeQuery)

    # Assume charName is slug if name is not in database.
    if charObj is None:
        del archetypeQuery["name"]
        archetypeQuery["slug"] = charName
        charObj = collection.find_one(archetypeQuery)

    # Assume character is not in database if slug is not in database.
    if charObj is None:
        return "Unfortunately, your main is not in Fightlike's database yet. You can fill this form to add them: https://fightlike.mohab.xyz/submit"

    # Pull archetype from charObj.
    archetype: str = charObj["archetype"][0]

    # Formulate query.
    query = {


@@ 44,9 65,9 @@ def getReplyFromDB(charName: str, flags: [str]):
    # Add flags to query.
    for flag in flags:
        if (flag == "rollback" or flag == "delay"):
            query["netcode"] = flag
            query["game.netcode"] = flag
        else:
            query["franchise"] = flag
            query["game.franchise"] = flag

    # Get similar characters and compose reply.
    reply = f'{archetype.capitalize()} character(s) to try:\n\n'


@@ 54,9 75,9 @@ def getReplyFromDB(charName: str, flags: [str]):
    for entry in collection.find(query):
        if (charName == entry["name"]):
            continue
        elif (entry["netcode"] != "rollback"):
            reply += f'- {entry["name"]} in {entry["game"]} [Delay Netcode].\n'
        elif (entry["game"]["netcode"] != "rollback"):
            reply += f'- {entry["name"]} in {entry["game"]["title"]} [Delay Netcode].\n'
        else:
            reply += f'- {entry["name"]} in {entry["game"]}.\n'
            reply += f'- {entry["name"]} in {entry["game"]["title"]}.\n'

    return reply

M test_db.py => test_db.py +51 -4
@@ 1,11 1,58 @@
import db


# Test for Chaos substring in MongoDB response.
def test_getReplyFromDB():
# Test for 'Nine the Phantom' substring in MongoDB response.
def test_getReplyFromDBWithGameFlag():

    reply = db.getReplyFromDB("Eddie", ["rollback", "blazblue"])
    reply = db.getReplyFromDB("Testament", [
        "Guilty Gear -STRIVE-", "rollback", "blazblue"
    ])

    assert reply.find(
        "Blazblue: Centralfiction"
        "Nine the Phantom"
    ) >= 0


# Test for 'Carl Clover' substring in MongoDB response.
def test_getReplyFromDBWithoutGameFlag():

    reply = db.getReplyFromDB("Eddie", [
        "rollback", "blazblue"
    ])

    assert reply.find(
        "Carl Clover"
    ) >= 0


# Test for 'Jam Kuradoberi' substring in MongoDB response.
def test_getReplyFromDBWithFullName():

    reply = db.getReplyFromDB("Leo Whitefang", [
        "rollback", "guilty gear"
    ])

    assert reply.find(
        "Jam Kuradoberi"
    ) >= 0


# Test for 'Jam Kuradoberi' substring in MongoDB response.
def test_getReplyFromDBWithFirstName():

    reply = db.getReplyFromDB("leo", [
        "rollback", "guilty gear"
    ])

    assert reply.find(
        "Jam Kuradoberi"
    ) >= 0


def test_getReplyFromDBIfCharacterDoesNotExist():

    reply = db.getReplyFromDB("mohab", [])

    assert reply.find(
        "https://fightlike.mohab.xyz/submit"
    ) >= 0

M test_util.py => test_util.py +9 -2
@@ 19,7 19,14 @@ def test_getCharName():
    assert charName == "Eddie"


def test_getFullCharName():

    charName = util.getCharName("@fightlike leo whitefang")

    assert charName == "Leo Whitefang"


def test_getFlags():
    flags = util.getFlags("@fightlike eddie !blazblue !rollback")
    flags = util.getFlags("@fightlike eddie !ggxxacpr !blazblue !rollback")

    assert flags[0] == "blazblue" and flags[1] == "rollback"
    assert flags[0] == "ggxxacpr" and flags[1] == "blazblue" and flags[2] == "rollback"

M util.py => util.py +15 -5
@@ 13,14 13,24 @@ def getTootText(tootContent: str):

# Pull character name from text.
def getCharName(tootText: str):
    tootFillet: str = tootText.split(" !").pop(0)
    def getFullCharName(rawName: str):

        firstName = rawName.split(" ").pop(0).capitalize()

        lastName = rawName.split(" ").pop(-1).capitalize()

    charName: str = ""
        if (firstName == lastName):
            return firstName
        else:
            return firstName + " " + lastName

    tootFillet: str = tootText.split(" !").pop(0)

    if (tootFillet.find("@fightlike") == 0):
        charName = tootFillet[11:].capitalize()
    elif (tootFillet.find("@fightlike@botsin.space") == 0):
        charName = tootFillet[25:].capitalize()
        charName = getFullCharName(tootFillet[11:])
        print(charName)
    else:
        charName = getFullCharName(tootFillet[25:])

    return charName