Exemple #1
0
 def load_from_file(self,map_path):
   conf_file = os.path.join(map_path,"map.json")
   
   #Open the file for reading
   f = open(conf_file,'r')
   
   if f:
     self.log_debug("File (%s) Opened" % (conf_file))
     
     #Read in the entire file and load it as json
     map_conf = load(f)
     self.log_debug("Loaded JSON from file")
     self.log_debug("JSON: %s" % str(map_conf))
     
     #Set the map settings
     try:
       self.title = map_conf["general"]["title"]
       self.x_size = map_conf["general"]["x_size"]
       self.y_size = map_conf["general"]["y_size"]
     except KeyError,e:
       self.log_error("Invalid config file: %s is missing" % str(e))
       return
     
     #Now setup the game board
     self.generate_grid(self.x_size,self.y_size)
     
     #Add locations to the grid
     for location_json in map_conf["locations"]:
       if "start_location" in location_json and location_json["start_location"]:
         start_location = True
       else:
         start_location = False
       self.log_debug("Processing location JSON %s" % str(location_json))
       location = Location(name=location_json["title"],symbol=location_json["symbol"],start_location=start_location)
       
       #Parse the jobs for this location
       if "jobs" in location_json:
         for job_json in location_json["jobs"]:
           self.log_debug("Processing job JSON %s" % str(job_json))
           job = Job(name=job_json["title"],
                     symbol=job_json["symbol"] if "symbol" in job_json else "",
                     availability=job_json["availability"],
                     pay=job_json["pay"],
                     rank=job_json["rank"],
                     location=location)
           location.add_job(job)
       if "education" in location_json:
         for class_json in location_json["education"]:
           self.log_debug("Processing class JSON %s" % str(class_json))
           course = Course(name=class_json["title"],
                           symbol=class_json["symbol"],
                           knowledge_value=class_json["knowledge_value"],
                           time=class_json["time"],
                           knowledge_required=class_json["knowledge_required"],
                           course_required=class_json["course_required"] if "course_required" in class_json else None,
                           cost=class_json["cost"])
           location.add_course(course)
       if "items" in location_json:
         for items_json in location_json["items"]:
           self.log_debug("Processing item JSON %s" % str(items_json))
           effects = {}
           if "effects" in items_json:
             for effect in items_json["effects"]:
               self.log_debug("Processing effect json: %s" % str(effect))
               effects[effect['attribute']] = effect['value']
           self.log_debug("Loaded effects as %s" % (str(effects)))
           item = Item(name=items_json["title"],
                     symbol=items_json["symbol"] if "symbol" in items_json else "",
                     availability=items_json["availability"],
                     cost=items_json["cost"],
                     effects=effects,
                     consumable=items_json["consumable"] if "consumable" in items_json else True,
                     )
           location.add_item(item)
           self.log_debug("Added item:\n%s" % item.debug_string())
       
       #Finally add this location to the map
       self.log_debug("Loaded location from file:\n%s" % location.debug_string())
       self.add_location(location_json["x"],location_json["y"],location)
     
     #Close the file
     f.close()