Report post

Are you sure you want to report the post shown below? This will send an email to the ATVO administrators. Please include a short reason for reporting.

Users reporting for no reason may be locked out.


Post

Posted by Leandro V
on
Hi Nick!
I'm a new ATVO user and I'm learning how to use scripts.
I used your code to test this feature:

using System;
using System.Data;
using ATVO.ThemesSDK;
using ATVO.ThemesSDK.Data.Entity;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Scripting.DotNET;
using System.Collections.Generic;

namespace Scripts
{
public class LiveChampionshipPoints : IScript
{
private Dictionary<int, int> _pointSystem = new Dictionary<int, int>();
        private Dictionary<int, DriverContender> _standings = new Dictionary<int, DriverContender>();

public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
{
            // Make sure point system and standings are loaded
            LoadPointSystem(item.Theme);
            LoadStandings(item.Theme);

            // Update the points
            UpdateSessionStandings(sim);

            // Sort by points and assign position
            // This updates positions for drivers in the session AND drivers not in the session
            UpdateChampPositions();

            // Also update the champ results for drivers in the session
            // in case you are showing these somewhere else (in another widget)
            UpdateSessionChampResults();

            // Finally update our Custom Data Sets so the widgets are showing the new results
            UpdateDataSets(item.Theme);
           
return value.ToString();
}

        private void UpdateSessionStandings(ISimulation sim)
        {
            // Loop through entity results in the session and update the standings
            foreach (var result in sim.Session.Current.Results)
            {
                DriverContender driver;

                // Check if there is a driver in the session who was NOT in the standings csv
                if (!_standings.ContainsKey(result.Entity.Id))
                {
                    // This driver isn't in the standings yet, let's add him
                    driver = new DriverContender();
                    driver.CustomerId = result.Entity.Id;
                    driver.Name = result.Entity.Name;
                    driver.CarNumber = result.Entity.Car.Number;
                    driver.ChampPoints = 0;
                    _standings.Add(driver.CustomerId, driver);
                }
                else
                {
                    // Otherwise, grab him from memory
                    driver = _standings[result.Entity.Id];
                }
               
                // Link him to the session entity
                driver.Entity = result.Entity;

                // Add the champ points obtained during this race
                driver.LiveChampPoints = driver.ChampPoints + _pointSystem[result.Position]; // Or result.LivePosition to update during a lap
            }
        }

        private void UpdateChampPositions()
        {
            // Sort the standings by live champ points and assign positions in order
            var sorted = _standings.Values.OrderByDescending(d => d.LiveChampPoints);

            int position = 1;
            foreach (var driver in sorted)
            {
                driver.LiveChampPosition = position;
                position += 1;
            }
        }

        private void UpdateSessionChampResults()
        {
            foreach (var driver in _standings.Values)
            {
                // If this driver is in the session, he has his Entity linked up
                // If not, Entity is null
                if (driver.Entity != null)
                {
                    // update the ChampionshipResult object
                    var champ = driver.Entity.ChampionshipResult;

                    champ.Position = driver.ChampPosition; // previous pos
                    champ.LivePosition = driver.LiveChampPosition; // updated pos
                    champ.Points = driver.ChampPoints; // previous points
                    champ.LivePoints = driver.LiveChampPoints; // updated points
                }
            }
        }

        private void UpdateDataSets(Theme theme)
        {
            // Assuming you have four Custom Data Set items:
            var cds_pos = theme.CustomDataSets.Find("cds_pos"); // for champ position
            var cds_name = theme.CustomDataSets.Find("cds_name"); // for driver name
            var cds_carnum = theme.CustomDataSets.Find("cds_carnum"); // for car num
            var cds_points = theme.CustomDataSets.Find("cds_points"); // for champ points

            // Clear their data
            cds_pos.Data.Clear();
            cds_name.Data.Clear();
            cds_carnum.Data.Clear();
            cds_points.Data.Clear();

            // Add the new data in order
            foreach (var driver in _standings.Values.OrderBy(d => d.LiveChampPosition))
            {
                cds_pos.Data.Add(new CustomDataSetItem(driver.LiveChampPosition));
                cds_name.Data.Add(new CustomDataSetItem(driver.Name));
                cds_carnum.Data.Add(new CustomDataSetItem(driver.CarNumber));
                cds_points.Data.Add(new CustomDataSetItem(driver.LiveChampPoints));
            }
        }

private void LoadPointSystem(Theme theme)
        {
            if (_pointSystem.Count == 0)
            {
                // Read point system from an embedded spreadsheet
                // Two columns: position, points
                var sheet = theme.Spreadsheets.Find("points_system.csv");

                foreach (var row in sheet.Table.Data.Rows.OfType<DataRow>())
                {
                    var position = row[0];
                    var points = row[1];
                    _pointSystem.Add(position, points);
                }
            }
        }

