Browse Source

- More small improvements as well as saving caught status to a db

master
Quildra 1 year ago
parent
commit
0858695265
  1. 67
      Site/OriginDex.py
  2. 99
      Site/templates/index.html

67
Site/OriginDex.py

@ -271,7 +271,74 @@ def pokemon_details(pfic):
conn.close()
return jsonify(encounters)
def init_user_db():
"""Initialize the user database and create necessary tables"""
conn = sqlite3.connect('user_data.db')
cursor = conn.cursor()
# Create the caught Pokemon table
cursor.execute('''
CREATE TABLE IF NOT EXISTS caught_pokemon (
pfic TEXT PRIMARY KEY,
caught_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()
@app.route('/toggle_catch/<string:pfic>', methods=['POST'])
def toggle_catch(pfic):
# First verify PFIC exists in pokemon_forms.db
ref_conn = sqlite3.connect('pokemon_forms.db')
ref_cursor = ref_conn.cursor()
ref_cursor.execute('SELECT 1 FROM pokemon_forms WHERE pfic = ?', (pfic,))
if not ref_cursor.fetchone():
ref_conn.close()
return jsonify({'error': 'Invalid PFIC'}), 400
ref_conn.close()
# Now handle the caught status in user_data.db
user_conn = sqlite3.connect('user_data.db')
user_cursor = user_conn.cursor()
try:
# Check if Pokemon is already caught
user_cursor.execute('SELECT pfic FROM caught_pokemon WHERE pfic = ?', (pfic,))
result = user_cursor.fetchone()
if result:
# Pokemon was caught, so uncatch it
user_cursor.execute('DELETE FROM caught_pokemon WHERE pfic = ?', (pfic,))
status = 'uncaught'
else:
# Pokemon wasn't caught, so catch it
user_cursor.execute('INSERT INTO caught_pokemon (pfic) VALUES (?)', (pfic,))
status = 'caught'
user_conn.commit()
return jsonify({'status': status, 'pfic': pfic})
except Exception as e:
user_conn.rollback()
return jsonify({'error': str(e)}), 500
finally:
user_conn.close()
@app.route('/get_caught_pokemon')
def get_caught_pokemon():
user_conn = sqlite3.connect('user_data.db')
user_cursor = user_conn.cursor()
user_cursor.execute('SELECT pfic FROM caught_pokemon')
caught = [row[0] for row in user_cursor.fetchall()]
user_conn.close()
return jsonify(caught)
if __name__ == '__main__':
init_user_db() # Initialize user database on startup
extra_files = ['.']
extra_dirs = ['./templates/', './static/']
for extra_dir in extra_dirs:

99
Site/templates/index.html

@ -451,6 +451,14 @@
justify-content: space-between;
gap: 10px;
}
.catch-count-pokeball.grayscale {
filter: grayscale(100%);
}
.target-image.grayscale {
filter: grayscale(100%);
}
</style>
</head>
<body>
@ -481,7 +489,7 @@
</div>
<div class="pokemon-info">
<div class="pokeball-container">
<img src="{{ url_for('static', filename='images/pokeball_color.png') }}" alt="Pokeball" class="pokeball-icon grayscale" onclick="togglePokeball(event, this)">
<img src="{{ url_for('static', filename='images/pokeball_color.png') }}" alt="Pokeball" class="pokeball-icon grayscale" data-pfic="{{ pokemon.pfic }}" onclick="togglePokeball(event, this)">
</div>
<div class="pokemon-number">#{{ '%04d'|format(pokemon.ID) }}</div>
{% if pokemon.MarkIcon %}
@ -529,7 +537,8 @@
<div class="catch-count-container">
<img src="{{ url_for('static', filename='images/pokeball_color.png') }}"
alt="Catch count"
class="catch-count-pokeball">
class="catch-count-pokeball grayscale"
data-pfic="{{ pokemon.pfic }}">
<span class="catch-count">{{ pokemon.catch_count }}</span>
</div>
{% if (pokemon.evolve_to|length > 0) or (pokemon.breed_for|length > 0) %}
@ -675,7 +684,72 @@
function togglePokeball(event, element) {
event.stopPropagation();
element.classList.toggle('grayscale');
const pfic = element.dataset.pfic;
fetch(`/toggle_catch/${pfic}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
})
.then(response => response.json())
.then(data => {
if (data.status === 'caught') {
element.classList.remove('grayscale');
} else {
element.classList.add('grayscale');
}
// Update the efficiency plan display
updateEfficiencyPlan(pfic, data.status === 'caught');
})
.catch(error => console.error('Error:', error));
}
function updateEfficiencyPlan(pfic, isCaught) {
const planTab = document.getElementById('efficiency-tab');
planTab.querySelectorAll('.pokemon-entry').forEach(entry => {
// Update for direct matches
if (entry.dataset.pfic === pfic) {
updateEntryDisplay(entry, isCaught);
}
// Update for evolution targets
entry.querySelectorAll('.target-card').forEach(card => {
if (card.dataset.pfic === pfic) {
updateTargetDisplay(card, entry, isCaught);
}
});
});
}
function updateEntryDisplay(entry, isCaught) {
const catchCount = entry.querySelector('.catch-count');
let count = parseInt(catchCount.textContent);
count += isCaught ? -1 : 1;
catchCount.textContent = count;
const pokeball = entry.querySelector('.catch-count-pokeball');
pokeball.classList.toggle('grayscale', count === 0);
updateGameTotal(entry, isCaught);
}
function updateTargetDisplay(card, parentEntry, isCaught) {
const targetImage = card.querySelector('.target-image');
targetImage.classList.toggle('grayscale', isCaught);
updateEntryDisplay(parentEntry, true); // Always decrease parent count
}
function updateGameTotal(entry, isCaught) {
const gameSection = entry.closest('.game-content');
if (gameSection) {
const gameTitle = gameSection.previousElementSibling;
const totalElement = gameTitle.querySelector('.game-catch-total');
let total = parseInt(totalElement.textContent.split(': ')[1]);
total += isCaught ? -1 : 1;
totalElement.textContent = `Catch: ${total}`;
}
}
// Lazy loading for images
@ -778,6 +852,25 @@
icon.textContent = '▲';
}
}
document.addEventListener('DOMContentLoaded', function() {
// Load caught Pokemon on page load
fetch('/get_caught_pokemon')
.then(response => response.json())
.then(caught => {
caught.forEach(pfic => {
const pokeball = document.querySelector(`.pokeball-icon[data-pfic="${pfic}"]`);
if (pokeball) {
pokeball.classList.remove('grayscale');
}
const planPokeball = document.querySelector(`.catch-count-pokeball[data-pfic="${pfic}"]`);
if (planPokeball) {
planPokeball.classList.remove('grayscale');
}
});
});
});
</script>
</body>
</html>

Loading…
Cancel
Save