1 /*
2 * DefaultLoader.java
3 *
4 * Created on February 6, 2003, 8:40 AM
5 */
6 package com.mlw.fps.load;
7
8 import java.util.Date;
9 import java.util.HashMap;
10 import java.util.Iterator;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import com.mlw.fps.model.business.dao.DAOFactory;
16 import com.mlw.fps.model.business.dao.GameDAO;
17 import com.mlw.fps.model.business.dao.MapDAO;
18 import com.mlw.fps.model.business.dao.PlayerDAO;
19 import com.mlw.fps.model.business.dao.RoundDAO;
20 import com.mlw.fps.model.business.dao.WeaponDAO;
21 import com.mlw.fps.model.business.vo.Game;
22 import com.mlw.fps.model.business.vo.GameRecap;
23 import com.mlw.fps.model.business.vo.Kill;
24 import com.mlw.fps.model.business.vo.KillPlayer;
25 import com.mlw.fps.model.business.vo.KillRole;
26 import com.mlw.fps.model.business.vo.Map;
27 import com.mlw.fps.model.business.vo.Player;
28 import com.mlw.fps.model.business.vo.PlayerStatus;
29 import com.mlw.fps.model.business.vo.Round;
30 import com.mlw.fps.model.business.vo.Weapon;
31 import com.mlw.fps.model.business.vo.WeaponUsage;
32
33 /***
34 *
35 * @author Matthew Wilson
36 */
37 public abstract class DefaultLoader implements Loader
38 {
39 private static final KillRole ROLE_KILLER= new KillRole("killer", "killer", new Double(1), new Double(0));
40 private static final KillRole ROLE_VICTIM= new KillRole("victim", "victim", new Double(0), new Double(1));
41
42 protected final static Log log= LogFactory.getLog(DefaultLoader.class);
43
44 private GameDAO gameDAO= (GameDAO)DAOFactory.getDAO(GameDAO.class.getName());
45 private RoundDAO roundDAO= (RoundDAO)DAOFactory.getDAO(RoundDAO.class.getName());
46 private MapDAO mapDAO= (MapDAO)DAOFactory.getDAO(MapDAO.class.getName());
47 private PlayerDAO playerDAO= (PlayerDAO)DAOFactory.getDAO(PlayerDAO.class.getName());
48 private WeaponDAO weaponDAO= (WeaponDAO)DAOFactory.getDAO(WeaponDAO.class.getName());
49
50 private java.util.Map players= new HashMap();
51
52 protected Map map;
53 protected Kill kill;
54 protected Game game;
55 protected Round round;
56
57 /*** @see Loader
58 */
59 public void startMap(Date date, String name)
60 {
61 if (null == (map= mapDAO.getMapByName(name)))
62 {
63 map= new Map(name);
64 mapDAO.insert(map);
65 }
66 }
67
68 /*** @see Loader
69 */
70 public void startGame(Date date)
71 {
72 game= new Game(map, date);
73 gameDAO.insert(game);
74 }
75
76 public void addWeaponUsage(String player, String weapon, WeaponUsage usage)
77 {
78 Player thePlayer= getPlayer(player);
79
80 Weapon theWeapon= weaponDAO.getWeaponByName(weapon);
81 if (theWeapon == null)
82 {
83 weaponDAO.insert(theWeapon= new Weapon(weapon));
84 }
85 usage.setWeapon(theWeapon);
86
87 GameRecap recap= (GameRecap)game.getRecaps().get(thePlayer.getPk());
88 if (recap == null)
89 {
90 game.getRecaps().put(thePlayer.getPk(), (recap= new GameRecap()));
91 }
92
93 recap.getWeaponUsage().put(theWeapon.getPk(), usage);
94 }
95
96 /*** @see Loader
97 */
98 public void endGame(Date date)
99 {
100 if (game.getRounds().isEmpty() || game.getKills().size() > 0)
101 {
102 gameDAO.update(game);
103 }
104 else
105 {
106 gameDAO.delete(game);
107 }
108
109 players.clear();
110 }
111
112 /*** @see Loader
113 */
114 public void startRound(Date date)
115 {
116 //bug in half-life debug logs!
117 if (map == null)
118 return;
119 game.getRounds().add(round= new Round(date));
120 }
121
122 /*** @see Loader
123 */
124 public void endRound(String winningTeam, Date date)
125 {
126 round.setEndDate(date);
127 round.setWinningTeam(winningTeam);
128
129 for( Iterator iter = players.values().iterator(); iter.hasNext();)
130 {
131 PlayerStatus status = ((PlayerStatus)iter.next()).reset(round.getLength());
132 round.getStatusOfPlayers().put(status.getPlayer().getName(), status);
133 }
134 roundDAO.insert(round);
135 }
136
137 /*** @see Loader
138 */
139 public void addKill(Date date, String killer, String weapon, String victim)
140 {
141 Player theKiller= getPlayer(killer);
142 Player theVictim= getPlayer(victim);
143
144 Weapon theWeapon= weaponDAO.getWeaponByName(weapon);
145 if (theWeapon == null)
146 {
147 weaponDAO.insert(theWeapon= new Weapon(weapon));
148 }
149
150 PlayerStatus status = (PlayerStatus)players.get(theVictim.getName());
151
152 long secondsAlive = (date.getTime() - round.getDate().getTime())/1000;
153 if( secondsAlive<0 )
154 {
155 throw new RuntimeException("a negative date: " + date);
156 }
157
158 if(status!=null)status.setStatus(PlayerStatus.DEAD, secondsAlive);
159 round.getKills().add(kill= new Kill(date, new KillPlayer(theKiller, ROLE_KILLER), theWeapon, new KillPlayer(theVictim, ROLE_VICTIM)));
160 }
161
162 /* (non-Javadoc)
163 * @see com.mlw.fps.load.Loader#setPlayerStatus(java.lang.String, java.lang.String, java.lang.String)
164 */
165 public void setPlayerStatus(String name, String team, String role)
166 {
167 Player player = getPlayer(name);
168 players.put(name, new PlayerStatus(team, player));
169 }
170
171 /*** helper methods **/
172
173 /***
174 *
175 * @param name
176 * @return
177 */
178 private Player getPlayer(String name)
179 {
180 Player player= playerDAO.getPlayerByName(name);
181 if (player == null)
182 {
183 playerDAO.insert(player= new Player(name));
184 }
185 return player;
186 }
187 }
This page was automatically generated by Maven