Changeset 6760ac9f7fd8d4e2b7062327eaac4055ff98f583
- Timestamp:
- 07/10/07 15:39:03
(1 year ago)
- Author:
- Christopher Jung <bktheg@web.de>
- git-committer:
- Christopher Jung <bktheg@web.de> 1184074743 +0200
- git-parent:
[7a698dad95e80782edc96d6337ae617f53930aa7]
- git-author:
- Christopher Jung <bktheg@web.de> 1184074743 +0200
- Message:
Cores auf Hibernate umgestellt
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r7a698da |
r6760ac9 |
|
| 7 | 7 | timeToIdleSeconds="5" |
|---|
| 8 | 8 | timeToLiveSeconds="10" |
|---|
| | 9 | overflowToDisk="false" |
|---|
| | 10 | /> |
|---|
| | 11 | <cache name="net.driftingsouls.ds2.server.bases.Cargo" |
|---|
| | 12 | maxElementsInMemory="20" |
|---|
| | 13 | eternal="true" |
|---|
| | 14 | timeToIdleSeconds="60" |
|---|
| | 15 | timeToLiveSeconds="600" |
|---|
| 9 | 16 | overflowToDisk="false" |
|---|
| 10 | 17 | /> |
|---|
| rd54fd1c |
r6760ac9 |
|
| 537 | 537 | |
|---|
| 538 | 538 | if( (base.getCore() > 0) && base.isCoreActive() ) { |
|---|
| 539 | | Core core = Core.getCore(context.getDatabase(), base.getCore() ); |
|---|
| 540 | | |
|---|
| | 539 | Core core = Core.getCore(base.getCore()); |
|---|
| | 540 | |
|---|
| 541 | 541 | stat.substractCargo(core.getConsumes()); |
|---|
| 542 | 542 | stat.addCargo(core.getProduces()); |
|---|
| r31f53b6 |
r6760ac9 |
|
| 19 | 19 | package net.driftingsouls.ds2.server.bases; |
|---|
| 20 | 20 | |
|---|
| 21 | | import java.util.HashMap; |
|---|
| 22 | | import java.util.Map; |
|---|
| | 21 | import javax.persistence.Entity; |
|---|
| | 22 | import javax.persistence.Id; |
|---|
| | 23 | import javax.persistence.Inheritance; |
|---|
| | 24 | import javax.persistence.InheritanceType; |
|---|
| | 25 | import javax.persistence.Table; |
|---|
| 23 | 26 | |
|---|
| 24 | 27 | import net.driftingsouls.ds2.server.cargo.Cargo; |
|---|
| 25 | | import net.driftingsouls.ds2.server.framework.caches.CacheManager; |
|---|
| 26 | | import net.driftingsouls.ds2.server.framework.caches.ControllableCache; |
|---|
| 27 | | import net.driftingsouls.ds2.server.framework.db.Database; |
|---|
| 28 | | import net.driftingsouls.ds2.server.framework.db.SQLResultRow; |
|---|
| | 28 | import net.driftingsouls.ds2.server.framework.ContextMap; |
|---|
| | 29 | |
|---|
| | 30 | import org.hibernate.annotations.Cache; |
|---|
| | 31 | import org.hibernate.annotations.CacheConcurrencyStrategy; |
|---|
| | 32 | import org.hibernate.annotations.DiscriminatorFormula; |
|---|
| | 33 | import org.hibernate.annotations.Immutable; |
|---|
| 29 | 34 | |
|---|
| 30 | 35 | //TODO: Warum Verbrauch/Produktion unterscheiden? |
|---|
| … | … | |
| 34 | 39 | * |
|---|
| 35 | 40 | */ |
|---|
| | 41 | @Entity |
|---|
| | 42 | @Table(name="cores") |
|---|
| | 43 | @Immutable |
|---|
| | 44 | @Inheritance(strategy=InheritanceType.SINGLE_TABLE) |
|---|
| | 45 | @DiscriminatorFormula("1") |
|---|
| | 46 | @Cache(usage=CacheConcurrencyStrategy.READ_ONLY) |
|---|
| 36 | 47 | public abstract class Core { |
|---|
| 37 | | private static Map<Integer,Core> coreCache = new HashMap<Integer,Core>(); |
|---|
| | 48 | @Id |
|---|
| | 49 | private int id; |
|---|
| 38 | 50 | |
|---|
| 39 | | static { |
|---|
| 40 | | CacheManager.getInstance().registerCache( |
|---|
| 41 | | new ControllableCache() { |
|---|
| 42 | | public void clear() { |
|---|
| 43 | | Core.clearCache(); |
|---|
| 44 | | } |
|---|
| 45 | | } |
|---|
| 46 | | ); |
|---|
| 47 | | } |
|---|
| 48 | | |
|---|
| 49 | | static void clearCache() { |
|---|
| 50 | | synchronized(coreCache) { |
|---|
| 51 | | coreCache.clear(); |
|---|
| 52 | | } |
|---|
| | 51 | /** |
|---|
| | 52 | * Konstruktor |
|---|
| | 53 | * |
|---|
| | 54 | */ |
|---|
| | 55 | public Core() { |
|---|
| | 56 | // EMPTY |
|---|
| 53 | 57 | } |
|---|
| 54 | 58 | |
|---|
| … | … | |
| 57 | 61 | * Sollte kein passender Coretyp existieren, wird <code>null</code> zurueckgegeben. |
|---|
| 58 | 62 | * |
|---|
| 59 | | * @param db Eine Datenbankverbindung |
|---|
| 60 | 63 | * @param id Die ID des Coretyps |
|---|
| 61 | 64 | * @return Eine Instanz der zugehoerigen Coreklasse |
|---|
| 62 | 65 | */ |
|---|
| 63 | | public static synchronized Core getCore(Database db, int id) { |
|---|
| 64 | | if( !coreCache.containsKey(id) ) { |
|---|
| 65 | | SQLResultRow row = db.first("SELECT * FROM cores WHERE id='",id,"'"); |
|---|
| 66 | | if( row.isEmpty() ) { |
|---|
| 67 | | coreCache.put(id, null); |
|---|
| 68 | | } |
|---|
| 69 | | else { |
|---|
| 70 | | coreCache.put(id, new DefaultCore(row)); |
|---|
| 71 | | } |
|---|
| 72 | | } |
|---|
| 73 | | return coreCache.get(id); |
|---|
| | 66 | public static Core getCore(int id) { |
|---|
| | 67 | org.hibernate.Session db = ContextMap.getContext().getDB(); |
|---|
| | 68 | |
|---|
| | 69 | return (Core)db.get(Core.class, id); |
|---|
| 74 | 70 | } |
|---|
| 75 | 71 | |
|---|
| … | … | |
| 78 | 74 | * @return die ID |
|---|
| 79 | 75 | */ |
|---|
| 80 | | public abstract int getID(); |
|---|
| | 76 | public final int getID() { |
|---|
| | 77 | return id; |
|---|
| | 78 | } |
|---|
| 81 | 79 | |
|---|
| 82 | 80 | /** |
|---|
| rae09dfe |
r6760ac9 |
|
| 19 | 19 | package net.driftingsouls.ds2.server.bases; |
|---|
| 20 | 20 | |
|---|
| | 21 | import javax.persistence.Column; |
|---|
| | 22 | import javax.persistence.DiscriminatorValue; |
|---|
| | 23 | import javax.persistence.Entity; |
|---|
| | 24 | |
|---|
| 21 | 25 | import net.driftingsouls.ds2.server.cargo.Cargo; |
|---|
| 22 | 26 | import net.driftingsouls.ds2.server.cargo.UnmodifiableCargo; |
|---|
| 23 | | import net.driftingsouls.ds2.server.framework.db.SQLResultRow; |
|---|
| | 27 | |
|---|
| | 28 | import org.hibernate.annotations.Immutable; |
|---|
| | 29 | import org.hibernate.annotations.Type; |
|---|
| 24 | 30 | |
|---|
| 25 | 31 | /** |
|---|
| … | … | |
| 31 | 37 | * |
|---|
| 32 | 38 | */ |
|---|
| | 39 | @Entity |
|---|
| | 40 | @Immutable |
|---|
| | 41 | @DiscriminatorValue("1") |
|---|
| 33 | 42 | public class DefaultCore extends Core { |
|---|
| 34 | | private SQLResultRow data; |
|---|
| | 43 | private String name; |
|---|
| | 44 | @Column(name="astitype") |
|---|
| | 45 | private int astiType; |
|---|
| | 46 | @Type(type="cargo") |
|---|
| | 47 | private Cargo buildcosts; |
|---|
| | 48 | @Type(type="cargo") |
|---|
| | 49 | private Cargo produces; |
|---|
| | 50 | @Type(type="cargo") |
|---|
| | 51 | private Cargo consumes; |
|---|
| | 52 | private int arbeiter; |
|---|
| | 53 | @Column(name="ever") |
|---|
| | 54 | private int eVerbrauch; |
|---|
| | 55 | @Column(name="eprodu") |
|---|
| | 56 | private int eProduktion; |
|---|
| | 57 | private int bewohner; |
|---|
| | 58 | @Column(name="techreq") |
|---|
| | 59 | private int techReq; |
|---|
| | 60 | private int eps; |
|---|
| 35 | 61 | |
|---|
| 36 | 62 | /** |
|---|
| 37 | 63 | * Erstellt eine neue Core-Instanz |
|---|
| 38 | | * @param row Eine SQL-Ergebniszeile mit den Core-Daten |
|---|
| 39 | 64 | */ |
|---|
| 40 | | public DefaultCore(SQLResultRow row) { |
|---|
| 41 | | data = row; |
|---|
| 42 | | data.put("consumes", new UnmodifiableCargo(new Cargo(Cargo.Type.STRING, data.getString("consumes"))) ); |
|---|
| 43 | | data.put("produces", new UnmodifiableCargo(new Cargo(Cargo.Type.STRING, data.getString("produces"))) ); |
|---|
| 44 | | data.put("buildcosts", new UnmodifiableCargo(new Cargo(Cargo.Type.STRING, data.getString("buildcosts"))) ); |
|---|
| 45 | | } |
|---|
| 46 | | |
|---|
| 47 | | @Override |
|---|
| 48 | | public int getID() { |
|---|
| 49 | | return data.getInt("id"); |
|---|
| | 65 | public DefaultCore() { |
|---|
| | 66 | // EMPTY |
|---|
| 50 | 67 | } |
|---|
| 51 | 68 | |
|---|
| 52 | 69 | @Override |
|---|
| 53 | 70 | public String getName() { |
|---|
| 54 | | return data.getString("name"); |
|---|
| | 71 | return name; |
|---|
| 55 | 72 | } |
|---|
| 56 | 73 | |
|---|
| 57 | 74 | @Override |
|---|
| 58 | 75 | public int getAstiType() { |
|---|
| 59 | | return data.getInt("astitype"); |
|---|
| | 76 | return astiType; |
|---|
| 60 | 77 | } |
|---|
| 61 | 78 | |
|---|
| 62 | 79 | @Override |
|---|
| 63 | 80 | public Cargo getBuildCosts() { |
|---|
| 64 | | return (Cargo)data.get("buildcosts"); |
|---|
| | 81 | return new UnmodifiableCargo(buildcosts); |
|---|
| 65 | 82 | } |
|---|
| 66 | 83 | |
|---|
| 67 | 84 | @Override |
|---|
| 68 | 85 | public Cargo getProduces() { |
|---|
| 69 | | return (Cargo)data.get("produces"); |
|---|
| | 86 | return new UnmodifiableCargo(produces); |
|---|
| 70 | 87 | } |
|---|
| 71 | 88 | |
|---|
| 72 | 89 | @Override |
|---|
| 73 | 90 | public Cargo getConsumes() { |
|---|
| 74 | | return (Cargo)data.get("consumes"); |
|---|
| | 91 | return new UnmodifiableCargo(consumes); |
|---|
| 75 | 92 | } |
|---|
| 76 | 93 | |
|---|
| 77 | 94 | @Override |
|---|
| 78 | 95 | public int getArbeiter() { |
|---|
| 79 | | return data.getInt("arbeiter"); |
|---|
| | 96 | return arbeiter; |
|---|
| 80 | 97 | } |
|---|
| 81 | 98 | |
|---|
| 82 | 99 | @Override |
|---|
| 83 | 100 | public int getEVerbrauch() { |
|---|
| 84 | | return data.getInt("ever"); |
|---|
| | 101 | return eVerbrauch; |
|---|
| 85 | 102 | } |
|---|
| 86 | 103 | |
|---|
| 87 | 104 | @Override |
|---|
| 88 | 105 | public int getEProduktion() { |
|---|
| 89 | | return data.getInt("eprodu"); |
|---|
| | 106 | return eProduktion; |
|---|
| 90 | 107 | } |
|---|
| 91 | 108 | |
|---|
| 92 | 109 | @Override |
|---|
| 93 | 110 | public int getBewohner() { |
|---|
| 94 | | return data.getInt("bewohner"); |
|---|
| | 111 | return bewohner; |
|---|
| 95 | 112 | } |
|---|
| 96 | 113 | |
|---|
| 97 | 114 | @Override |
|---|
| 98 | 115 | public int getTechRequired() { |
|---|
| 99 | | return data.getInt("techreq"); |
|---|
| | 116 | return techReq; |
|---|
| 100 | 117 | } |
|---|
| 101 | 118 | |
|---|
| 102 | 119 | @Override |
|---|
| 103 | 120 | public int getEPS() { |
|---|
| 104 | | return data.getInt("eps"); |
|---|
| | 121 | return eps; |
|---|
| 105 | 122 | } |
|---|
| 106 | 123 | |
|---|
| r283e6c7 |
r6760ac9 |
|
| 92 | 92 | Alle Gebaeude deaktivieren |
|---|
| 93 | 93 | */ |
|---|
| 94 | | Core core = Core.getCore(db, base.getCore()); |
|---|
| | 94 | Core core = Core.getCore(base.getCore()); |
|---|
| 95 | 95 | |
|---|
| 96 | 96 | if( (base.getCore() != 0) && base.isCoreActive() ) { |
|---|
| r4a172a3 |
r6760ac9 |
|
| 275 | 275 | //------------------ |
|---|
| 276 | 276 | if( base.getCore() > 0 ) { |
|---|
| 277 | | Core core = Core.getCore(db, base.getCore()); |
|---|
| | 277 | Core core = Core.getCore(base.getCore()); |
|---|
| 278 | 278 | t.set_var( "core.name", Common._plaintitle(core.getName()) ); |
|---|
| 279 | 279 | } |
|---|
| r283e6c7 |
r6760ac9 |
|
| 164 | 164 | SQLQuery coreID = db.query("SELECT id FROM cores ORDER BY name,astitype"); |
|---|
| 165 | 165 | while( coreID.next() ) { |
|---|
| 166 | | Core core = Core.getCore(db, coreID.getInt("id")); |
|---|
| | 166 | Core core = Core.getCore(coreID.getInt("id")); |
|---|
| 167 | 167 | if( !user.hasResearched(core.getTechRequired()) ) { |
|---|
| 168 | 168 | continue; |
|---|
| rb7af64d |
r6760ac9 |
|
| 93 | 93 | } |
|---|
| 94 | 94 | |
|---|
| 95 | | Core core = Core.getCore(db, build); |
|---|
| | 95 | Core core = Core.getCore(build); |
|---|
| 96 | 96 | if( core == null ) { |
|---|
| 97 | 97 | addError("Der angegebene Core-Typ existiert nicht", Common.buildUrl(getContext(), "default", "module", "base", "col", base.getID())); |
|---|
| … | … | |
| 187 | 187 | } |
|---|
| 188 | 188 | |
|---|
| 189 | | Core core = Core.getCore(db, base.getCore()); |
|---|
| | 189 | Core core = Core.getCore(base.getCore()); |
|---|
| 190 | 190 | |
|---|
| 191 | 191 | db.update("UPDATE bases " + |
|---|
| … | … | |
| 220 | 220 | } |
|---|
| 221 | 221 | |
|---|
| 222 | | Core core = Core.getCore(db, base.getCore()); |
|---|
| | 222 | Core core = Core.getCore(base.getCore()); |
|---|
| 223 | 223 | if( core.getArbeiter()+base.getArbeiter() > base.getBewohner() ) { |
|---|
| 224 | 224 | t.set_var( "core.message", "<span style=\"color:#ff0000\">Nicht genügend Arbeiter</span>" ); |
|---|
| … | … | |
| 245 | 245 | |
|---|
| 246 | 246 | private void showCore() { |
|---|
| 247 | | Database db = getDatabase(); |
|---|
| 248 | | TemplateEngine t = getTemplateEngine(); |
|---|
| 249 | | |
|---|
| 250 | | Core core = Core.getCore(db, base.getCore()); |
|---|
| | 247 | TemplateEngine t = getTemplateEngine(); |
|---|
| | 248 | |
|---|
| | 249 | Core core = Core.getCore(base.getCore()); |
|---|
| 251 | 250 | |
|---|
| 252 | 251 | t.set_var( "core.astitype", core.getAstiType(), |
|---|
| … | … | |
| 290 | 289 | SQLQuery coreID = db.query("SELECT id FROM cores WHERE astitype='",base.getKlasse(),"'"); |
|---|
| 291 | 290 | while( coreID.next() ) { |
|---|
| 292 | | Core core = Core.getCore(db, coreID.getInt("id")); |
|---|
| | 291 | Core core = Core.getCore(coreID.getInt("id")); |
|---|
| 293 | 292 | |
|---|
| 294 | 293 | if( !user.hasResearched(core.getTechRequired()) ) { |
|---|
| r283e6c7 |
r6760ac9 |
|
| 268 | 268 | SQLQuery coreRow = db.query("SELECT id FROM cores WHERE techreq=",this.research.getID()); |
|---|
| 269 | 269 | while( coreRow.next() ) { |
|---|
| 270 | | Core core = Core.getCore(db, coreRow.getInt("id")); |
|---|
| | 270 | Core core = Core.getCore(coreRow.getInt("id")); |
|---|
| 271 | 271 | |
|---|
| 272 | 272 | t.start_record(); |
|---|
| r29cd7e5 |
r6760ac9 |
|
| 20 | 20 | <!-- config_felsbrocken_systems --> |
|---|
| 21 | 21 | <!-- config_vacmodes --> |
|---|
| 22 | | <!-- cores [net.driftingsouls.ds2.server.bases.Core migrieren] --> |
|---|
| | 22 | <mapping class="net.driftingsouls.ds2.server.bases.Core" /> |
|---|
| | 23 | <mapping class="net.driftingsouls.ds2.server.bases.DefaultCore" /> |
|---|
| 23 | 24 | <mapping class="net.driftingsouls.ds2.server.entities.FactionOffer" /> |
|---|
| 24 | 25 | <mapping class="net.driftingsouls.ds2.server.entities.FactionShopEntry" /> |
|---|