def saveNpc(npcNum):
    # todo: rework the id part

    # check if npc already exists
    query = database.sendQuery("SELECT * FROM npcs WHERE id=%i;" % (npcNum+1))
    rows = query.fetchone()

    if rows == None:
        # npc doesnt exist, create it
        query = database.sendQuery("INSERT INTO npcs (id, name, sprite, attack_say, spawn_secs, behavior, range, drop_chance, drop_item, drop_item_value, stat_strength, stat_defense, stat_magic, stat_speed) \
                                                           VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", \
                                                                (int(npcNum+1), \
                                                                NPC[npcNum].name,  \
                                                                NPC[npcNum].sprite,   \
                                                                NPC[npcNum].attackSay,  \
                                                                NPC[npcNum].spawnSecs, \
                                                                NPC[npcNum].behaviour, \
                                                                NPC[npcNum].range, \
                                                                NPC[npcNum].dropChance, \
                                                                NPC[npcNum].dropItem, \
                                                                NPC[npcNum].dropItemValue, \
                                                                NPC[npcNum].stat[Stats.strength], \
                                                                NPC[npcNum].stat[Stats.defense], \
                                                                NPC[npcNum].stat[Stats.magic], \
                                                                NPC[npcNum].stat[Stats.speed]))

    elif len(rows) > 0:
        # npc already exists, so update the npc
        query = database.sendQuery("UPDATE npcs SET name=?, \
                                                          sprite=?, \
                                                          attack_say=?, \
                                                          spawn_secs=?, \
                                                          behavior=?, \
                                                          range=?, \
                                                          drop_chance=?, \
                                                          drop_item=?, \
                                                          drop_item_value=?, \
                                                          stat_strength=?, \
                                                          stat_defense=?, \
                                                          stat_magic=?, \
                                                          stat_speed=? \
                                                          WHERE id=?;", (NPC[npcNum].name,  \
                                                                           NPC[npcNum].sprite,   \
                                                                           NPC[npcNum].attackSay,  \
                                                                           NPC[npcNum].spawnSecs, \
                                                                           NPC[npcNum].behaviour, \
                                                                           NPC[npcNum].range, \
                                                                           NPC[npcNum].dropChance, \
                                                                           NPC[npcNum].dropItem, \
                                                                           NPC[npcNum].dropItemValue, \
                                                                           NPC[npcNum].stat[Stats.strength], \
                                                                           NPC[npcNum].stat[Stats.defense], \
                                                                           NPC[npcNum].stat[Stats.magic], \
                                                                           NPC[npcNum].stat[Stats.speed], \
                                                                           int(npcNum+1)))

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