    001    package net.minecraft.src;
    002
    003    import argo.jdom.JdomParser;
    004    import argo.jdom.JsonNode;
    005    import argo.jdom.JsonRootNode;
    006    import argo.jdom.JsonStringNode;
    007    import argo.saj.InvalidSyntaxException;
    008    import cpw.mods.fml.common.Side;
    009    import cpw.mods.fml.common.asm.SideOnly;
    010    import java.io.File;
    011    import java.util.HashMap;
    012    import java.util.Iterator;
    013    import java.util.List;
    014    import java.util.Map;
    015    import java.util.Map.Entry;
    016
    017    @SideOnly(Side.CLIENT)
    018    public class StatFileWriter
    019    {
    020        private Map field_77457_a = new HashMap();
    021        private Map field_77455_b = new HashMap();
    022        private boolean field_77456_c = false;
    023        private StatsSyncher statsSyncher;
    024
    025        public StatFileWriter(Session par1Session, File par2File)
    026        {
    027            File var3 = new File(par2File, "stats");
    028
    029            if (!var3.exists())
    030            {
    031                var3.mkdir();
    032            }
    033
    034            File[] var4 = par2File.listFiles();
    035            int var5 = var4.length;
    036
    037            for (int var6 = 0; var6 < var5; ++var6)
    038            {
    039                File var7 = var4[var6];
    040
    041                if (var7.getName().startsWith("stats_") && var7.getName().endsWith(".dat"))
    042                {
    043                    File var8 = new File(var3, var7.getName());
    044
    045                    if (!var8.exists())
    046                    {
    047                        System.out.println("Relocating " + var7.getName());
    048                        var7.renameTo(var8);
    049                    }
    050                }
    051            }
    052
    053            this.statsSyncher = new StatsSyncher(par1Session, this, var3);
    054        }
    055
    056        public void readStat(StatBase par1StatBase, int par2)
    057        {
    058            this.writeStatToMap(this.field_77455_b, par1StatBase, par2);
    059            this.writeStatToMap(this.field_77457_a, par1StatBase, par2);
    060            this.field_77456_c = true;
    061        }
    062
    063        private void writeStatToMap(Map par1Map, StatBase par2StatBase, int par3)
    064        {
    065            Integer var4 = (Integer)par1Map.get(par2StatBase);
    066            int var5 = var4 == null ? 0 : var4.intValue();
    067            par1Map.put(par2StatBase, Integer.valueOf(var5 + par3));
    068        }
    069
    070        public Map func_77445_b()
    071        {
    072            return new HashMap(this.field_77455_b);
    073        }
    074
    075        /**
    076         * write a whole Map of stats to the statmap
    077         */
    078        public void writeStats(Map par1Map)
    079        {
    080            if (par1Map != null)
    081            {
    082                this.field_77456_c = true;
    083                Iterator var2 = par1Map.keySet().iterator();
    084
    085                while (var2.hasNext())
    086                {
    087                    StatBase var3 = (StatBase)var2.next();
    088                    this.writeStatToMap(this.field_77455_b, var3, ((Integer)par1Map.get(var3)).intValue());
    089                    this.writeStatToMap(this.field_77457_a, var3, ((Integer)par1Map.get(var3)).intValue());
    090                }
    091            }
    092        }
    093
    094        public void func_77452_b(Map par1Map)
    095        {
    096            if (par1Map != null)
    097            {
    098                Iterator var2 = par1Map.keySet().iterator();
    099
    100                while (var2.hasNext())
    101                {
    102                    StatBase var3 = (StatBase)var2.next();
    103                    Integer var4 = (Integer)this.field_77455_b.get(var3);
    104                    int var5 = var4 == null ? 0 : var4.intValue();
    105                    this.field_77457_a.put(var3, Integer.valueOf(((Integer)par1Map.get(var3)).intValue() + var5));
    106                }
    107            }
    108        }
    109
    110        public void func_77448_c(Map par1Map)
    111        {
    112            if (par1Map != null)
    113            {
    114                this.field_77456_c = true;
    115                Iterator var2 = par1Map.keySet().iterator();
    116
    117                while (var2.hasNext())
    118                {
    119                    StatBase var3 = (StatBase)var2.next();
    120                    this.writeStatToMap(this.field_77455_b, var3, ((Integer)par1Map.get(var3)).intValue());
    121                }
    122            }
    123        }
    124
    125        public static Map func_77453_b(String par0Str)
    126        {
    127            HashMap var1 = new HashMap();
    128
    129            try
    130            {
    131                String var2 = "local";
    132                StringBuilder var3 = new StringBuilder();
    133                JsonRootNode var4 = (new JdomParser()).parse(par0Str);
    134                List var5 = var4.getArrayNode(new Object[] {"stats-change"});
    135                Iterator var6 = var5.iterator();
    136
    137                while (var6.hasNext())
    138                {
    139                    JsonNode var7 = (JsonNode)var6.next();
    140                    Map var8 = var7.getFields();
    141                    Entry var9 = (Entry)var8.entrySet().iterator().next();
    142                    int var10 = Integer.parseInt(((JsonStringNode)var9.getKey()).getText());
    143                    int var11 = Integer.parseInt(((JsonNode)var9.getValue()).getText());
    144                    StatBase var12 = StatList.getOneShotStat(var10);
    145
    146                    if (var12 == null)
    147                    {
    148                        System.out.println(var10 + " is not a valid stat");
    149                    }
    150                    else
    151                    {
    152                        var3.append(StatList.getOneShotStat(var10).statGuid).append(",");
    153                        var3.append(var11).append(",");
    154                        var1.put(var12, Integer.valueOf(var11));
    155                    }
    156                }
    157
    158                MD5String var14 = new MD5String(var2);
    159                String var15 = var14.getMD5String(var3.toString());
    160
    161                if (!var15.equals(var4.getStringValue(new Object[] {"checksum"})))
    162                {
    163                    System.out.println("CHECKSUM MISMATCH");
    164                    return null;
    165                }
    166            }
    167            catch (InvalidSyntaxException var13)
    168            {
    169                var13.printStackTrace();
    170            }
    171
    172            return var1;
    173        }
    174
    175        public static String func_77441_a(String par0Str, String par1Str, Map par2Map)
    176        {
    177            StringBuilder var3 = new StringBuilder();
    178            StringBuilder var4 = new StringBuilder();
    179            boolean var5 = true;
    180            var3.append("{\r\n");
    181
    182            if (par0Str != null && par1Str != null)
    183            {
    184                var3.append("  \"user\":{\r\n");
    185                var3.append("    \"name\":\"").append(par0Str).append("\",\r\n");
    186                var3.append("    \"sessionid\":\"").append(par1Str).append("\"\r\n");
    187                var3.append("  },\r\n");
    188            }
    189
    190            var3.append("  \"stats-change\":[");
    191            Iterator var6 = par2Map.keySet().iterator();
    192
    193            while (var6.hasNext())
    194            {
    195                StatBase var7 = (StatBase)var6.next();
    196
    197                if (var5)
    198                {
    199                    var5 = false;
    200                }
    201                else
    202                {
    203                    var3.append("},");
    204                }
    205
    206                var3.append("\r\n    {\"").append(var7.statId).append("\":").append(par2Map.get(var7));
    207                var4.append(var7.statGuid).append(",");
    208                var4.append(par2Map.get(var7)).append(",");
    209            }
    210
    211            if (!var5)
    212            {
    213                var3.append("}");
    214            }
    215
    216            MD5String var8 = new MD5String(par1Str);
    217            var3.append("\r\n  ],\r\n");
    218            var3.append("  \"checksum\":\"").append(var8.getMD5String(var4.toString())).append("\"\r\n");
    219            var3.append("}");
    220            return var3.toString();
    221        }
    222
    223        /**
    224         * Returns true if the achievement has been unlocked.
    225         */
    226        public boolean hasAchievementUnlocked(Achievement par1Achievement)
    227        {
    228            return this.field_77457_a.containsKey(par1Achievement);
    229        }
    230
    231        /**
    232         * Returns true if the parent has been unlocked, or there is no parent
    233         */
    234        public boolean canUnlockAchievement(Achievement par1Achievement)
    235        {
    236            return par1Achievement.parentAchievement == null || this.hasAchievementUnlocked(par1Achievement.parentAchievement);
    237        }
    238
    239        public int writeStat(StatBase par1StatBase)
    240        {
    241            Integer var2 = (Integer)this.field_77457_a.get(par1StatBase);
    242            return var2 == null ? 0 : var2.intValue();
    243        }
    244
    245        public void syncStats()
    246        {
    247            this.statsSyncher.syncStatsFileWithMap(this.func_77445_b());
    248        }
    249
    250        public void func_77449_e()
    251        {
    252            if (this.field_77456_c && this.statsSyncher.func_77425_c())
    253            {
    254                this.statsSyncher.beginSendStats(this.func_77445_b());
    255            }
    256
    257            this.statsSyncher.func_77422_e();
    258        }
    259    }