Esempio n. 1
0
 void AddResponseGroup(RSResponseGroup responseGroup)
 {
     if (this.responseGroups.ContainsKey(responseGroup.Name))
     {
         throw new Exception("The responseGroup [" + responseGroup.Name + "] is already defined, cannot add a new responseGroup named this");
     }
     this.responseGroups.Add(responseGroup.Name, responseGroup);
 }
Esempio n. 2
0
        void LoadResponsesCSV(string path)
        {
            Debug.Log("Loading Responses CSV: " + path);
            Dictionary <string, int> columnIDs = new Dictionary <string, int>();

            using (var streamRdr = new StreamReader(path)) {
                var             csvReader     = new CsvReader(streamRdr, ",");
                RSResponseGroup responseGroup = null;
                while (csvReader.Read())
                {
                    // load column names
                    if (columnIDs.Count == 0)
                    {
                        for (int i = 0; i < csvReader.FieldsCount; i++)
                        {
                            columnIDs.Add(csvReader[i], i);
                        }

                        // check that file is valid. if not, we die.
                        if (!(columnIDs.ContainsKey("name") && columnIDs.ContainsKey("flags") && columnIDs.ContainsKey("responsetype") && columnIDs.ContainsKey("response") && columnIDs.ContainsKey("delay") && columnIDs.ContainsKey("odds") && columnIDs.ContainsKey("resaydelay") && columnIDs.ContainsKey("weight")))
                        {
                            throw new Exception("The Responses CSV file [" + path + "] does not contain all the columns we require");
                        }
                        continue;
                    }
                    // load in response
                    string name  = csvReader[columnIDs["name"]];
                    string flags = csvReader[columnIDs["flags"]];
                    string responseTypeString = csvReader[columnIDs["responsetype"]];
                    string responseValue      = csvReader[columnIDs["response"]];

                    if (name == "" && responseTypeString == "" && responseValue == "")
                    {
                        // skip empty rows
                        continue;
                    }

                    string delayString = csvReader[columnIDs["delay"]];
                    bool   delayAuto   = true;
                    float  delay       = 0;
                    if (delayString != "")
                    {
                        delayAuto = false;
                        delay     = float.Parse(delayString, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    string oddsString = csvReader[columnIDs["odds"]];
                    float  odds       = 1;
                    if (oddsString != "")
                    {
                        // we use 0-100 in the spreadsheet but 0-1 internally.
                        odds = float.Parse(delayString, CultureInfo.InvariantCulture.NumberFormat) / 100;
                    }
                    string resaydelayString = csvReader[columnIDs["resaydelay"]];
                    float  resaydelay       = 0;
                    if (resaydelayString != "")
                    {
                        resaydelay = float.Parse(resaydelayString, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    string weightString = csvReader[columnIDs["weight"]];
                    float  weight       = 1;
                    if (weightString != "")
                    {
                        weight = float.Parse(weightString, CultureInfo.InvariantCulture.NumberFormat);
                    }

                    if (name != "")
                    {
                        // add old responseGroup to our list of 'em
                        if (responseGroup != null)
                        {
                            this.AddResponseGroup(responseGroup);
                        }

                        // create new responsegroup
                        responseGroup = new RSResponseGroup(name, flags);
                        // clear flags below so if this is a single-line response
                        //  group, these group flags aren't counted for *it* too.
                        flags = "";
                    }
                    if (responseTypeString != "" && responseValue != "")
                    {
                        // add new response to responseGroup
                        responseGroup.Add(new RSResponse(flags, responseTypeString, responseValue, delayAuto, delay, odds, resaydelay, weight));
                    }
                }
                if (responseGroup != null)
                {
                    this.AddResponseGroup(responseGroup);
                }
            }
        }