-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncounter.cpp
More file actions
89 lines (76 loc) · 2.52 KB
/
Encounter.cpp
File metadata and controls
89 lines (76 loc) · 2.52 KB
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
#include "Menu.h"
#include <fstream>
#include <SDL3/SDL.h>
dnd::Encounter parse_encounter_from_file(void* s, const std::string& filename) {
ToolState* state = (ToolState*)s;
auto start = SDL_GetTicks();
std::ifstream(f);
f.open(filename);
if (!f.is_open()) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not open file: %s",
filename.c_str());
return dnd::Encounter();
}
json j;
try {
j = json::parse(f);
} catch (const json::parse_error& e) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "JSON parse error in file %s: %s",
filename.c_str(), e.what());
return dnd::Encounter();
}
dnd::Encounter encounter;
try {
if (j.contains("name"))
encounter.set_name(j.value("name", "Unnamed Encounter"));
if (j.contains("creatures") && j["creatures"].is_array()) {
std::vector<std::string> creatures =
j["creatures"].get<std::vector<std::string>>();
encounter.set_creatures(creatures);
}
if (j.contains("notes") && j["notes"].is_array()) {
std::vector<std::string> notes = j["notes"].get<std::vector<std::string>>();
encounter.set_notes(notes);
}
} catch (const std::exception& e) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error parsing encounter: %s",
e.what());
return dnd::Encounter();
}
int i = 0;
std::vector<std::string> notes =
encounter.get_notes().value_or(std::vector<std::string>());
for (const auto& creature_name : encounter.get_creatures()) {
bool found = false;
for (auto creature : state->creatures) {
std::string name = creature.get_name().value_or("Unnamed Creature");
name += " (" + creature.original_list + ")";
if (name == creature_name) {
if (i < notes.size())
creature.set_notes(notes[i]);
creature.id = i; // Assign an ID based on order
encounter.creature_objects.push_back(creature);
found = true;
break;
}
}
if (!found) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Creature '%s' not found in loaded creatures.",
creature_name.c_str());
}
i++;
}
auto end = SDL_GetTicks();
SDL_Log("Time taken to parse encounter: %u ms", end - start);
return encounter;
}
void save_encounter_to_file(const dnd::Encounter& encounter,
const std::string& filename) {
json j = json::array();
j = encounter.to_json();
std::ofstream o(filename);
o << j;
o.close();
SDL_Log("Encounter saved to %s", filename.c_str());
}