        private void LoadStandings(Theme theme)
        {
            if (_standings.Count == 0)
            {
                // Read current standings from embedded csv
                // Columns: customer_id, position, carnumber, lastname, firstname, points
                var sheet = theme.Spreadsheets.Find("/csv/standings.csv");

                foreach (var row in sheet.Table.Data.Rows.OfType<DataRow>())
                {
                    var custid = row[0];
                    var position = row[1];
                    var carnum = row[2];
                    var name = String.Concat(row[4], row[3]);
                    var points = row[5];
                   
                    var driver = new DriverContender();
                    driver.CustomerId = custid;
                    driver.ChampPosition = position;
                    driver.CarNumber = carnum;
                    driver.Name = name;
                    driver.ChampPoints = points;

                    _standings.Add(custid, driver);
                }
            }
        }
}

public class DriverContender
{
public IEntity Entity {get;set;}
        public int CustomerId {get;set;}
        public string CarNumber {get;set;}
        public string Name {get;set;}
        public int ChampPosition {get;set;}
        public int ChampPoints {get;set;}
        public int LiveChampPosition {get;set;}
public int LiveChampPoints {get;set;}
}
}


So, here are the errors in ATVO Theme Editor:

LiveChampionshipPoints.cs (Line 74) 'Dictionary<int, DriverContender>.ValueCollection' does not contain a definition for 'OrderByDescending' and no extension method 'OrderByDescending' accepting a first argument of type 'Dictionary<int, DriverContender>.ValueCollection' could be found (are you missing a using directive or an assembly reference?)
LiveChampionshipPoints.cs (Line 77) foreach statement cannot operate on variables of type '?' because '?' does not contain a public definition for 'GetEnumerator'
LiveChampionshipPoints.cs (Line 118) 'Dictionary<int, DriverContender>.ValueCollection' does not contain a definition for 'OrderBy' and no extension method 'OrderBy' accepting a first argument of type 'Dictionary<int, DriverContender>.ValueCollection' could be found (are you missing a using directive or an assembly reference?)
LiveChampionshipPoints.cs (Line 135) 'DataRowCollection' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'DataRowCollection' could be found (are you missing a using directive or an assembly reference?)
LiveChampionshipPoints.cs (Line 152) 'DataRowCollection' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'DataRowCollection' could be found (are you missing a using directive or an assembly reference?)

And here are the errors when I'm using Visual Studio Code:


'Dictionary <int, DriverContender> .ValueCollection' does not contain a definition for 'OrderByDescending' and could not find any 'OrderByDescending' extension method that accepts a first argument of type 'Dictionary <int, DriverContender> .ValueCollection' (you are forgetting to use a directive or assembly reference?) (CS1061) [project]

'IEntity' does not contain a definition for "ChampionshipResult" and could not find any "ChampionshipResult" extension method that accepts a first argument of type 'IEntity' (are you forgetting to use a directive or an assembly reference?) ( CS1061) [project]

'Dictionary <int, DriverContender> .ValueCollection' does not contain a definition for 'OrderBy' and could not find any 'OrderBy' extension method that accepts a first argument of type 'Dictionary <int, DriverContender> .ValueCollection' (you are forgetting to use a directive or assembly reference?) (CS1061) [project]

'DataRowCollection' does not contain a definition for "OfType" and could not find any "OfType" extension method that accepts a first argument of type 'DataRowCollection' (are you forgetting to use a directive or an assembly reference?) ( CS1061) [project]

‘DataRowCollection’ não contém uma definição para "OfType" e não foi possível encontrar nenhum método de extensão "OfType" que aceite um primeiro argumento do tipo ‘DataRowCollection’ (você está se esquecendo de usar uma diretiva ou uma referência de assembly?) (CS1061) [project]

Sorry for this long post here.
If you can help me with this problem, I'll appreciate.

Thanks!