2 dcf33c24 2021-09-10 xhr * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
4 dcf33c24 2021-09-10 xhr * Permission to use, copy, modify, and distribute this software for any
5 dcf33c24 2021-09-10 xhr * purpose with or without fee is hereby granted, provided that the above
6 dcf33c24 2021-09-10 xhr * copyright notice and this permission notice appear in all copies.
8 dcf33c24 2021-09-10 xhr * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 dcf33c24 2021-09-10 xhr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 dcf33c24 2021-09-10 xhr * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 dcf33c24 2021-09-10 xhr * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 dcf33c24 2021-09-10 xhr * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 dcf33c24 2021-09-10 xhr * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 dcf33c24 2021-09-10 xhr * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 dcf33c24 2021-09-10 xhr #include <json-c/json.h>
19 bd88fb1a 2021-09-11 xhr #include <errno.h>
20 dcf33c24 2021-09-10 xhr #include <limits.h>
21 bd88fb1a 2021-09-11 xhr #include <string.h>
23 8a2e76b6 2021-09-10 xhr #include "isscrolls.h"
26 bd88fb1a 2021-09-11 xhr cmd_undertake_a_journey(char *cmd)
28 bd88fb1a 2021-09-11 xhr struct character *curchar = get_current_character();
31 bd88fb1a 2021-09-11 xhr int ival[2] = { -1, -1 };
34 bd88fb1a 2021-09-11 xhr if (curchar == NULL) {
35 bd88fb1a 2021-09-11 xhr printf("No character loaded. Use 'cd' to load a character\n");
39 bd88fb1a 2021-09-11 xhr ival[0] = curchar->wits;
40 bd88fb1a 2021-09-11 xhr if (strlen(cmd) > 0) {
42 bd88fb1a 2021-09-11 xhr lval = strtol(cmd, &ep, 10);
43 bd88fb1a 2021-09-11 xhr if (cmd[0] == '\0' || *ep != '\0') {
44 bd88fb1a 2021-09-11 xhr printf("Please provide a number as argument\n");
47 bd88fb1a 2021-09-11 xhr if ((errno == ERANGE || lval <= 0 || lval > 10)) {
48 bd88fb1a 2021-09-11 xhr printf("Please provide a number between 1 and 10\n");
52 bd88fb1a 2021-09-11 xhr ival[1] = lval;
53 bd88fb1a 2021-09-11 xhr log_debug("Arg provided %d\n", ival[1]);
56 bd88fb1a 2021-09-11 xhr if (curchar->journey_active == 0) {
57 bd88fb1a 2021-09-11 xhr ask_for_journey_difficulty();
58 bd88fb1a 2021-09-11 xhr curchar->journey_active = 1;
61 bd88fb1a 2021-09-11 xhr ret = action_roll(ival);
62 bd88fb1a 2021-09-11 xhr if (ret == 8) {
63 bd88fb1a 2021-09-11 xhr printf("You reach a waypoint and can choose one option -> Rulebook\n");
64 bd88fb1a 2021-09-11 xhr mark_journey_progress();
65 bd88fb1a 2021-09-11 xhr } else if (ret == 4) {
66 bd88fb1a 2021-09-11 xhr printf("You reach a waypoint, but suffer -1 supply\n");
67 bd88fb1a 2021-09-11 xhr change_char_value("supply", DECREASE, 1);
68 bd88fb1a 2021-09-11 xhr mark_journey_progress();
70 bd88fb1a 2021-09-11 xhr printf("Pay the price -> Rulebook\n");
72 bd88fb1a 2021-09-11 xhr update_prompt();
76 319d4050 2021-09-16 xhr cmd_reach_your_destination(char *cmd)
78 bd88fb1a 2021-09-11 xhr struct character *curchar = get_current_character();
79 319d4050 2021-09-16 xhr double dval[2] = { -1.0, -1.0 };
82 bd88fb1a 2021-09-11 xhr if (curchar == NULL) {
83 bd88fb1a 2021-09-11 xhr printf("No character loaded. Use 'cd' to load a character\n");
87 bd88fb1a 2021-09-11 xhr if (curchar->journey_active == 0) {
88 bd88fb1a 2021-09-11 xhr printf("You must start a journey with 'undertakeajourney' first\n");
92 319d4050 2021-09-16 xhr dval[0] = curchar->j->progress;
93 319d4050 2021-09-16 xhr dval[1] = get_int_from_cmd(cmd);
95 319d4050 2021-09-16 xhr ret = progress_roll(dval);
96 bd88fb1a 2021-09-11 xhr if (ret == 8) {
97 3f1228d5 2021-09-16 xhr printf("You reach your destination and the situation favors you -> "\
98 3f1228d5 2021-09-16 xhr "Rulebook\n");
99 bd88fb1a 2021-09-11 xhr curchar->journey_active = 0;
100 bd88fb1a 2021-09-11 xhr curchar->j->progress = 0;
101 bd88fb1a 2021-09-11 xhr delete_journey(curchar->id);
102 bd88fb1a 2021-09-11 xhr } else if (ret == 4) {
103 3f1228d5 2021-09-16 xhr printf("You reach your destination but face an unforseen complication "\
104 3f1228d5 2021-09-16 xhr "-> Rulebook\n");
105 bd88fb1a 2021-09-11 xhr curchar->journey_active = 0;
106 bd88fb1a 2021-09-11 xhr curchar->j->progress = 0;
107 bd88fb1a 2021-09-11 xhr delete_journey(curchar->id);
109 bd88fb1a 2021-09-11 xhr reach_your_destination_failed();
112 bd88fb1a 2021-09-11 xhr update_prompt();
116 b653ad5b 2021-09-11 xhr mark_journey_progress()
118 b653ad5b 2021-09-11 xhr struct character *curchar = get_current_character();
120 b653ad5b 2021-09-11 xhr if (curchar == NULL) {
121 8a2e76b6 2021-09-10 xhr log_debug("No character loaded. Cannot calculate progress\n");
125 b653ad5b 2021-09-11 xhr if (curchar->journey_active == 0) {
126 b653ad5b 2021-09-11 xhr printf("You need start a journey before you can mark progress\n");
130 b653ad5b 2021-09-11 xhr switch (curchar->j->difficulty) {
132 b653ad5b 2021-09-11 xhr curchar->j->progress += 3;
135 b653ad5b 2021-09-11 xhr curchar->j->progress += 2;
138 b653ad5b 2021-09-11 xhr curchar->j->progress += 1;
141 b653ad5b 2021-09-11 xhr curchar->j->progress += 0.5;
144 b653ad5b 2021-09-11 xhr curchar->j->progress += 0.25;
147 4554fab2 2021-09-16 xhr curchar->j->difficulty = 1;
148 4554fab2 2021-09-16 xhr log_errx(1, "Unknown difficulty. This should not happen. Set it to 1\n");
151 b653ad5b 2021-09-11 xhr if (curchar->j->progress > 10) {
152 b653ad5b 2021-09-11 xhr printf("Your reached all milestones of your journey. Consider ending it\n");
153 b653ad5b 2021-09-11 xhr curchar->j->progress = 10;
156 b653ad5b 2021-09-11 xhr update_prompt();
160 fcc4d354 2021-09-11 xhr reach_your_destination_failed()
162 fcc4d354 2021-09-11 xhr struct character *curchar = get_current_character();
165 fcc4d354 2021-09-11 xhr if (curchar == NULL) {
166 fcc4d354 2021-09-11 xhr log_debug("No character loaded.\n");
170 fcc4d354 2021-09-11 xhr if (curchar->journey_active == 0) {
171 fcc4d354 2021-09-11 xhr log_debug("No active journey.\n");
175 fcc4d354 2021-09-11 xhr printf("Please decide what to do\n\n");
176 fcc4d354 2021-09-11 xhr printf("1\t - End your journey and pay the price -> Rulebook\n");
177 fcc4d354 2021-09-11 xhr printf("2\t - Continue your journey -> progress is lost, difficulty +1\n");
179 fcc4d354 2021-09-11 xhr a = ask_for_value("Enter a value between 1 and 2: ", 2);
180 fcc4d354 2021-09-11 xhr if (a == 1) {
181 fcc4d354 2021-09-11 xhr curchar->journey_active = 0;
182 fcc4d354 2021-09-11 xhr curchar->j->progress = 0;
183 fcc4d354 2021-09-11 xhr delete_journey(curchar->id);
185 fcc4d354 2021-09-11 xhr curchar->j->progress = 0;
186 fcc4d354 2021-09-11 xhr if (curchar->j->difficulty <= 5)
187 fcc4d354 2021-09-11 xhr curchar->j->difficulty += 1;
192 dcf33c24 2021-09-10 xhr save_journey()
194 dcf33c24 2021-09-10 xhr struct character *curchar = get_current_character();
195 dcf33c24 2021-09-10 xhr char path[_POSIX_PATH_MAX];
196 d5faed8f 2021-09-16 xhr json_object *root, *items, *id;
197 dcf33c24 2021-09-10 xhr int temp_n, i;
199 dcf33c24 2021-09-10 xhr if (curchar == NULL) {
200 dcf33c24 2021-09-10 xhr log_debug("No character loaded. No journey to save.\n");
204 635b768e 2021-09-11 xhr if (curchar->journey_active == 0) {
205 635b768e 2021-09-11 xhr log_debug("No active journey to save.\n");
209 dcf33c24 2021-09-10 xhr json_object *cobj = json_object_new_object();
210 dcf33c24 2021-09-10 xhr json_object_object_add(cobj, "id", json_object_new_int(curchar->id));
211 3f1228d5 2021-09-16 xhr json_object_object_add(cobj, "difficulty",
212 3f1228d5 2021-09-16 xhr json_object_new_int(curchar->j->difficulty));
213 3f1228d5 2021-09-16 xhr json_object_object_add(cobj, "progress",
214 3f1228d5 2021-09-16 xhr json_object_new_double(curchar->j->progress));
216 dcf33c24 2021-09-10 xhr snprintf(path, sizeof(path), "%s/journey.json", get_isscrolls_dir());
217 dcf33c24 2021-09-10 xhr if ((root = json_object_from_file(path)) == NULL) {
218 dcf33c24 2021-09-10 xhr log_debug("No journey JSON file found\n");
219 dcf33c24 2021-09-10 xhr root = json_object_new_object();
221 dcf33c24 2021-09-10 xhr log_errx(1, "Cannot create journey JSON object\n");
223 dcf33c24 2021-09-10 xhr items = json_object_new_array();
224 dcf33c24 2021-09-10 xhr json_object_array_add(items, cobj);
225 dcf33c24 2021-09-10 xhr json_object_object_add(root, "journey", items);
227 dcf33c24 2021-09-10 xhr /* Get existing character array from JSON */
228 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "journey", &items)) {
229 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [journey] array in %s\n", path);
230 fc6936c9 2021-09-16 xhr items = json_object_new_array();
231 fc6936c9 2021-09-16 xhr json_object_object_add(root, "journey", items);
234 dcf33c24 2021-09-10 xhr temp_n = json_object_array_length(items);
235 dcf33c24 2021-09-10 xhr for (i = 0; i < temp_n; i++) {
236 dcf33c24 2021-09-10 xhr json_object *temp = json_object_array_get_idx(items, i);
237 d5faed8f 2021-09-16 xhr json_object_object_get_ex(temp, "id", &id);
238 dcf33c24 2021-09-10 xhr if (curchar->id == json_object_get_int(id)) {
239 dcf33c24 2021-09-10 xhr log_debug("Update journey entry for %s\n", curchar->name);
240 dcf33c24 2021-09-10 xhr json_object_array_del_idx(items, i, 1);
241 dcf33c24 2021-09-10 xhr json_object_array_add(items, cobj);
245 dcf33c24 2021-09-10 xhr log_debug("No journey entry for %s found, adding new one\n", curchar->name);
246 dcf33c24 2021-09-10 xhr json_object_array_add(items, cobj);
250 dcf33c24 2021-09-10 xhr if (json_object_to_file(path, root))
251 dcf33c24 2021-09-10 xhr printf("Error saving %s\n", path);
253 dcf33c24 2021-09-10 xhr log_debug("Successfully saved %s\n", path);
255 dcf33c24 2021-09-10 xhr json_object_put(root);
259 60bf4095 2021-09-11 xhr delete_journey(int id)
261 60bf4095 2021-09-11 xhr char path[_POSIX_PATH_MAX];
262 d5faed8f 2021-09-16 xhr json_object *root, *lid;
263 60bf4095 2021-09-11 xhr int temp_n, i;
265 60bf4095 2021-09-11 xhr snprintf(path, sizeof(path), "%s/journey.json", get_isscrolls_dir());
266 60bf4095 2021-09-11 xhr if ((root = json_object_from_file(path)) == NULL) {
267 60bf4095 2021-09-11 xhr log_debug("No journey JSON file found\n");
271 fc6936c9 2021-09-16 xhr json_object *journey;
272 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "journey", &journey)) {
273 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [journey] array in %s\n", path);
277 60bf4095 2021-09-11 xhr temp_n = json_object_array_length(journey);
278 60bf4095 2021-09-11 xhr for (i = 0; i < temp_n; i++) {
279 60bf4095 2021-09-11 xhr json_object *temp = json_object_array_get_idx(journey, i);
280 d5faed8f 2021-09-16 xhr json_object_object_get_ex(temp, "id", &lid);
281 60bf4095 2021-09-11 xhr if (id == json_object_get_int(lid)) {
282 60bf4095 2021-09-11 xhr json_object_array_del_idx(journey, i, 1);
283 60bf4095 2021-09-11 xhr log_debug("Deleted journey entry for %d\n", id);
287 60bf4095 2021-09-11 xhr if (json_object_to_file(path, root))
288 60bf4095 2021-09-11 xhr printf("Error saving %s\n", path);
290 60bf4095 2021-09-11 xhr log_debug("Successfully saved %s\n", path);
292 60bf4095 2021-09-11 xhr json_object_put(root);
296 dcf33c24 2021-09-10 xhr load_journey(int id)
298 dcf33c24 2021-09-10 xhr struct character *curchar = get_current_character();
299 dcf33c24 2021-09-10 xhr char path[_POSIX_PATH_MAX];
300 d5faed8f 2021-09-16 xhr json_object *root, *lid;
301 15d8381d 2021-09-11 xhr int temp_n, i;
303 dcf33c24 2021-09-10 xhr if (curchar == NULL) {
304 dcf33c24 2021-09-10 xhr log_debug("No character loaded\n");
308 dcf33c24 2021-09-10 xhr snprintf(path, sizeof(path), "%s/journey.json", get_isscrolls_dir());
309 dcf33c24 2021-09-10 xhr if ((root = json_object_from_file(path)) == NULL) {
310 dcf33c24 2021-09-10 xhr log_debug("No journey JSON file found\n");
314 fc6936c9 2021-09-16 xhr json_object *journey;
315 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "journey", &journey)) {
316 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [journey] array in %s\n", path);
320 dcf33c24 2021-09-10 xhr temp_n = json_object_array_length(journey);
321 dcf33c24 2021-09-10 xhr for (i=0; i < temp_n; i++) {
322 dcf33c24 2021-09-10 xhr json_object *temp = json_object_array_get_idx(journey, i);
323 d5faed8f 2021-09-16 xhr json_object_object_get_ex(temp, "id", &lid);
324 dcf33c24 2021-09-10 xhr if (id == json_object_get_int(lid)) {
325 dcf33c24 2021-09-10 xhr log_debug("Loading journey for id: %d\n", json_object_get_int(lid));
327 5609edc8 2021-09-16 xhr json_object *cval;
328 5609edc8 2021-09-16 xhr json_object_object_get_ex(temp, "difficulty", &cval);
329 5609edc8 2021-09-16 xhr curchar->j->difficulty = json_object_get_int(cval);
330 5609edc8 2021-09-16 xhr json_object_object_get_ex(temp, "progress", &cval);
331 5609edc8 2021-09-16 xhr curchar->j->progress = json_object_get_double(cval);
335 dcf33c24 2021-09-10 xhr json_object_put(root);