def savePlayer(index):
    # check if character/player already exists
    query = database.sendQuery("SELECT * FROM characters WHERE name='%s';" % getPlayerName(index))
    rows = query.fetchone()

    if rows == None:
        # character/player doesnt exist, create it

        # get account id
        query = database.sendQuery("SELECT id FROM accounts WHERE username='%s';" % getPlayerLogin(index))
        result = query.fetchone()
        accountID = result['id']

        # save character
        query = database.sendQuery("INSERT INTO characters (account_id, name, class, sprite, level, exp, access, map, x, y, direction, helmet, armor, weapon, shield, stats_strength, stats_defense, stats_speed, stats_magic, vital_hp, vital_mp, vital_sp) \
                                                           VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", \
                                                             (accountID,                \
                                                              getPlayerName(index),     \
                                                              getPlayerClass(index),    \
                                                              getPlayerSprite(index),   \
                                                              getPlayerLevel(index),    \
                                                              getPlayerExp(index),      \
                                                              getPlayerAccess(index),   \
                                                              getPlayerMap(index),      \
                                                              getPlayerX(index),        \
                                                              getPlayerY(index),        \
                                                              getPlayerDir(index),      \
                                                              getPlayerEquipmentSlot(index, Equipment.helmet), \
                                                              getPlayerEquipmentSlot(index, Equipment.armor), \
                                                              getPlayerEquipmentSlot(index, Equipment.weapon), \
                                                              getPlayerEquipmentSlot(index, Equipment.shield), \
                                                              getPlayerStat(index, Stats.strength),  \
                                                              getPlayerStat(index, Stats.defense),  \
                                                              getPlayerStat(index, Stats.speed),  \
                                                              getPlayerStat(index, Stats.magic),  \
                                                              getPlayerVital(index, Vitals.hp), \
                                                              getPlayerVital(index, Vitals.mp), \
                                                              getPlayerVital(index, Vitals.sp)))

    elif len(rows) > 0:
        # character/player exists, so update the character
        charId = rows['id']

        query = database.sendQuery("""UPDATE characters SET sprite=?,
                                                          level=?,
                                                          exp=?,
                                                          map=?,
                                                          x=?,
                                                          y=?,
                                                          access=?,
                                                          direction=?,
                                                          helmet=?, armor=?, weapon=?, shield=?,
                                                          stats_strength=?, stats_defense=?, stats_speed=?, stats_magic=?,
                                                          vital_hp=?, vital_mp=?, vital_sp=?
                                                          WHERE name=?;""", (getPlayerSprite(index),     \
                                                                               getPlayerLevel(index),    \
                                                                               getPlayerExp(index),      \
                                                                               getPlayerMap(index),      \
                                                                               getPlayerX(index),        \
                                                                               getPlayerY(index),        \
                                                                               getPlayerAccess(index),   \
                                                                               getPlayerDir(index),      \
                                                                               getPlayerEquipmentSlot(index, Equipment.helmet), \
                                                                               getPlayerEquipmentSlot(index, Equipment.armor), \
                                                                               getPlayerEquipmentSlot(index, Equipment.weapon), \
                                                                               getPlayerEquipmentSlot(index, Equipment.shield), \
                                                                               getPlayerStat(index, Stats.strength),  \
                                                                               getPlayerStat(index, Stats.defense),  \
                                                                               getPlayerStat(index, Stats.speed),  \
                                                                               getPlayerStat(index, Stats.magic),  \
                                                                               getPlayerVital(index, Vitals.hp), \
                                                                               getPlayerVital(index, Vitals.mp), \
                                                                               getPlayerVital(index, Vitals.sp), \
                                                                               getPlayerName(index)))

        # save inventory
        for i in range(MAX_INV):
            if getPlayerInvItemNum(index, i) != None:
                # add item to inventory if its not already there
                itemNum = getPlayerInvItemNum(index, i)

                # check if its already there
                invQuery = database.sendQuery("SELECT * FROM inventory WHERE (character_id=%i AND item_id=%i);" % (charId, (itemNum+1)))
                invRows = query.fetchone()

                if invRows == None:
                    # doesnt exist so add item
                    invQuery = database.sendQuery('INSERT INTO inventory (character_id, item_id, value, durability) \
                                                               VALUES (%i, %i, %i, %i);' \
                                                               % (charId, \
                                                                  (getPlayerInvItemNum(index, i)+1),
                                                                  getPlayerInvItemValue(index, i),
                                                                  getPlayerInvItemDur(index, i)))

                elif len(invRows) > 0:
                    # item has already been added, update value and durability
                    # get id
                    rowId = invRows['id']
                    invQuery = database.sendQuery('UPDATE inventory SET value=%i, durability=%i WHERE id=%i;' % (getPlayerInvItemValue(index, i), getPlayerInvItemDur(index, i), rowId))

        for i in range(MAX_PLAYER_SPELLS):
            if getPlayerSpell(index, i) is not None:
                spellNum = getPlayerSpell(index, i)

                # check if its already there
                spbQuery = database.sendQuery("SELECT * FROM spellbook WHERE (character_id=%i AND spellnum=%i);" % (charId, (spellNum+1)))
                spbRows = query.fetchone()

                if spbRows is None:
                    # doesnt exist, so add the spell
                    spbQuery = database.sendQuery('INSERT INTO spellbook (character_id, spellnum) \
                                                               VALUES (?, ?);', \
                                                                 (charId, \
                                                                 spellNum+1))

                elif len(spbRows) > 0:
                    # item is already there, so do nothing
                    continue

    database.saveChanges()
dataset/ETHPy150Open marcusmoller/pyorpg-server/src/database.py/savePlayer/original