@@ -35,6 +35,7 @@ private void Awake()
35
35
[ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Code Quality" , "IDE0051:Remove unused private members" ) ]
36
36
private void Start ( )
37
37
{
38
+ StartHooks ( ) ;
38
39
Reading ( ) ;
39
40
}
40
41
@@ -56,79 +57,85 @@ private async void Reading()
56
57
57
58
//read out of the file until the EOF
58
59
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
+ }
60
71
61
72
//update the last max offset
62
73
lastMaxOffset = reader . BaseStream . Position ;
63
74
}
64
75
}
65
76
}
66
-
67
77
68
- public static void InitializeHooks ( )
78
+
79
+ public static void StartHooks ( )
69
80
{
70
81
// On run end
82
+ // LogTime and LogStagesCleared won't be needed after stats are done
71
83
On . RoR2 . RunReport . Generate += ( orig , run , resulttype ) =>
72
84
{
73
85
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
+
74
92
LogTime ( ) ;
75
93
LogStagesCleared ( ) ;
76
94
return valid ; // Required if the hooked command has a return value
77
95
} ;
78
96
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 ) =>
81
102
{
82
- orig ( run ) ;
83
103
if ( Run . instance )
84
104
{
105
+ NetworkUser user = FindNetworkUserForConnectionServer ( conn ) ;
106
+ GetStats ( user ) ;
107
+
85
108
LogTime ( ) ;
86
109
LogStagesCleared ( ) ;
87
110
}
111
+ orig ( run , conn ) ;
88
112
} ;
89
113
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 ) =>
92
116
{
93
- orig ( run , conn ) ;
117
+ orig ( run ) ;
94
118
if ( Run . instance )
95
119
{
96
120
LogTime ( ) ;
97
121
LogStagesCleared ( ) ;
98
122
}
99
123
} ;
100
124
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 ) =>
106
128
{
129
+ orig ( run , conn ) ;
107
130
if ( Run . instance )
108
131
{
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
-
125
132
LogTime ( ) ;
126
133
LogStagesCleared ( ) ;
127
134
}
128
- orig ( run , conn ) ;
129
135
} ;
130
136
}
131
137
138
+ // Will be removed on new stat tracking
132
139
private static void LogTime ( )
133
140
{
134
141
if ( ! Run . instance )
@@ -138,6 +145,7 @@ private static void LogTime()
138
145
Debug . Log ( "Run time is " + Run . instance . GetRunStopwatch ( ) . ToString ( ) ) ;
139
146
}
140
147
148
+ // Will be removed on new stat tracking
141
149
private static void LogStagesCleared ( )
142
150
{
143
151
if ( ! Run . instance )
@@ -147,6 +155,24 @@ private static void LogStagesCleared()
147
155
Debug . Log ( "Stages cleared: " + Run . instance . NetworkstageClearCount . ToString ( ) ) ;
148
156
}
149
157
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
+
150
176
// Borrowed from R2DSEssentials.Util.Networking
151
177
private static NetworkUser FindNetworkUserForConnectionServer ( NetworkConnection connection )
152
178
{
0 commit comments