## Copyright (c) 2012 by Jose Antonio Martin <jantonio.martin AT gmail DOT com>
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU Affero General Public License as published by the
## Free Software Foundation, either version 3 of the License, or (at your option
## any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
## for more details.
##
## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/agpl.txt>.
##
## This license is also included in the file COPYING
##
## AUTHOR: Jose Antonio Martin <jantonio.martin AT gmail DOT com>

from django.db import models

from transmeta import TransMeta

import condottieri_scenarios.managers as managers

class Scenario(models.Model):
	""" This class defines a Condottieri scenario. """

	__metaclass__ = TransMeta
	
	name = models.SlugField(_("slug"), max_length=20, unique=True)
	title = models.CharField(max_length=128, verbose_name=_("title"))
	description = models.TextField(verbose_name=_("description"))
	start_year = models.PositiveIntegerField(_("start year"))
	number_of_players = models.PositiveIntegerField(_("number of players"), default=0)
	enabled = models.BooleanField(_("enabled"), default=False)

	class Meta:
		verbose_name = _("scenario")
		verbose_name_plural = _("scenarios")
		ordering = ["title",]
		translate = ('title', 'description',)

	def get_max_players(self):
		return self.number_of_players

	def get_min_players(self):
		max_players = self.get_max_players()
		neutrals = self.neutral_set.count()
		return max_players - neutrals

	def __unicode__(self):
		return self.title

	def get_absolute_url(self):
		return "scenario/%s" % self.slug

	def _get_countries(self):
		return Country.objects.filter(home__scenario=self).distinct()

	countries = property(_get_countries)

class SpecialUnit(models.Model):
	""" A SpecialUnit describes the attributes of a unit that costs more ducats
	than usual and can be more powerful or more loyal """
	__metaclass__ = TransMeta
	static_title = models.CharField(_("static title"), max_length=50)
	title = CharField(_("title"), max_length=50)
	cost = models.PositiveIntegerField(_("cost"))
	power = models.PositiveIntegerField(_("power"))
	loyalty = models.PositiveIntegerField(_("loyalty"))

	class Meta:
		verbose_name = _("special unit")
		verbose_name_plural = _("special units")
		translate = ("title",)

	def __unicode__(self):
		return _("%(title)s (%(cost)sd)") % {'title': self.title,
											'cost': self.cost}

	def describe(self):
		return _("Costs %(cost)s; Strength %(power)s; Loyalty %(loyalty)s") % {
			'cost': self.cost,
			'power': self.power,
			'loyalty': self.loyalty}

class Country(models.Model):
	__metaclass__ = TransMeta
	name = models.CharField(_("name"), max_length=20, unique=True)
	can_excommunicate = models.BooleanField(_("can excommunicate"), default=False)
	static_name = models.CharField(_("static name"), max_length=20, default="")
	special_units = models.ManyToManyField(SpecialUnit, verbose_name="special units")
	
	objects = managers.CountryManager()

	class Meta:
		verbose_name = _("country")
		verbose_name_plural = _("countries")
		ordering = ["name", ]
		translate = ("name",)

	def __unicode__(self):
		return self.name


@@ -3515,8 +3515,1260 @@
 f.name%0A%0A
+class Neutral(models.Model):%0A%09%22%22%22 Defines a country that will not be used when a game has less players%0A%09than the default. %22%22%22%0A%09scenario = models.ForeignKey(Scenario, verbose_name=_(%22scenario%22))%0A%09country = models.ForeignKey(Country, verbose_name=_(%22country%22))%0A%09priority = models.PositiveIntegerField(_(%22priority%22), default=0)%0A%0A%09class Meta:%0A%09%09verbose_name = _(%22neutral country%22)%0A%09%09verbose_name_plural = _(%22neutral countries%22)%0A%09%09ordering = %5B'scenario', 'priority',%5D%0A%09%09unique_together = (('scenario', 'country'), ('scenario', 'priority'),)%0A%0A%09def __unicode__(self):%0A%09%09return %22(%25s) %25s%22 %25 (self.priority, self.country)%0A%0Aclass Treasury(models.Model):%0A%09%22%22%22%0A%09This class represents the initial amount of ducats that a Country starts%0A%09each Scenario with%0A%09%22%22%22%0A%09scenario = models.ForeignKey(Scenario, verbose_name=_(%22scenario%22))%0A%09country = models.ForeignKey(Country, verbose_name=_(%22country%22))%0A%09ducats = models.PositiveIntegerField(_(%22ducats%22), default=0)%0A%09double = models.BooleanField(_(%22double income%22), default=False)%0A%0A%09def __unicode__(self):%0A%09%09return %22%25s starts %25s with %25s ducats%22 %25 (self.country, self.scenario,%0A%09%09%09self.ducats)%0A%0A%09class Meta:%0A%09%09verbose_name = _(%22treasury%22)%0A%09%09verbose_name_plural = _(%22treasuries%22)%0A%09%09unique_together = ((%22scenario%22, %22country%22),)%0A%0A

add Neutral and Treasury classes