@@ -4,8 +4,7 @@ use crate::epsilon::server::instance_type::InstanceType;
4
4
use crate :: k8s:: label:: Label ;
5
5
use crate :: { EResult , Kube } ;
6
6
use anyhow:: format_err;
7
- use async_minecraft_ping:: ServerDescription :: Plain ;
8
- use async_minecraft_ping:: { ConnectionConfig , ServerPlayers , ServerVersion , StatusResponse } ;
7
+ use async_minecraft_ping:: { ConnectionConfig , StatusResponse } ;
9
8
use k8s_openapi:: api:: core:: v1:: Pod ;
10
9
use std:: sync:: Arc ;
11
10
use std:: time:: Duration ;
@@ -103,27 +102,20 @@ impl Instance {
103
102
let timeout = Duration :: from_millis ( 500 ) ;
104
103
let config = ConnectionConfig :: build ( address) . with_timeout ( timeout) ;
105
104
106
- match config. connect ( ) . await {
107
- Ok ( connection) => Ok ( connection. status ( ) . await ?. status ) ,
108
- Err ( _) => Ok ( StatusResponse {
109
- version : ServerVersion {
110
- name : "" . to_string ( ) ,
111
- protocol : 0 ,
112
- } ,
113
- players : ServerPlayers {
114
- max : 10 ,
115
- online : 0 ,
116
- sample : None ,
117
- } ,
118
- description : Plain ( "Description" . to_string ( ) ) ,
119
- favicon : None ,
120
- } ) ,
121
- }
105
+ Ok ( config. connect ( ) . await . unwrap ( ) . status ( ) . await ?. status )
122
106
} else {
123
107
Err ( format_err ! ( "No address found" ) )
124
108
}
125
109
}
126
110
111
+ pub async fn get_online_count ( & self ) -> i32 {
112
+ self . get_info ( ) . await . unwrap ( ) . players . online as i32
113
+ }
114
+
115
+ pub async fn get_available_slots ( & self ) -> i32 {
116
+ self . get_instance_slots ( ) - self . get_online_count ( ) . await
117
+ }
118
+
127
119
pub fn need_close ( & self ) -> bool {
128
120
let phase = self . pod . status . as_ref ( ) . unwrap ( ) . phase . as_ref ( ) . unwrap ( ) ;
129
121
@@ -143,7 +135,7 @@ impl Instance {
143
135
. any ( |condition| condition. type_ == "Ready" && condition. status == "True" )
144
136
&& status. phase . as_ref ( ) . unwrap ( ) == "Running" ;
145
137
146
- let label = & labels. get ( "epsilon.fr/in-game" ) ;
138
+ let label = & labels. get ( Label :: IN_GAME_LABEL ) ;
147
139
let is_in_game = label. is_some ( ) && label. unwrap ( ) == "true" ;
148
140
149
141
let is_stopping = metadata. deletion_timestamp . is_some ( ) || self . need_close ( ) ;
@@ -165,7 +157,7 @@ impl Instance {
165
157
. labels
166
158
. as_ref ( )
167
159
. unwrap ( )
168
- . contains_key ( "epsilon.fr/main" )
160
+ . contains_key ( Label :: DEFAULT_LABEL )
169
161
}
170
162
171
163
pub async fn to_json ( & self ) -> InstanceJson {
@@ -186,16 +178,27 @@ impl Instance {
186
178
187
179
#[ async_trait]
188
180
pub trait VectorOfInstance {
189
- async fn get_online_count ( & self ) -> EResult < u32 > ;
181
+ async fn get_available_slots ( & self ) -> EResult < i32 > ;
182
+ async fn get_online_count ( & self ) -> EResult < i32 > ;
190
183
}
191
184
192
185
#[ async_trait]
193
186
impl VectorOfInstance for Vec < Instance > {
194
- async fn get_online_count ( & self ) -> EResult < u32 > {
187
+ async fn get_available_slots ( & self ) -> EResult < i32 > {
188
+ let mut available_slots = 0 ;
189
+
190
+ for instance in self {
191
+ available_slots += instance. get_available_slots ( ) . await ;
192
+ }
193
+
194
+ Ok ( available_slots)
195
+ }
196
+
197
+ async fn get_online_count ( & self ) -> EResult < i32 > {
195
198
let mut number = 0 ;
196
199
197
200
for instance in self {
198
- number += instance. get_info ( ) . await ? . players . online ;
201
+ number += instance. get_online_count ( ) . await
199
202
}
200
203
201
204
Ok ( number)
0 commit comments