Fix errors
This commit is contained in:
@@ -205,23 +205,61 @@ class GameConsumer(AsyncWebsocketConsumer):
|
|||||||
current_question = quiz_game.quiz_id.questions.all()[quiz_game.current_question_index]
|
current_question = quiz_game.quiz_id.questions.all()[quiz_game.current_question_index]
|
||||||
question_data = json.loads(current_question.data)
|
question_data = json.loads(current_question.data)
|
||||||
target_location = question_data['target_location']
|
target_location = question_data['target_location']
|
||||||
tolerance_radius = question_data['tolerance_radius']
|
tolerance_radius = question_data.get('tolerance_radius', 1000) # Default 1km if not set
|
||||||
print(f"target_location: {target_location}")
|
|
||||||
print(f"answer_location: {answer_location}")
|
print(f"Raw target_location: {target_location}")
|
||||||
|
print(f"Raw answer_location: {answer_location}")
|
||||||
print(f"tolerance_radius: {tolerance_radius}")
|
print(f"tolerance_radius: {tolerance_radius}")
|
||||||
|
|
||||||
|
# Parse coordinates if they are strings
|
||||||
|
def parse_coords(coord):
|
||||||
|
if isinstance(coord, str):
|
||||||
|
try:
|
||||||
|
# Handle both comma and space separated coordinates
|
||||||
|
if ',' in coord:
|
||||||
|
lat, lng = map(float, [x.strip() for x in coord.split(',')])
|
||||||
|
else:
|
||||||
|
# Try splitting by space if no comma
|
||||||
|
parts = coord.strip().split()
|
||||||
|
if len(parts) >= 2:
|
||||||
|
lat, lng = map(float, parts[:2])
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid coordinate format")
|
||||||
|
return (lat, lng)
|
||||||
|
except (ValueError, AttributeError) as e:
|
||||||
|
print(f"Error parsing coordinates {coord}: {e}")
|
||||||
|
return None
|
||||||
|
elif isinstance(coord, (list, tuple)) and len(coord) >= 2:
|
||||||
|
return (float(coord[0]), float(coord[1]))
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Parse the coordinates
|
||||||
|
target_location = parse_coords(target_location)
|
||||||
|
answer_location = parse_coords(answer_location)
|
||||||
|
|
||||||
|
if not target_location or not answer_location:
|
||||||
|
print("Invalid coordinates, cannot calculate score")
|
||||||
|
is_correct = False
|
||||||
|
score = 0
|
||||||
|
else:
|
||||||
|
print(f"Parsed target_location: {target_location}")
|
||||||
|
print(f"Parsed answer_location: {answer_location}")
|
||||||
|
|
||||||
# Configuration - Make these easily adjustable
|
# Configuration - Make these easily adjustable
|
||||||
MAX_DISTANCE_KM = 1000 # Maximum distance for scoring (in km)
|
MAX_DISTANCE_KM = 1000 # Maximum distance for scoring (in km)
|
||||||
DIRECT_HIT_BONUS = 200 # Extra points for direct hit within tolerance
|
DIRECT_HIT_BONUS = 200 # Extra points for direct hit within tolerance
|
||||||
|
|
||||||
# Calculate base score
|
try:
|
||||||
|
# Calculate distance
|
||||||
distance_km = geodesic(answer_location, target_location).kilometers
|
distance_km = geodesic(answer_location, target_location).kilometers
|
||||||
tolerance_km = int(tolerance_radius) / 1000 # Convert meters to kilometers
|
tolerance_km = float(tolerance_radius) / 1000 # Convert meters to kilometers
|
||||||
|
|
||||||
|
print(f"Distance: {distance_km} km, Tolerance: {tolerance_km} km")
|
||||||
|
|
||||||
# Calculate base score with non-linear decrease
|
# Calculate base score with non-linear decrease
|
||||||
if distance_km <= tolerance_km:
|
if distance_km <= tolerance_km:
|
||||||
# Direct hit - full points + bonus
|
# Direct hit - full points + bonus
|
||||||
base_score = 1000 + DIRECT_HIT_BONUS # 1000 + 200 bonus
|
base_score = 1000 + DIRECT_HIT_BONUS
|
||||||
is_correct = True
|
is_correct = True
|
||||||
else:
|
else:
|
||||||
if distance_km > MAX_DISTANCE_KM:
|
if distance_km > MAX_DISTANCE_KM:
|
||||||
@@ -230,13 +268,10 @@ class GameConsumer(AsyncWebsocketConsumer):
|
|||||||
is_correct = False
|
is_correct = False
|
||||||
else:
|
else:
|
||||||
# Non-linear decrease using exponential falloff
|
# Non-linear decrease using exponential falloff
|
||||||
# This creates a steeper drop-off at the beginning
|
|
||||||
progress = (distance_km - tolerance_km) / (MAX_DISTANCE_KM - tolerance_km)
|
progress = (distance_km - tolerance_km) / (MAX_DISTANCE_KM - tolerance_km)
|
||||||
# Using a power function to create non-linear falloff
|
|
||||||
falloff = progress ** 1.5 # 1.5 makes the falloff steeper at the beginning
|
falloff = progress ** 1.5 # 1.5 makes the falloff steeper at the beginning
|
||||||
base_score = 1000 * (1 - falloff)
|
base_score = 1000 * (1 - falloff)
|
||||||
is_correct = True
|
is_correct = True
|
||||||
base_score = max(0, min(1000, int(base_score)))
|
|
||||||
|
|
||||||
# Apply time penalty (reduces score based on time taken)
|
# Apply time penalty (reduces score based on time taken)
|
||||||
if is_correct and base_score > 0:
|
if is_correct and base_score > 0:
|
||||||
@@ -251,8 +286,16 @@ class GameConsumer(AsyncWebsocketConsumer):
|
|||||||
else:
|
else:
|
||||||
final_score = base_score # 0 points for incorrect answers
|
final_score = base_score # 0 points for incorrect answers
|
||||||
|
|
||||||
|
# Apply max score limit after all calculations (including bonus)
|
||||||
|
final_score = min(final_score, 1200) # Cap at 1200 (1000 + 200 bonus)
|
||||||
|
|
||||||
score = int(round(final_score))
|
score = int(round(final_score))
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error calculating score: {e}")
|
||||||
|
is_correct = False
|
||||||
|
score = 0
|
||||||
|
|
||||||
# Save the answer
|
# Save the answer
|
||||||
answer_obj, created = QuizAnswer.objects.get_or_create(
|
answer_obj, created = QuizAnswer.objects.get_or_create(
|
||||||
participant=participant,
|
participant=participant,
|
||||||
|
|||||||
Reference in New Issue
Block a user