Skip to content

Commit

Permalink
Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMacocian committed Aug 12, 2024
2 parents 88b01a0 + dc7808e commit abc0f68
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Bot/Dependencies/Headquarter
81 changes: 66 additions & 15 deletions GuildWarsPartySearch.Bot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ extern "C" {
#define FAILED_TO_LOAD_CONFIG 4
#define FAILED_TO_CONNECT 5

#define MapID_Ascalon_PreSearing 148
#define MapID_Kamadan 449
#define MapID_Kamadan_Halloween 818
#define MapID_Kamadan_Wintersday 819
#define MapID_Kamadan_Canthan_New_Year 820
#define MapID_EmbarkBeach 857
#define MapID_GreatTempleOfBalthazar 148

struct BotConfiguration {
std::string web_socket_url = "";
std::string api_key = "development";
uint32_t map_id = 857; // Embark beach
uint32_t map_id = 0; // Embark beach
District district = District::DISTRICT_AMERICAN;
uint32_t district_number = 0;
int32_t connection_retries = 10;
Expand Down Expand Up @@ -101,6 +109,9 @@ uint32_t map_id = 0;
char character_name[42] = { 0 };
char account_uuid[128] = { 0 };

uint32_t wanted_map_id = 0;
District wanted_district = District::DISTRICT_CURRENT;

static PluginObject* plugin_hook;
static BotConfiguration bot_configuration;

Expand Down Expand Up @@ -210,21 +221,26 @@ static PartySearchAdvertisement* create_party_search_advertisement(Event* event)
return party;
}

static bool is_websocket_ready(easywsclient::WebSocket::pointer ws) {
return ws && ws->getReadyState() == easywsclient::WebSocket::OPEN;
}

static int send_websocket(const std::string& payload) {
connect_websocket();
if(!is_websocket_ready(ws))
connect_websocket();
LogInfo("Websocket send:");
printf("%s\n", payload.c_str());
last_websocket_message = time_get_ms();
ws->send(payload);
return 0;
}

static int send_ping() {
connect_websocket();
static void send_ping() {
if (!is_websocket_ready(ws))
return; // No need to connect if its not ready
LogInfo("Sending ping");
last_websocket_message = time_get_ms();
ws->sendPing();
return 0;
}

static int send_party_advertisements() {
Expand Down Expand Up @@ -256,6 +272,26 @@ static void collect_instance_info() {
assert(GetAccountUuid(account_uuid, ARRAY_SIZE(account_uuid)) > 0);
}

static uint32_t calculate_map_to_visit(uint32_t map_id_requested, District* district_out) {
// @TODO: Figure out if a district for a map is available; this should already be given because we can see it on the world map
if (map_id_requested && IsMapUnlocked(map_id_requested)) {
// The map given is accessible
if (district_out && !*district_out)
*district_out = District::DISTRICT_CURRENT;
return map_id_requested;
}
uint32_t fallback_map_ids[] = {
857, // Embark
148, // Gtob
};
for (auto fallback_id : fallback_map_ids) {
if (IsMapUnlocked(fallback_id))
return fallback_id;
}
return 0;
}


static void on_map_left(Event* event, void* params) {
clear_party_search_advertisements();
}
Expand Down Expand Up @@ -352,31 +388,47 @@ static void load_configuration() {
if (bot_configuration.map_id == -1) {
exit_with_status("No map specified. Use -mapid", FAILED_TO_LOAD_CONFIG);
}

wanted_map_id = bot_configuration.map_id;
wanted_district = bot_configuration.district;
}
// If the given map id isn't the normal one (i.e. festival, return the original. Useful for comparisons)
static uint32_t get_original_map_id(uint32_t map_id) {
switch(map_id) {
case MapID_Kamadan_Halloween:
case MapID_Kamadan_Wintersday:
case MapID_Kamadan_Canthan_New_Year:
return MapID_Kamadan;
// TODO: Other outposts that have festival versions
}
return map_id;
}

static bool in_correct_outpost() {
if (!GetIsIngame())
return false;
if (!bot_configuration.map_id)
return true;
if (!map_id)
return false;

return ((!bot_configuration.map_id || map_id == bot_configuration.map_id) &&
(bot_configuration.district == District::DISTRICT_CURRENT || district == bot_configuration.district) &&
uint32_t check_map_id = get_original_map_id(map_id);

return ((!wanted_map_id || check_map_id == wanted_map_id) &&
(wanted_district == District::DISTRICT_CURRENT || district == wanted_district) &&
(!bot_configuration.district_number || district_number == bot_configuration.district_number));
}

static void ensure_correct_outpost() {
wanted_map_id = calculate_map_to_visit(bot_configuration.map_id, &wanted_district);
if (!wanted_map_id)
exit_with_status("calculate_map_to_visit failed, ensure this character has gtob unlocked", 1);
if (in_correct_outpost())
return;
LogInfo("Zoning into outpost");
int res = 0;
size_t retries = 4;
assert(bot_configuration.map_id);
for (size_t i = 0; i < retries && !in_correct_outpost(); i++) {
LogInfo("Travel attempt %d of %d", i + 1, retries);
res = travel_wait(bot_configuration.map_id, bot_configuration.district, bot_configuration.district_number);
res = travel_wait(wanted_map_id, wanted_district, bot_configuration.district_number);
if (res == 38) {
retries = 50; // District full, more retries
}
Expand Down Expand Up @@ -406,9 +458,8 @@ static void disconnect_websocket() {
ws = NULL;
}
static easywsclient::WebSocket::pointer connect_websocket() {
if (ws && ws->getReadyState() == easywsclient::WebSocket::OPEN)
if (is_websocket_ready(ws))
return ws;

assert(*account_uuid && map_id);
char user_agent[255];
char uuid_less_hyphens[ARRAY_SIZE(account_uuid)] = { 0 };
Expand All @@ -434,7 +485,7 @@ static easywsclient::WebSocket::pointer connect_websocket() {
// Wait for websocket to open
for (auto j = 0; j < 5000; j+=50) {
ws->poll();
if (ws->getReadyState() == easywsclient::WebSocket::OPEN) {
if (is_websocket_ready(ws)) {
LogInfo("Websocket opened successfully");
party_advertisements_pending = true;
last_websocket_message = time_get_ms();
Expand Down Expand Up @@ -474,14 +525,14 @@ static int main_bot(void* param)
while (running) {
wait_until_ingame();
ensure_correct_outpost();
connect_websocket();
if (party_advertisements_pending) {
send_party_advertisements();
party_advertisements_pending = false;
}
if (ws) {
if (time_get_ms() - last_websocket_message > 30000) {
send_ping();

}
ws->dispatch(on_websocket_message);
ws->poll();
Expand Down

0 comments on commit abc0f68

Please sign in to comment.