-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExposeSessionStatus.cs
73 lines (66 loc) · 2.56 KB
/
ExposeSessionStatus.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
using FrooxEngine;
using Newtonsoft.Json;
using ResoniteModLoader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Timers;
namespace ExposeSessionStatus
{
public class SessionExposer : ResoniteMod
{
private const string DataHandlerUrl = "http://localhost:9393/ingest/";
private static readonly HttpClient SharedClient = new HttpClient();
public override string Name => "ExposeSessionStatus";
public override string Author => "Nutcake";
public override string Version => "0.0.3";
public override void OnEngineInit()
{
Engine.Current.RunPostInit(() =>
{
Timer timer = new Timer(5000.0);
timer.Elapsed += WorldsChangedListener;
timer.AutoReset = true;
timer.Enabled = true;
});
}
private static async void WorldsChangedListener(object o, ElapsedEventArgs args)
{
try
{
var source = new List<World>();
Engine.Current.WorldManager.GetWorlds(source);
var dictionary = source
.Where(world => !string.IsNullOrEmpty(world.RawName) && world.RawName != "Local" && world.RawName != "Userspace").ToDictionary(world => world.RawName, world =>
new Dictionary<string, object>
{
{
"activeUserCount",
world.ActiveUserCount
},
{
"userCount",
world.UserCount
},
{
"accessLevel",
world.AccessLevel.ToString()
},
{
"hidden",
world.HideFromListing
}
});
if (dictionary.Count == 0)
return;
var content = JsonConvert.SerializeObject(dictionary);
await SharedClient.PostAsync(DataHandlerUrl + Engine.Current.Cloud.Session.CurrentUsername, new StringContent(content));
}
catch (Exception ex)
{
Console.WriteLine("[ExposeSessionStatus] Failed to send session status: " + ex.Message + ex.StackTrace);
}
}
}
}