generated from petrspelos/template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
99 lines (77 loc) · 2.91 KB
/
Program.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
using Microsoft.EntityFrameworkCore;
using System.Text;
using VrRetreat.Infrastructure;
using VrRetreat.Infrastructure.Entities;
var logBuilder = new StringBuilder();
void Log(string message)
{
logBuilder.AppendLine($"{DateTime.Now:G} - {message}");
Console.WriteLine($"{DateTime.Now:G} - {message}");
};
DbContextOptions<ApplicationDbContext> options = new();
var context = new ApplicationDbContext(options, "connString.txt");
bool ShouldUserBeChecked(VrRetreatUser user)
=> user is not null && user.IsParticipating && !user.FailedChallenge;
var vrc = new VrChat(JsonConfiguration.FromFile("VrChatConfig.json"));
var usersToCheck = context.Users.Where(ShouldUserBeChecked).ToList();
var challengeStart = new DateOnly(2022, 1, 1);
var logoutRequired = false;
foreach (var user in usersToCheck)
{
await Task.Delay(TimeSpan.FromMinutes(1));
Log($"Processing {user.UserName}");
var vrcUser = await vrc.GetPlayerByIdAsync(user.VrChatId);
logoutRequired = true;
if (vrcUser is null)
{
//NOTE(Peter): If we fail to fetch a user from vrc
// then something larger than us failed
// let's ignore it for now.
Log("Couldn't fetch VRChat account information. Skipping...");
continue;
}
if (user.VrChatAvatarUrl != vrcUser.AvatarUrl)
user.VrChatAvatarUrl = vrcUser.AvatarUrl;
if (user.VrChatLastLogin is null)
{
Log("The user doesn't have a last known login time.");
if (vrcUser.LastLogin is null)
{
Log("VRChat was unable to provide a last login time. Forced to skip...");
continue;
}
Log("Updating last login time...");
user.VrChatLastLogin = vrcUser.LastLogin;
continue;
}
if (vrcUser.LastLogin is null)
{
Log("LastLogin is null, the user most likely unfriended the bot. Failing the challenge...");
user.FailedChallenge = true;
continue;
}
var vrcLastLogin = vrcUser.LastLogin.Value;
var knownLastLogin = user.VrChatLastLogin.Value;
if (DateOnly.FromDateTime(vrcLastLogin) <= challengeStart)
{
Log("The last login is within the challenge's grace period. Applying last login.");
if (vrcLastLogin != knownLastLogin)
user.VrChatLastLogin = vrcLastLogin;
continue;
}
if (DateOnly.FromDateTime(vrcLastLogin) > DateOnly.FromDateTime(knownLastLogin))
{
Log($"The user failed the challenge because {vrcLastLogin}(as a day) > {knownLastLogin}(as a day)");
user.FailedChallenge = true;
user.VrChatLastLogin = vrcLastLogin;
}
else
{
Log($"The user is still in the challenge because {vrcLastLogin}(as a day) <= {knownLastLogin}(as a day)");
}
}
await context.SaveChangesAsync();
if (logoutRequired)
await vrc.LogoutAsync();
await context.DisposeAsync();
File.WriteAllText("bgworker.log", logBuilder.ToString());