0

I've been working on converting an old server from a Flash game into something I can use as a starting point for a new server-client system, however I just hit a stopping point I can't really figure out. Can someone look at this code and possibly translate it into C#?

var dists:Array = [];
for(a=0;a<deltas.length;a++) {
    dists.push({offset:Math.abs(deltas[a]-avg), 
                time:deltas[a], 
                toString:function(){
                     return "offset:" + 
                     this.offset + ", 
                     time: " + this.time }
               })
}

As a note, these numbers are doubles - aside from the int in the for loop. And further clarification, Dists I've translated into a double list array, but that might be wrong. This is what I have so far:

List<Double> Dist = new List<double>();

for (int i = 0; i < Deltas.Count; i++)
{
    Dist.Add(Math.Abs(Deltas[i] - Average));
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Merlin
  • 929
  • 12
  • 33

2 Answers2

1

From the information given, this is what I could work out...

void Main()
{
    Dictionary<int, double> deltas = new Dictionary<int, double>();
    int avg = 0;

    List<Item> dists = new List<Item>();
    for (int a = 0; a < deltas.Count(); a++)
    {
        dists.Add(new Item { Offset = Math.Abs(deltas[a] - avg), Time = deltas[a].ToDateTime()});
    }
}

public class Item 
{
    public double Offset { get; set; }
    public DateTime Time { get; set; }
    public string DisplayString { get; set; }

    public override string ToString()
    {
        return string.Format("offset: {0}, time: {1}", this.Offset, this.Time);
    }
}

public static class Extension
{
    public static DateTime ToDateTime(this double time)
    {
        return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(time);
    }
}
Aydin
  • 15,016
  • 4
  • 32
  • 42
  • Aydin, that's what I was thinking as well, that the array translated into a Dictonary. One thing about Flash AS3 is that it's Array feature had so many more functions that it makes guessing what array translates into which in a more strictly typed language confusing. – Merlin Apr 23 '15 at 21:52
0

Declare a new array dists:

var dists:Array = [];

For each value in deltas:

for( a=0;a<deltas.length;a++){

Insert a new object into the dists array:

dists.push(...)

The object will have two properties - offset and time - and will have a toString() method that formats the data in a friendly way.

The offset value will be the difference between the current delta and the average:

offset:Math.abs(deltas[a]-avg)

And the time value will be the current delta:

time:deltas[a]

Phil Walton
  • 963
  • 6
  • 11