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