Changeset 8f9e52279a0268453622470ae513c0f6e074289f

Show
Ignore:
Timestamp:
10/21/07 11:07:14 (1 year ago)
Author:
Christopher Jung <bktheg@web.de>
git-committer:
Christopher Jung <bktheg@web.de> 1192957634 +0200
git-parent:

[66c964ad09c488671af661381a7b63e1a068d093]

git-author:
Christopher Jung <bktheg@web.de> 1192957634 +0200
Message:

Battle auf Hibernate umgestellt

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/net/driftingsouls/ds2/server/AdminCommands.java

    rf034a00 r8f9e522  
    319319                if( cmd.equals("end") ) { 
    320320                        int battleid = Integer.parseInt( command[2] );   
    321                         Database db = context.getDatabase(); 
    322                          
    323                         SQLResultRow battledata = db.first("SELECT commander1,commander2,x,y,system FROM battles WHERE id="+battleid); 
    324                  
    325                         if( battledata.isEmpty() ) { 
     321                        org.hibernate.Session db = context.getDB(); 
     322                         
     323                        Battle battle = (Battle)db.get(Battle.class, battleid); 
     324                 
     325                        if( battle == null ) { 
    326326                                return "Die angegebene Schlacht existiert nicht\n"; 
    327327                        } 
     
    329329                        User sourceUser = (User)context.getDB().get(User.class, -1); 
    330330                         
    331                         PM.send(sourceUser, battledata.getInt("commander1"), "Schlacht beendet", "Die Schlacht bei "+Location.fromResult(battledata)+" wurde durch die Administratoren beendet"); 
    332                         PM.send(sourceUser, battledata.getInt("commander2"), "Schlacht beendet", "Die Schlacht bei "+Location.fromResult(battledata)+" wurde durch die Administratoren beendet"); 
    333                  
    334                         int comid = battledata.getInt("commander1"); 
    335  
    336                         Battle battle = new Battle(); 
    337                         battle.load(battleid, comid, 0, 0, 0); 
     331                        PM.send(sourceUser, battle.getCommander(0).getID(), "Schlacht beendet", "Die Schlacht bei "+battle.getLocation()+" wurde durch die Administratoren beendet"); 
     332                        PM.send(sourceUser, battle.getCommander(1).getID(), "Schlacht beendet", "Die Schlacht bei "+battle.getLocation()+" wurde durch die Administratoren beendet"); 
     333                 
     334                        battle.load(battle.getCommander(0), null, null, 0); 
    338335                        battle.endBattle(0, 0, false); 
    339336                } 
  • src/net/driftingsouls/ds2/server/battles/Battle.java

    r144f38e r8f9e522  
    2323import java.io.FileWriter; 
    2424import java.io.IOException; 
     25import java.io.Serializable; 
    2526import java.util.ArrayList; 
    2627import java.util.HashMap; 
     
    3031import java.util.Map; 
    3132import java.util.Set; 
    32 import java.util.Map.Entry; 
     33 
     34import javax.persistence.Entity; 
     35import javax.persistence.FetchType; 
     36import javax.persistence.GeneratedValue; 
     37import javax.persistence.Id; 
     38import javax.persistence.JoinColumn; 
     39import javax.persistence.ManyToOne; 
     40import javax.persistence.Table; 
     41import javax.persistence.Transient; 
    3342 
    3443import net.driftingsouls.ds2.server.ContextCommon; 
     
    4756import net.driftingsouls.ds2.server.framework.ContextMap; 
    4857import net.driftingsouls.ds2.server.framework.Loggable; 
    49 import net.driftingsouls.ds2.server.framework.db.Database; 
    50 import net.driftingsouls.ds2.server.framework.db.PreparedQuery; 
    51 import net.driftingsouls.ds2.server.framework.db.SQLResultRow; 
    5258import net.driftingsouls.ds2.server.scripting.Quests; 
    5359import net.driftingsouls.ds2.server.scripting.ScriptParser; 
     
    6268import org.apache.commons.lang.SystemUtils; 
    6369import org.apache.commons.lang.math.RandomUtils; 
     70import org.hibernate.CallbackException; 
     71import org.hibernate.Session; 
     72import org.hibernate.classic.Lifecycle; 
    6473 
    6574/** 
     
    6877 * 
    6978 */ 
    70 public class Battle implements Loggable, Locatable { 
     79@Entity 
     80@Table(name="battles") 
     81public class Battle implements Lifecycle,Loggable, Locatable { 
    7182        private static final int LOGFORMAT = 2; 
    7283         
     
    137148        public static final int FLAG_BLOCK_SECONDROW_1 = 16; 
    138149         
    139         private int id = 0; 
    140         private int x = 0; 
    141         private int y = 0; 
    142         private int system = 0; 
    143         private int flags = 0; 
     150        @Id @GeneratedValue 
     151        private int id; 
     152        private int x; 
     153        private int y; 
     154        private int system; 
     155        private int ally1; 
     156        private int ally2; 
     157        @ManyToOne(fetch=FetchType.LAZY) 
     158        @JoinColumn(name="commander1", nullable=false) 
     159        private User commander1; 
     160        @ManyToOne(fetch=FetchType.LAZY) 
     161        @JoinColumn(name="commander2", nullable=false) 
     162        private User commander2; 
     163        private boolean ready1; 
     164        private boolean ready2; 
     165        private String com1Msg = ""; 
     166        private String com2Msg = ""; 
     167        @SuppressWarnings("unused") 
     168        private int com1Points; 
     169        @SuppressWarnings("unused") 
     170        private int com2Points; 
     171        private boolean com1BETAK = true; 
     172        private boolean com2BETAK = true; 
     173        private int takeCommand1; 
     174        private int takeCommand2; 
     175        private int blockcount = 2; 
     176        @SuppressWarnings("unused") 
     177        private long lastaction; 
     178        private long lastturn; 
     179        private int flags; 
     180        private int inakt; 
     181        private String onend; 
     182        private String visibility; 
     183        private Integer quest; 
     184         
     185        @Transient 
    144186        private String ownShipGroup = "0"; 
     187        @Transient 
    145188        private String enemyShipGroup = "0"; 
    146189         
    147         private int ownSide = 0; 
    148         private int enemySide = 0; 
    149          
     190        @Transient 
     191        private int ownSide; 
     192        @Transient 
     193        private int enemySide; 
     194         
     195        @Transient 
    150196        private List<BattleShip> ownShips = new ArrayList<BattleShip>(); 
     197        @Transient 
    151198        private List<BattleShip> enemyShips = new ArrayList<BattleShip>(); 
    152  
     199         
     200        @Transient 
    153201        private int[] ally = new int[2]; 
    154         private int[] commander = new int[2]; 
     202        @Transient 
     203        private User[] commander = new User[2]; 
     204        @Transient 
    155205        private boolean[] ready = new boolean[2]; 
     206        @Transient 
    156207        private String[] comMsg = new String[2]; 
     208        @Transient 
    157209        private boolean[] betak = new boolean[2]; 
     210        @Transient 
    158211        private int[] takeCommand = new int[2]; 
     212        @Transient 
    159213        private List<List<Integer>> addCommanders = new ArrayList<List<Integer>>(); 
    160          
    161         private int blockcount = 0; 
    162         private long lastturn = 0; 
    163         private String visibility = ""; 
    164         private int quest = 0; 
     214 
     215        @Transient 
    165216        private boolean guest = false; 
    166217         
    167         private SQLResultRow tableBuffer = null; 
    168          
     218        @Transient 
    169219        private Map<Integer,Integer> ownShipTypeCount = new HashMap<Integer,Integer>(); 
     220        @Transient 
    170221        private Map<Integer,Integer> enemyShipTypeCount = new HashMap<Integer,Integer>(); 
    171222         
     223        @Transient 
    172224        private int activeSOwn = 0; 
     225        @Transient 
    173226        private int activeSEnemy = 0; 
    174227         
     228        @Transient 
    175229        private StringBuilder logoutputbuffer = new StringBuilder(); 
     230        @Transient 
    176231        private StringBuilder logenemybuffer = new StringBuilder(); 
    177232         
     
    186241 
    187242                return "[tooltip="+shiptype.getNickname()+"]"+ship.getName()+"[/tooltip] ("+ship.getId()+")"; 
     243        } 
     244         
     245        /** 
     246         * Konstruktor 
     247         * 
     248         */ 
     249        public Battle() { 
     250                // EMPTY 
     251        } 
     252         
     253        public boolean onDelete(Session s) throws CallbackException { 
     254                return false; 
     255        } 
     256 
     257        public void onLoad(Session s, Serializable id) { 
     258                this.ally = new int[] {this.ally1, this.ally2}; 
     259                this.commander = new User[] {this.commander1, this.commander2}; 
     260                this.ready = new boolean[] {this.ready1, this.ready2}; 
     261                this.comMsg = new String[] {this.com1Msg, this.com2Msg}; 
     262                this.betak = new boolean[] {this.com1BETAK, this.com2BETAK}; 
     263                this.takeCommand = new int[] {this.takeCommand1, this.takeCommand2}; 
     264                 
     265                this.addCommanders.add(0, new ArrayList<Integer>()); 
     266                this.addCommanders.add(1, new ArrayList<Integer>()); 
     267        } 
     268 
     269        public boolean onSave(Session s) throws CallbackException { 
     270                this.ally1 = this.ally[0]; 
     271                this.ally2 = this.ally[1]; 
     272                this.commander1 = this.commander[0]; 
     273                this.commander2 = this.commander[1]; 
     274                this.ready1 = this.ready[0]; 
     275                this.ready2 = this.ready[1]; 
     276                this.com1Msg = this.comMsg[0]; 
     277                this.com2Msg = this.comMsg[1]; 
     278                this.com1BETAK = this.betak[0]; 
     279                this.com2BETAK = this.betak[1]; 
     280                this.takeCommand1 = this.takeCommand[0]; 
     281                this.takeCommand2 = this.takeCommand[1]; 
     282                 
     283                return false; 
     284        } 
     285 
     286        public boolean onUpdate(Session s) throws CallbackException { 
     287                this.ally1 = this.ally[0]; 
     288                this.ally2 = this.ally[1]; 
     289                this.commander1 = this.commander[0]; 
     290                this.commander2 = this.commander[1]; 
     291                this.ready1 = this.ready[0]; 
     292                this.ready2 = this.ready[1]; 
     293                this.com1Msg = this.comMsg[0]; 
     294                this.com2Msg = this.comMsg[1]; 
     295                this.com1BETAK = this.betak[0]; 
     296                this.com2BETAK = this.betak[1]; 
     297                this.takeCommand1 = this.takeCommand[0]; 
     298                this.takeCommand2 = this.takeCommand[1]; 
     299                 
     300                return false; 
    188301        } 
    189302         
     
    331444         */ 
    332445        public void addComMessage( int side, String text ) { 
    333                 Database db = ContextMap.getContext().getDatabase(); 
    334                  
    335                 db.prepare("UPDATE battles SET com"+(side+1)+"Msg=CONCAT(com"+(side+1)+"Msg, ?) WHERE id= ?") 
    336                         .update(text, this.id); 
     446                this.comMsg[side] += text; 
    337447        } 
    338448         
     
    345455                        side = this.ownSide; 
    346456                } 
    347                 Database db = ContextMap.getContext().getDatabase(); 
    348                  
    349                 db.update("UPDATE battles SET com",(side+1),"Msg='' WHERE id=",this.id); 
     457 
    350458                comMsg[side] = ""; 
    351459        } 
     
    584692         * @param ignoreinakt Soll das Inaktivitaetsfeld nicht zurueckgesetzt werden (<code>true</code>)? 
    585693         */ 
    586         public void save( boolean ignoreinakt  ) { 
    587                 Context context = ContextMap.getContext(); 
    588                 Database db = context.getDatabase(); 
    589                  
    590                 List<String> update = new ArrayList<String>(); 
    591                 List<Object> updateData = new ArrayList<Object>(); 
    592                 List<String> where = new ArrayList<String>(); 
    593                 List<Object> whereData = new ArrayList<Object>(); 
    594                 SQLResultRow data = new SQLResultRow(); 
    595  
    596                 data.put("ally1", ally[0]); 
    597                 data.put("ally2", this.ally[1]); 
    598                 data.put("commander1", this.commander[0]); 
    599                 data.put("commander2", this.commander[1]); 
    600                 data.put("ready1", this.ready[0]); 
    601                 data.put("ready2", this.ready[1]); 
    602                 data.put("com1BETAK", this.betak[0]); 
    603                 data.put("com2BETAK", this.betak[1]); 
    604                 data.put("takeCommand1", this.takeCommand[0]); 
    605                 data.put("takeCommand2", this.takeCommand[1]); 
    606                 data.put("blockcount", this.blockcount); 
    607                 data.put("lastturn", this.lastturn); 
    608                 data.put("flags", this.flags); 
    609                  
    610                 for( Entry<String,Object> entry : data.entrySet() ) { 
    611                         Object element = entry.getValue(); 
    612                         String key = entry.getKey(); 
    613                         if( ((element != null) && !element.equals(this.tableBuffer.get(key))) || (this.tableBuffer.get(key) != null) ) { 
    614                                 update.add(key+"= ? "); 
    615                                 updateData.add(element); 
    616                                 where.add(key+"= ? "); 
    617                                 whereData.add(this.tableBuffer.get(key)); 
    618                         } 
    619                 } 
    620                                  
    621                 if( this.tableBuffer.getInt("quest") != this.quest ) { 
    622                         if( this.quest != 0 ) { 
    623                                 update.add("quest= ? "); 
    624                                 updateData.add(this.quest); 
    625                         } 
    626                         else { 
    627                                 update.add("quest=NULL"); 
    628                         } 
    629                         if( this.tableBuffer.getInt("quest") != 0 ) { 
    630                                 where.add("quest= ? "); 
    631                                 whereData.add(this.tableBuffer.getInt("quest")); 
    632                         } 
    633                         else { 
    634                                 where.add("quest=NULL"); 
    635                         } 
    636                 } 
    637                  
    638                 if( (this.visibility != null && !this.visibility.equals(this.tableBuffer.get("visibility"))) || 
    639                         (this.tableBuffer.get("visibility") != null) ) { 
    640                         if( this.visibility != null ) { 
    641                                 update.add("visibility= ? "); 
    642                                 updateData.add(this.visibility); 
    643                         } 
    644                         else { 
    645                                 update.add("visibility=NULL"); 
    646                         } 
    647                         if( this.tableBuffer.get("vsibility") != null ) { 
    648                                 where.add("visibility= ? "); 
    649                                 whereData.add(this.tableBuffer.get("visibility")); 
    650                         } 
    651                         else { 
    652                                 where.add("visibility=NULL"); 
    653                         } 
    654                 } 
    655                  
     694        @Deprecated 
     695        public void save( boolean ignoreinakt  ) {       
    656696                if( !ignoreinakt ) { 
    657                         update.add("inakt= ? "); 
    658                         updateData.add(0); 
    659                 } 
    660                 update.add("lastaction= ? "); 
    661                 updateData.add(Common.time()); 
    662                  
    663                 where.add("id= ? "); 
    664                 whereData.add(this.id); 
    665          
    666                 PreparedQuery pq = db.prepare("UPDATE battles ", 
    667                                         "SET ",Common.implode(",",update)+" ", 
    668                                         "WHERE "+Common.implode(" AND ",where)); 
    669                  
    670                 int index=1; 
    671                 for( int i=0; i < updateData.size(); i++ ) { 
    672                         pq.setObject(index++, updateData.get(i)); 
    673                 } 
    674                 for( int i=0; i < whereData.size(); i++ ) { 
    675                         pq.setObject(index++, whereData.get(i)); 
    676                 } 
    677                 pq.tUpdate(1); 
    678                 pq.close(); 
     697                        this.inakt = 0; 
     698                } 
    679699        } 
    680700         
     
    701721                Context context = ContextMap.getContext(); 
    702722                org.hibernate.Session db = context.getDB(); 
    703                 Database database = context.getDatabase(); 
     723 
    704724                System.err.println("battle: "+id+" :: "+ownShipID+" :: "+enemyShipID); 
    705725                // Kann der Spieler ueberhaupt angreifen (Noob-Schutz?) 
     
    804824                        } 
    805825                         
    806                         BattleShip battleShip = new BattleShip(0, aShip); 
     826                        BattleShip battleShip = new BattleShip(null, aShip); 
    807827                         
    808828                        ShipTypeData shiptype = aShip.getBaseType(); 
     
    866886                // 
    867887 
    868                 database.update("INSERT INTO battles (x,y,system,ally1,ally2,commander1,com1Points,commander2,com2Points,lastaction,lastturn,flags,com1Msg,com2Msg) ", 
    869                                                         "VALUES (",ownShip.getShip().getX(),",",ownShip.getShip().getY(),",",ownShip.getShip().getSystem(),",", 
    870                                                         (ownShip.getOwner().getAlly() != null ? ownShip.getOwner().getAlly().getId() : "0"),",", 
    871                                                         (enemyShip.getOwner().getAlly() != null ? enemyShip.getOwner().getAlly().getId() : "0"),", ", 
    872                                                         ownShip.getOwner().getID(),",0,",enemyShip.getOwner().getID(),",0,",Common.time(),",",Common.time(),",'",FLAG_FIRSTROUND,"','','')"); 
    873                 battle.id = database.insertID(); 
    874  
    875                 if( database.affectedRows() == 0 ) { 
    876                         context.addError("<span style=\"color:red\">Die Schlacht konnte nicht erfolgreich erstellt werden</span>"); 
    877                         return null; 
    878                 } 
     888                battle.x = ownShip.getShip().getX(); 
     889                battle.y = ownShip.getShip().getY(); 
     890                battle.system = ownShip.getShip().getSystem(); 
     891                battle.ally[0] = ownShip.getOwner().getAlly() != null ? ownShip.getOwner().getAlly().getId() : 0; 
     892                battle.ally[1] = enemyShip.getOwner().getAlly() != null ? enemyShip.getOwner().getAlly().getId() : 0; 
     893                battle.commander[0] = ownShip.getOwner(); 
     894                battle.commander[1] = enemyShip.getOwner(); 
     895                battle.lastaction = Common.time(); 
     896                battle.lastturn = Common.time(); 
     897                battle.flags = FLAG_FIRSTROUND; 
     898                battle.comMsg[0] = ""; 
     899                battle.comMsg[1] = ""; 
     900                db.save(battle); 
    879901 
    880902                // 
     
    894916                idlist.add(enemyShip.getId()); 
    895917                 
    896                 enemyShip.setBattleId(battle.id); 
     918                enemyShip.setBattle(battle); 
    897919                db.persist(enemyShip); 
    898920                 
    899                 enemyShip.getShip().setBattle(battle.id); 
     921                enemyShip.getShip().setBattle(battle); 
    900922 
    901923                for( int i=0; i < battle.enemyShips.size(); i++ ) { 
     
    911933                        idlist.add(ship.getId()); 
    912934                         
    913                         ship.setBattleId(battle.id); 
     935                        ship.setBattle(battle); 
    914936                        db.persist(ship); 
    915937                         
    916                         ship.getShip().setBattle(battle.id); 
     938                        ship.getShip().setBattle(battle); 
    917939                } 
    918940                if( startlist.size() > 0 ) { 
     
    928950                idlist.add(ownShip.getId()); 
    929951                 
    930                 ownShip.setBattleId(battle.id); 
     952                ownShip.setBattle(battle); 
    931953                db.persist(ownShip); 
    932954                 
    933                 ownShip.getShip().setBattle(battle.id); 
     955                ownShip.getShip().setBattle(battle); 
    934956                ownShip.getShip().setDocked(""); 
    935957 
     
    945967                        idlist.add(ship.getId()); 
    946968                         
    947                         ship.setBattleId(battle.id); 
     969                        ship.setBattle(battle); 
    948970                        db.persist(ship); 
    949971                         
    950                         ship.getShip().setBattle(battle.id); 
     972                        ship.getShip().setBattle(battle); 
    951973                } 
    952974                if( startOwn && startlist.size() > 0 ) { 
     
    10451067                 
    10461068                // Zuerst berechnen wir die eigenen AP 
    1047                 int ownPoints = battle.getActionPoints(battle.ownSide); 
     1069                battle.com1Points = battle.getActionPoints(battle.ownSide); 
    10481070 
    10491071                // Nun berechnen wir die gegnerischen AP  
    1050                 int enemyPoints = battle.getActionPoints(battle.enemySide); 
    1051                  
    1052                 database.update("UPDATE battles SET com1Points=",ownPoints,",com2Points=",enemyPoints," WHERE id=",battle.id); 
    1053                  
     1072                battle.com2Points = battle.getActionPoints(battle.enemySide); 
     1073 
    10541074                return battle; 
    10551075        } 
     
    11301150                List<User> users = context.query("select distinct bs.ship.owner " + 
    11311151                                        "from BattleShip bs " + 
    1132                                         "where bs.battleid="+this.id+" and bs.side="+this.enemySide, User.class); 
     1152                                        "where bs.battle="+this.id+" and bs.side="+this.enemySide, User.class); 
    11331153 
    11341154                for( User euser : users ) { 
     
    12141234                                } 
    12151235                                 
    1216                                 BattleShip sid2bs = new BattleShip(this.id, sid2); 
     1236                                BattleShip sid2bs = new BattleShip(this, sid2); 
    12171237                                sid2bs.setAction(sid2Action); 
    12181238                                sid2bs.setSide(this.ownSide); 
     
    12241244                                db.persist(sid2bs); 
    12251245                                 
    1226                                 sid2.setBattle(this.id); 
     1246                                sid2.setBattle(this); 
    12271247                        } 
    12281248                 
     
    12341254                        } 
    12351255 
    1236                         BattleShip aBattleShip = new BattleShip(this.id, aship); 
     1256                        BattleShip aBattleShip = new BattleShip(this, aship); 
    12371257                        aBattleShip.setAction(sidAction); 
    12381258                        aBattleShip.setSide(side); 
     
    12441264                        db.persist(aBattleShip); 
    12451265                         
    1246                         aship.setBattle(this.id); 
     1266                        aship.setBattle(this); 
    12471267                } 
    12481268                 
     
    12581278                        this.logme("Die "+log_shiplink(shipd)+" ist der Schlacht beigetreten\n\n"); 
    12591279                 
    1260                         shipd.setBattle(this.id); 
     1280                        shipd.setBattle(this); 
    12611281                } 
    12621282                 
     
    12661286        /** 
    12671287         * Laedt eine Schlacht aus der Datenbank 
    1268          * @param battleID die ID der Schlacht 
    1269          * @param id Die ID des aktiven Spielers 
    1270          * @param ownShipID die ID eines auszuwaehlenden eigenen Schiffes (oder 0) 
    1271          * @param enemyShipID die ID eines auszuwaehlenden gegnerischen Schiffes (oder 0) 
     1288         * @param battleId die ID der Schlacht 
     1289         * @param user Der aktive Spieler 
     1290         * @param ownShip Das auszuwaehlende eigene Schiff (oder <code>null</code>) 
     1291         * @param enemyShip Das auszuwaehlende gegnerische Schiff (oder <code>null</code>) 
     1292         * @param forcejoin Die ID einer Seite (1 oder 2), welche als die eigene zu waehlen ist. Falls 0 wird automatisch eine gewaehlt 
     1293         *  
     1294         * @return Die Schlacht oder <code>null</code> 
     1295         */ 
     1296        public static Battle loadBattle(int battleId, User user, Ship ownShip, Ship enemyShip, int forcejoin) { 
     1297                Context context = ContextMap.getContext(); 
     1298                org.hibernate.Session db = context.getDB(); 
     1299                 
     1300                Battle battle = (Battle)db.get(Battle.class, battleId); 
     1301                if( battle == null ) { 
     1302                        db.createQuery("update Ship set battle=null where id>0 and battle=?") 
     1303                                .setInteger(0, battleId) 
     1304                                .executeUpdate(); 
     1305                 
     1306                        context.addError("Die Schlacht ist bereits zuende!"); 
     1307                        return null; 
     1308                } 
     1309                 
     1310                battle.load(user, ownShip, enemyShip, forcejoin); 
     1311                 
     1312                return battle; 
     1313        } 
     1314         
     1315        /** 
     1316         * Laedt weitere Schlachtdaten aus der Datenbank 
     1317         * @param user Der aktive Spieler 
     1318         * @param ownShip Das auszuwaehlende eigene Schiff (oder <code>null</code>) 
     1319         * @param enemyShip Das auszuwaehlende gegnerische Schiff (oder <code>null</code>) 
    12721320         * @param forcejoin Die ID einer Seite (1 oder 2), welche als die eigene zu waehlen ist. Falls 0 wird automatisch eine gewaehlt 
    12731321         *  
    12741322         * @return <code>true</code>, falls die Schlacht erfolgreich geladen wurde 
    12751323         */ 
    1276         public boolean load(int battleID, int id, int ownShipID, int enemyShipID, int forcejoin ) { 
     1324        public boolean load(User user, Ship ownShip, Ship enemyShip, int forcejoin ) { 
    12771325                Context context = ContextMap.getContext(); 
    1278                 Database database = context.getDatabase(); 
    12791326                org.hibernate.Session db = context.getDB(); 
    12801327                 
     
    12841331                                - Update der Schiffe, falls ein Spieler nicht mehr einer der beiden Seiten angehoert (und ggf auch update der Kommandanten) 
    12851332                */ 
    1286                 SQLResultRow battledata = database.first( "SELECT * FROM battles WHERE id=",battleID); 
    1287          
    1288                 if( battledata.isEmpty() ) { 
    1289                         db.createQuery("update Ship set battle=null where id>0 and battle=?") 
    1290                                 .setInteger(0, battleID) 
    1291                                 .executeUpdate(); 
    1292                          
    1293                         context.addError("Die Schlacht ist bereits zuende!"); 
    1294                         return false; 
    1295                 } 
    1296          
    1297                 this.id = battleID; 
    1298                 this.ally[0] = battledata.getInt("ally1"); 
    1299                 this.ally[1] = battledata.getInt("ally2"); 
    1300                 this.commander[0] = battledata.getInt("commander1"); 
    1301                 this.commander[1] = battledata.getInt("commander2"); 
    1302                 this.x = battledata.getInt("x"); 
    1303                 this.y = battledata.getInt("y"); 
    1304                 this.system = battledata.getInt("system"); 
    1305                 this.ready[0] = battledata.getBoolean("ready1"); 
    1306                 this.ready[1] = battledata.getBoolean("ready2"); 
    1307                 this.comMsg[0] = battledata.getString("com1Msg"); 
    1308                 this.comMsg[1] = battledata.getString("com2Msg"); 
    1309                 this.betak[0] = battledata.getBoolean("com1BETAK"); 
    1310                 this.betak[1] = battledata.getBoolean("com2BETAK"); 
    1311                 this.takeCommand[0] = battledata.getInt("takeCommand1"); 
    1312                 this.takeCommand[1] = battledata.getInt("takeCommand2"); 
    1313                 this.blockcount = battledata.getInt("blockcount"); 
    1314                 this.lastturn = battledata.getInt("lastturn"); 
    1315                 this.visibility = battledata.getString("visibility").length() == 0 ? null : battledata.getString("visibility"); 
    1316                 this.quest = battledata.getInt("quest"); 
    1317                 this.guest = false; 
    1318                 this.flags = battledata.getInt("flags"); 
    1319                  
    1320                 this.addCommanders.add(0, new ArrayList<Integer>()); 
    1321                 this.addCommanders.add(1, new ArrayList<Integer>()); 
    1322          
    1323                 this.tableBuffer = battledata; 
    1324          
    1325                 User auser = (User)context.getDB().get(User.class, id); 
    1326                  
     1333 
    13271334                // 
    13281335                // Weitere Commander in Folge von Questschlachten feststellen 
    13291336                // 
    1330                 if( (this.quest != 0) && !Common.inArray(id,this.commander) && ((this.commander[0] < 0) ^ (this.commander[1] < 0) ) ) { 
    1331                         if( auser.hasFlag(User.FLAG_QUEST_BATTLES) || auser.getAccessLevel() > 20 ) { 
    1332                                 if( this.commander[0] < 0 )  { 
    1333                                         this.addCommanders.get(0).add(id); 
     1337                if( (this.quest != null) && !Common.inArray(user.getID(),this.commander) && ((this.commander[0].getID() < 0) ^ (this.commander[1].getID() < 0) ) ) { 
     1338                        if( user.hasFlag(User.FLAG_QUEST_BATTLES) || user.getAccessLevel() > 20 ) { 
     1339                                if( this.commander[0].getID() < 0 )  { 
     1340                                        this.addCommanders.get(0).add(user.getID()); 
    13341341                                } 
    13351342                                else { 
    1336                                         this.addCommanders.get(1).add(id); 
     1343                                        this.addCommanders.get(1).add(user.getID()); 
    13371344                                }        
    13381345                        } 
     
    13461353         
    13471354                if( forcejoin == 0 ) { 
    1348                         if( ( (auser.getAlly() != null) && !Common.inArray(auser.getAlly().getId(),this.ally) && !this.isCommander(auser) ) || 
    1349                                 ( (auser.getAlly() == null) && !this.isCommander(auser) ) ) { 
     1355                        if( ( (user.getAlly() != null) && !Common.inArray(user.getAlly().getId(),this.ally) && !this.isCommander(user) ) || 
     1356                                ( (user.getAlly() == null) && !this.isCommander(user) ) ) { 
    13501357         
    13511358                                // Hat der Spieler ein Schiff in der Schlacht 
    1352                                 BattleShip aship = (BattleShip)db.createQuery("from BattleShip where id>0 and ship.owner=? and battleid=?") 
    1353                                         .setEntity(0, auser) 
    1354                                         .setInteger(1, this.id
     1359                                BattleShip aship = (BattleShip)db.createQuery("from BattleShip where id>0 and ship.owner=? and battle=?") 
     1360                                        .setEntity(0, user) 
     1361                                        .setEntity(1, this
    13551362                                        .setMaxResults(1) 
    13561363                                        .uniqueResult(); 
     
    13611368                                else { 
    13621369                                        //Mehr ueber den Spieler herausfinden 
    1363                                         if( auser.getAccessLevel() > 20 ) { 
     1370                                        if( user.getAccessLevel() > 20 ) { 
    13641371                                                this.guest = true; 
    13651372                                        } 
    1366                                         else if( auser.hasFlag(User.FLAG_VIEW_BATTLES) ) { 
     1373                                        else if( user.hasFlag(User.FLAG_VIEW_BATTLES) ) { 
    13671374                                                this.guest = true; 
    13681375                                        } 
     
    13711378                                                                "where owner= :user and x= :x and y= :y and system= :sys and " + 
    13721379                                                                        "battle is null and shiptype.shipClass in (11,13)") 
    1373                                                                 .setEntity("user", auser) 
     1380                                                                .setEntity("user", user) 
    13741381                                                                .setInteger("x", this.x) 
    13751382                                                                .setInteger("y", this.y) 
     
    13951402                // 
    13961403         
    1397                 if( (auser.getAlly() != null && (auser.getAlly().getId() == this.ally[0])) || this.isCommander(auser,0) || this.guest || forceSide == 0 ) { 
     1404                if( (user.getAlly() != null && (user.getAlly().getId() == this.ally[0])) || this.isCommander(user,0) || this.guest || forceSide == 0 ) { 
    13981405                        this.ownSide = 0; 
    13991406                        this.enemySide = 1; 
    14001407                } 
    1401                 else if( (auser.getAlly() != null && (auser.getAlly().getId() == this.ally[1])) || this.isCommander(auser,1) || forceSide == 1 ) { 
     1408 
     1409                else if( (user.getAlly() != null && (user.getAlly().getId() == this.ally[1])) || this.isCommander(user,1) || forceSide == 1 ) { 
    14021410                        this.ownSide = 1; 
    14031411                        this.enemySide = 0; 
     
    14151423 
    14161424                List ships = db.createQuery("from BattleShip bs inner join fetch bs.ship as s " + 
    1417                                                         "where s.id>0 and bs.battleid=? " + 
     1425                                                        "where s.id>0 and bs.battle=? " + 
    14181426                                                                "order by s.shiptype,s.id") 
    1419                                                 .setInteger(0, this.id
     1427                                                .setEntity(0, this
    14201428                                                .list(); 
    14211429         
     
    14541462                this.activeSOwn = 0; 
    14551463         
    1456                 if( enemyShipID != 0 ) { 
     1464                if( enemyShip != null ) { 
    14571465                        for( int i=0; i < enemyShips.size(); i++ ) { 
    1458                                 if( enemyShips.get(i).getId() == enemyShipID ) { 
     1466                                if( enemyShips.get(i).getId() == enemyShip.getId() ) { 
    14591467                                        this.activeSEnemy = i; 
    14601468                                        break; 
     
    14631471                } 
    14641472         
    1465                 if( ownShipID != 0 ) { 
     1473                if( ownShip != null ) { 
    14661474                        for( int i=0; i < ownShips.size(); i++ ) { 
    1467                                 if( ownShips.get(i).getId() == ownShipID ) { 
     1475                                if( ownShips.get(i).getId() == ownShip.getId() ) { 
    14681476                                        this.activeSOwn = i; 
    14691477                                        break; 
     
    16761684                 
    16771685                if( (owncount == 0) && (enemycount == 0) ) { 
    1678                         User user1 = (User)context.getDB().get(User.class, this.commander[this.enemySide]); 
    1679                         User user2 = (User)context.getDB().get(User.class, this.commander[this.ownSide]); 
    1680  
    1681                         PM.send(user1, this.commander[this.ownSide], "Schlacht unentschieden", "Die Schlacht bei "+this.system+" : "+this.x+"/"+this.y+" gegen "+user1.getName()+" wurde mit einem Unendschieden beendet!"); 
    1682                         PM.send(user2, this.commander[this.enemySide], "Schlacht unentschieden", "Die Schlacht bei "+this.system+" : "+this.x+"/"+this.y+" gegen "+user2.getName()+" wurde mit einem Unendschieden beendet!"); 
     1686                        PM.send(this.commander[this.enemySide], this.commander[this.ownSide].getID(), "Schlacht unentschieden", "Die Schlacht bei "+this.getLocation()+" gegen "+this.commander[this.enemySide].getName()+" wurde mit einem Unendschieden beendet!"); 
     1687                        PM.send(this.commander[this.ownSide], this.commander[this.enemySide].getID(), "Schlacht unentschieden", "Die Schlacht bei "+this.getLocation()+" gegen "+this.commander[this.ownSide].getName()+" wurde mit einem Unendschieden beendet!"); 
    16831688 
    16841689                        // Schlacht beenden - unendschieden 
     
    16891694 
    16901695                        if( calledByUser ) { 
    1691                                 context.getResponse().getContent().append("Du hast die Schlacht gegen "+Common._title(user1.getName())+" mit einem Unendschieden beendet!"); 
     1696                                context.getResponse().getContent().append("Du hast die Schlacht gegen "+Common._title( this.commander[this.enemySide].getName())+" mit einem Unendschieden beendet!"); 
    16921697                        } 
    16931698                        return false; 
    16941699                } 
    16951700                else if( owncount == 0 ) { 
    1696                         User user1 = (User)context.getDB().get(User.class, this.commander[this.enemySide]); 
    1697