// ============================================================================
// File:               $File$
//
// Project:            
//
// Purpose:            
//
// Author:             Rammi
//
// Copyright Notice:   (c) 2008  Rammi (rammi@caff.de)
//                     This code is in the public domain.
//                     Use at own risk.
//                     No guarantees given.
//
// Latest change:      $Date$
//
// History:	       $Log$
//=============================================================================
package de.caff.asteroid;

import java.util.LinkedList;

/**
 *  Frame preparer which works against score overflow.
 *
 *  This class is part of a solution for a
 *  <a href="http://www.heise.de/ct/creativ/08/02/details/">competition by the German computer magazine c't</a>
 */
public class ScoreFixer
        implements FramePreparer,
                   GameData
{

  /**
   * Prepare the frame(s).
   *
   * @param frameInfos the collected frame infos
   */
  public void prepareFrames(LinkedList<FrameInfo> frameInfos)
  {
    if (frameInfos.size() >= 2) {
      FrameInfo lastFrame = frameInfos.getLast();
      int lastScore = lastFrame.getScore();
      FrameInfo prevFrame = frameInfos.get(frameInfos.size() - 2);
      int prevScore = prevFrame.getScore();
      if (prevFrame.isGameRunning()) {
        if (prevScore > lastScore) {
          if (lastScore == -1) {
            lastFrame.setScore(prevScore);
          }
          else {
            int fixScore = lastScore + (prevScore / GAME_SCORE_WRAP) * GAME_SCORE_WRAP;
            if (fixScore < prevScore) {
              fixScore += GAME_SCORE_WRAP;
            }
            lastFrame.setScore(fixScore);
          }
        }
      }
    }
  }
}
