Hmm. So confused!
I'm not really a fan of debugging someone's entire
file to get it to work, but in this case it's easier than
pointing out the problems piecemeal, since that
would take another couple of weeks.
#include <lib.h>
#include <save.h>
#include <privs.h>
#ifndef SAVE_FORGES
#define SAVE_FORGES DIR_SECURE_SAVE "/forges"
#endif
inherit LIB_DAEMON;
/* This is NOT an example of how to make a forge daemon.
* This is an example of how to initialize a mapping
* such that it does not error and such that it
* makes some logical sense.
*
* I don't know exactly how you're visualizing the data for
* your forges, or how they're supposed to work. I'm setting up
* a sample mapping here that assumes you'll be storing the
* data in the form of "what are the components for finished
* products?" and "how much of those components are required?"
* Here you see that a knife requires 50 steel, 10 wood, and 5 leather.
* The units are just to illustrate quantity handling, I have no
* idea (nor really do I want to know) the nature of your amounts.
*
* Therefore a query for "knife" in GetForgeRequirements() would return this:
* ([ "steel" : 50, "wood" : 10, "leather" : 5 ])
*
* Internally to the object, that mapping above lives here:
* Forges["requirements"]["knife"]
*
* Another way to write it is:
* Forges["requirements"]["knife"] = ([ "steel" : 50, "wood" : 10, "leather" : 5 ]);
*
* I don't understand the idea behind the components element, but
* I'm including it here since your original data set includes it.
*
*/
private mapping Forges = ([
"requirements" : ([ "knife" : ([ "steel" : 50, "wood" : 10, "leather" : 5 ]) ]),
"components" : ({ "steel", "iron", "bronze", "copper", "wood", "leather" }),
]);
static void create() {
daemon::create();
if( unguarded((: file_size(SAVE_FORGES __SAVE_EXTENSION__) :)) > 0 )
unguarded((: restore_object(SAVE_FORGES) :));
if (!Forges) Forges = ([]);
}
static private void validate() {
if( !((int)master()->valid_apply(({ PRIV_ASSIST }))) )
error("Illegal attempt to modify forge data");
}
mixed GetForgeRequirements(string str){
if(!Forges["requirements"][str]) return 0;
return Forges["requirements"][str];
}
string array GetForgeComponents(string str){
return Forges["components"];
}
/* I haven't really messed with the function below because I
* don't want to get involved in how you're parsing
* your config files. You'll eventually need to work out
* string manipulation, it might as well be now.
* I've only poked at it so it doesn't error.
*
* You'll need to go over the logic of it because it's
* not doing what I think you want it to. For example,
* it doesn't seem to be actually setting anything
* in the Forges mapping.
*/
void AddForge(string file, int player) {
string forge, requirements, components, test_string;
int x, y, z;
validate();
if( !file_exists(file) ) error("No such file: " + file);
forge = last_string_element(file,"/");
foreach(string line in explode(read_file(file),"\n")){
test_string = first_string_element(line," ");
if(!test_string || !sizeof(test_string)) test_string = line;
switch(test_string) {
case "REQUIREMENT":
requirements = replace_string(line, "REQUIREMENT ", "");
return Forges["requirements"];
break;
case "COMPONENT":
components = replace_string(line, "COMPONENT ", "");
return Forges["components"];
break;
case "FORGE":
forge = replace_string(line, "FORGE ", "");
if( Forges[forge] ) error(forge+": Forge item already exists");
break;
default:
break;
}
}
save_object(SAVE_FORGES);
}
void RemoveForge(string forge) {
validate();
map_delete(Forges, forge);
if(!Forges[forge])
save_object(SAVE_FORGES);
}
You know I like you and want to encourage you, etc, so
I won't beat around the bush with excessive courtesy.
The main problem is that you're leaping directly into relatively
complicated data manipulation with very little groundwork
leading up to it. It's like skipping multiplication in school
and heading straight for algebra.
That's fine if you're up to the challenge of brute forcing
stuff into your brain without doing the homework first, but
chances are you're going to wind up with code that
doesn't update and you have no idea what it would look
like in a working form, because you don't understand
the principles behind how the working structure would operate.
I'm presenting your code to you in a form that updates, not
because it fixes your problem, but because it at least now
provides you a stepping stone toward understanding how
mappings work in relation to your forge idea.
At this point, you need to study why it does what it does, figure
out why the Set function doesn't change anything, and
make it so that it does.
-Crat