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 Emmanuel S
on
Thanks for this answer, if I understand with this code we supposed to see in a specific widget use custom data could show together pilot in session and pilot in csv.

As you know the code in exemple has some errors, so I try to make correction, now it is ok (no errors visible) but impossible to get together driver in and out in my widget.

So I put my correction and if you have time or if someone could test it it will be nice to say me what is wrong

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;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scripts
{
public class championnat : 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)
{
if (value == null)
{
// do something appropriate, in this case probably just return nothing
return null;
}
// 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.ChampPosition = (1000-driver.ChampPoints);
driver.Name = result.Entity.Name;
driver.CarNumber = result.Entity.Car.Number;
driver.ChampPoints = 0;
// driver.Exist = "1";
_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))
{
//var LiveChampPosition2 = LiveChampPosition.ToString();
cds_pos.Data.Add(new CustomDataSetItem(driver.LiveChampPosition.ToString()));
cds_name.Data.Add(new CustomDataSetItem(driver.Name));
cds_carnum.Data.Add(new CustomDataSetItem(driver.CarNumber));
cds_points.Data.Add(new CustomDataSetItem(driver.LiveChampPoints.ToString()));
}
}

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 = Convert.ToInt32(row[0]);
var points = Convert.ToInt32(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("standings.csv");

foreach (var row in sheet.Table.Data.Rows.OfType<DataRow>())
{
var custid = Convert.ToInt32(row[0]);
var position = Convert.ToInt32(row[1]);
var carnum = Convert.ToString(row[2]);
var name = String.Concat(row[4], row[3]);
var points = Convert.ToInt32(row[5]);
// var exist = Convert.ToString(row[6]);

var driver = new DriverContender();
driver.CustomerId = custid;
driver.ChampPosition = position;
driver.CarNumber = carnum;
driver.Name = name;
driver.ChampPoints = points;
// driver.Exist = exist;

_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 string Exist {get;set;}
public int LiveChampPoints {get;set;}
}
}