<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

public class DResource : ITurnUpdatable
{
    private static int resourceIDCounter = 0;
    private static Dictionary<string, int> resourceNameToIDMap = new Dictionary<string, int>();

    private int resourceID;
    private string resourceName = "NameUndefined";
    private int resourceAmount = 0;
        
    // Primary Factory
    public static DResource Create(string name, int amount)
    {
        if (resourceNameToIDMap.ContainsKey(name))
        {
            return new DResource(resourceNameToIDMap[name], name, amount);
        }
        else
        {
            return new DResource(name, amount);
        }
    }

    public static DResource Create(string name)
    {
        return Create(name, Constants.DEFAULT_RESOURCE_VALUE);
    }

    public static DResource Create(DResource resource)
    {
        return Create(resource.Name);
    }

    public static DResource Create(DResource resource, int amount)
    {
        return Create(resource.Name, amount);
    }

    private DResource(int ID, string name, int amount)
    {
        resourceID = ID;
        resourceName = name;
        resourceAmount = amount;
    }

    private DResource(string name, int amount)
    {

        resourceID = resourceIDCounter++;
        resourceNameToIDMap.Add(name, resourceID);
        resourceName = name;
        resourceAmount = amount;
    }

    public static int NameToID(string name)
    {
        if (resourceNameToIDMap.ContainsKey(name))
        {
            return resourceNameToIDMap[name];
        }
        else
        {
            throw new ResourceNameNotFoundException("Resource name does not exist: " + name);
        }
    }
    
    
    // TurnUpdate is called once per Turn
    public void TurnUpdate(int numDaysPassed)
    {

    }

    #region Properties

    public int ID
    {
        get { return resourceID; }
    }

    public string Name
    {
        get { return resourceName; }
    }

    public int Amount
    {
        get { return resourceAmount; }
        set { resourceAmount = Math.Max(0, value); }
    }

    #endregion

    public static JSONNode SaveResourceIDMapToJSON()
    {
        JSONNode returnNode = new JSONArray();

        // Save all the currently created resources (the master list)
        foreach (var kvp in resourceNameToIDMap)
        {
            JSONNode resourceEntry = new JSONObject();
            resourceEntry.Add("name", new JSONString(kvp.Key));
            resourceEntry.Add("ID", new JSONNumber(kvp.Value.ToString()));

            returnNode.Add(resourceEntry);
        }

        return returnNode;
    }

    public JSONNode SaveToJSON()
    {
        JSONNode returnNode = new JSONObject();

        returnNode.Add("name", new JSONString(resourceName));
        returnNode.Add("ID", new JSONNumber(resourceID));
        returnNode.Add("amount", new JSONNumber(resourceAmount));

        return returnNode;
    }

    public static void LoadResourceIDMapFromJSON(JSONNode jsonNode)
    {
        Dictionary<string, int> resourceMap = new Dictionary<string, int>();

        // Load all the currently created resources (the master list)
        foreach (JSONNode resource in jsonNode.AsArray)
        {
            resourceMap.Add(resource["name"], resource["ID"].AsInt);
        }

        resourceNameToIDMap = resourceMap;
    }

    public static DResource LoadFromJSON(JSONNode jsonNode)
    {
        if (jsonNode == null)
            return null;

        DResource loadedResource = Create(
            jsonNode["name"],
            RandJSON.JSONInt(jsonNode["amount"]));

        if (loadedResource.ID != jsonNode["ID"].AsInt)
        {
            throw new ResourceDictionaryMismatchException(
                string.Format("Resource ({1}) has saved ID ({2}), but ID should be ({3}).",
                    jsonNode["name"],
                    loadedResource.ID,
                    jsonNode["ID"])
                );
        }

        return loadedResource;
    }
}
<commit_msg>Temporary fix for DResource loading altered city JSONs<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

public class DResource : ITurnUpdatable
{
    private static int resourceIDCounter = 0;
    private static Dictionary<string, int> resourceNameToIDMap = new Dictionary<string, int>();

