From b53166cca04b24bb81abc92b934fcde3cedb9fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Hord=C3=A9?= Date: Sat, 16 Jan 2016 01:57:24 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20modification=20du=20code=20pour=20foncti?= =?UTF-8?q?onnement=20de=20la=20base=20sous=20android=20correction=20de=20?= =?UTF-8?q?bogue=20sur=20la=20base=20de=20donn=C3=A9e=20animation=20du=20m?= =?UTF-8?q?enu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/fr/evolving/UI/ServerList.java | 8 +- core/src/fr/evolving/UI/Worldlist.java | 10 +- core/src/fr/evolving/assets/AssetLoader.java | 17 +- .../fr/evolving/database/DatabaseManager.java | 14 +- core/src/fr/evolving/database/LocalBase.java | 512 +++++++++--------- core/src/fr/evolving/screens/LevelScreen.java | 196 +++++-- 6 files changed, 456 insertions(+), 301 deletions(-) diff --git a/core/src/fr/evolving/UI/ServerList.java b/core/src/fr/evolving/UI/ServerList.java index e94b10d..6fc52f6 100644 --- a/core/src/fr/evolving/UI/ServerList.java +++ b/core/src/fr/evolving/UI/ServerList.java @@ -61,10 +61,11 @@ public class ServerList extends List { Gdx.net.sendHttpRequest (httpGet, new HttpResponseListener() { public void handleHttpResponse(HttpResponse httpResponse) { - String Response = ""; - Array resultxml; - Array resultstring=new Array(); if (httpResponse.getStatus().getStatusCode()==200) + { + String Response = ""; + Array resultxml; + Array resultstring=new Array(); Response = httpResponse.getResultAsString(); XmlReader xml = new XmlReader(); XmlReader.Element xml_element = xml.parse(Response); @@ -79,6 +80,7 @@ public class ServerList extends List { ServerList.this.setSelectedIndex(resultstring.indexOf(old, false)); if (list!=null && ServerList.this.model==Base.datatype.gamedata) list.Refresh(); + } } @Override public void failed(Throwable t) { diff --git a/core/src/fr/evolving/UI/Worldlist.java b/core/src/fr/evolving/UI/Worldlist.java index f5eab53..6edbc47 100644 --- a/core/src/fr/evolving/UI/Worldlist.java +++ b/core/src/fr/evolving/UI/Worldlist.java @@ -1,5 +1,6 @@ package fr.evolving.UI; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.scenes.scene2d.ui.List; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.utils.Array; @@ -15,10 +16,11 @@ public class Worldlist extends List { } public void Refresh() { - Array worlds=AssetLoader.Datahandler.game().getworlds(); - String world=Preference.prefs.getString("world"); - if (!worlds.contains(world, false)) - worlds.add(world); + Array worlds=null; + if (AssetLoader.Datahandler.game()!=null) + worlds=AssetLoader.Datahandler.game().getworlds(); + if (worlds==null) + worlds=new Array(); this.setItems(worlds); } } diff --git a/core/src/fr/evolving/assets/AssetLoader.java b/core/src/fr/evolving/assets/AssetLoader.java index d2efa6c..51bd247 100644 --- a/core/src/fr/evolving/assets/AssetLoader.java +++ b/core/src/fr/evolving/assets/AssetLoader.java @@ -205,12 +205,21 @@ public class AssetLoader { Datahandler.RegisterBackend(SqlBase.class); Databasemanagerfrompref(); } - + public static void Databasemanagerfrompref() { Datahandler.CloseAll(); - Datahandler.Attach(datatype.userdata, Preference.prefs.getString("userdata")); - Datahandler.Attach(datatype.statdata, Preference.prefs.getString("statdata")); - Datahandler.Attach(datatype.gamedata, Preference.prefs.getString("gamedata")); + if (Datahandler.Attach(datatype.userdata, Preference.prefs.getString("userdata"))) + Gdx.app.debug("AssetLoader","Base user ok"); + else + Gdx.app.debug("AssetLoader","Base user erreur"); + if (Datahandler.Attach(datatype.statdata, Preference.prefs.getString("statdata"))) + Gdx.app.debug("AssetLoader","Base stat ok"); + else + Gdx.app.debug("AssetLoader","Base stat erreur"); + if (Datahandler.Attach(datatype.gamedata, Preference.prefs.getString("gamedata"))) + Gdx.app.debug("AssetLoader","Base jeu ok"); + else + Gdx.app.debug("AssetLoader","Base jeu erreur"); } public static Transmuter getTransmuter(String Name) { diff --git a/core/src/fr/evolving/database/DatabaseManager.java b/core/src/fr/evolving/database/DatabaseManager.java index b6895f9..1fe561a 100644 --- a/core/src/fr/evolving/database/DatabaseManager.java +++ b/core/src/fr/evolving/database/DatabaseManager.java @@ -25,6 +25,10 @@ public class DatabaseManager { return bases[Base.datatype.gamedata.ordinal()]; } + public boolean verifyall() { + return (bases[0]!=null && bases[1]!=null && bases[2]!=null); + } + public DatabaseManager(){ bases=new Base[3]; old=new String[3]; @@ -53,7 +57,11 @@ public class DatabaseManager { return true; } else + { + bases[model.ordinal()]=null; + old[model.ordinal()]=null; return false; + } } public Base getBackend(Base.datatype model, String Url) { @@ -64,9 +72,11 @@ public class DatabaseManager { for(Class classe:backends) { Base back; try { - back = (Base) classe.getDeclaredConstructor(cArg).newInstance(model,Url); - if (back.getprefix().equals(Type)) + back = (Base) classe.newInstance(); + if (back.getprefix().equals(Type)) { + back = (Base) classe.getDeclaredConstructor(cArg).newInstance(model,Url); return back; + } } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { diff --git a/core/src/fr/evolving/database/LocalBase.java b/core/src/fr/evolving/database/LocalBase.java index 86651a2..90c69f5 100644 --- a/core/src/fr/evolving/database/LocalBase.java +++ b/core/src/fr/evolving/database/LocalBase.java @@ -20,6 +20,7 @@ import com.badlogic.gdx.sql.DatabaseCursor; import com.badlogic.gdx.sql.DatabaseFactory; import com.badlogic.gdx.sql.SQLiteGdxException; import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.Base64Coder; import fr.evolving.assets.AssetLoader; import fr.evolving.automata.Grid; @@ -30,203 +31,214 @@ import fr.evolving.database.Base.datatype; public class LocalBase extends Base { private static Database dbHandler; private String databasename = "base.db"; - private String creation; - private String param; - - //Contructeur de la base de donnée - - public String getParam() { - return this.param; - } - - public LocalBase() { - } - - public LocalBase(datatype model,String param) { - super(model,param); - String[] params=param.split(":"); - this.param=param; - if (params.length>1) - databasename=params[1]; - switch(Gdx.app.getType()) { - case Android: - try { - FileChannel source = ((FileInputStream) Gdx.files.internal("bases/"+databasename).read()).getChannel(); - FileChannel destination = new FileOutputStream("/data/data/fr.evolving.game.android/databases/"+databasename).getChannel(); - destination.transferFrom(source, 0, source.size()); - source.close(); - destination.close(); + private String creation; + private String param; + + //Contructeur de la base de donnée + + public String getParam() { + return this.param; + } + + public LocalBase() { + } + + public LocalBase(datatype model,String param) { + super(model,param); + String[] params=param.split(":"); + this.param=param; + if (params.length>1) + databasename=params[1]; + switch(Gdx.app.getType()) { + case Android: + try { + if (!Gdx.files.absolute("/data/data/fr.evolving.game.android/databases/"+databasename).exists()) { + Gdx.app.log("Base", "Copie de la base de donnee android"); + byte[] ByteSource = Gdx.files.internal("bases/"+databasename).readBytes(); + FileOutputStream destination = new FileOutputStream("/data/data/fr.evolving.game.android/databases/"+databasename); + destination.write(ByteSource); + destination.close(); + } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } - case Desktop: - FileHandle newbase=Gdx.files.local(databasename); - if (!newbase.exists()) - Gdx.files.internal("bases/"+databasename).copyTo(newbase); - } - if (dbHandler!=null) - Gdx.app.log("Local", "Reprise de la base '"+databasename+"', table:"+model.toString()); - else - { - Gdx.app.log("Local", "Utilisation de la base '"+databasename+"', table:"+model.toString()); - dbHandler = DatabaseFactory.getNewDatabase(databasename,1, "", null); - dbHandler.setupDatabase(); + break; + case Desktop: + Gdx.app.log("Base", "Copie de la base de donnee desktop"); + FileHandle newbase=Gdx.files.local(databasename); + try { + if (!newbase.exists()) + Gdx.files.internal("bases/"+databasename).copyTo(newbase); + } catch (Exception e1) { + e1.printStackTrace(); + } + break; + } + if (dbHandler!=null) + Gdx.app.log("Local", "Reprise de la base '"+databasename+"', table:"+model.toString()); + else + { + Gdx.app.log("Local", "Utilisation de la base '"+databasename+"', table:"+model.toString()); + dbHandler = DatabaseFactory.getNewDatabase(databasename,1, null, null); + dbHandler.setupDatabase(); - try { - dbHandler.openOrCreateDatabase(); - } - catch (SQLiteGdxException e) { - e.printStackTrace(); - } - } - try { - if (model==datatype.statdata) - creation = "create table if not exists stat (id integer)"; - else if (model==datatype.userdata) { - dbHandler.execSQL("CREATE TABLE if not exists locks(date DATETIME DEFAULT CURRENT_TIMESTAMP, level INTEGER NOT NULL, user INTEGER NOT NULL, PRIMARY KEY(level,user));"); - dbHandler.execSQL("CREATE TABLE if not exists grids(date DATETIME DEFAULT CURRENT_TIMESTAMP, level INTEGER NOT NULL, user INTEGER NOT NULL, object TEXT, PRIMARY KEY(level,user,date));"); - dbHandler.execSQL("CREATE TABLE if not exists transmuters(date DATETIME DEFAULT CURRENT_TIMESTAMP, user INTEGER NOT NULL, object TEXT, PRIMARY KEY(user));"); - dbHandler.execSQL("CREATE TABLE if not exists research(date DATETIME DEFAULT CURRENT_TIMESTAMP, user INTEGER NOT NULL, value INT, PRIMARY KEY(user));"); - - } - else - dbHandler.execSQL("CREATE TABLE if not exists worlds(date DATETIME DEFAULT CURRENT_TIMESTAMP, desc TEXT NOT NULL, object TEXT, PRIMARY KEY(desc));"); - } catch (SQLiteGdxException e) { - e.printStackTrace(); - } - } - - //Gestion model type gamedata + try { + dbHandler.openOrCreateDatabase(); + } + catch (SQLiteGdxException e) { + e.printStackTrace(); + Gdx.app.log("Local", "Erreur à l'ouverture de la base"); + } + } + try { + if (model==datatype.statdata) + creation = "create table if not exists stat (id integer)"; + else if (model==datatype.userdata) { + dbHandler.execSQL("CREATE TABLE if not exists locks(date DATETIME DEFAULT CURRENT_TIMESTAMP, level INTEGER NOT NULL, user INTEGER NOT NULL, PRIMARY KEY(level,user));"); + dbHandler.execSQL("CREATE TABLE if not exists grids(date DATETIME DEFAULT CURRENT_TIMESTAMP, level INTEGER NOT NULL, user INTEGER NOT NULL, object TEXT, PRIMARY KEY(level,user,date));"); + dbHandler.execSQL("CREATE TABLE if not exists transmuters(date DATETIME DEFAULT CURRENT_TIMESTAMP, user INTEGER NOT NULL, object TEXT, PRIMARY KEY(user));"); + dbHandler.execSQL("CREATE TABLE if not exists research(date DATETIME DEFAULT CURRENT_TIMESTAMP, user INTEGER NOT NULL, value INT, PRIMARY KEY(user));"); + } + else + dbHandler.execSQL("CREATE TABLE if not exists worlds(date DATETIME DEFAULT CURRENT_TIMESTAMP, desc TEXT NOT NULL, object TEXT, PRIMARY KEY(desc));"); + } catch (SQLiteGdxException e) { + e.printStackTrace(); + } + } - public Array getworlds() { - DatabaseCursor cursor=null; - try { - cursor = dbHandler.rawQuery("select desc,date from worlds;"); - } catch (SQLiteGdxException e) { - return null; - } - Array returnvalue=new Array(); - while (cursor.next()) - returnvalue.add(cursor.getString(0)); - return returnvalue; - } + //Gestion model type gamedata + + public Array getworlds() { + DatabaseCursor cursor=null; + try { + cursor = dbHandler.rawQuery("select desc,date from worlds;"); + } catch (SQLiteGdxException e) { + return null; + } + Array returnvalue=new Array(); + while (cursor.next()) + returnvalue.add(cursor.getString(0)); + return returnvalue; + } + + public Array getworld(String description) { + DatabaseCursor cursor=null; + try { + cursor = dbHandler.rawQuery("select object from worlds where desc='"+description+"';"); + } catch (SQLiteGdxException e) { + return null; + } + Level[] mc=null; + if (cursor.next()) + try { + Gdx.app.debug(getClass().getSimpleName(),"TEST2"); + byte[] bytes = Base64Coder.decodeLines(cursor.getString(0)); + Gdx.app.debug(getClass().getSimpleName(),"TEST3"); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + ObjectInputStream ins = new ObjectInputStream(bais); + mc=(Level[]) ins.readObject(); + ins.close(); + return new Array(mc); + } + catch (Exception e) { + e.printStackTrace(); + } + return null; + } - public Array getworld(String description) { - DatabaseCursor cursor=null; - try { - cursor = dbHandler.rawQuery("select object from worlds where desc='"+description+"';"); - } catch (SQLiteGdxException e) { - return null; - } - Level[] mc=null; - if (cursor.next()) - try { - byte[] bytes = DatatypeConverter.parseBase64Binary(cursor.getString(0)); - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - ObjectInputStream ins = new ObjectInputStream(bais); - mc=(Level[]) ins.readObject(); - ins.close(); - return new Array(mc); - } - catch (Exception e) { - e.printStackTrace(); - } - return null; - } - public boolean deleteworld(String description) { - try { - dbHandler.rawQuery("delete from worlds where desc='"+description+"';"); - } catch (SQLiteGdxException e) { - return false; - } - return true; - } + try { + dbHandler.rawQuery("delete from worlds where desc='"+description+"';"); + } catch (SQLiteGdxException e) { + return false; + } + return true; + } + + public boolean setworld(Array world, String description) { + String encoded = ""; + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(world.toArray()); + oos.flush(); + oos.close(); + bos.close(); + byte[] bytes = bos.toByteArray(); + encoded = Base64Coder.encodeLines(bytes); + dbHandler.rawQuery("replace into worlds (desc,object) values ('"+description+"','"+encoded+"');"); + } catch (Exception e) { + return false; + } + return true; + } - public boolean setworld(Array world, String description) { - String encoded = ""; - try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(world.toArray()); - oos.flush(); - oos.close(); - bos.close(); - byte[] bytes = bos.toByteArray(); - encoded = DatatypeConverter.printBase64Binary(bytes); - dbHandler.rawQuery("replace into worlds (desc,object) values ('"+description+"','"+encoded+"');"); - } catch (Exception e) { - return false; - } - return true; - } - //Gestion de données type userdata - + public boolean getlevellock(int user,int level) { DatabaseCursor cursor=null; - try { - cursor=dbHandler.rawQuery("select user from locks where user="+user+" and level="+level+";"); - } catch (SQLiteGdxException e) { - return false; - } - if (cursor.next()) - return true; - else return false; - } - + try { + cursor=dbHandler.rawQuery("select user from locks where user="+user+" and level="+level+";"); + } catch (SQLiteGdxException e) { + return false; + } + if (cursor.next()) + return true; + else return false; + } + public boolean setlevelunlock(int user,int level){ try { dbHandler.rawQuery("insert into locks (user,level) values ("+user+","+level+");"); } catch (SQLiteGdxException e) { - return false; + return false; } return true; } - + public Array getTransmuters(int user){ - DatabaseCursor cursor=null; - try { - cursor = dbHandler.rawQuery("select object from transmuters where user="+user+";"); - } catch (SQLiteGdxException e) { - return null; - } - Transmuter[] mc=null; - if (cursor.next()) - try { - byte[] bytes = DatatypeConverter.parseBase64Binary(cursor.getString(0)); - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - ObjectInputStream ins = new ObjectInputStream(bais); - mc=(Transmuter[]) ins.readObject(); - ins.close(); - return new Array(mc); - } - catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public boolean setTransmuters(int user,Array transmuters){ - String encoded = ""; - try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(transmuters.toArray()); - oos.flush(); - oos.close(); - bos.close(); - byte[] bytes = bos.toByteArray(); - encoded = DatatypeConverter.printBase64Binary(bytes); - dbHandler.rawQuery("replace into transmuters (user,object) values ("+user+",'"+encoded+"');"); - } catch (Exception e) { - return false; - } - return true; + DatabaseCursor cursor=null; + try { + cursor = dbHandler.rawQuery("select object from transmuters where user="+user+";"); + } catch (SQLiteGdxException e) { + return null; + } + Transmuter[] mc=null; + if (cursor.next()) + try { + byte[] bytes = DatatypeConverter.parseBase64Binary(cursor.getString(0)); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + ObjectInputStream ins = new ObjectInputStream(bais); + mc=(Transmuter[]) ins.readObject(); + ins.close(); + return new Array(mc); + } + catch (Exception e) { + e.printStackTrace(); + } + return null; } - - + + public boolean setTransmuters(int user,Array transmuters){ + String encoded = ""; + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(transmuters.toArray()); + oos.flush(); + oos.close(); + bos.close(); + byte[] bytes = bos.toByteArray(); + encoded = DatatypeConverter.printBase64Binary(bytes); + dbHandler.rawQuery("replace into transmuters (user,object) values ("+user+",'"+encoded+"');"); + } catch (Exception e) { + return false; + } + return true; + } + + public int getResearchpoint(int user){ DatabaseCursor cursor=null; try { @@ -239,88 +251,88 @@ public class LocalBase extends Base { else return 0; } - + public boolean setResearchpoint(int user, int point){ - try { - dbHandler.rawQuery("replace into research (user,value) values ("+user+","+point+");"); - } catch (Exception e) { - return false; - } - return true; + try { + dbHandler.rawQuery("replace into research (user,value) values ("+user+","+point+");"); + } catch (Exception e) { + return false; + } + return true; } - + public Grid getGrid(int user,int level,int place){ - DatabaseCursor cursor=null; - try { - cursor = dbHandler.rawQuery("select object from grids where user="+user+" and level="+level+" order by date desc limit "+place+",1;"); - } catch (SQLiteGdxException e) { - return null; - } - Grid mc=null; - if (cursor.next()) - try { - byte[] bytes = DatatypeConverter.parseBase64Binary(cursor.getString(0)); - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - ObjectInputStream ins = new ObjectInputStream(bais); - mc=(Grid) ins.readObject(); - ins.close(); - return mc; - } - catch (Exception e) { - e.printStackTrace(); - } - return null; - } - + DatabaseCursor cursor=null; + try { + cursor = dbHandler.rawQuery("select object from grids where user="+user+" and level="+level+" order by date desc limit "+place+",1;"); + } catch (SQLiteGdxException e) { + return null; + } + Grid mc=null; + if (cursor.next()) + try { + byte[] bytes = DatatypeConverter.parseBase64Binary(cursor.getString(0)); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + ObjectInputStream ins = new ObjectInputStream(bais); + mc=(Grid) ins.readObject(); + ins.close(); + return mc; + } + catch (Exception e) { + e.printStackTrace(); + } + return null; + } + public boolean setGrid(int user,int level, Grid data){ - String encoded = ""; - try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(data); - oos.flush(); - oos.close(); - bos.close(); - byte[] bytes = bos.toByteArray(); - encoded = DatatypeConverter.printBase64Binary(bytes); - dbHandler.rawQuery("insert into grids (user,level,object) values ("+user+","+level+",'"+encoded+"');"); - } catch (Exception e) { - return false; - } - return true; + String encoded = ""; + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(data); + oos.flush(); + oos.close(); + bos.close(); + byte[] bytes = bos.toByteArray(); + encoded = DatatypeConverter.printBase64Binary(bytes); + dbHandler.rawQuery("insert into grids (user,level,object) values ("+user+","+level+",'"+encoded+"');"); + } catch (Exception e) { + return false; + } + return true; } public Array getGrids(int user, int level){ - DatabaseCursor cursor=null; - try { - cursor = dbHandler.rawQuery("select date from grids where level="+level+" and user="+user+" order by date desc;"); - } catch (SQLiteGdxException e) { - return null; - } - Array returnvalue=new Array(); - while (cursor.next()) - returnvalue.add(cursor.getString(0)); - return returnvalue; - } - + DatabaseCursor cursor=null; + try { + cursor = dbHandler.rawQuery("select date from grids where level="+level+" and user="+user+" order by date desc;"); + } catch (SQLiteGdxException e) { + return null; + } + Array returnvalue=new Array(); + while (cursor.next()) + returnvalue.add(cursor.getString(0)); + return returnvalue; + } + //Gestion type Stat - - + + //Commun - + public boolean Eraseall(datatype base){ - try { - dbHandler.execSQL("drop table if exists stat;"); - dbHandler.execSQL("drop table if exists locks;"); - dbHandler.execSQL("drop table if exists grids;"); - dbHandler.execSQL("drop table if exists global;"); - dbHandler.execSQL("drop table if exists worlds;"); - return true; - } catch (SQLiteGdxException e1) { - return false; + try { + dbHandler.execSQL("drop table if exists stat;"); + dbHandler.execSQL("drop table if exists locks;"); + dbHandler.execSQL("drop table if exists grids;"); + dbHandler.execSQL("drop table if exists global;"); + dbHandler.execSQL("drop table if exists worlds;"); + return true; + } catch (SQLiteGdxException e1) { + return false; + } } - } - + public void Close() { try { if (dbHandler!=null) { @@ -332,12 +344,12 @@ public class LocalBase extends Base { e.printStackTrace(); } } - + public String getprefix() { return "local"; } - - public static boolean isHandling(datatype base){ + + public static boolean isHandling(datatype base){ return true; } diff --git a/core/src/fr/evolving/screens/LevelScreen.java b/core/src/fr/evolving/screens/LevelScreen.java index aceb61a..318c55f 100644 --- a/core/src/fr/evolving/screens/LevelScreen.java +++ b/core/src/fr/evolving/screens/LevelScreen.java @@ -7,7 +7,10 @@ import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; +import com.badlogic.gdx.scenes.scene2d.actions.Actions; +import com.badlogic.gdx.scenes.scene2d.actions.Actions.*; import com.badlogic.gdx.scenes.scene2d.ui.Button; +import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.ImageButton; import com.badlogic.gdx.scenes.scene2d.ui.List; import com.badlogic.gdx.scenes.scene2d.ui.Label; @@ -30,6 +33,7 @@ import fr.evolving.renderers.LevelRenderer; import fr.evolving.UI.ButtonLevel; import fr.evolving.UI.Objectives; import fr.evolving.UI.ServerList; +import fr.evolving.UI.WarnDialog; import fr.evolving.UI.Worldlist; import fr.evolving.game.main; @@ -55,8 +59,10 @@ public class LevelScreen implements Screen { private TimerTask ScrollTask; private Stage stage; private Table table; + private WarnDialog dialog; private ImageButton Previous,Next,Exit; - public ImageButton logosmall,MenuSolo,MenuMulti,MenuScenario; + public ImageButton logosmall; + public Image MenuSolo,MenuMulti,MenuScenario; private ImageTextButton cout,tech,cycle,temp,rayon,nrj; private TextButton buttonConnect,buttonPlay,buttonStat,buttonSave, buttonApply, buttonPlaythis; private ServerList Statdata,Userdata,Gamedata; @@ -79,22 +85,27 @@ public class LevelScreen implements Screen { public void play() { //thelevels= InitWorlds.go(); - Gdx.app.debug(getClass().getSimpleName(),"Chargement des mondes depuis la base."); - try { - if (world<0) - world=0; - thelevels=AssetLoader.Datahandler.game().getworld(Preference.prefs.getString("world")); - loadWorld(world); - Previous.setVisible(true); - Next.setVisible(true); - buttonPlay.setVisible(true); - TextDescriptive.setVisible(true); - } - catch (Exception e) { - Previous.setVisible(false); - Next.setVisible(false); - buttonPlay.setVisible(false); - TextDescriptive.setVisible(false); + if (!AssetLoader.Datahandler.verifyall()) + Gdx.app.debug(getClass().getSimpleName(),"Pilotes de bases de donnée défaillant."); + else + { + Gdx.app.debug(getClass().getSimpleName(),"Chargement des mondes depuis la base."); + try { + if (world<0) + world=0; + thelevels=AssetLoader.Datahandler.game().getworld(Preference.prefs.getString("world")); + loadWorld(world); + Previous.setVisible(true); + Next.setVisible(true); + buttonPlay.setVisible(true); + TextDescriptive.setVisible(true); + } + catch (Exception e) { + Previous.setVisible(false); + Next.setVisible(false); + buttonPlay.setVisible(false); + TextDescriptive.setVisible(false); + } } } @@ -128,12 +139,44 @@ public class LevelScreen implements Screen { Worlddata.setVisible(false); buttonSave.setVisible(false); buttonApply.setVisible(false); + initlevel(); + MenuSolo.setRotation(0); + MenuSolo.setScale(1f); + MenuSolo.setColor(1f, 1f, 1f, 1f); + MenuSolo.setPosition(0, AssetLoader.height*17/20-300); + MenuMulti.setRotation(0); + MenuMulti.setScale(1f); + MenuMulti.setColor(1f, 1f, 1f, 1f); + MenuMulti.setPosition(0, AssetLoader.height*12/20-300); + MenuScenario.setRotation(0); + MenuScenario.setScale(1f); + MenuScenario.setColor(1f, 1f, 1f, 1f); + MenuScenario.setPosition(0, AssetLoader.height*7/20-300); + MenuSolo.addAction(Actions.sequence(Actions.moveTo((AssetLoader.width-MenuSolo.getWidth())/2, AssetLoader.height*17/20-300, 0.25f))); + MenuMulti.addAction(Actions.sequence(Actions.fadeIn(0.1f),Actions.moveTo((AssetLoader.width-MenuMulti.getWidth())/2, AssetLoader.height*12/20-300, 0.25f))); + MenuScenario.addAction(Actions.sequence(Actions.fadeIn(0.2f),Actions.moveTo((AssetLoader.width-MenuScenario.getWidth())/2, AssetLoader.height*7/20-300, 0.25f))); + + } + + public void initlevel() { + selected=null; + buttonPlay.setVisible(false); + TextDescriptive.setVisible(false); + cout.setVisible(false); + tech.setVisible(false); + cycle.setVisible(false); + temp.setVisible(false); + rayon.setVisible(false); + nrj.setVisible(false); + Previous.setVisible(false); + Next.setVisible(false); + Victory.setVisible(false); if (buttonLevels!=null) - for (int j=0;j<10;j++) - if (buttonLevels[j]!=null) { - buttonLevels[j].remove(); - buttonLevels[j]=null; - } + for (int j=0;j<10;j++) + if (buttonLevels[j]!=null) { + buttonLevels[j].remove(); + buttonLevels[j]=null; + } } public void level() { @@ -149,7 +192,7 @@ public class LevelScreen implements Screen { } public void SetButtonConnect() { - buttonStat.setColor(buttonConnect.getColor()); + buttonStat.setColor(1f,1f,1f,1f); buttonConnect.setColor(1f,0,0,1f); Statdata.setVisible(true); Userdata.setVisible(true); @@ -161,11 +204,12 @@ public class LevelScreen implements Screen { buttonApply.setVisible(true); Worlddatalabel.setVisible(true); Worlddata.setVisible(true); - buttonPlaythis.setVisible(true); + buttonPlaythis.setVisible(true); } public void SetButtonStat() { - buttonConnect.setColor(buttonStat.getColor()); + buttonConnect.setColor(1f,1f,1f,1f); + buttonStat.setColor(1f,0,0,1f); buttonStat.setColor(1f,0,0,1f); Statdata.setVisible(false); Userdata.setVisible(false); @@ -235,6 +279,7 @@ public class LevelScreen implements Screen { stage = new Stage(AssetLoader.viewport); table = new Table(); Renderer=new LevelRenderer(this); + dialog=new WarnDialog(AssetLoader.Skin_ui); Gdx.app.debug(getClass().getSimpleName(),"Mise en place du timer."); ScrollTimer=new Timer(); ScrollTask = new TimerTask() @@ -247,17 +292,48 @@ public class LevelScreen implements Screen { }; ScrollTimer.scheduleAtFixedRate(ScrollTask, 0, 30); Gdx.app.debug(getClass().getSimpleName(),"Création du menu."); - MenuSolo=new ImageButton(AssetLoader.Skin_level,"MenuSolo"); - MenuSolo.setPosition((AssetLoader.width-MenuSolo.getWidth())/2, AssetLoader.height-550); + MenuSolo=new Image(AssetLoader.Skin_level,"menu1"); + MenuSolo.setOrigin(MenuSolo.getWidth()/2, MenuSolo.getHeight()/2); MenuSolo.addListener(new ClickListener() { public void clicked(InputEvent event, float x, float y) { - level(); + MenuMulti.addAction(Actions.fadeOut(0.5f)); + MenuScenario.addAction(Actions.fadeOut(0.5f)); + MenuSolo.addAction(Actions.sequence(Actions.parallel(Actions.rotateBy(640, 0.5f),Actions.scaleTo(0.05f, 0.05f, 0.5f)),Actions.run(new Runnable() { + public void run() { + level(); + } + }))); + + } + }); + MenuMulti=new Image(AssetLoader.Skin_level,"menu2"); + MenuMulti.setOrigin(MenuMulti.getWidth()/2, MenuMulti.getHeight()/2); + MenuMulti.addListener(new ClickListener() { + public void clicked(InputEvent event, float x, float y) { + MenuSolo.addAction(Actions.fadeOut(0.5f)); + MenuScenario.addAction(Actions.fadeOut(0.5f)); + MenuMulti.addAction(Actions.sequence(Actions.parallel(Actions.rotateBy(640, 0.5f),Actions.scaleTo(0.05f, 0.05f, 0.5f)),Actions.run(new Runnable() { + public void run() { + level(); + } + }))); + + } + }); + MenuScenario=new Image(AssetLoader.Skin_level,"menu3"); + MenuScenario.setOrigin(MenuScenario.getWidth()/2, MenuScenario.getHeight()/2); + MenuScenario.addListener(new ClickListener() { + public void clicked(InputEvent event, float x, float y) { + MenuMulti.addAction(Actions.fadeOut(0.5f)); + MenuSolo.addAction(Actions.fadeOut(0.5f)); + MenuScenario.addAction(Actions.sequence(Actions.parallel(Actions.rotateBy(640, 0.5f),Actions.scaleTo(0.05f, 0.05f, 0.5f)),Actions.run(new Runnable() { + public void run() { + level(); + } + }))); + } }); - MenuMulti=new ImageButton(AssetLoader.Skin_level,"MenuMulti"); - MenuMulti.setPosition((AssetLoader.width-MenuMulti.getWidth())/2, AssetLoader.height-900); - MenuScenario=new ImageButton(AssetLoader.Skin_level,"MenuScenario"); - MenuScenario.setPosition((AssetLoader.width-MenuScenario.getWidth())/2, AssetLoader.height-1250); Gdx.app.debug(getClass().getSimpleName(),"Création des boutons."); logosmall=new ImageButton(AssetLoader.Skin_level,"logosmall"); logosmall.setPosition(20, AssetLoader.height-175+logosmall.getHeight()/2); @@ -267,11 +343,29 @@ public class LevelScreen implements Screen { buttonApply.setBounds(1680, 350, 190, 40); buttonApply.addListener(new ClickListener() { public void clicked(InputEvent event, float x, float y) { - menu(); AssetLoader.Datahandler.CloseAll(); AssetLoader.Datahandler.Attach(Userdata.getModel(),Userdata.getUrl()); - AssetLoader.Datahandler.Attach(Gamedata.getModel(),Gamedata.getUrl()); AssetLoader.Datahandler.Attach(Statdata.getModel(),Statdata.getUrl()); + AssetLoader.Datahandler.Attach(Gamedata.getModel(),Gamedata.getUrl()); + if (!AssetLoader.Datahandler.verifyall()) { + dialog.Show("Un problème est survenu lors du changement de base de donnée.",stage); + initlevel(); + } + else + menu(); + if (AssetLoader.Datahandler.stat()==null) + Statdata.setColor(1f,0,0,1f); + else + Statdata.setColor(1f,1f,1f,1f); + if (AssetLoader.Datahandler.game()==null) + Gamedata.setColor(1f,0,0,1f); + else + Gamedata.setColor(1f,1f,1f,1f); + if (AssetLoader.Datahandler.user()==null) + Userdata.setColor(1f,0,0,1f); + else + Userdata.setColor(1f,1f,1f,1f); + Worlddata.Refresh(); } }); buttonSave = new TextButton("Sauvegarder", AssetLoader.Skin_ui); @@ -282,6 +376,7 @@ public class LevelScreen implements Screen { Preference.prefs.putString("userdata", Userdata.getUrl()); Preference.prefs.putString("gamedata", Gamedata.getUrl()); Preference.prefs.putString("statdata", Statdata.getUrl()); + dialog.Show("Vous devez redemarrer pour bénéfier des changements.",stage); } }); buttonConnect = new TextButton("Connexions", AssetLoader.Skin_ui); @@ -312,9 +407,19 @@ public class LevelScreen implements Screen { buttonPlaythis.setBounds(1480, 50, 190, 40); buttonPlaythis.addListener(new ClickListener() { public void clicked(InputEvent event, float x, float y) { - Preference.prefs.putString("world", (String)Worlddata.getSelected()); - Preference.prefs.flush(); - play(); + if (!AssetLoader.Datahandler.verifyall()) + dialog.Show("Impossible de jouer sans bases de données correctement configurée, cliquer sur RAZ si vous ne savez pas revenir à une situation jouable.", stage); + else + { + if (Worlddata.getSelected()==null) + dialog.Show("Aucun monde n'a été sélectionné", stage); + else + { + Preference.prefs.putString("world", (String)Worlddata.getSelected()); + Preference.prefs.flush(); + play(); + } + } } }); Exit=new ImageButton(AssetLoader.Skin_level,"Exit"); @@ -322,7 +427,10 @@ public class LevelScreen implements Screen { Exit.addListener(new ClickListener(){ @Override public void clicked(InputEvent event, float x, float y) { - Gdx.app.exit(); + if (Exit.getX()<1210) + menu(); + else + Gdx.app.exit(); } }); Next=new ImageButton(AssetLoader.Skin_level,"Next"); @@ -384,7 +492,19 @@ public class LevelScreen implements Screen { Worlddatalabel.setPosition(1480, 300); Gamedata.setWorldlist(Worlddata); Statdata.Refresh(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } Userdata.Refresh(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } Gamedata.Refresh(); Gdx.app.debug(getClass().getSimpleName(),"Affichage du menu."); if (aworld!=-1)