<commit_before>use v6;

use LacunaCookbuk::Model::Body::SpaceStation;
use LacunaCookbuk::Model::Body::Planet;
use PerlStore::FileStore;
use LacunaCookbuk::Model::LacunaBuilding;
use LacunaCookbuk::Model::Empire;
use Terminal::ANSIColor;

#| Class is responsible for reading bodies and storing them
unit class LacunaCookbuk::Logic::BodyBuilder;

my Planet @planets;
my SpaceStation @stations;


submethod read {
    my $path_planets = make_path('planets.pl');
    my $path_stations = make_path('stations.pl');

#= I want this code back
    @planets = from_file($path_planets);
    @stations = from_file($path_stations);
    #moar hack
#    say 'Readin $path_planets';
#    my $plan = slurp $path_planets;
#    @planets = EVAL $plan;

    #moar hack
#    say 'Readin $path_stations';
#    my $stat =  slurp $path_stations;
#    @stations = EVAL $stat; 


}

submethod write {
    my $path_planets = make_path('planets.pl');
    my $path_stations = make_path('stations.pl');

    to_file($path_planets, @planets);
    to_file($path_stations, @stations);
}

#this not something I'm proud of
submethod process_all_bodies {
    @planets = ();
    @stations = ();
    for Empire.planets_hash.keys -> $planet_id {
	#TODO report rakudobug for .=
	my Body $body = Body.new(id => $planet_id);
	$body.get_buildings;	
       
	if $body.is_station {
	    my SpaceStation $station .= new(id => $planet_id, buildings => $body.buildings,  x => $body.x, y => $body.y);
	    say $station.name ~ " is a Space Station";
	    @stations.push($station)
	} elsif $body.is_planet {
	    my Planet $planet .= new(id => $planet_id, buildings => $body.buildings, ore => $body.ore, x => $body.x, y => $body.y);
	    say $planet.name ~ " is a Planet";
	    @planets.push($planet)
	}else {
	    warn $body.name ~ " Cannot be used -- neither planet nor station";
	}
    } 
    LacunaCookbuk::Logic::BodyBuilder.write;
}

sub home_planet(--> Planet) is export {
    for @planets -> Planet $planet {
	return $planet if $planet.is_home;
    }
    Planet;
}

sub planets is export {
    @planets
}

sub find_planet(Str $s) is export {
    for @planets -> $p {
        return $p if $p.name ~~ $s;
    }

}

sub stations is export {
    @stations
}


submethod report_waste {
    for @planets -> $body {
        my $waste = $body.get_waste_stored;
        my $wasteh = $body.get_waste_hour;
        my $wastec = $body.get_waste_capacity;
        my $wasteload;
        if $wastec == 0 {$wasteload = -100}else{$wasteload = $waste*100/$wastec}
        
        my $color = "default";
	$color = "blue" if $wasteload == 0;
	$color = "yellow" if $wasteload < 0;
        $color = "green" if $wasteh == 0;
        $color = "red" if $wasteload > 99;
	
	say colored("{$body.name} {$waste}({$wasteload}%) at {$wasteh}",$color);
    }
}

constant $ZONE_SIZE = 250;
submethod report_zones {
    for @planets, @stations -> $body {
	my Int $zone_x = (+$body.x / $ZONE_SIZE).Int;	
	my Int $zone_y = (+$body.y / $ZONE_SIZE).Int;<commit_msg>Removing moar hack. Hope no longer will be needed<commit_after>use v6;

use LacunaCookbuk::Model::Body::SpaceStation;
use LacunaCookbuk::Model::Body::Planet;
use PerlStore::FileStore;
use LacunaCookbuk::Model::LacunaBuilding;
use LacunaCookbuk::Model::Empire;
use Terminal::ANSIColor;

#| Class is responsible for reading bodies and storing them
unit class LacunaCookbuk::Logic::BodyBuilder;

my Planet @planets;
my SpaceStation @stations;


submethod read {
    my $path_planets = make_path('planets.pl');
    my $path_stations = make_path('stations.pl');

    @planets = from_file($path_planets);
    @stations = from_file($path_stations);
}

submethod write {
    my $path_planets = make_path('planets.pl');
    my $path_stations = make_path('stations.pl');

    to_file($path_planets, @planets);
    to_file($path_stations, @stations);
}

#this not something I'm proud of
submethod process_all_bodies {
    @planets = ();
    @stations = ();
    for Empire.planets_hash.keys -> $planet_id {
	#TODO report rakudobug for .=
	my Body $body = Body.new(id => $planet_id);
	$body.get_buildings;	
       
	if $body.is_station {
	    my SpaceStation $station .= new(id => $planet_id, buildings => $body.buildings,  x => $body.x, y => $body.y);
	    say $station.name ~ " is a Space Station";
	    @stations.push($station)
	} elsif $body.is_planet {
	    my Planet $planet .= new(id => $planet_id, buildings => $body.buildings, ore => $body.ore, x => $body.x, y => $body.y);
	    say $planet.name ~ " is a Planet";
	    @planets.push($planet)
	}else {
	    warn $body.name ~ " Cannot be used -- neither planet nor station";
	}
    } 
    LacunaCookbuk::Logic::BodyBuilder.write;
}

sub home_planet(--> Planet) is export {
    for @planets -> Planet $planet {
	return $planet if $planet.is_home;
    }
    Planet;
}

sub planets is export {
    @planets
}

sub find_planet(Str $s) is export {
    for @planets -> $p {
        return $p if $p.name ~~ $s;
    }

}

sub stations is export {
    @stations
}


submethod report_waste {
    for @planets -> $body {
        my $waste = $body.get_waste_stored;
        my $wasteh = $body.get_waste_hour;
        my $wastec = $body.get_waste_capacity;
        my $wasteload;
        if $wastec == 0 {$wasteload = -100}else{$wasteload = $waste*100/$wastec}
        
        my $color = "default";
	$color = "blue" if $wasteload == 0;
	$color = "yellow" if $wasteload < 0;
        $color = "green" if $wasteh == 0;
        $color = "red" if $wasteload > 99;
	
	say colored("{$body.name} {$waste}({$wasteload}%) at {$wasteh}",$color);
    }
}

constant $ZONE_SIZE = 250;
submethod report_zones {
    for (@planets, @stations).flat -> $body {
	my Int $zone_x = (+$body.x / $ZONE_SIZE).Int;	
	my Int $zone_y = (+$body.y / $ZONE_SIZE).Int;
{'fraction_non_alphanumeric': 0.1505039972193257, 'fraction_numerical': 0.005561348627042058, 'mean_word_length': 3.314092953523238, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '6424997', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2139, 'n_words': 512}