Skip to content

Commit 6353cca

Browse files
committed
Addressed some of the PR issues
1 parent 2bb1603 commit 6353cca

File tree

20 files changed

+61
-83
lines changed

20 files changed

+61
-83
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ members = [
4545
"example_apps/stocks_simulated",
4646
"example_apps/kafka_ingress_connector",
4747
"example_apps/kafka_egress_connector",
48-
"example_apps/game",
49-
"example_apps/game/game-model"
48+
"example_apps/game"
5049
]
5150

5251
[workspace.package]
@@ -184,4 +183,5 @@ http-body-util = "0.1.2"
184183
hyper-util = "0.1.5"
185184
rdkafka = "0.36"
186185
apache-avro = "0.17.0"
187-
time = "0.3.36"
186+
time = "0.3.36"
187+
rand_distr = "0.4.3"

example_apps/game/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ clap = { workspace = true, features = ["derive"]}
1515
axum = { version = "0.6.20", features = ["tokio", "http1"]}
1616
tokio-stream = { workspace = true, features = ["io-util"]}
1717
tokio-util = { workspace = true, features = ["io"]}
18-
game-model = { path = "game-model" }
18+
rand = { workspace = true }
19+
rand_distr = { workspace = true }

example_apps/game/game-model/Cargo.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

example_apps/game/game-model/src/lib.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

example_apps/game/src/agents/game.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
use std::{cell::RefCell, collections::VecDeque, time::Duration};
1616