    private int resourceID;
    private string resourceName = "NameUndefined";
    private int resourceAmount = 0;
        
    // Primary Factory
    public static DResource Create(string name, int amount)
    {
        if (resourceNameToIDMap.ContainsKey(name))
        {
            return new DResource(resourceNameToIDMap[name], name, amount);
        }
        else
        {
            return new DResource(name, amount);
        }
    }

    public static DResource Create(string name)
    {
        return Create(name, Constants.DEFAULT_RESOURCE_VALUE);
    }

    public static DResource Create(DResource resource)
    {
        return Create(resource.Name);
    }

    public static DResource Create(DResource resource, int amount)
    {
        return Create(resource.Name, amount);
    }

    private DResource(int ID, string name, int amount)
    {
        resourceID = ID;
        resourceName = name;
        resourceAmount = amount;
    }

    private DResource(string name, int amount)
    {

        resourceID = resourceIDCounter++;
        resourceNameToIDMap.Add(name, resourceID);
        resourceName = name;
        resourceAmount = amount;
    }

    public static int NameToID(string name)
    {
        if (resourceNameToIDMap.ContainsKey(name))
        {
            return resourceNameToIDMap[name];
        }
        else
        {
            throw new ResourceNameNotFoundException("Resource name does not exist: " + name);
        }
    }
    
    
    // TurnUpdate is called once per Turn
    public void TurnUpdate(int numDaysPassed)
    {

    }

    #region Properties

    public int ID
    {
        get { return resourceID; }
    }

    public string Name
    {
        get { return resourceName; }
    }

    public int Amount
    {
        get { return resourceAmount; }
        set { resourceAmount = Math.Max(0, value); }
    }

    #endregion

    public static JSONNode SaveResourceIDMapToJSON()
    {
        JSONNode returnNode = new JSONArray();

        // Save all the currently created resources (the master list)
        foreach (var kvp in resourceNameToIDMap)
        {
            JSONNode resourceEntry = new JSONObject();
            resourceEntry.Add("name", new JSONString(kvp.Key));
            resourceEntry.Add("ID", new JSONNumber(kvp.Value.ToString()));

            returnNode.Add(resourceEntry);
        }

        return returnNode;
    }

    public JSONNode SaveToJSON()
    {
        JSONNode returnNode = new JSONObject();

        returnNode.Add("name", new JSONString(resourceName));
        returnNode.Add("ID", new JSONNumber(resourceID));
        returnNode.Add("amount", new JSONNumber(resourceAmount));

        return returnNode;
    }

    public static void LoadResourceIDMapFromJSON(JSONNode jsonNode)
    {
        Dictionary<string, int> resourceMap = new Dictionary<string, int>();

        // Load all the currently created resources (the master list)
        foreach (JSONNode resource in jsonNode.AsArray)
        {
            resourceMap.Add(resource["name"], resource["ID"].AsInt);
        }

        resourceNameToIDMap = resourceMap;
    }

    public static DResource LoadFromJSON(JSONNode jsonNode)
    {
        if (jsonNode == null)
            return null;

        DResource loadedResource = Create(
            jsonNode["name"],
            RandJSON.JSONInt(jsonNode["amount"]));

        /* TODO: Fix this properly
        if (loadedResource.ID != jsonNode["ID"].AsInt)
        {
            throw new ResourceDictionaryMismatchException(
                string.Format("Resource ({1}) has saved ID ({2}), but ID should be ({3}).",
                    jsonNode["name"],
                    loadedResource.ID,
                    jsonNode["ID"])
                );
        }
        */

        return loadedResource;
    }
}

{'fraction_non_alphanumeric': 0.0890039744670601, 'fraction_numerical': 0.0014452607491268216, 'mean_word_length': 2.147839272175891, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4417578', 'n_tokens_mistral': 2204, 'n_tokens_neox': 2135, 'n_words': 575}