Skip to content

Commit 2565dc7

Browse files
committed
Able to read stats from all players on run end
1 parent cdfec00 commit 2565dc7

File tree

3 files changed

+75
-31
lines changed

3 files changed

+75
-31
lines changed

BotCommands.cs

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private void Awake()
3535
[System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members")]
3636
private void Start()
3737
{
38+
StartHooks();
3839
Reading();
3940
}
4041

@@ -56,79 +57,85 @@ private async void Reading()
5657

5758
//read out of the file until the EOF
5859
while ((line = reader.ReadLine()) != null)
59-
RoR2.Console.instance.SubmitCmd(null, line);
60+
{
61+
// Exception handling, I guess
62+
try
63+
{
64+
RoR2.Console.instance.SubmitCmd(null, line);
65+
}
66+
catch
67+
{
68+
Debug.Log("No sir, partner.");
69+
}
70+
}
6071

6172
//update the last max offset
6273
lastMaxOffset = reader.BaseStream.Position;
6374
}
6475
}
6576
}
66-
6777

68-
public static void InitializeHooks()
78+
79+
public static void StartHooks()
6980
{
7081
// On run end
82+
// LogTime and LogStagesCleared won't be needed after stats are done
7183
On.RoR2.RunReport.Generate += (orig, run, resulttype) =>
7284
{
7385
RunReport valid = orig(run, resulttype); // Required if the hooked command has a return value
86+
87+
foreach (var user in NetworkUser.readOnlyInstancesList)
88+
{
89+
GetStats(user);
90+
}
91+
7492
LogTime();
7593
LogStagesCleared();
7694
return valid; // Required if the hooked command has a return value
7795
};
7896

79-
// On scene change (unloaded, new scene not yet loaded)
80-
On.RoR2.FadeToBlackManager.OnSceneUnloaded += (orig, run) =>
97+
// On player leave
98+
// NOTE: Ensure that if a player joins and leaves multiple times during a run, their stats aren't reset. Maybe cache the stats for each player in the run locally and only upload to the DB when the run ends
99+
// Alternatively, make it so stats are only logged if you complete a run (aka delete this hook)
100+
// LogTime and LogStagesCleared won't be needed after stats are done
101+
On.RoR2.Networking.GameNetworkManager.OnServerDisconnect += (orig, run, conn) =>
81102
{
82-
orig(run);
83103
if (Run.instance)
84104
{
105+
NetworkUser user = FindNetworkUserForConnectionServer(conn);
106+
GetStats(user);
107+
85108
LogTime();
86109
LogStagesCleared();
87110
}
111+
orig(run, conn);
88112
};
89113

90-
// On player join
91-
On.RoR2.Networking.GameNetworkManager.OnServerConnect += (orig, run, conn) =>
114+
// On scene change (unloaded, new scene not yet loaded)
115+
On.RoR2.FadeToBlackManager.OnSceneUnloaded += (orig, run) =>
92116
{
93-
orig(run, conn);
117+
orig(run);
94118
if (Run.instance)
95119
{
96120
LogTime();
97121
LogStagesCleared();
98122
}
99123
};
100124

101-
// On player leave
102-
// Currently works when they leave mid-game
103-
// Needs to be used for end of game as well, changed to support each player (will have to retrieve all networkusers)
104-
// NOTE: Ensure that if a player joins and leaves multiple times during a game, their stats aren't multiplied. Maybe cache the stats for each player in the run locally and only upload to the DB when the run ends
105-
On.RoR2.Networking.GameNetworkManager.OnServerDisconnect += (orig, run, conn) =>
125+
// On player join
126+
// Will be removed on new stat tracking
127+
On.RoR2.Networking.GameNetworkManager.OnServerConnect += (orig, run, conn) =>
106128
{
129+
orig(run, conn);
107130
if (Run.instance)
108131
{
109-
// Stats
110-
NetworkUser user = FindNetworkUserForConnectionServer(conn);
111-
GameObject playerMasterObject = user.masterObject;
112-
StatSheet statSheet;
113-
PlayerStatsComponent component = playerMasterObject.GetComponent<PlayerStatsComponent>();
114-
statSheet = ((component != null) ? component.currentStats : null);
115-
// Print the statsheet to console / log
116-
// Will be changing this to parse and add to the database
117-
// Don't need all the stats they access though, should only use some of the fields (may be able to split up by category)
118-
string[] array = new string[statSheet.fields.Length];
119-
for (int i = 0; i < array.Length; i++)
120-
{
121-
array[i] = string.Format("[\"{0}\"]={1}", statSheet.fields[i].name, statSheet.fields[i].ToString());
122-
}
123-
Debug.Log(string.Join("\n", array));
124-
125132
LogTime();
126133
LogStagesCleared();
127134
}
128-
orig(run, conn);
129135
};
130136
}
131137

138+
// Will be removed on new stat tracking
132139
private static void LogTime()
133140
{
134141
if (!Run.instance)
@@ -138,6 +145,7 @@ private static void LogTime()
138145
Debug.Log("Run time is " + Run.instance.GetRunStopwatch().ToString());
139146
}
140147

148+
// Will be removed on new stat tracking
141149
private static void LogStagesCleared()
142150
{
143151
if (!Run.instance)
@@ -147,6 +155,24 @@ private static void LogStagesCleared()
147155
Debug.Log("Stages cleared: " + Run.instance.NetworkstageClearCount.ToString());
148156
}
149157

158+
private static void GetStats(NetworkUser user)
159+
{
160+
GameObject playerMasterObject = user.masterObject;
161+
StatSheet statSheet;
162+
PlayerStatsComponent component = playerMasterObject.GetComponent<PlayerStatsComponent>();
163+
statSheet = (component?.currentStats);
164+
// Print the statsheet to console / log
165+
// Will be changing this to parse and add to the database
166+
// Don't need all the stats they access though, should only use some of the fields (may be able to split up by category)
167+
string[] array = new string[statSheet.fields.Length];
168+
for (int i = 0; i < array.Length; i++)
169+
{
170+
array[i] = string.Format("[\"{0}\"]={1}", statSheet.fields[i].name, statSheet.fields[i].ToString());
171+
}
172+
// Literally all I have to do is parse the array to be used by the db
173+
Debug.Log(string.Join("\n", array));
174+
}
175+
150176
// Borrowed from R2DSEssentials.Util.Networking
151177
private static NetworkUser FindNetworkUserForConnectionServer(NetworkConnection connection)
152178
{

StatsToTrack.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
["totalTimeAlive"]=23.0000011995435
2+
["totalKills"]=0
3+
["totalDeaths"]=0
4+
["totalGoldCollected"]=15
5+
["totalDistanceTraveled"]=2520.38142833216
6+
["totalItemsCollected"]=1
7+
["totalStagesCompleted"]=0
8+
["totalPurchases"]=0

dynamodb.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace BotCommands
6+
{
7+
class Dynamodb
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)