super.defaultMethod(am, af, req, res);
this.session = HibernateSessions.getSessionFactory().openSession();
try {
ViewScoresForm form=(ViewScoresForm) af;
form.setMyUserId(webUser.getId());
form.reset();
long id=Long.parseLong(req.getParameter("id"));
GameHiber g=GameDao.findGameHiber(id,session);
Game g2=GameDao.findGame(id,session);
form.setGame(g2);
ArrayList<GameScoreHiber> gs=null;
ArrayList<GameScore> gs2=null;
if (req.getParameter("my")!=null) gs=GameScoreDao.getMyScoresForGame(g,req.getParameter("sort"),webUser.getId(),session);
else {
if (!SecurityCenter.canIViewScoresGame(webUser.getId(), form.getGame().getId(),session)) {
MessageBean.setMyMessage(req, ("Error: Insufficient privileges."));
form.reset();
return am.findForward("error");
}
gs=GameScoreDao.getAllScoresForGame(g,req.getParameter("sort"),session);
}
gs2=new ArrayList<GameScore>();
for (GameScoreHiber gameScore : gs) {
GameScore toAdd=GameScore.asGameScoreObject(gameScore);
//construct player name
try {
User usr=UserDao.findUser(gameScore.getPlayer(),session);
String name=usr.getLastName()+" "+usr.getFirstName();
if ((usr.getLastName()==null) && (usr.getFirstName()==null)) name=usr.getMsisdn()+"";
toAdd.setPlayerString(name);
} catch (Exception eee){
//leave as is
}
gs2.add(toAdd);
}
form.setGameScores(gs2);
boolean first=true;
int avg=0;
int maxofthesame=0;
for (GameScoreHiber score : gs) {
int sc=score.getCompleteScore();
if (first) {
first=false;
form.setMin(sc);
form.setMax(sc);
}
if (form.getMax()<sc) form.setMax(sc);
if (form.getMin()>sc) form.setMin(sc);
avg+=score.getCompleteScore();
Integer i=form.getGameScoresCount().get(new Integer(sc));
if (i==null) i=new Integer(0);
i=new Integer(i.intValue()+1);
if (maxofthesame<i.intValue()) maxofthesame=i.intValue();
form.getGameScoresCount().put(new Integer(sc), i);
}
if (gs.size()>0) {
double c=((double)avg)/gs.size();
long cc=Math.round(c*100);
form.setAverage((double)cc/100);
}
form.setMaxOfTheSame(maxofthesame);
return am.findForward("ok");
} finally {
this.session.close();
}
}