1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![allow(unused)]
use std::{borrow::Borrow, time::Duration};
use chrono::{DateTime, Local};
use sf_api::{
command::{Command, ExpeditionSetting, TimeSkip},
gamestate::{
items::{Enchantment, EquipmentSlot},
tavern::{AvailableTasks, CurrentAction},
},
misc::EnumMapGet,
session::SimpleSession,
};
use sha1::digest::Update;
use tokio::time::sleep;
#[tokio::main]
pub async fn main() {
let mut session = login_with_env().await;
loop {
sleep(Duration::from_secs(2)).await;
let gs = session.game_state().unwrap();
match &gs.tavern.current_action {
CurrentAction::Idle => match gs.tavern.available_tasks() {
AvailableTasks::Quests(q) => {
// You should pick the best quest here, but this is an
// example
let best_quest = q.first().unwrap();
if best_quest.base_length
> gs.tavern.thirst_for_adventure_sec
{
let has_extra_beer = gs
.character
.equipment
.has_enchantment(Enchantment::ThirstyWanderer);
if gs.character.mushrooms > 0
&& gs.tavern.beer_drunk
< (10 + has_extra_beer as u8)
{
println!("Buying beer");
session
.send_command(Command::BuyBeer)
.await
.unwrap();
continue;
} else {
println!("Starting city guard");
session
.send_command(Command::StartWork { hours: 10 })
.await
.unwrap();
break;
}
}
println!("Starting the next quest");
if best_quest.item.is_some()
&& gs.character.inventory.free_slot().is_none()
{
println!("Inventory is full. Stopping!");
// You should sell/use/throw away/equip items at this
// point
break;
}
session
.send_command(Command::StartQuest {
quest_pos: 0,
overwrite_inv: true,
})
.await
.unwrap();
continue;
}
AvailableTasks::Expeditions(_) => {
if !gs.tavern.can_change_questing_preference() {
println!(
"We can not do quests, because we have done \
expeditions today already"
);
break;
}
println!("Changing questing setting");
session
.send_command(Command::SetQuestsInsteadOfExpeditions {
value: ExpeditionSetting::PreferQuests,
})
.await
.unwrap();
continue;
}
},
CurrentAction::Quest {
quest_idx,
busy_until,
} => {
let remaining = time_remaining(busy_until);
let mut skip = None;
if remaining > Duration::from_secs(60) {
if gs.tavern.quicksand_glasses > 0 {
skip = Some(TimeSkip::Glass);
} else if gs.character.mushrooms > 0
&& gs.tavern.mushroom_skip_allowed
{
skip = Some(TimeSkip::Mushroom);
}
}
if let Some(skip) = skip {
println!(
"Skipping the remaining {remaining:?} with a {skip:?}"
);
session
.send_command(Command::FinishQuest { skip: Some(skip) })
.await
.unwrap();
} else {
println!(
"Waiting {remaining:?} until the quest is finished"
);
sleep(remaining).await;
session
.send_command(Command::FinishQuest { skip })
.await
.unwrap();
}
}
CurrentAction::CityGuard { hours, busy_until } => {
let remaining = time_remaining(busy_until);
if remaining > Duration::from_secs(60 * 60) {
// You should not do this is practice. This should at least
// check if you can quest anymore
println!("Canceling the city guard job");
session.send_command(Command::CancelWork).await;
} else {
println!(
"Waiting {remaining:?} until the city guard is \
finished"
);
sleep(time_remaining(busy_until)).await;
session.send_command(Command::FinishWork).await;
}
continue;
}
_ => {
println!("Expeditions are not part of this example");
break;
}
}
}
}
pub fn time_remaining<T: Borrow<DateTime<Local>>>(time: T) -> Duration {
(*time.borrow() - Local::now()).to_std().unwrap_or_default()
}
pub async fn login_with_env() -> SimpleSession {
let username = std::env::var("USERNAME").unwrap();
let password = std::env::var("PASSWORD").unwrap();
let server = std::env::var("SERVER").unwrap();
SimpleSession::login(&username, &password, &server)
.await
.unwrap()
}