#!/usr/bin/perl -w

# This contains the thing-template functions for project.pm

# (c) Copyright by Tels 1999-2000. All Rights Reserved. Do NOT distribute.
# Updates and support on bloodgate.com/thief

# 2000-02-03 v0.0.1 - from project.pm
# 2000-02-09 v0.0.2 - added: asText

package Cowed::Things;
require Exporter;
@ISA     = qw(Exporter);
@EXPORT_OK  = qw( 
             loadThings getThing getThingList putThing
             clearError getError new asText
             );
@EXPORT = qw();

use Cowgen::Text;
use strict 'vars';

my $VERSION = "0.0.2";

##############################################################################
# things
##############################################################################

sub new
  {
  # create new Thinglist
  # parameters: dirs to load things from
  my $c = shift;
  my $s = {}; bless $s, $c; 
  my $rc = $s->init(@_);
  $s->{error} = $rc if (defined $rc);
  return $s; 
  }

sub init
  {
  my $s = shift; my $n = shift;
  $s->{error} = ""; $s->{id} = 1;
  $s->loadThings($n);
  }

sub clearError
  {
  # clear error text
  my $s = shift;
  $s->{error} = "";
  }

sub getError
  {
  # return error text
  my $s = shift;
  return $s->{error};
  }

sub loadThings
  {
  # load things from one or more dirs
  # output: undefined or string on error
  # parameter: dirs
  my $s = shift;
  my @dirs = @_;
  my ($dir,$rc,$file,@files);
  foreach $dir (@dirs)
    {
    opendir DIR, $dir || return $s->error ("Cant read dir $dir: $!\n");
    @files = readdir DIR;
    closedir DIR;
    foreach $file (@files)
      {
      next if ($file =~ /^\.\.?$/);
      $rc = $s->loadThing ("$dir/$file");
      if (!defined $s->{things}->{$rc})
        {
        return $s->error($rc); # error, return error string
        }
      }
    }
  }

sub loadThing     
  {
  # load a thing from a file (absolute or relative path+name),
  # internal only
  # output: id (numeric) or string on error
  # parameters: filename
  my $s = shift;
  my $file = shift || return;
  return $s->error("File $file does not exist.") if (!-e $file);
  my $keywords;
  $keywords->{0} = { template => 'things', };
  my $t = Cowgen::Text::fromText ($file,$keywords); 
  if (ref($t) ne 'HASH')
    {
    return $t;
    } 
  foreach (@{$t->{things}})
    {
    $s->{things}->{$s->{id}++} = $_;
    }
  return $s->{id}-1;    
  }

sub getThing     
  {
  # return a things properties from id
  # output: ref to hash, or string on error
  # parameters: id
  my $s = shift;
  my $id = shift || return;
  return $s->error("Thing does not exist") if (!defined $s->{things}->{$id});
  my $p = {};
  foreach (keys%{$s->{things}->{$id}})
    {
    $p->{$_} = $s->{things}->{$id}->{$_};
    }
  $p;
  }

sub getThingList     
  {
  # return a hash containing {id} => { "name", "description" }
  # output: ref to hash
  my $s = shift;
  my $p = {};
  foreach my $id (keys %{$s->{things}})
    {
    $p->{$id} = { 
      name => $s->{things}->{$id}->{name}, 
      description => $s->{things}->{$id}->{description}, 
      };
    }
  $p;
  }

sub asText
  {
  # return a thing as text
  # parameters: id
  my $s = shift;
  my $id = shift || return;
  return $s->error("Thing does not exist") if (!defined $s->{things}->{$id});
  my $map = {};
  my $dontput = { id => 1, };
  return Cowgen::Text->asText ($s->{things}->{$id},'Thing',1,$map,$dontput);
  }

sub putThing     
  {
  # set a things properties by id and hash
  # output: id (numeric) or string on error
  # parameters: id
  my $s = shift;
  my $id = shift || return;
  my $hash = shift || {};
  return $s->error("Thing does not exist") if (!defined $s->{things}->{$id});
  $s->{things}->{$id} = {};
  foreach (keys %$hash)
    {
    $s->{things}->{$id}->{$_} = $hash->{$_};
    }
  return $id;
  }

sub error
  {
  my $s = shift;
  $s->{error} = shift;
  }

# EOF

1;
