-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseReporter.cs
185 lines (169 loc) · 6.26 KB
/
BaseReporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace DVPathTracer
{
public static class BaseReporter
{
public const string defaultFilename = "DVTracedPath.csv";
public static string fileName = defaultFilename;
private const string basePath = "./Mods/DVPathTracer/sessions/";
public const float seaLevel = 110;
private static float startTime = 0f; // Time that the tracer was activated, in seconds since the game was started
private static float nextReportTime = 0f; // Time that the next report should be made, in seconds since the tracer was activated
private static float nextUpdateTime = 0f; // Time that the list of tracked rolling stock should be updated, in seconds since the tracer was activated
public static bool isReady = false;
public static bool isActive = false;
public static PlayerReporter player;
/**
* Informs the reporter that the PlayerManager is ready
*/
public static void ManagerIsSet()
{
if (!isReady)
{
player = new PlayerReporter();
isReady = true;
Main.Log("Ready to go");
}
}
/**
* Prepares the file and allows the reporter to start reporting
*/
public static void Activate()
{
PrepareFile();
isActive = true;
Main.settings.isActive = true;
startTime = Time.time;
Main.Log($"Reporting activated at {startTime}");
}
/**
* Stops the reporter from reporting
*/
public static void Deactivate()
{
isActive = false;
Main.settings.isActive = false;
startTime = 0f;
nextReportTime = 0f;
nextUpdateTime = 0f;
Main.Log($"Reporting ended at {Time.time}");
}
/**
* Creates an empty .csv file named as the current date & time, unless told to use the default
*/
private static void PrepareFile()
{
if (!Directory.Exists(basePath))
{
Directory.CreateDirectory(basePath);
Main.Log($"Session folder created");
}
if (Main.settings.useSystemTime)
{
var currentTime = DateTime.Now;
fileName = $"{currentTime:yyyyMMdd_HHmm}.csv"; // System time to nearest minute
}
else
{
fileName = defaultFilename;
}
File.WriteAllText(basePath + fileName, $"DVPathTracer,{Main.entry.Info.Version},Verbose Tracing,{Main.verboseTracing}\n");
WriteToFile($"Time,{PlayerReporter.Headings},{StockReporter.Headings},{StockReporter.Headings}\n");
Main.Log($"File {fileName} readied");
}
/**
* Appends the given text to the set file
*/
public static void WriteToFile(string text)
{
File.AppendAllText(basePath + fileName, text);
}
/**
* Return a string of various current properties about the player and rolling stock ordered appropriately
*/
public static string GetReport(float time)
{
if (!isReady)
{
throw new Exception("Not yet ready");
}
string report = $"{time},{player.Values},";
List<int> toRemove = new List<int>();
int index = 0;
int remainingStock = StockFinder.numStock;
while (index < StockFinder.TrackedStock.Keys.Count && remainingStock > 0)
{
if (StockFinder.TrackedStock[index] == null) // No car to report on
{
if (Main.verboseTracing)
{
// Preserve column order
report += $"{StockReporter.Headings},";
}
}
else if (StockFinder.TrackedStock[index].Target.ID == String.Empty) // Car no longer exists
{
if (Main.verboseTracing)
{
report += $"{StockReporter.Headings},";
}
else
{
report += $"{StockFinder.TrackedStock[index].Removed},";
}
toRemove.Add(index);
remainingStock--;
}
else
{
string values = StockFinder.TrackedStock[index].Values;
// Adding the comma is awkward but I want it done here for consistency
if (values != "") report += $"{values},";
remainingStock--;
}
index++;
}
foreach (int i in toRemove)
{
StockFinder.Remove(i);
}
return report;
}
/**
* Periodically reports information to a local .csv file as set by the player.
*/
public static void TimedReport()
{
float upTime = Time.time - startTime;
if (isActive)
{
if (upTime >= nextReportTime)
{
string report;
try
{
report = GetReport(upTime);
}
catch //(Exception e) // No Report Available, skip
// Should only ever happen while the game is loading
{
Main.Log($"No report at {upTime} available");
//Main.Log(e.ToString());
nextReportTime += Main.settings.logRate;
return;
}
WriteToFile(report + "\n");
nextReportTime += Main.settings.logRate;
}
else if (upTime >= nextUpdateTime) // use 'else' to avoid doing too much in one cycle
{
StockFinder.UpdateTrackedStock();
nextUpdateTime += 10;
}
}
}
}
}