-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleInfoService.cs
120 lines (104 loc) · 4.17 KB
/
VehicleInfoService.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
using PoliceMP.Shared.Models;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using PoliceMP.Core.Server.Interfaces.Factories;
using PoliceMP.Core.Server.Interfaces.Services;
using PoliceMP.Core.Shared.Extensions;
namespace PoliceMP.Server.Services
{
public class VehicleInfoService : IVehicleInfoService
{
private readonly IVehicleInfoFactory _vehicleInfoFactory;
private readonly IPedInfoService _pedInfoService;
private readonly ConcurrentDictionary<int, VehicleInfo> _vehicles;
public VehicleInfoService(IVehicleInfoFactory vehicleInfoFactory, IPedInfoService pedInfoService)
{
_vehicleInfoFactory = vehicleInfoFactory;
_pedInfoService = pedInfoService;
_vehicles = new ConcurrentDictionary<int, VehicleInfo>();
}
public VehicleInfo GetByNetworkId(int networkId, string plate, int ownerPedNetworkId = -1)
{
VehicleInfo vehicleInfo;
if (networkId < 1)
{
vehicleInfo = _vehicles
.FirstOrDefault(x => x.Value.Plate.EqualsIgnoreCase(plate))
.Value;
if (vehicleInfo != null) return vehicleInfo;
}
else if (_vehicles.TryGetValue(networkId, out vehicleInfo))
{
if (vehicleInfo.Plate.EqualsIgnoreCase(plate))
return vehicleInfo;
_vehicles.TryRemove(networkId, out _);
}
string ownerName = null;
if (ownerPedNetworkId > 0)
{
var pedInfo = _pedInfoService.GetByNetworkId(ownerPedNetworkId);
ownerName = pedInfo?.FullName;
}
vehicleInfo = _vehicleInfoFactory.Random(networkId, plate, ownerName);
return _vehicles.TryAdd(networkId, vehicleInfo) ? vehicleInfo : new VehicleInfo { NetworkId = -1 };
}
public bool UpdateMarkers(int networkId, List<string> markers)
{
var tryGet = _vehicles.TryGetValue(networkId, out VehicleInfo vehicleInfo);
if (!tryGet) return false;
vehicleInfo.Markers = markers;
return true;
}
public bool UpdateExpiredMarkers(int networkId, List<bool> expiredMarkers)
{
var tryGet = _vehicles.TryGetValue(networkId, out VehicleInfo vehicleInfo);
if (!tryGet) return false;
var rnd = new Random();
var taxExpired = expiredMarkers[0];
var motExpired = expiredMarkers[1];
var insuranceExpired = expiredMarkers[2];
if (taxExpired != vehicleInfo.IsTaxExpired)
{
if (taxExpired)
{
var taxExpiredDays = rnd.Next(1, 366);
vehicleInfo.TaxExpiryDate = DateTime.Now.AddDays(-taxExpiredDays);
}
else
{
var taxDays = rnd.Next(1, 366);
vehicleInfo.TaxExpiryDate = DateTime.Now.AddDays(taxDays);
}
}
if (motExpired != vehicleInfo.IsMotExpired)
{
if (motExpired)
{
var motExpiredDays = rnd.Next(1, 366);
vehicleInfo.MotExpiryDate = DateTime.Now.AddDays(-motExpiredDays);
}
else
{
var motDays = rnd.Next(1, 366);
vehicleInfo.MotExpiryDate = DateTime.Now.AddDays(motDays);
}
}
if (insuranceExpired != vehicleInfo.IsInsuranceExpired)
{
if (insuranceExpired)
{
var insuranceExpiredDays = rnd.Next(1, 366);
vehicleInfo.InsuranceExpiryDate = DateTime.Now.AddDays(-insuranceExpiredDays);
}
else
{
var insuranceDays = rnd.Next(1, 366);
vehicleInfo.InsuranceExpiryDate = DateTime.Now.AddDays(insuranceDays);
}
}
return true;
}
}
}