12 changed files with 481 additions and 281 deletions
Binary file not shown.
@ -1,112 +0,0 @@ |
|||||||
#!/usr/bin/env python3 |
|
||||||
""" |
|
||||||
Simple Profile Manager with API Integration Demo |
|
||||||
""" |
|
||||||
|
|
||||||
import json |
|
||||||
from user_profile_integration import UserProfileIntegration |
|
||||||
|
|
||||||
def main(): |
|
||||||
print("=== Profile Manager with API Integration ===") |
|
||||||
|
|
||||||
# Initialize the integration |
|
||||||
integration = UserProfileIntegration() |
|
||||||
|
|
||||||
print(f"\\nDevice ID: {integration.get_device_id()}") |
|
||||||
print(f"Active Profile: {integration.get_profile_name()}") |
|
||||||
print(f"API Server: {'Connected' if integration.api_enabled else 'Offline'}") |
|
||||||
|
|
||||||
while True: |
|
||||||
print("\\n=== MAIN MENU ===") |
|
||||||
print("1. Show Profile Info") |
|
||||||
print("2. View Online Leaderboard") |
|
||||||
print("3. View All Device Users") |
|
||||||
print("4. Submit Test Score") |
|
||||||
print("5. Create New Profile") |
|
||||||
print("6. Exit") |
|
||||||
|
|
||||||
try: |
|
||||||
choice = input("\\nSelect option (1-6): ").strip() |
|
||||||
|
|
||||||
if choice == "1": |
|
||||||
# Show profile info |
|
||||||
info = integration.get_profile_info() |
|
||||||
if info: |
|
||||||
print(f"\\n=== PROFILE INFO ===") |
|
||||||
for key, value in info.items(): |
|
||||||
print(f"{key.replace('_', ' ').title()}: {value}") |
|
||||||
else: |
|
||||||
print("No active profile loaded") |
|
||||||
|
|
||||||
elif choice == "2": |
|
||||||
# Show leaderboard |
|
||||||
if not integration.api_enabled: |
|
||||||
print("API server not available") |
|
||||||
continue |
|
||||||
|
|
||||||
print(f"\\n=== LEADERBOARD ===") |
|
||||||
leaderboard = integration.get_device_leaderboard(10) |
|
||||||
if leaderboard: |
|
||||||
for entry in leaderboard: |
|
||||||
print(f"{entry['rank']}. {entry['user_id']}: {entry['best_score']} pts ({entry['total_games']} games)") |
|
||||||
else: |
|
||||||
print("No scores recorded yet") |
|
||||||
|
|
||||||
elif choice == "3": |
|
||||||
# Show all users |
|
||||||
if not integration.api_enabled: |
|
||||||
print("API server not available") |
|
||||||
continue |
|
||||||
|
|
||||||
print(f"\\n=== DEVICE USERS ===") |
|
||||||
users = integration.get_all_device_users() |
|
||||||
if users: |
|
||||||
for user in users: |
|
||||||
print(f"{user['user_id']}: Best {user['best_score']}, {user['total_scores']} games") |
|
||||||
else: |
|
||||||
print("No users registered yet") |
|
||||||
|
|
||||||
elif choice == "4": |
|
||||||
# Submit test score |
|
||||||
if not integration.current_profile: |
|
||||||
print("No active profile to submit score for") |
|
||||||
continue |
|
||||||
|
|
||||||
try: |
|
||||||
score = int(input("Enter test score: ")) |
|
||||||
result = integration.update_game_stats(score, True) |
|
||||||
if result: |
|
||||||
print(f"Score {score} submitted successfully!") |
|
||||||
else: |
|
||||||
print("Failed to submit score") |
|
||||||
except ValueError: |
|
||||||
print("Invalid score entered") |
|
||||||
|
|
||||||
elif choice == "5": |
|
||||||
# Create new profile - simplified for demo |
|
||||||
name = input("Enter new profile name: ").strip() |
|
||||||
if not name: |
|
||||||
print("Name cannot be empty") |
|
||||||
continue |
|
||||||
|
|
||||||
success = integration.register_new_user(name) |
|
||||||
if success or not integration.api_enabled: |
|
||||||
print(f"Profile '{name}' created successfully!") |
|
||||||
else: |
|
||||||
print("Failed to create profile") |
|
||||||
|
|
||||||
elif choice == "6": |
|
||||||
print("Goodbye!") |
|
||||||
break |
|
||||||
|
|
||||||
else: |
|
||||||
print("Invalid choice") |
|
||||||
|
|
||||||
except KeyboardInterrupt: |
|
||||||
print("\\nGoodbye!") |
|
||||||
break |
|
||||||
except Exception as e: |
|
||||||
print(f"Error: {e}") |
|
||||||
|
|
||||||
if __name__ == "__main__": |
|
||||||
main() |
|
||||||
@ -1,140 +0,0 @@ |
|||||||
#!/usr/bin/env python3 |
|
||||||
""" |
|
||||||
Test script for Mice Game Score API |
|
||||||
""" |
|
||||||
|
|
||||||
import requests |
|
||||||
import json |
|
||||||
import time |
|
||||||
|
|
||||||
API_BASE_URL = "http://localhost:8000" |
|
||||||
|
|
||||||
def test_api(): |
|
||||||
"""Test all API endpoints""" |
|
||||||
print("Testing Mice Game Score API...") |
|
||||||
print("=" * 50) |
|
||||||
|
|
||||||
# Test device and user IDs |
|
||||||
device_id = "DEV-TEST001" |
|
||||||
user1 = "TestUser1" |
|
||||||
user2 = "TestUser2" |
|
||||||
|
|
||||||
try: |
|
||||||
# 1. Test root endpoint |
|
||||||
print("\n1. Testing root endpoint:") |
|
||||||
response = requests.get(f"{API_BASE_URL}/") |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
print(f"Response: {response.json()}") |
|
||||||
|
|
||||||
# 2. Test user signup |
|
||||||
print(f"\n2. Testing user signup for {user1}:") |
|
||||||
response = requests.post(f"{API_BASE_URL}/signup/{device_id}/{user1}") |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
print(f"Response: {response.json()}") |
|
||||||
|
|
||||||
print(f"\n3. Testing user signup for {user2}:") |
|
||||||
response = requests.post(f"{API_BASE_URL}/signup/{device_id}/{user2}") |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
print(f"Response: {response.json()}") |
|
||||||
|
|
||||||
# 4. Test duplicate signup (should fail) |
|
||||||
print(f"\n4. Testing duplicate signup for {user1} (should fail):") |
|
||||||
response = requests.post(f"{API_BASE_URL}/signup/{device_id}/{user1}") |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
print(f"Response: {response.json()}") |
|
||||||
|
|
||||||
# 5. Test getting users for device |
|
||||||
print(f"\n5. Testing get users for device {device_id}:") |
|
||||||
response = requests.get(f"{API_BASE_URL}/users/{device_id}") |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
print(f"Response: {response.json()}") |
|
||||||
|
|
||||||
# 6. Test score submission |
|
||||||
print(f"\n6. Testing score submission for {user1}:") |
|
||||||
score_data = { |
|
||||||
"user_id": user1, |
|
||||||
"device_id": device_id, |
|
||||||
"score": 1500, |
|
||||||
"game_completed": True |
|
||||||
} |
|
||||||
response = requests.post( |
|
||||||
f"{API_BASE_URL}/score/{device_id}/{user1}", |
|
||||||
json=score_data |
|
||||||
) |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
print(f"Response: {response.json()}") |
|
||||||
|
|
||||||
# 7. Test more score submissions |
|
||||||
scores_to_test = [ |
|
||||||
(user1, 2000, True), |
|
||||||
(user1, 1200, False), |
|
||||||
(user2, 1800, True), |
|
||||||
(user2, 2500, True) |
|
||||||
] |
|
||||||
|
|
||||||
print(f"\n7. Testing multiple score submissions:") |
|
||||||
for user, score, completed in scores_to_test: |
|
||||||
score_data = { |
|
||||||
"user_id": user, |
|
||||||
"device_id": device_id, |
|
||||||
"score": score, |
|
||||||
"game_completed": completed |
|
||||||
} |
|
||||||
response = requests.post( |
|
||||||
f"{API_BASE_URL}/score/{device_id}/{user}", |
|
||||||
json=score_data |
|
||||||
) |
|
||||||
print(f" {user} - Score: {score}, Completed: {completed} -> Status: {response.status_code}") |
|
||||||
time.sleep(0.1) # Small delay between requests |
|
||||||
|
|
||||||
# 8. Test score submission for non-registered user (should fail) |
|
||||||
print(f"\n8. Testing score submission for non-registered user (should fail):") |
|
||||||
score_data = { |
|
||||||
"user_id": "NonExistentUser", |
|
||||||
"device_id": device_id, |
|
||||||
"score": 1000, |
|
||||||
"game_completed": True |
|
||||||
} |
|
||||||
response = requests.post( |
|
||||||
f"{API_BASE_URL}/score/{device_id}/NonExistentUser", |
|
||||||
json=score_data |
|
||||||
) |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
print(f"Response: {response.json()}") |
|
||||||
|
|
||||||
# 9. Test getting user scores |
|
||||||
print(f"\n9. Testing get scores for {user1}:") |
|
||||||
response = requests.get(f"{API_BASE_URL}/scores/{device_id}/{user1}") |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
scores = response.json() |
|
||||||
print(f"Number of scores: {len(scores)}") |
|
||||||
for score in scores: |
|
||||||
print(f" Score: {score['score']}, Completed: {score['game_completed']}, Time: {score['timestamp']}") |
|
||||||
|
|
||||||
# 10. Test leaderboard |
|
||||||
print(f"\n10. Testing leaderboard for device {device_id}:") |
|
||||||
response = requests.get(f"{API_BASE_URL}/leaderboard/{device_id}") |
|
||||||
print(f"Status: {response.status_code}") |
|
||||||
leaderboard = response.json() |
|
||||||
print("Leaderboard:") |
|
||||||
for entry in leaderboard: |
|
||||||
print(f" Rank {entry['rank']}: {entry['user_id']} - Best Score: {entry['best_score']} ({entry['total_games']} games)") |
|
||||||
|
|
||||||
# 11. Test final user list |
|
||||||
print(f"\n11. Final user list for device {device_id}:") |
|
||||||
response = requests.get(f"{API_BASE_URL}/users/{device_id}") |
|
||||||
users = response.json() |
|
||||||
for user in users: |
|
||||||
print(f" {user['user_id']}: Best Score: {user['best_score']}, Total Games: {user['total_scores']}") |
|
||||||
|
|
||||||
print("\n" + "=" * 50) |
|
||||||
print("API Testing completed successfully!") |
|
||||||
|
|
||||||
except requests.exceptions.ConnectionError: |
|
||||||
print("ERROR: Could not connect to API server.") |
|
||||||
print("Make sure the API server is running with: python score_api.py") |
|
||||||
except Exception as e: |
|
||||||
print(f"ERROR: {e}") |
|
||||||
|
|
||||||
if __name__ == "__main__": |
|
||||||
test_api() |
|
||||||
Loading…
Reference in new issue