Parking/Assets/Scripts/DataImporter.cs

83 lines
3.3 KiB
C#
Raw Normal View History

2022-09-01 22:31:57 +02:00
using System;
2022-08-25 18:38:38 +02:00
using System.Collections.Generic;
2022-09-01 22:31:57 +02:00
using System.Globalization;
using System.IO;
2022-09-07 21:47:41 +02:00
using System.Text;
2022-09-10 20:53:06 +02:00
using Unity.VisualScripting;
2022-08-25 18:38:38 +02:00
namespace Parking
2022-08-25 18:38:38 +02:00
{
public class DataImporter
2022-08-25 18:38:38 +02:00
{
public static List<Driver> Drivers = new();
2022-09-07 21:47:41 +02:00
public static bool ReadFile(string path)
2022-08-25 18:38:38 +02:00
{
2022-09-07 21:47:41 +02:00
if (!File.Exists(path))
return false;
string fileData = File.ReadAllText(path);//Encoding.GetEncoding(28605));
2022-09-05 22:01:52 +02:00
string[] lines = fileData.Split("\n"[0]);
int counter = 0;
2022-09-09 23:48:31 +02:00
char separator = ',';
2022-09-05 22:01:52 +02:00
foreach (string line in lines) {
if (line == "")
2022-09-04 00:35:45 +02:00
continue;
2022-09-05 22:01:52 +02:00
if (counter == 0) {
counter++;
2022-09-09 23:48:31 +02:00
if (line.Contains(';'))
separator = ';';
continue;
}
2022-09-09 23:48:31 +02:00
string[] lineData = line.Trim().Split(separator);
2022-09-05 22:01:52 +02:00
ParkingPreference preference = lineData[0] == ""
? ParkingPreference.Any
2022-09-04 00:35:45 +02:00
: lineData[1] == "P"
? ParkingPreference.Front
: ParkingPreference.Back;
Size size;
2022-09-10 20:53:06 +02:00
string sizeString = lineData[2];
List<int> chars = new();
foreach (char c in sizeString) {
chars.Add((int)c);
}
if (sizeString == "Mały" || sizeString == "Ma" + (char)0xB3 + "y" || (sizeString.Contains("Ma") && sizeString.Contains("y"))) {
2022-09-10 20:53:06 +02:00
size = Size.A;
}else if (sizeString == "Średni" || sizeString == (char)0xD7 + "redni" || sizeString.Contains("redni")) {
2022-09-10 20:53:06 +02:00
size = Size.B;
}else if (sizeString == "Duży" || sizeString == "Du" + (char)0xBF + "y" || sizeString.Contains("Du")) {
2022-09-10 20:53:06 +02:00
size = Size.C;
}else if (sizeString == "Max") {
size = Size.D;
}
else {
size = Size.A;
}
2022-09-05 22:01:52 +02:00
DateTime realArrival = DateTime.Parse(lineData[5].Contains(':') ? lineData[5] : lineData[5] + ":00",
CultureInfo.InvariantCulture);
DateTime realDeparture = DateTime.Parse(lineData[6].Contains(':') ? lineData[6] : lineData[6] + ":00",
CultureInfo.InvariantCulture);
DateTime plannedArrival = DateTime.Parse(lineData[3].Contains(':') ? lineData[3] : lineData[3] + ":00",
CultureInfo.InvariantCulture);
DateTime plannedDeparture =
DateTime.Parse(lineData[4].Contains(':') ? lineData[4] : lineData[4] + ":00",
CultureInfo.InvariantCulture);
2022-09-04 00:35:45 +02:00
2022-09-05 22:01:52 +02:00
if (!int.TryParse(lineData[0], out int priority))
2022-09-04 00:35:45 +02:00
priority = 3;
2022-09-01 22:31:57 +02:00
2022-09-05 22:01:52 +02:00
if (!int.TryParse(lineData[7], out int updateInterval))
2022-09-04 00:35:45 +02:00
updateInterval = 3;
2022-09-05 22:01:52 +02:00
Driver driver = new(size, counter, preference, priority, updateInterval);
2022-09-04 00:35:45 +02:00
driver.Times = new[] {plannedArrival, plannedDeparture, realArrival, realDeparture};
2022-09-01 22:31:57 +02:00
Drivers.Add(driver);
2022-08-25 18:38:38 +02:00
counter++;
}
2022-09-07 21:47:41 +02:00
return true;
2022-08-25 18:38:38 +02:00
}
}
}