17-
use game_model::game::Game;
17+
use crate::generator::game::Game;
1818
use swimos::agent::{
19-
agent_lifecycle::utility::HandlerContext,
19+
agent_lifecycle::HandlerContext,
2020
event_handler::{EventHandler, HandlerActionExt},
2121
lanes::{CommandLane, MapLane, ValueLane},
2222
lifecycle, projections, AgentLaneModel,
@@ -126,8 +126,8 @@ fn generate_match(
126126
let round = game.generate_round();
127127
context.send_command(
128128
None,
129-
format!("/match/{id}", id = round.id),
130-
"publish".to_string(),
129+
format!("/match/{id}", id = round.id).as_str(),
130+
"publish",
131131
round,
132132
)
133133
}

example_apps/game/src/agents/leaderboard.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
use std::{cell::RefCell, collections::HashMap, time::Duration};
1616

17-
use game_model::config;
17+
use crate::generator::config;
1818
use swimos::{
1919
agent::{
20-
agent_lifecycle::utility::HandlerContext,
20+
agent_lifecycle::HandlerContext,
2121
event_handler::{EventHandler, HandlerAction, HandlerActionExt},
2222
lanes::{CommandLane, DemandLane, JoinValueLane, ValueLane},
2323
lifecycle, projections, AgentLaneModel,

example_apps/game/src/agents/model/leaderboard.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{cmp::Ordering, sync::Arc};
15+
use std::cmp::Ordering;
1616

1717
use super::stats::PlayerTotals;
1818

19-
type Comparator = Arc<dyn Fn(&PlayerTotals, &PlayerTotals) -> Ordering + Sync + Send>;
19+
type Comparator = fn(&PlayerTotals, &PlayerTotals) -> Ordering;
2020

2121
#[derive(Clone)]
2222
pub struct Leaderboard {
@@ -31,30 +31,30 @@ impl Leaderboard {
3131
pub fn kill_count(capacity: usize) -> Self {
3232
Leaderboard::new(
3333
capacity,
34-
Arc::new(|p1, p2| p1.total_kills.cmp(&p2.total_kills)),
34+
|p1, p2| p1.total_kills.cmp(&p2.total_kills),
3535
)
3636
}
3737

3838
pub fn death_count(capacity: usize) -> Self {
3939
Leaderboard::new(
4040
capacity,
41-
Arc::new(|p1, p2| p1.total_deaths.cmp(&p2.total_deaths)),
41+
|p1, p2| p1.total_deaths.cmp(&p2.total_deaths),
4242
)
4343
}
4444

4545
pub fn kd_ratio(capacity: usize) -> Self {
4646
Leaderboard::new(
4747
capacity,
48-
Arc::new(|p1, p2| {
48+
|p1, p2| {
4949
p1.kd_ratio
5050
.partial_cmp(&p2.kd_ratio)
5151
.unwrap_or_else(|| p1.total_kills.cmp(&p2.total_kills))
52-
}),
52+
},
5353
)
5454
}
5555

5656
pub fn xp(capacity: usize) -> Self {
57-
Leaderboard::new(capacity, Arc::new(|p1, p2| p1.total_xp.cmp(&p2.total_xp)))
57+
Leaderboard::new(capacity, |p1, p2| p1.total_xp.cmp(&p2.total_xp))
5858
}
5959

6060
pub fn new(capacity: usize, f: Comparator) -> Self {
@@ -93,7 +93,7 @@ impl Leaderboard {
9393
return false;
9494
};
9595

96-
let mover_stats = self.items.get(index).unwrap();
96+
let mover_stats = &self.items[index];
9797
if (self.comparator)(mover_stats, self.items.get(index + 1).unwrap()) != Ordering::Less {
9898
return false;
9999
}
@@ -108,7 +108,7 @@ impl Leaderboard {
108108
return false;
109109
};
110110

111-
let mover_stats = self.items.get(index).unwrap();
111+
let mover_stats = &self.items[index];
112112
if (self.comparator)(mover_stats, self.items.get(index - 1).unwrap()) != Ordering::Greater {
113113
return false;
114114
};

example_apps/game/src/agents/model/stats.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use game_model::round::{PlayerRound, Round};
15+
use crate::generator::round::{PlayerRound, Round};
1616
use std::collections::HashMap;
1717
use swimos_form::Form;
1818

@@ -35,8 +35,8 @@ pub struct MatchSummary {
3535
pub player_stats: HashMap<usize, MatchStats>,
3636
}
3737

38-
impl MatchSummary {
39-
pub fn from_round(round: Round) -> MatchSummary {
38+
impl From<Round> for MatchSummary {
39+
fn from(round: Round) -> MatchSummary {
4040
MatchSummary {
4141
match_id: round.id,
4242
duration: round.duration,

example_apps/game/src/agents/player.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
use std::{cell::RefCell, collections::VecDeque};
1616

17-
use game_model::gamertag;
17+
use crate::generator::gamertag;
1818
use swimos::agent::{
19-
agent_lifecycle::utility::HandlerContext,
19+
agent_lifecycle::HandlerContext,
2020
event_handler::{EventHandler, HandlerActionExt},
2121
lanes::{CommandLane, MapLane, ValueLane},
2222
lifecycle, projections, AgentLaneModel,
@@ -109,7 +109,7 @@ impl PlayerLifecycle {
109109
}),
110110
)
111111
.followed_by(context.get_agent_uri().and_then(move |uri| {
112-
context.send_command(None, "/player".to_string(), "addPlayer".to_string(), uri)
112+
context.send_command(None, "/player", "addPlayer", uri)
113113
}))
114114
}
115115

example_apps/game/src/agents/round.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use game_model::round::Round;
15+
use crate::generator::round::Round;
1616
use swimos::agent::{
17-
agent_lifecycle::utility::HandlerContext,
17+
agent_lifecycle::HandlerContext,
1818
event_handler::{EventHandler, HandlerActionExt, Sequentially},
1919
lanes::{CommandLane, ValueLane},
2020
lifecycle, projections, AgentLaneModel,
@@ -51,7 +51,7 @@ impl MatchLifecycle {
5151
info!(id = round.id, "New match published.");
5252
round
5353
})
54-
.map(MatchSummary::from_round)
54+
.map(Round::into)
5555
.and_then(move |summary: MatchSummary| {
5656
context
5757
.set_value(MatchAgent::STATS, Some(summary.clone()))
@@ -81,14 +81,14 @@ fn forward_match_summary(
8181
let forward_to_players = match_summary
8282
.player_stats
8383
.keys()
84-
.map(|id| command_match_summary(context, match_summary.clone(), format!("/player/{id}")))
84+
.map(|id| command_match_summary(context, match_summary.clone(), format!("/player/{id}").as_str()))
8585
.collect::<Vec<_>>();
8686
let forward_to_teams = match_summary
8787
.team_stats
8888
.keys()
89-
.map(|name| command_match_summary(context, match_summary.clone(), format!("/team/{name}")))
89+
.map(|name| command_match_summary(context, match_summary.clone(), format!("/team/{name}").as_str()))
9090
.collect::<Vec<_>>();
91-
let forward_to_game = command_match_summary(context, match_summary, "/match".to_string());
91+
let forward_to_game = command_match_summary(context, match_summary, "/match");
9292
forward_to_game
9393
.followed_by(Sequentially::new(forward_to_players))
9494
.followed_by(Sequentially::new(forward_to_teams))
@@ -97,7 +97,7 @@ fn forward_match_summary(
9797
fn command_match_summary(
9898
context: HandlerContext<MatchAgent>,
9999
match_summary: MatchSummary,
100-
node_uri: String,
100+
node_uri: &str,
101101
) -> impl EventHandler<MatchAgent> {
102-
context.send_command(None, node_uri, "addMatch".to_string(), match_summary)
102+
context.send_command(None, node_uri, "addMatch", match_summary)
103103
}

0 commit comments

Comments
 (0)