20 |
20 |
21 import java.io.File; |
21 import java.io.File; |
22 import java.io.IOException; |
22 import java.io.IOException; |
23 import java.util.ArrayList; |
23 import java.util.ArrayList; |
24 import java.util.Collections; |
24 import java.util.Collections; |
|
25 import java.util.Comparator; |
25 import java.util.List; |
26 import java.util.List; |
26 |
27 |
|
28 import org.hedgewars.hedgeroid.Utils; |
27 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports; |
29 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports; |
28 import org.hedgewars.hedgeroid.frontlib.Flib; |
30 import org.hedgewars.hedgeroid.frontlib.Flib; |
29 import org.hedgewars.hedgeroid.frontlib.Frontlib.TeamPtr; |
31 import org.hedgewars.hedgeroid.frontlib.Frontlib.TeamPtr; |
30 |
32 |
31 import android.util.Log; |
33 import android.content.Context; |
32 |
34 |
33 public final class Team { |
35 public final class Team { |
34 public static final String DIRECTORY_TEAMS = "teams"; |
36 public static final String DIRECTORY_TEAMS = "teams"; |
35 |
37 |
36 public static final int maxNumberOfHogs = PascalExports.HWgetMaxNumberOfHogs(); |
38 public static final int HEDGEHOGS_PER_TEAM = Flib.INSTANCE.flib_get_hedgehogs_per_team(); |
37 public static final int maxNumberOfTeams = PascalExports.HWgetMaxNumberOfTeams(); |
39 public static final int maxNumberOfTeams = PascalExports.HWgetMaxNumberOfTeams(); |
38 |
40 |
39 public final String name, grave, flag, voice, fort; |
41 public final String name, grave, flag, voice, fort; |
40 public final List<Hog> hogs; |
42 public final List<Hog> hogs; |
41 |
43 |
42 public Team(String name, String grave, String flag, String voice, String fort, List<Hog> hogs) { |
44 public Team(String name, String grave, String flag, String voice, String fort, List<Hog> hogs) { |
43 if(hogs.size() != maxNumberOfHogs) { |
45 if(hogs.size() != HEDGEHOGS_PER_TEAM) { |
44 throw new IllegalArgumentException("A team must consist of "+maxNumberOfHogs+" hogs."); |
46 throw new IllegalArgumentException("A team must consist of "+HEDGEHOGS_PER_TEAM+" hogs."); |
45 } |
47 } |
46 this.name = name; |
48 this.name = name; |
47 this.grave = grave; |
49 this.grave = grave; |
48 this.flag = flag; |
50 this.flag = flag; |
49 this.voice = voice; |
51 this.voice = voice; |
50 this.fort = fort; |
52 this.fort = fort; |
51 this.hogs = Collections.unmodifiableList(new ArrayList<Hog>(hogs)); |
53 this.hogs = Collections.unmodifiableList(new ArrayList<Hog>(hogs)); |
52 } |
54 } |
53 |
55 |
54 public void save(File f) throws IOException { |
56 public void save(File f) throws IOException { |
55 Log.d("Team", "saving to "+f.getAbsolutePath()); |
|
56 TeamPtr teamPtr = TeamPtr.createJavaOwned(this); |
57 TeamPtr teamPtr = TeamPtr.createJavaOwned(this); |
57 if(Flib.INSTANCE.flib_team_to_ini(f.getAbsolutePath(), teamPtr) != 0) { |
58 if(Flib.INSTANCE.flib_team_to_ini(f.getAbsolutePath(), teamPtr) != 0) { |
58 throw new IOException("Error saving team "+name); |
59 throw new IOException("Error saving team "+name); |
59 } |
60 } |
60 } |
61 } |
67 return team; |
68 return team; |
68 } else { |
69 } else { |
69 return null; |
70 return null; |
70 } |
71 } |
71 } |
72 } |
72 |
73 |
|
74 public static File getTeamfileByName(Context c, String teamName) { |
|
75 return new File(new File(c.getFilesDir(), DIRECTORY_TEAMS), Utils.replaceBadChars(teamName)+".hwt"); |
|
76 } |
|
77 |
73 @Override |
78 @Override |
74 public String toString() { |
79 public String toString() { |
75 return "Team [name=" + name + ", grave=" + grave + ", flag=" + flag |
80 return "Team [name=" + name + ", grave=" + grave + ", flag=" + flag |
76 + ", voice=" + voice + ", fort=" + fort + ", hogs=" + hogs |
81 + ", voice=" + voice + ", fort=" + fort + ", hogs=" + hogs |
77 + "]"; |
82 + "]"; |
78 } |
83 } |
|
84 |
|
85 public static Comparator<Team> NAME_ORDER = new Comparator<Team>() { |
|
86 public int compare(Team lhs, Team rhs) { |
|
87 return lhs.name.compareToIgnoreCase(rhs.name); |
|
88 } |
|
89 }; |
79 } |
90 } |