package domain;

import hibernate.HibernateUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = Player.TABLE)
@PrimaryKeyJoinColumn(name="username")
public class Player extends User {
	public static final String TABLE = "PLAYER";
	
	@Column(unique = true)
	private String mail;
	
	public Player (String user, String name, String sName, String pass, String mail) {
		super(user, name, sName, pass);
		this.mail = mail;
		games = new ArrayList<Game>(2);
		Collections.addAll(games, null, null);
		
		HibernateUtil.update(this);
	}
	
	public Player () {}
		
	@OneToMany(mappedBy = "player", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	private List<Game> games;
	
	public Game getOldGame () {
		return games.get(0);
	}
	
	public void setOldGame (Game g) {
		games.add(0, g);
	}
	
	public Game getCurrentGame () {
		return games.get(1);
	}
	
	public void setCurrentGame (Game g) {
		if (games.size() == 0) {
			Collections.addAll(games, null, g);
		} else {
			Game aged = games.get(1);
		    this.setOldGame(aged);
			games.add(g);
		}
	}
}

@@ -1258,16 +1258,70 @@
 ll, g);%0A
+%09%09%7D %0A%09%09else if (games.size() == 1) %7B%0A%09%09%09games.add(g);%0A
 %09%09%7D else
@@ -1390,24 +1390,27 @@
 %09%09games.add(
+1, 
 g);%0A%09%09%7D%0A%09%7D%0A%7D

Fix bug setting currentGame to domain.Player