/*
	cursus - Race series management program
	Copyright 2011  Simon Arlott

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.lp0.cursus.scoring;

import eu.lp0.cursus.db.data.Penalty;
import eu.lp0.cursus.db.data.Pilot;
import eu.lp0.cursus.db.data.Race;

public class GenericRacePenaltiesData<T extends ScoredData> extends AbstractRacePenaltiesData<T> {
	public GenericRacePenaltiesData(T scores) {
		super(scores);
	}

	@Override
	public int getRacePenalties(Pilot pilot, Race race) {
		// Count previous automatic penalties
		int autoPenalties = 0;
		for (Race previousRace : race.getEvent().getRaces()) {
			if (previousRace == race) {
				break;
			} else {
				for (Penalty penalty : race.getAttendees().get(pilot).getPenalties()) {
					if (penalty.getType() == Penalty.Type.AUTOMATIC) {
						autoPenalties += penalty.getValue();
						// Don't allow negative automatic penalties, that just gets silly
						if (autoPenalties < 0) {
							autoPenalties = 0;
						}
					}
				}
			}
		}

		// Calculate race penalties
		int penalties = 0;
		for (Penalty penalty : race.getAttendees().get(pilot).getPenalties()) {
			switch (penalty.getType()) {
			case AUTOMATIC:
				if (penalty.getValue() >= 0) {
					// Add multiple automatic penalties
					for (int i = 0; i < penalty.getValue(); i++) {
						penalties += ++autoPenalties;
					}
				} else {
					// Remove multiple automatic penalties
					for (int i = penalty.getValue(); i > 0; i--) {
						if (autoPenalties > 0) {
							penalties -= autoPenalties--;
						}
					}
				}
				break;

			case FIXED:
				// Fixed penalty added/removed
				penalties += penalty.getValue();
				break;
			}
		}

		return penalties;
	}
}
@@ -834,16 +834,59 @@
 ta.Race;
+%0Aimport eu.lp0.cursus.db.data.RaceAttendee;
 %0A%0Apublic
@@ -1110,16 +1110,118 @@
 race) %7B%0A
+%09%09RaceAttendee attendee = race.getAttendees().get(pilot);%0A%09%09if (attendee == null) %7B%0A%09%09%09return 0;%0A%09%09%7D%0A%0A
 %09%09// Cou
@@ -1396,61 +1396,160 @@
 %09%09%09%09
-for (Penalty penalty : race.getAttendees().get(pilot)
+RaceAttendee previousAttendee = previousRace.getAttendees().get(pilot);%0A%09%09%09%09if (previousAttendee != null) %7B%0A%09%09%09%09%09for (Penalty penalty : previousAttendee
 .get
@@ -1559,24 +1559,25 @@
 alties()) %7B%0A
+%09
 %09%09%09%09%09if (pen
@@ -1616,24 +1616,25 @@
 UTOMATIC) %7B%0A
+%09
 %09%09%09%09%09%09autoPe
@@ -1670,16 +1670,17 @@
 ;%0A%09%09%09%09%09%09
+%09
 // Don't
@@ -1739,24 +1739,25 @@
 silly%0A%09%09%09%09%09%09
+%09
 if (autoPena
@@ -1768,32 +1768,33 @@
 s %3C 0) %7B%0A%09%09%09%09%09%09%09
+%09
 autoPenalties = 
@@ -1796,16 +1796,25 @@
 es = 0;%0A
+%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09%7D%0A
@@ -1916,38 +1916,16 @@
 y : 
-race.getAttendees().get(pilot)
+attendee
 .get

handle race penalty calculation if the pilot didn't attend the race correctly calculate previous automatic race penalties