<commit_before><?php 

class ItemRepository implements ItemRepositoryInterface
{
  protected $database;

  public function __construct()
  {
    $this->parse();
  }

  public function find($id)
  {
    $item = App::make('Item');
    if(isset($this->database[$id])) 
    {
      $item->load($this->database[$id]);
      return $item;  
    }

    if(isset($this->database["vehicle_parts/$id"]))
    {
      $item->load($this->database["vehicle_parts/$id"]);
      return $item;
    }
    $item->load(json_decode('{"id":"'.$id.'","name":"?'.$id.'?"}'));
    return $item;
  }

  public function where($text)
  {
    error_log("searching for $text...");

    $results = array();
    foreach($this->database as $item)
    {
      $item = $this->find($item->id);
      if($item->matches($text))
      {
        $results[] = $this->find($item->id);
      }
    }
    return $results;
  }

  protected function parse()
  {
    $this->database = $this->read();
  }

  protected function snapshot()
  {
  }

  protected function read()
  {
    $items = array();

    error_log("Building item database..");
    $path = \Config::get("cataclysm.dataPath");
    foreach(scandir("$path/items") as $file)
    {
      if($file[0]==".") continue;
      $json = (array) json_decode(file_get_contents("$path/items/$file"));
      foreach($json as $item)
      {
        $item->recipes = array();
        $item->disassembly = array();
        $item->toolFor = array();
        $item->toolForCategory = array();
        $item->componentFor = array();
        $items[$item->id] = $item;
      }
    }
    $json = (array) json_decode(file_get_contents("$path/vehicle_parts.json"));
    foreach($json as $item)
    {
      $item->recipes = array();
      $item->disassembly = array();
      $item->toolFor = array();
      $item->toolForCategory = array();      
      $item->componentFor = array();
      $items["vehicle_parts/$item->item"] = $item;
      $items[$item->id] = $item;
    }
    $json = (array) json_decode(file_get_contents("$path/bionics.json"));
    foreach($json as $item)
    {
      $item->recipes = array();
      $item->disassembly = array();
      $item->toolFor = array();
      $item->toolForCategory = array();
      $item->componentFor = array();
      $item->weight = 2000;
      $item->volume = 10;
      $item->bashing = 8;
      $item->cutting = 0;
      $item->to_hit = 0;
      $items[$item->id] = $item;
    }

    $items["toolset"] = json_decode('{"id":"toolset","name":"integrated toolset"}');
    $items["fire"] = json_decode('{"id":"fire","name":"nearby fire"}');
    return $items;
  }

  public function link($type, $id, $recipe)
  {
    $keys = array(
        "result"=>"recipes",
        "tool"=>"toolFor",
        "component"=>"toolFor",
        "learn"=>"learn"
    );
    $key = $keys[$type];
    if(isset($this->database[$id]))
    {
      if($key=="recipes" and $recipe->category=="CC_NONCRAFT")
      {
        $this->database[$id]->disassembly[] = $recipe->id;
        return;
      }
      if($key=="toolFor")
      {
        $this->database[$id]->{"toolForCategory"}[$recipe->category][] = $recipe->id;
      }
      $this->database[$id]->{$key}[] = $recipe->id;
      return;
    }
    if(isset($this->database["vehicle_parts/$id"]))
    {
      $this->database["vehicle_parts/$id"]->{$key}[] = $recipe->id;
      return;
    }
  }
}<commit_msg>Disassemble reversible recipes.<commit_after><?php 

class ItemRepository implements ItemRepositoryInterface
{
  protected $database;

  public function __construct()
  {
    $this->parse();
  }

  public function find($id)
  {
    $item = App::make('Item');
    if(isset($this->database[$id])) 
    {
      $item->load($this->database[$id]);
      return $item;  
    }

    if(isset($this->database["vehicle_parts/$id"]))
    {
      $item->load($this->database["vehicle_parts/$id"]);
      return $item;
    }
    $item->load(json_decode('{"id":"'.$id.'","name":"?'.$id.'?"}'));
    return $item;
  }

  public function where($text)
  {
    error_log("searching for $text...");

    $results = array();
    foreach($this->database as $item)
    {
      $item = $this->find($item->id);
      if($item->matches($text))
      {
        $results[] = $this->find($item->id);
      }
    }
    return $results;
  }

  protected function parse()
  {
    $this->database = $this->read();
  }

  protected function snapshot()
  {
  }

  protected function read()
  {
    $items = array();

    error_log("Building item database..");
    $path = \Config::get("cataclysm.dataPath");
    foreach(scandir("$path/items") as $file)
    {
      if($file[0]==".") continue;
      $json = (array) json_decode(file_get_contents("$path/items/$file"));
      foreach($json as $item)
      {
        $item->recipes = array();
        $item->disassembly = array();
        $item->toolFor = array();
        $item->toolForCategory = array();
        $item->componentFor = array();
        $items[$item->id] = $item;
      }
    }
    $json = (array) json_decode(file_get_contents("$path/vehicle_parts.json"));
    foreach($json as $item)
    {
      $item->recipes = array();
      $item->disassembly = array();
      $item->toolFor = array();
      $item->toolForCategory = array();      
      $item->componentFor = array();
      $items["vehicle_parts/$item->item"] = $item;
      $items[$item->id] = $item;
    }
    $json = (array) json_decode(file_get_contents("$path/bionics.json"));
    foreach($json as $item)
    {
      $item->recipes = array();
      $item->disassembly = array();
      $item->toolFor = array();
      $item->toolForCategory = array();
      $item->componentFor = array();
      $item->weight = 2000;
      $item->volume = 10;
      $item->bashing = 8;
      $item->cutting = 0;
      $item->to_hit = 0;
      $items[$item->id] = $item;
    }

    $items["toolset"] = json_decode('{"id":"toolset","name":"integrated toolset"}');
    $items["fire"] = json_decode('{"id":"fire","name":"nearby fire"}');
    return $items;
  }

  public function link($type, $id, $recipe)
  {
    $keys = array(
        "result"=>"recipes",
        "tool"=>"toolFor",
        "component"=>"toolFor",
        "learn"=>"learn"
    );
    $key = $keys[$type];
    if(isset($this->database[$id]))
    {
      if($key=="recipes" and $recipe->category=="CC_NONCRAFT")
      {
        $this->database[$id]->disassembly[] = $recipe->id;
        return;
      }
      if($key=="recipes" and isset($recipe->reversible) and $recipe->reversible=="true")      
      {
        $this->database[$id]->disassembly[] = $recipe->id;
      }

      if($key=="toolFor")
      {
        $this->database[$id]->{"toolForCategory"}[$recipe->category][] = $recipe->id;
      }
      $this->database[$id]->{$key}[] = $recipe->id;
      return;
    }
    if(isset($this->database["vehicle_parts/$id"]))
    {
      $this->database["vehicle_parts/$id"]->{$key}[] = $recipe->id;
      return;
    }
  }
}
{'fraction_non_alphanumeric': 0.21262793714862332, 'fraction_numerical': 0.0028830906732016724, 'mean_word_length': 2.6286610878661087, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 128, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6796653', 'n_tokens_mistral': 2377, 'n_tokens_neox': 2308, 'n_words': 353}