Skip to content

Commit c8c41a6

Browse files
committed
Try to correct bug and refactor little bit
1 parent 72706d7 commit c8c41a6

File tree

3 files changed

+57
-57
lines changed

3 files changed

+57
-57
lines changed

src/epsilon/server/instance.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,8 @@ impl Instance {
112112
Ok(self.get_info().await?.players.online as i32)
113113
}
114114

115-
pub async fn get_available_slots(&self) -> i32 {
116-
let online_count_result = self.get_online_count().await;
117-
118-
match online_count_result {
119-
Ok(online_count) => self.get_instance_slots() - online_count,
120-
Err(_) => 0
121-
}
115+
pub async fn get_available_slots(&self) -> EResult<i32> {
116+
Ok(self.get_instance_slots() - self.get_online_count().await?)
122117
}
123118

124119
pub fn is_succeeded(&self) -> bool {
@@ -188,7 +183,7 @@ impl Instance {
188183
#[async_trait]
189184
pub trait VectorOfInstance {
190185
async fn get_available_slots(&self) -> EResult<i32>;
191-
async fn get_online_count(&self) -> EResult<i32>;
186+
async fn get_online_count(&self) -> i32;
192187
}
193188

194189
#[async_trait]
@@ -197,19 +192,19 @@ impl VectorOfInstance for Vec<Instance> {
197192
let mut available_slots = 0;
198193

199194
for instance in self {
200-
available_slots += instance.get_available_slots().await;
195+
available_slots += instance.get_available_slots().await?;
201196
}
202197

203198
Ok(available_slots)
204199
}
205200

206-
async fn get_online_count(&self) -> EResult<i32> {
201+
async fn get_online_count(&self) -> i32 {
207202
let mut number = 0;
208203

209204
for instance in self {
210205
number += instance.get_online_count().await.unwrap_or(0)
211206
}
212207

213-
Ok(number)
208+
number
214209
}
215210
}

src/tasks/hub_task.rs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,47 +70,45 @@ impl Task for HubTask {
7070
)
7171
.await?;
7272

73-
if let Ok(hub_online_count) = hubs_ready.get_online_count().await {
74-
let hub_number = hubs_ready.len() as u32;
73+
let hub_online_count = hubs_ready.get_online_count().await;
74+
let hub_number = hubs_ready.len() as u32;
7575

76-
let hub_necessary = ((hub_online_count as f32 * 1.6
77-
/ self.hub_template.slots as f32)
78-
+ 1.0) as u32;
76+
let hub_necessary =
77+
((hub_online_count as f32 * 1.6 / self.hub_template.slots as f32) + 1.0) as u32;
7978

80-
if hub_number < hub_necessary {
81-
self.instance_provider.start_instance(template_name).await?;
82-
}
79+
if hub_number < hub_necessary {
80+
self.instance_provider.start_instance(template_name).await?;
81+
}
8382

84-
if hub_number > hub_necessary {
85-
if self.time != *DEFAULT_TIME {
86-
self.time += 1;
83+
if hub_number > hub_necessary {
84+
if self.time != *DEFAULT_TIME {
85+
self.time += 1;
8786

88-
return Ok(());
89-
} else {
90-
self.time = 0;
91-
}
87+
return Ok(());
88+
} else {
89+
self.time = 0;
90+
}
9291

93-
let mut n = 0;
94-
let mut hub_option = None;
92+
let mut n = 0;
93+
let mut hub_option = None;
9594

96-
for instance in hubs_ready {
97-
let online_player_result = instance.get_online_count().await;
95+
for instance in hubs_ready {
96+
let online_player_result = instance.get_online_count().await;
9897

99-
if let Ok(online_player) = online_player_result {
100-
if instance.get_state() == EpsilonState::Running && online_player <= n {
101-
n = online_player;
102-
hub_option = Some(instance);
103-
}
104-
};
105-
}
98+
if let Ok(online_player) = online_player_result {
99+
if instance.get_state() == EpsilonState::Running && online_player <= n {
100+
n = online_player;
101+
hub_option = Some(instance);
102+
}
103+
};
104+
}
106105

107-
if let Some(hub) = hub_option {
108-
let name = hub.get_name();
106+
if let Some(hub) = hub_option {
107+
let name = hub.get_name();
109108

110-
self.instance_provider.remove_instance(name).await?;
109+
self.instance_provider.remove_instance(name).await?;
111110

112-
info!("Hub {} is removed with {} online players", name, n);
113-
}
111+
info!("Hub {} is removed with {} online players", name, n);
114112
}
115113
}
116114
}

src/tasks/queue_task.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,36 @@ impl Task for QueueTask {
5353
)
5454
.await?;
5555

56-
if instances_starting.is_empty()
57-
&& (instances_ready.is_empty()
58-
|| instances_ready.get_available_slots().await? < 1)
59-
{
56+
if instances_starting.is_empty() && instances_ready.is_empty() {
6057
self.instance_provider.start_instance(template_name).await?;
6158
}
6259

63-
for instance in instances_ready {
64-
let mut available_slots = instance.get_available_slots().await;
60+
let ready_available_slots_result = instances_ready.get_available_slots().await;
6561

66-
while !queue.is_empty() && available_slots > 0 {
67-
if let Some(group) = queue.pop() {
68-
let group_size = group.players.len() as i32;
62+
if let Ok(ready_available_slots) = ready_available_slots_result {
63+
if instances_starting.is_empty() && ready_available_slots < 1 {
64+
self.instance_provider.start_instance(template_name).await?;
65+
}
66+
}
67+
68+
for instance in instances_ready {
69+
if let Ok(mut available_slots) = instance.get_available_slots().await {
70+
while !queue.is_empty() && available_slots > 0 {
71+
if let Some(group) = queue.pop() {
72+
let group_size = group.players.len() as i32;
6973

70-
if group_size <= available_slots {
71-
info!("Sending group ({})", available_slots);
74+
if group_size <= available_slots {
75+
info!("Sending group ({})", available_slots);
7276

73-
available_slots -= group_size;
77+
available_slots -= group_size;
7478

75-
info!("New sending group ({})", available_slots);
79+
info!("New sending group ({})", available_slots);
7680

77-
self.epsilon_api
78-
.send(SendToServer(group, String::from(instance.get_name())));
81+
self.epsilon_api.send(SendToServer(
82+
group,
83+
String::from(instance.get_name()),
84+
));
85+
}
7986
}
8087
}
8188
}

0 commit comments

Comments
 (0)