Changeset 5e49daa22ec8d86487ddac25102c415a950874e8

Show
Ignore:
Timestamp:
07/20/08 18:15:48 (4 months ago)
Author:
Christopher Jung <bktheg@web.de>
git-committer:
Christopher Jung <bktheg@web.de> 1216570548 +0200
git-parent:

[a53bf3df2ee843c7dcbff98462aa9c06d33cf7c1]

git-author:
Sebastian Gift <Madison@gt-knm.de> 1216570548 +0200
Message:

[feature] Schiffe koennen direkt vom Flottenmenue aus gebaut werden.

Files:

Legend:

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

    r487013a r5e49daa  
    2020 
    2121import java.util.ArrayList; 
     22import java.util.Comparator; 
     23import java.util.HashSet; 
    2224import java.util.Iterator; 
    2325import java.util.List; 
     26import java.util.PriorityQueue; 
     27import java.util.Set; 
    2428 
    2529import net.driftingsouls.ds2.server.Location; 
     
    3943import net.driftingsouls.ds2.server.ships.ShipTypeData; 
    4044import net.driftingsouls.ds2.server.ships.ShipTypes; 
     45import net.driftingsouls.ds2.server.werften.WerftObject; 
    4146 
    4247import org.apache.commons.lang.StringUtils; 
     
    830835         
    831836        /** 
     837         * Baut ein Schiffstyp n-mal in allen Werften der Flotte, die dazu in der Lage sind 
     838         */ 
     839        @Action(ActionType.DEFAULT) 
     840        @SuppressWarnings("unchecked") 
     841        public void buildAction() { 
     842                TemplateEngine t = getTemplateEngine(); 
     843                parameterNumber("buildcount"); 
     844                parameterNumber("buildid"); 
     845                 
     846                int buildCount = getInteger("buildcount"); 
     847                final int buildid = getInteger("buildid"); 
     848                 
     849                if(buildCount <= 0) { 
     850                        return; 
     851                } 
     852                 
     853                org.hibernate.Session db = getDB(); 
     854                User user = (User)getUser(); 
     855                List<Ship> shipyards = db.createQuery("from Ship where id>0 and owner=? and fleet=? and shiptype.werft > 0 order by id") 
     856                .setEntity(0, user) 
     857                .setEntity(1, this.fleet) 
     858                .list(); 
     859                 
     860                for(Iterator<Ship> it = shipyards.iterator(); it.hasNext();) { 
     861                        Ship ship = it.next(); 
     862                        if(ship.getTypeData().getWerft() == 0) { 
     863                                it.remove(); 
     864                        } 
     865                } 
     866                 
     867                if(shipyards.isEmpty()) { 
     868                        return; 
     869                } 
     870                 
     871                //Build 
     872                while(buildCount > 0) { 
     873                        int couldNotBuild = 0; 
     874                        for(Ship ship: shipyards) { 
     875                                WerftObject shipyard = (WerftObject)db.createQuery("from WerftObject where shipid=?").setInteger(0, ship.getId()).uniqueResult(); 
     876                                if(shipyard.buildShip(buildid, false, false)) { 
     877                                        buildCount--; 
     878                                } 
     879                                else { 
     880                                        couldNotBuild++; 
     881                                } 
     882                                 
     883                                if(buildCount == 0) { 
     884                                        break; 
     885                                } 
     886                        } 
     887                         
     888                        //No shipyard could build -> stop 
     889                        if(couldNotBuild == shipyards.size()) { 
     890                                buildCount = 0; 
     891                        } 
     892                } 
     893                 
     894                //Reload main page 
     895                t.setVar("jscript.reloadmain",  1); 
     896                 
     897                this.redirect(); 
     898        } 
     899         
     900        /** 
    832901         * Teil eines Formatierungsstrings fuer Schiffsnamen 
    833902         */ 
     
    10161085                        .setEntity(1, this.fleet) 
    10171086                        .list(); 
     1087                 
     1088                Set<ShipType> buildableShips = new HashSet<ShipType>(); 
    10181089                for( Iterator iter=ships.iterator(); iter.hasNext(); ) { 
    10191090                        Ship ship = (Ship)iter.next(); 
     
    10211092                        ShipTypeData shiptype = ship.getTypeData(); 
    10221093                        Location loc = ship.getLocation(); 
     1094                         
     1095                        if(shiptype.getWerft() > 0) { 
     1096                                WerftObject werft = (WerftObject)db.createQuery("from WerftObject where shipid=?").setInteger(0, ship.getId()).uniqueResult(); 
     1097                                buildableShips.addAll(werft.getBuildableShips()); 
     1098                        } 
    10231099                         
    10241100                        t.setVar(       "ship.id",                      ship.getId(), 
     
    10341110                         
    10351111                        t.parse("ships.list", "ships.listitem", true); 
    1036                 }        
     1112                } 
     1113                 
     1114                //List of buildable ships 
     1115                if(!buildableShips.isEmpty()) { 
     1116                        t.setBlock("_FLEETMGNT", "buildableships.listitem", "buildableships.list"); 
     1117                        PriorityQueue<ShipType> sortedBuildableShips = new PriorityQueue<ShipType>(11, new Comparator<ShipType>() { 
     1118                                        public int compare(ShipType o1, ShipType o2) { 
     1119                                                if(o1.getId() == o2.getId()) { 
     1120                                                        return 0; 
     1121                                                } 
     1122                                                 
     1123                                                if(o1.getId() > o2.getId()) { 
     1124                                                        return 1; 
     1125                                                } 
     1126                                                return -1; 
     1127                                        } 
     1128                                }); 
     1129                         
     1130                        sortedBuildableShips.addAll(buildableShips); 
     1131                         
     1132                        ShipType ship; 
     1133                        while((ship = sortedBuildableShips.poll()) != null) { 
     1134                                t.setVar(       "buildableships.id",    ship.getId(), 
     1135                                                        "buildableships.name",  ship.getNickname()); 
     1136                         
     1137                                t.parse("buildableships.list", "buildableships.listitem", true); 
     1138                        } 
     1139                } 
    10371140         
    10381141                // Jaegerliste bauen 
  • src/net/driftingsouls/ds2/server/werften/WerftObject.java

    r36f1a99 r5e49daa  
    2222import java.util.Arrays; 
    2323import java.util.HashMap; 
     24import java.util.HashSet; 
    2425import java.util.Iterator; 
    2526import java.util.List; 
    2627import java.util.Map; 
     28import java.util.Set; 
    2729import java.util.SortedMap; 
    2830import java.util.TreeMap; 
     
    10291031         
    10301032        /** 
     1033         * Gibt die Schiffstypen zurueck, die auf dieser Werft gebaut werden koennen. 
     1034         *  
     1035         * @return Schiffstypen, die auf dieser Werft gebaut werden koennen. 
     1036         */ 
     1037        @SuppressWarnings("unchecked") 
     1038        public Set<ShipType> getBuildableShips() { 
     1039                Context context = ContextMap.getContext(); 
     1040                org.hibernate.Session db = context.getDB(); 
     1041                User owner = this.getOwner(); 
     1042                 
     1043                Set<ShipType> shipTypes = new HashSet<ShipType>(); 
     1044                boolean militaryAllowed = Systems.get().system(this.getSystem()).isMilitaryAllowed(); 
     1045                boolean flagshipAllowed = owner.hasFlagschiffSpace(); 
     1046                 
     1047                String query = "from ShipBaubar where werftslots <= ?"; 
     1048                if(!flagshipAllowed) { 
     1049                        query += "!flagschiff "; 
     1050                } 
     1051                if(!militaryAllowed) { 
     1052                        query += "systemReq = 0 "; 
     1053                } 
     1054                query = query.trim(); 
     1055                List<ShipBaubar> buildableShips = (List<ShipBaubar>)db.createQuery(query).setInteger(0, this.getWerftSlots()).list(); 
     1056                 
     1057                //Allowed by draft 
     1058                Set<IEDraftShip> drafts = getUsableShipDrafts(); 
     1059                for(IEDraftShip draft: drafts) { 
     1060                        shipTypes.add((ShipType)db.get(ShipType.class, draft.getShipType())); 
     1061                } 
     1062                 
     1063                 
     1064                //Filter parts which are not database checkable 
     1065                for(ShipBaubar buildableShip: buildableShips) { 
     1066                        if(!Rassen.get().rasse(owner.getRace()).isMemberIn(buildableShip.getRace())) { 
     1067                                continue; 
     1068                        } 
     1069                         
     1070                        if( !owner.hasResearched(buildableShip.getRes(1)) || !owner.hasResearched(buildableShip.getRes(2)) || !owner.hasResearched(buildableShip.getRes(3))) { 
     1071                                continue; 
     1072                        } 
     1073                         
     1074                        shipTypes.add(buildableShip.getType()); 
     1075                } 
     1076                 
     1077                //Filter disallowed shiptypes 
     1078                Set<ShipType> disabledShips = getDisabledShips(); 
     1079                for (Iterator<ShipType> it = shipTypes.iterator(); it.hasNext(); ) { 
     1080                        ShipType shipType = it.next(); 
     1081                        if(disabledShips.contains(shipType)) { 
     1082                                it.remove(); 
     1083                                continue; 
     1084                        } 
     1085                } 
     1086                 
     1087                return shipTypes; 
     1088        } 
     1089         
     1090        /** 
     1091         * Gibt alle Schiffstypen zurueck, die diese Werft derzeit nicht bauen kann. 
     1092         *  
     1093         * @return Schiffstypen, die nicht baubar sind. 
     1094         */ 
     1095        private Set<ShipType> getDisabledShips() { 
     1096                Context context = ContextMap.getContext(); 
     1097                org.hibernate.Session db = context.getDB(); 
     1098                 
     1099                Cargo availablecargo = this.getCargo(false); 
     1100                Cargo allyitems = getAllyItems(); 
     1101                Set<ShipType> disabledShips = new HashSet<ShipType>(); 
     1102                 
     1103                Cargo allitems = new Cargo(); 
     1104                allitems.addCargo(allyitems); 
     1105                allitems.addCargo(availablecargo); 
     1106                 
     1107                for(ItemCargoEntry item: allitems.getItemsWithEffect( ItemEffect.Type.DISABLE_SHIP )) 
     1108                { 
     1109                        IEDisableShip disableShip = (IEDisableShip)item.getItemEffect(); 
     1110                        disabledShips.add((ShipType)db.load(ShipType.class, disableShip.getShipType())); 
     1111                } 
     1112                 
     1113                return disabledShips; 
     1114        } 
     1115         
     1116        private Set<ItemCargoEntry> getAllItems() { 
     1117                Cargo allyitems = getAllyItems(); 
     1118                Cargo shipyardCargo = this.getCargo(false); 
     1119                 
     1120                Set<ItemCargoEntry> items = new HashSet<ItemCargoEntry>(); 
     1121                items.addAll(allyitems.getItemsWithEffect(ItemEffect.Type.DRAFT_SHIP)); 
     1122                items.addAll(shipyardCargo.getItemsWithEffect(ItemEffect.Type.DRAFT_SHIP)); 
     1123                 
     1124                return items; 
     1125        } 
     1126         
     1127        /** 
     1128         * Gibt alle Bauplaene auf der Werft zurueck, die derzeit benutzt werden koennen. 
     1129         *  
     1130         * @return Nutzbare Bauplaene. 
     1131         */ 
     1132        private Set<IEDraftShip> getUsableShipDrafts() { 
     1133                User owner = this.getOwner(); 
     1134                Set<IEDraftShip> shipDrafts = new HashSet<IEDraftShip>(); 
     1135 
     1136                 
     1137                //All Drafts - ally and local 
     1138                Set<ItemCargoEntry> items = getAllItems(); 
     1139                 
     1140                for(ItemCargoEntry item: items) { 
     1141                        if(item.getItemEffect().getType() != ItemEffect.Type.DRAFT_SHIP) { 
     1142                                continue; 
     1143                        } 
     1144                         
     1145                        IEDraftShip draft = (IEDraftShip)item.getItemEffect(); 
     1146                         
     1147                        if( draft.getWerftSlots() > this.getWerftSlots() ) { 
     1148                                continue; 
     1149                        } 
     1150 
     1151                        if( !owner.hasFlagschiffSpace() && draft.isFlagschiff() ) { 
     1152                                continue; 
     1153                        } 
     1154         
     1155                        if(!owner.hasResearched(draft.getTechReq(1)) || !owner.hasResearched(draft.getTechReq(2)) || !owner.hasResearched(draft.getTechReq(3))) { 
     1156                                continue; 
     1157                        } 
     1158                         
     1159                        shipDrafts.add(draft); 
     1160                } 
     1161                 
     1162                return shipDrafts; 
     1163        } 
     1164         
     1165        private Cargo getAllyItems() { 
     1166                User owner = this.getOwner(); 
     1167                if( owner.getAlly() != null ) { 
     1168                        return new Cargo(Cargo.Type.ITEMSTRING, owner.getAlly().getItems()); 
     1169                } 
     1170                 
     1171                return new Cargo(); 
     1172        } 
     1173         
     1174        /** 
    10311175         * Liefert die Liste aller theoretisch baubaren Schiffe auf dieser Werft. 
    10321176         * Das vorhanden sein von Resourcen wird hierbei nicht beruecksichtigt. 
     
    10351179         *                      zur Bestimmung ob und wenn ja welcher Bauplan benoetigt wird zum bauen 
    10361180         */ 
     1181        @Deprecated 
    10371182        public SQLResultRow[] getBuildShipList() { 
    10381183                List<SQLResultRow> result = new ArrayList<SQLResultRow>(); 
     
    12631408         
    12641409        /** 
     1410         * Findet heraus welches Item zum Bau benoetigt wird und baut danach das Schiff. 
     1411         * @param typeid Die ID des zu bauenden Schifftyps 
     1412         * @param costsPerTick Sollen die Baukosten pro Tick (<code>true</code>) oder der Gesamtbetrag jetzt (<code>false</code>) abgezogen werden 
     1413         * @param testOnly Soll nur getestet (true) oder wirklich gebaut (false) werden? 
     1414         * @see WerftObject#buildShip 
     1415         *  
     1416         * @return true, wenn kein Fehler aufgetreten ist. 
     1417         */ 
     1418        public boolean buildShip(int typeid, boolean costsPerTick, boolean testOnly) { 
     1419                Context context = ContextMap.getContext(); 
     1420                org.hibernate.Session db = context.getDB(); 
     1421                int item = -1; 
     1422                ShipType type = (ShipType)db.load(ShipType.class, typeid); 
     1423                ShipBaubar ship = (ShipBaubar)db.createQuery("from ShipBaubar where type=?").setEntity(0, type).uniqueResult(); 
     1424                 
     1425                if(ship == null) { 
     1426                        for(ItemCargoEntry entry: getAllItems()) { 
     1427                                if(entry.getItemEffect().getType() == ItemEffect.Type.DRAFT_SHIP) { 
     1428                                        IEDraftShip draft = (IEDraftShip)entry.getItemEffect(); 
     1429                                        if(draft.getShipType() == typeid) { 
     1430                                                item = entry.getItemID(); 
     1431                                                break; 
     1432                                        } 
     1433                                } 
     1434                        } 
     1435                } 
     1436                else { 
     1437                        item = 0; 
     1438                } 
     1439                 
     1440                //Cannot build without item, correct item not found 
     1441                if(item == -1) { 
     1442                        return false; 
     1443                } 
     1444                 
     1445                if(item > 0) { 
     1446                        return buildShip(0, item, costsPerTick, testOnly); 
     1447                } 
     1448                return buildShip(ship.getId(), item, costsPerTick, testOnly); 
     1449        } 
     1450         
     1451        /** 
    12651452         * Baut ein Schiff in der Werft auf Basis der angegebenen Schiffbau-ID und der 
    12661453         * angegebenen Item-ID (Bauplan). {@link DSObject#MESSAGE} enthaelt die Hinweistexte 
  • templates/fleetmgnt.html

    ref36dc7 r5e49daa  
    202202                                                document.getElementById("alarm").style.display = 'none'; 
    203203                                        } 
     204                                         
     205                                        if( action == "build" ) { 
     206                                                document.getElementById("buildcount").style.display = 'inline'; 
     207                                                document.getElementById("buildid").style.display = 'inline'; 
     208                                        } 
     209                                        else { 
     210                                                document.getElementById("buildcount").style.display = 'none'; 
     211                                                document.getElementById("buildid").style.display = 'none'; 
     212                                        } 
    204213                                } 
    205214                        --> 
     
    226235                                        {if fleetcombine.list} 
    227236                                                <option value="fleetcombine">Flotten zusammenlegen</option> 
     237                                        {/endif} 
     238                                        {if buildableships.list} 
     239                                                <option value="build">Schiffe bauen</option> 
    228240                                        {/endif} 
    229241                                        <option value="rename">Flotte umbenennen</option> 
     
    253265                                        <option value="1">rot</option> 
    254266                                </select> 
     267                                {if buildableships.list} 
     268                                <input type="text" id="buildcount" name="buildcount" style="display:none" size="3" maxlength="3" /> 
     269                                <select id="buildid" name="buildid" size="1" style="display:none"> 
     270                                                <!-- BEGIN buildableships.listitem --> 
     271                                                        <option value="{buildableships.id}">{buildableships.name}</option> 
     272                                                <!-- END buildableships.listitem --> 
     273                                </select> 
     274                                {/endif} 
    255275                                <input type="submit" value="ok" /> 
    256276                        </form>