    001package net.minecraft.stats;
    002
    003import argo.jdom.JdomParser;
    004import argo.jdom.JsonNode;
    005import argo.jdom.JsonRootNode;
    006import argo.jdom.JsonStringNode;
    007import argo.saj.InvalidSyntaxException;
    008import cpw.mods.fml.common.asm.ReobfuscationMarker;
    009import cpw.mods.fml.relauncher.Side;
    010import cpw.mods.fml.relauncher.SideOnly;
    011import java.io.File;
    012import java.util.HashMap;
    013import java.util.Iterator;
    014import java.util.List;
    015import java.util.Map;
    016import java.util.Map.Entry;
    017import net.minecraft.client.stats.StatPlaceholder;
    018import net.minecraft.util.MD5String;
    019import net.minecraft.util.Session;
    020
    021@ReobfuscationMarker
    022@SideOnly(Side.CLIENT)
    023public class StatFileWriter
    024{
    025    private Map field_77457_a = new HashMap();
    026    private Map field_77455_b = new HashMap();
    027    private boolean field_77456_c = false;
    028    private StatsSyncher statsSyncher;
    029
    030    public StatFileWriter(Session par1Session, File par2File)
    031    {
    032        File file2 = new File(par2File, "stats");
    033
    034        if (!file2.exists())
    035        {
    036            file2.mkdir();
    037        }
    038
    039        File[] afile = par2File.listFiles();
    040        int i = afile.length;
    041
    042        for (int j = 0; j < i; ++j)
    043        {
    044            File file3 = afile[j];
    045
    046            if (file3.getName().startsWith("stats_") && file3.getName().endsWith(".dat"))
    047            {
    048                File file4 = new File(file2, file3.getName());
    049
    050                if (!file4.exists())
    051                {
    052                    System.out.println("Relocating " + file3.getName());
    053                    file3.renameTo(file4);
    054                }
    055            }
    056        }
    057
    058        this.statsSyncher = new StatsSyncher(par1Session, this, file2);
    059    }
    060
    061    public void readStat(StatBase par1StatBase, int par2)
    062    {
    063        this.writeStatToMap(this.field_77455_b, par1StatBase, par2);
    064        this.writeStatToMap(this.field_77457_a, par1StatBase, par2);
    065        this.field_77456_c = true;
    066    }
    067
    068    private void writeStatToMap(Map par1Map, StatBase par2StatBase, int par3)
    069    {
    070        Integer integer = (Integer)par1Map.get(par2StatBase);
    071        int j = integer == null ? 0 : integer.intValue();
    072        par1Map.put(par2StatBase, Integer.valueOf(j + par3));
    073    }
    074
    075    public Map func_77445_b()
    076    {
    077        return new HashMap(this.field_77455_b);
    078    }
    079
    080    /**
    081     * write a whole Map of stats to the statmap
    082     */
    083    public void writeStats(Map par1Map)
    084    {
    085        if (par1Map != null)
    086        {
    087            this.field_77456_c = true;
    088            Iterator iterator = par1Map.keySet().iterator();
    089
    090            while (iterator.hasNext())
    091            {
    092                StatBase statbase = (StatBase)iterator.next();
    093                this.writeStatToMap(this.field_77455_b, statbase, ((Integer)par1Map.get(statbase)).intValue());
    094                this.writeStatToMap(this.field_77457_a, statbase, ((Integer)par1Map.get(statbase)).intValue());
    095            }
    096        }
    097    }
    098
    099    public void func_77452_b(Map par1Map)
    100    {
    101        if (par1Map != null)
    102        {
    103            Iterator iterator = par1Map.keySet().iterator();
    104
    105            while (iterator.hasNext())
    106            {
    107                StatBase statbase = (StatBase)iterator.next();
    108                Integer integer = (Integer)this.field_77455_b.get(statbase);
    109                int i = integer == null ? 0 : integer.intValue();
    110                this.field_77457_a.put(statbase, Integer.valueOf(((Integer)par1Map.get(statbase)).intValue() + i));
    111            }
    112        }
    113    }
    114
    115    public void func_77448_c(Map par1Map)
    116    {
    117        if (par1Map != null)
    118        {
    119            this.field_77456_c = true;
    120            Iterator iterator = par1Map.keySet().iterator();
    121
    122            while (iterator.hasNext())
    123            {
    124                StatBase statbase = (StatBase)iterator.next();
    125                this.writeStatToMap(this.field_77455_b, statbase, ((Integer)par1Map.get(statbase)).intValue());
    126            }
    127        }
    128    }
    129
    130    public static Map func_77453_b(String par0Str)
    131    {
    132        HashMap hashmap = new HashMap();
    133
    134        try
    135        {
    136            String s1 = "local";
    137            StringBuilder stringbuilder = new StringBuilder();
    138            JsonRootNode jsonrootnode = (new JdomParser()).parse(par0Str);
    139            List list = jsonrootnode.getArrayNode(new Object[] {"stats-change"});
    140            Iterator iterator = list.iterator();
    141
    142            while (iterator.hasNext())
    143            {
    144                JsonNode jsonnode = (JsonNode)iterator.next();
    145                Map map = jsonnode.getFields();
    146                Entry entry = (Entry)map.entrySet().iterator().next();
    147                int i = Integer.parseInt(((JsonStringNode)entry.getKey()).getText());
    148                int j = Integer.parseInt(((JsonNode)entry.getValue()).getText());
    149                StatBase statbase = StatList.getOneShotStat(i);
    150
    151                if (statbase == null)
    152                {
    153                    System.out.println(i + " is not a valid stat, creating place-holder");
    154                    statbase = (new StatPlaceholder(i)).registerStat();
    155                }
    156
    157                stringbuilder.append(StatList.getOneShotStat(i).statGuid).append(",");
    158                stringbuilder.append(j).append(",");
    159                hashmap.put(statbase, Integer.valueOf(j));
    160            }
    161
    162            MD5String md5string = new MD5String(s1);
    163            String s2 = md5string.getMD5String(stringbuilder.toString());
    164
    165            if (!s2.equals(jsonrootnode.getStringValue(new Object[] {"checksum"})))
    166            {
    167                System.out.println("CHECKSUM MISMATCH");
    168                return null;
    169            }
    170        }
    171        catch (InvalidSyntaxException invalidsyntaxexception)
    172        {
    173            invalidsyntaxexception.printStackTrace();
    174        }
    175
    176        return hashmap;
    177    }
    178
    179    public static String func_77441_a(String par0Str, String par1Str, Map par2Map)
    180    {
    181        StringBuilder stringbuilder = new StringBuilder();
    182        StringBuilder stringbuilder1 = new StringBuilder();
    183        boolean flag = true;
    184        stringbuilder.append("{\r\n");
    185
    186        if (par0Str != null && par1Str != null)
    187        {
    188            stringbuilder.append("  \"user\":{\r\n");
    189            stringbuilder.append("    \"name\":\"").append(par0Str).append("\",\r\n");
    190            stringbuilder.append("    \"sessionid\":\"").append(par1Str).append("\"\r\n");
    191            stringbuilder.append("  },\r\n");
    192        }
    193
    194        stringbuilder.append("  \"stats-change\":[");
    195        Iterator iterator = par2Map.keySet().iterator();
    196
    197        while (iterator.hasNext())
    198        {
    199            StatBase statbase = (StatBase)iterator.next();
    200
    201            if (flag)
    202            {
    203                flag = false;
    204            }
    205            else
    206            {
    207                stringbuilder.append("},");
    208            }
    209
    210            stringbuilder.append("\r\n    {\"").append(statbase.statId).append("\":").append(par2Map.get(statbase));
    211            stringbuilder1.append(statbase.statGuid).append(",");
    212            stringbuilder1.append(par2Map.get(statbase)).append(",");
    213        }
    214
    215        if (!flag)
    216        {
    217            stringbuilder.append("}");
    218        }
    219
    220        MD5String md5string = new MD5String(par1Str);
    221        stringbuilder.append("\r\n  ],\r\n");
    222        stringbuilder.append("  \"checksum\":\"").append(md5string.getMD5String(stringbuilder1.toString())).append("\"\r\n");
    223        stringbuilder.append("}");
    224        return stringbuilder.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 integer = (Integer)this.field_77457_a.get(par1StatBase);
    246        return integer == null ? 0 : integer.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}