Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions evolverstage.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,21 +810,25 @@ def parse_nmars_output(raw_output):
return [], []
scores = []
warriors = []
#note nMars will sort by score regardless of the order in the command-line, so match up score with warrior
# Note nMars will sort by score regardless of the order in the command-line,
# so we must match up the score with the warrior ID on each line.
output = raw_output.splitlines()
numline=0
for line in output:
numline=numline+1
if "scores" in line:
if VERBOSE:
print(line.strip())
splittedline=line.split()
# Ensure line has enough parts to avoid IndexError
if len(splittedline) > 4:
scores.append(int(splittedline[4]))
warriors.append(int(splittedline[0]))
if VERBOSE:
print(numline)
parts = line.split()
try:
# The score is the value immediately following the 'scores' keyword.
# The warrior ID is assumed to be the first element on the line.
s_idx = parts.index("scores")
if s_idx > 0 and s_idx + 1 < len(parts):
warrior_id = int(parts[0])
score_val = int(parts[s_idx + 1])
warriors.append(warrior_id)
scores.append(score_val)
except (ValueError, IndexError):
continue
return scores, warriors

def determine_winner(scores, warriors):
Expand Down
31 changes: 23 additions & 8 deletions tests/test_battle_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@ def test_parse_nmars_output_none(self):
self.assertEqual(scores, [])
self.assertEqual(warriors, [])

def test_parse_nmars_output_malformed(self):
"""Test parsing malformed lines."""
# Line with "scores" but too short
def test_parse_nmars_output_minimal(self):
"""Test parsing minimal output lines."""
# Minimal format is now supported
raw_output = (
"1 scores 100\n" # split len 3
"2 Warrior2 Author scores 50" # valid
"1 scores 100\n"
"2 Warrior2 Author scores 50"
)
scores, warriors = evolverstage.parse_nmars_output(raw_output)

# Should only capture the valid line
self.assertEqual(scores, [50])
self.assertEqual(warriors, [2])
self.assertEqual(scores, [100, 50])
self.assertEqual(warriors, [1, 2])

def test_parse_nmars_output_with_spaces(self):
"""Test parsing lines with spaces in name/author."""
raw_output = "1 My Warrior Author Name scores 100"
scores, warriors = evolverstage.parse_nmars_output(raw_output)

self.assertEqual(scores, [100])
self.assertEqual(warriors, [1])

def test_parse_nmars_output_actually_malformed(self):
"""Test parsing actually malformed lines."""
# No score following "scores", or not a number
raw_output = "1 scores\n2 scores not_a_number"
scores, warriors = evolverstage.parse_nmars_output(raw_output)
self.assertEqual(scores, [])
self.assertEqual(warriors, [])

def test_parse_nmars_output_no_scores_keyword(self):
"""Test lines without 'scores' keyword are ignored."""
Expand Down