Compare commits
No commits in common. "91d0f50fd42e214f9cc7d1166bcc354babb5e17a" and "15a2f37a3d1bbf84ebb548d2a40fa64089d88218" have entirely different histories.
91d0f50fd4
...
15a2f37a3d
@ -1,20 +0,0 @@
|
||||
name: Podman Image CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["main"]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Podman
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y podman
|
||||
- name: Build the image
|
||||
run: podman build --tag git.jeffa.io/jeff/trade_bot .
|
||||
- name: Push the image
|
||||
run: podman push git.jeffa.io/jeff/trade_bot
|
@ -6,7 +6,6 @@ orientation = 0
|
||||
"iron|sword.greatsword|sword.long" = 1_000
|
||||
|
||||
[sell_prices]
|
||||
"food.cheese" = 1_000_000
|
||||
"consumable.potion_minor" = 150
|
||||
|
||||
# Modular Weapons
|
||||
|
157
src/bot.rs
157
src/bot.rs
@ -33,13 +33,18 @@ use crate::config::PriceList;
|
||||
|
||||
const COINS: ItemDefinitionId =
|
||||
ItemDefinitionId::Simple(Cow::Borrowed("common.items.utility.coins"));
|
||||
const CLIENT_TPS: Duration = Duration::from_millis(33);
|
||||
const TRADE_ACTION_DELAY: Duration = Duration::from_millis(300);
|
||||
const ACCOUNCEMENT_DELAY: Duration = Duration::from_mins(45);
|
||||
const OUCH_DELAY: Duration = Duration::from_secs(2);
|
||||
|
||||
/// TODO: Implement Display
|
||||
#[derive(Debug)]
|
||||
pub struct Reciept {
|
||||
pub my_items: HashMap<String, u32>,
|
||||
pub their_items: HashMap<String, u32>,
|
||||
}
|
||||
|
||||
/// An active connection to the Veloren server that will attempt to run every time the `tick`
|
||||
/// function is called.
|
||||
///
|
||||
/// See the [module-level documentation](index.html) for more information.
|
||||
pub struct Bot {
|
||||
username: String,
|
||||
position: Pos,
|
||||
@ -86,7 +91,7 @@ impl Bot {
|
||||
log::info!("Connecting to veloren");
|
||||
|
||||
let mut client = connect_to_veloren(game_server, auth_server, &username, password)?;
|
||||
let mut clock = Clock::new(CLIENT_TPS);
|
||||
let mut clock = Clock::new(Duration::from_secs_f64(1.0 / 30.0));
|
||||
|
||||
client.load_character_list();
|
||||
|
||||
@ -129,7 +134,10 @@ impl Bot {
|
||||
let position = if let Some(coords) = position {
|
||||
Pos(coords.into())
|
||||
} else {
|
||||
client.position().map(Pos).ok_or("Failed to get position")?
|
||||
client
|
||||
.position()
|
||||
.map(|coords| Pos(coords))
|
||||
.ok_or("Failed to get position")?
|
||||
};
|
||||
let orientation = if let Some(orientation) = orientation {
|
||||
Ori::new(Quaternion::rotation_z(orientation.to_radians()))
|
||||
@ -184,7 +192,7 @@ impl Bot {
|
||||
self.handle_veloren_event(event)?;
|
||||
}
|
||||
|
||||
if self.last_trade_action.elapsed() > TRADE_ACTION_DELAY {
|
||||
if self.last_trade_action.elapsed() > Duration::from_millis(300) {
|
||||
self.client.respawn();
|
||||
self.handle_position_and_orientation()?;
|
||||
self.handle_lantern();
|
||||
@ -224,7 +232,7 @@ impl Bot {
|
||||
}
|
||||
}
|
||||
|
||||
if self.last_announcement.elapsed() > ACCOUNCEMENT_DELAY {
|
||||
if self.last_announcement.elapsed() > Duration::from_mins(45) {
|
||||
self.handle_announcement()?;
|
||||
|
||||
self.last_announcement = Instant::now();
|
||||
@ -297,7 +305,7 @@ impl Bot {
|
||||
}
|
||||
"price" => {
|
||||
for item_name in split_content {
|
||||
self.send_price_info(&sender, item_name)?;
|
||||
self.send_price_info(&sender, &item_name)?;
|
||||
}
|
||||
|
||||
None
|
||||
@ -364,7 +372,7 @@ impl Bot {
|
||||
..
|
||||
}) => {
|
||||
if let Some(uid) = self.client.uid() {
|
||||
if uid == target && self.last_ouch.elapsed() > OUCH_DELAY {
|
||||
if uid == target && self.last_ouch.elapsed() > Duration::from_secs(2) {
|
||||
self.client
|
||||
.send_command("say".to_string(), vec!["Ouch!".to_string()]);
|
||||
|
||||
@ -380,7 +388,7 @@ impl Bot {
|
||||
if let Some(uid) = self.client.uid() {
|
||||
if uid == info.target
|
||||
&& info.amount.is_sign_negative()
|
||||
&& self.last_ouch.elapsed() > OUCH_DELAY
|
||||
&& self.last_ouch.elapsed() > Duration::from_secs(1)
|
||||
{
|
||||
self.client
|
||||
.send_command("say".to_string(), vec!["That hurt!".to_string()]);
|
||||
@ -511,8 +519,6 @@ impl Bot {
|
||||
/// 6. If the total value of my offer is greater than the total value of their offer:
|
||||
/// 1. If I am offering coins, remove them to balance.
|
||||
/// 2. If I am not offering coins, add theirs to balance.
|
||||
/// 7. If the trade is still unbalanced, tell them the value of the greater offer and the
|
||||
/// other party's total coin amount.
|
||||
///
|
||||
/// See the inline comments for more details.
|
||||
#[allow(clippy::comparison_chain)]
|
||||
@ -521,7 +527,6 @@ impl Bot {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let phase = trade.phase;
|
||||
let my_offer_index = trade
|
||||
.which_party(self.client.uid().ok_or("Failed to get uid")?)
|
||||
.ok_or("Failed to get offer index")?;
|
||||
@ -532,11 +537,6 @@ impl Bot {
|
||||
&trade.offers[their_offer_index],
|
||||
)
|
||||
};
|
||||
let their_uid = trade.parties[their_offer_index];
|
||||
let their_name = self
|
||||
.find_player_alias(&their_uid)
|
||||
.ok_or("Failed to find player name")?
|
||||
.clone();
|
||||
|
||||
let inventories = self.client.inventories();
|
||||
let me = self.client.entity();
|
||||
@ -554,16 +554,6 @@ impl Bot {
|
||||
let coins_owned = COINS.to_owned();
|
||||
let get_my_coins = my_inventory.get_slot_of_item_by_def_id(&coins_owned);
|
||||
let get_their_coins = their_inventory.get_slot_of_item_by_def_id(&coins_owned);
|
||||
let my_coin_amount = if let Some(coins) = get_my_coins {
|
||||
my_inventory.get(coins).unwrap().amount()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let their_coin_amount = if let Some(coins) = get_their_coins {
|
||||
their_inventory.get(coins).unwrap().amount()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let mut receipt = Reciept {
|
||||
my_items: HashMap::new(),
|
||||
their_items: HashMap::new(),
|
||||
@ -669,6 +659,8 @@ impl Bot {
|
||||
|
||||
drop(inventories);
|
||||
|
||||
let phase = trade.phase;
|
||||
|
||||
// If the trade hasn't changed, do nothing to avoid spamming the server.
|
||||
if let Some(previous) = &self.previous_trade {
|
||||
if previous == &trade {
|
||||
@ -680,8 +672,6 @@ impl Bot {
|
||||
// offer now. The trade action is infallible from here.
|
||||
self.previous_trade = Some(trade);
|
||||
|
||||
log::info!("Performing trade action with {their_name}");
|
||||
|
||||
// Before running any actual trade logic, remove items that are not for sale or not being
|
||||
// purchased. End this trade action if an item was removed.
|
||||
|
||||
@ -709,7 +699,8 @@ impl Bot {
|
||||
|
||||
// The if/else statements below implement the bot's main feature: buying, selling and
|
||||
// trading items according to the values set in the configuration file. Coins are used to
|
||||
// balance the value of the trade.
|
||||
// balance the value of the trade. In the case that we try to add more coins than are
|
||||
// available, the server will correct it by adding all of the available coins.
|
||||
|
||||
// If the trade is balanced
|
||||
if difference == 0 {
|
||||
@ -717,92 +708,48 @@ impl Bot {
|
||||
|
||||
// Accept
|
||||
self.client.perform_trade_action(TradeAction::Accept(phase));
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// If they are offering more
|
||||
if difference > 0 {
|
||||
} else if difference > 0 {
|
||||
// If they are offering coins
|
||||
if their_offered_coin_amount > 0 {
|
||||
if let Some(their_coins) = get_their_coins {
|
||||
if their_coin_amount >= difference as u32 {
|
||||
// Remove their coins to balance
|
||||
self.client.perform_trade_action(TradeAction::RemoveItem {
|
||||
item: their_coins,
|
||||
quantity: difference as u32,
|
||||
ours: false,
|
||||
});
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
// Remove their coins to balance
|
||||
self.client.perform_trade_action(TradeAction::RemoveItem {
|
||||
item: their_coins,
|
||||
quantity: difference as u32,
|
||||
ours: false,
|
||||
});
|
||||
}
|
||||
// If they are not offering coins
|
||||
} else if let Some(my_coins) = get_my_coins {
|
||||
if my_coin_amount >= difference as u32 {
|
||||
// Add my coins to balanace
|
||||
self.client.perform_trade_action(TradeAction::AddItem {
|
||||
item: my_coins,
|
||||
quantity: difference as u32,
|
||||
ours: true,
|
||||
});
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
// Add my coins to balanace
|
||||
self.client.perform_trade_action(TradeAction::AddItem {
|
||||
item: my_coins,
|
||||
quantity: difference as u32,
|
||||
ours: true,
|
||||
});
|
||||
}
|
||||
|
||||
self.client.send_command(
|
||||
"tell".to_string(),
|
||||
vec![
|
||||
their_name,
|
||||
format!(
|
||||
"The value of your offer is {their_offered_items_value} coins. I only have {my_coin_amount} coins.",
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// If I am offering more
|
||||
if difference < 0 {
|
||||
} else if difference < 0 {
|
||||
// If I am offering coins
|
||||
if my_offered_coin_amount > 0 {
|
||||
if let Some(my_coins) = get_my_coins {
|
||||
if my_coin_amount >= difference.unsigned_abs() {
|
||||
// Remove my coins to balance
|
||||
self.client.perform_trade_action(TradeAction::RemoveItem {
|
||||
item: my_coins,
|
||||
quantity: difference.unsigned_abs(),
|
||||
ours: true,
|
||||
});
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
// Remove my coins to balance
|
||||
self.client.perform_trade_action(TradeAction::RemoveItem {
|
||||
item: my_coins,
|
||||
quantity: difference.unsigned_abs(),
|
||||
ours: true,
|
||||
});
|
||||
}
|
||||
// If I am not offering coins
|
||||
} else if let Some(their_coins) = get_their_coins {
|
||||
if their_coin_amount >= difference.unsigned_abs() {
|
||||
// Add their coins to balance
|
||||
self.client.perform_trade_action(TradeAction::AddItem {
|
||||
item: their_coins,
|
||||
quantity: difference.unsigned_abs(),
|
||||
ours: false,
|
||||
});
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
// Add their coins to balance
|
||||
self.client.perform_trade_action(TradeAction::AddItem {
|
||||
item: their_coins,
|
||||
quantity: difference.unsigned_abs(),
|
||||
ours: false,
|
||||
});
|
||||
}
|
||||
|
||||
let their_name = self.find_player_alias(&their_uid).unwrap().clone();
|
||||
|
||||
self.client.send_command(
|
||||
"tell".to_string(),
|
||||
vec![
|
||||
their_name,
|
||||
format!(
|
||||
"The value of my offer is {my_offered_items_value} coins. You only have {their_coin_amount} coins.",
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -1003,12 +950,6 @@ impl Bot {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Reciept {
|
||||
pub my_items: HashMap<String, u32>,
|
||||
pub their_items: HashMap<String, u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum TradeMode {
|
||||
AdminAccess,
|
||||
|
Loading…
Reference in New Issue
Block a user