diff --git a/evolverstage.py b/evolverstage.py index df2cda6..d8162bd 100644 --- a/evolverstage.py +++ b/evolverstage.py @@ -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): diff --git a/tests/test_battle_logic.py b/tests/test_battle_logic.py index f6aca01..59b319b 100644 --- a/tests/test_battle_logic.py +++ b/tests/test_battle_logic.py @@ -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."""