--
-- PostgreSQL database dump
--

SET client_encoding = 'LATIN1';
SET check_function_bodies = false;

SET SESSION AUTHORIZATION 'postgres';

SET search_path = public, pg_catalog;

--
-- TOC entry 152 (OID 274254)
-- Name: plpgsql_call_handler(); Type: FUNC PROCEDURAL LANGUAGE; Schema: public; Owner: postgres
--

CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler
    AS '$libdir/plpgsql', 'plpgsql_call_handler'
    LANGUAGE c;


SET SESSION AUTHORIZATION DEFAULT;

--
-- TOC entry 150 (OID 274255)
-- Name: plpgsql; Type: PROCEDURAL LANGUAGE; Schema: public; Owner: 
--

CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql HANDLER plpgsql_call_handler;


--
-- TOC entry 151 (OID 274255)
-- Name: plpgsql; Type: ACL LANGUAGE; Schema: public; Owner: 
--

REVOKE ALL ON LANGUAGE plpgsql FROM PUBLIC;
SET SESSION AUTHORIZATION postgres;
GRANT ALL ON LANGUAGE plpgsql TO postgres WITH GRANT OPTION;
RESET SESSION AUTHORIZATION;
SET SESSION AUTHORIZATION postgres;
GRANT ALL ON LANGUAGE plpgsql TO PUBLIC;
RESET SESSION AUTHORIZATION;


SET SESSION AUTHORIZATION 'postgres';

--
-- TOC entry 4 (OID 2200)
-- Name: public; Type: ACL; Schema: -; Owner: postgres
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
GRANT ALL ON SCHEMA public TO PUBLIC;


SET SESSION AUTHORIZATION 'postgres';

--
-- TOC entry 153 (OID 274254)
-- Name: plpgsql_call_handler(); Type: ACL; Schema: public; Owner: postgres
--

REVOKE ALL ON FUNCTION plpgsql_call_handler() FROM PUBLIC;
REVOKE ALL ON FUNCTION plpgsql_call_handler() FROM postgres;
GRANT ALL ON FUNCTION plpgsql_call_handler() TO PUBLIC;


SET SESSION AUTHORIZATION 'pbouvier';

--
-- TOC entry 6 (OID 978567)
-- Name: id_mess_auto; Type: SEQUENCE; Schema: public; Owner: pbouvier
--

CREATE SEQUENCE id_mess_auto
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 7 (OID 978572)
-- Name: characters; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE characters (
    char_id serial NOT NULL,
    user_id integer,
    char_pos point DEFAULT point((0)::double precision, (0)::double precision),
    char_name character varying(24),
    char_infos character varying(128),
    char_sex boolean,
    char_money integer DEFAULT 100,
    char_pv integer DEFAULT 10,
    char_pa integer DEFAULT 24,
    char_papm integer DEFAULT 1,
    char_equipmax integer DEFAULT 10,
    char_inventmax integer DEFAULT 10,
    char_head integer DEFAULT 1,
    char_ears integer DEFAULT 1,
    char_corps integer DEFAULT 1,
    char_tail integer DEFAULT 1,
    char_strength integer DEFAULT 5,
    char_const integer DEFAULT 5,
    char_dext integer DEFAULT 5,
    char_psy integer DEFAULT 5,
    char_id_dad integer DEFAULT 1 NOT NULL,
    char_id_mum integer DEFAULT 1 NOT NULL,
    char_pvmax integer DEFAULT 24,
    char_skincolor character varying(6) DEFAULT 0 NOT NULL,
    char_inshop boolean DEFAULT false
);


--
-- TOC entry 12 (OID 978577)
-- Name: equipements; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE equipements (
    equip_id serial NOT NULL,
    char_id integer,
    obj_id integer,
    obj_equip boolean,
    equip_used integer,
    equip_duration integer,
    obj_date timestamp without time zone,
    egg_id_dad integer DEFAULT 0
);


--
-- TOC entry 16 (OID 978590)
-- Name: objects; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE objects (
    obj_id serial NOT NULL,
    obj_name character varying(24),
    obj_infos character varying(128),
    obj_weight integer DEFAULT 10,
    obj_use_skill_id integer DEFAULT 7,
    obj_use_dist_max integer,
    obj_use_dist_min integer,
    obj_use_pa_self integer DEFAULT 12,
    obj_use_pa_target integer DEFAULT 13,
    obj_use_pv_self integer DEFAULT 7,
    obj_use_pv_target integer DEFAULT 8,
    obj_use_limit integer DEFAULT 8,
    obj_impact_skill_id integer DEFAULT 2,
    obj_impact_pa_self integer DEFAULT 6,
    obj_impact_pa_target integer DEFAULT 8,
    obj_impact_pv_self integer DEFAULT 9,
    obj_impact_dam_min integer DEFAULT 7,
    obj_impact_dam_max integer DEFAULT 10,
    obj_equip boolean,
    obj_equip_pa integer DEFAULT 6,
    obj_equip_defense integer DEFAULT 7,
    obj_equip_bonus_strength integer DEFAULT 9,
    obj_equip_bonus_const integer DEFAULT 6,
    obj_equip_bonus_dext integer DEFAULT 10,
    obj_equip_bonus_psy integer DEFAULT 5,
    obj_equip_bonus_pa integer DEFAULT 8,
    obj_inshops boolean,
    obj_graph integer DEFAULT 1,
    obj_skill_id_distance integer DEFAULT 6,
    obj_base_cost integer DEFAULT 8,
    obj_use_dead boolean,
    obj_duration time without time zone
);


--
-- TOC entry 19 (OID 993720)
-- Name: users; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE users (
    user_id serial NOT NULL,
    user_login character varying,
    user_pass character varying,
    user_mail character varying,
    user_guild_name character varying,
    user_guild_color character varying,
    user_guild_infos character varying,
    user_guild_master_id integer DEFAULT currval('public.users_user_id_seq'::text),
    user_date_crea_old time without time zone,
    user_last_connect_old time with time zone,
    user_money integer,
    user_guild_emblem character varying(255),
    user_last_connect timestamp without time zone,
    user_date_crea timestamp without time zone
);


SET SESSION AUTHORIZATION 'lmoineau';

--
-- TOC entry 154 (OID 993756)
-- Name: fired_from_guild(integer, integer); Type: FUNCTION; Schema: public; Owner: lmoineau
--

CREATE FUNCTION fired_from_guild(integer, integer) RETURNS void
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_victim ALIAS FOR $2;
DECLARE user_name STRING;
DECLARE user_guild STRING;
DECLARE entitle STRING;
DECLARE subject STRING; 
DECLARE date TIMESTAMP;  



BEGIN
 SELECT char_name FROM characters WHERE char_id=$1 INTO user_name;
 SELECT user_guild_name FROM users WHERE user_id=$1 INTO user_guild;
  entitle=''tu as été viré de ta guilde '';
 subject=user_name || '' t\\''as viré de la guilde '' || user_guild;
 date = CURRENT_TIMESTAMP;

 INSERT INTO messages VALUES (nextval(''public.user_id_seq''::text),
              id_user,id_victim ,entitle,subject,date);

 UPDATE users SET user_guild_master_id = 0 WHERE user_id = id_victim;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 164 (OID 993757)
-- Name: is_master(integer); Type: FUNCTION; Schema: public; Owner: lmoineau
--

CREATE FUNCTION is_master(integer) RETURNS boolean
    AS 'DECLARE master_id INTEGER;


BEGIN

SELECT user_guild_master_id from users where user_id= $1 INTO master_id;

 if(master_id=0) then
     RETURN TRUE;
   else
     RETURN FALSE; 
   end if;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 156 (OID 993758)
-- Name: invite_into_guild(integer, integer); Type: FUNCTION; Schema: public; Owner: lmoineau
--

CREATE FUNCTION invite_into_guild(integer, integer) RETURNS void
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_victim ALIAS FOR $2;
DECLARE user_name STRING;
DECLARE user_guild STRING;
DECLARE test_master BOOLEAN;  
DECLARE entitle STRING;
DECLARE subject STRING; 
DECLARE date TIMESTAMP;  


BEGIN
 SELECT char_name FROM characters WHERE char_id=$1 INTO user_name;
 SELECT user_guild_name FROM users WHERE user_id=$1 INTO user_guild;
 entitle=''proposition d\\''alliance'';
 SELECT * INTO test_master FROM is_master(id_user);
 
if (test_master = TRUE) then
subject= user_name || '' vous propose de faire partie de l\\''alliance '' || user_guild ||'' :
<a href="resAlliance.php?boul=1"> ! Accepter !</a> | <a href="resAlliance.php?boul=0"> ! Refuser !</a>'';

else 
subject= user_name || ''vous propose de faire partie de l\\''alliance '' || user_guild ||'', mais vous faite deja partie d\\''une alliance. Voulez vous la quitter pour rejoindre l\\''alliance '' || user_guild ||'' :
 <a href="resAlliance.php?boul=2"> ! Quitter et rejoindre la nouvel alliance !</a> | <a href="resAlliance.php?boul=0"> ! Refuser !</a>'';

end if
 
date = CURRENT_TIMESTAMP;
INSERT INTO messages VALUES (nextval(''public.user_id_seq''::text),
              id_user,id_victim ,entitle,subject,date);

END'
    LANGUAGE plpgsql;


--
-- TOC entry 158 (OID 993759)
-- Name: join_guild(integer, integer); Type: FUNCTION; Schema: public; Owner: lmoineau
--

CREATE FUNCTION join_guild(integer, integer) RETURNS void
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_victim ALIAS FOR $2;
DECLARE victim_name STRING;
DECLARE user_guild STRING;
DECLARE entitle STRING;
DECLARE entitle2 STRING;
DECLARE subject STRING; 
DECLARE subject2 STRING;
DECLARE date TIMESTAMP;  


BEGIN
 SELECT char_name FROM characters WHERE char_id=$1 INTO user_name;
 SELECT char_name FROM characters WHERE char_id=$2 INTO victim_name;
 SELECT user_guild_name FROM users WHERE user_id=$1 INTO user_guild;
 
 entitle =''Acceptation d\\''alliance'';
 entitle2 =''Accusé de reception'';
 subject = victim_name || ''a accepter de faire partie de votre alliance'' ||user_guild_name;
 subject2 = user_guild || ''a bien recu l\\''acceptiration de faire partie de votre alliance'' ||user_guild_name;
 date = CURRENT_TIMESTAMP;
 
 INSERT INTO messages VALUES (nextval(''public.user_id_seq''::text),
              id_user,id_victim ,entitle,subject,date);
 INSERT INTO messages VALUES (nextval(''public.user_id_seq''::text),
              id_victim, id_user,entitle2,subject2,date);
 UPDATE users SET user_guild_master_id = id_user WHERE user_id = id_victim OR user_guild_master_id= id_victim;
END'
    LANGUAGE plpgsql;


--
-- TOC entry 160 (OID 993760)
-- Name: quit_guild(integer, integer); Type: FUNCTION; Schema: public; Owner: lmoineau
--

CREATE FUNCTION quit_guild(integer, integer) RETURNS void
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_master ALIAS FOR $2;
DECLARE user_name STRING;
DECLARE entitle STRING;
DECLARE subject STRING; 
DECLARE date TIMESTAMP;  



BEGIN
 SELECT char_name FROM characters WHERE char_id=$1 INTO user_name;
 entitle=''Un membre de votre alliance est parti est parti vers d\\''autres cieux'';
 subject=user_name || '' a quitté ta guilde de mimichants pas beau'';
 date = CURRENT_TIMESTAMP;

 INSERT INTO messages VALUES (nextval(''public.user_id_seq''::text),
              id_user,id_master ,entitle,subject,date);

 UPDATE users SET user_guild_master_id = 0 WHERE user_id = id_user;

END;
 '
    LANGUAGE plpgsql;


--
-- TOC entry 162 (OID 993761)
-- Name: refuse_guild(integer, integer); Type: FUNCTION; Schema: public; Owner: lmoineau
--

CREATE FUNCTION refuse_guild(integer, integer) RETURNS void
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_victim ALIAS FOR $2;
DECLARE victim_name STRING;
DECLARE user_guild STRING;
DECLARE entitle STRING;
DECLARE entitle2 STRING;
DECLARE subject STRING;
DECLARE subject2 STRING; 
DECLARE date TIMESTAMP;  


BEGIN
 SELECT char_name FROM characters WHERE char_id=$1 INTO user_name;
 SELECT char_name FROM characters WHERE char_id=$2 INTO victim_name;
 SELECT user_guild_name FROM users WHERE user_id=$1 INTO user_guild;
 
 entitle=''Refus d\\''alliance'';
 entitle2=''Accusé de reception'';
 subject= victim_name || ''a refusé de faire partie de votre alliance'' ||user_guild_name;
 subject2= user_guild || ''a bien recu votre refus de faire partie de votre alliance'' ||user_guild_name;
 date = CURRENT_TIMESTAMP;
 
 INSERT INTO messages VALUES (nextval(''public.user_id_seq''::text),
              id_victim,id_user ,entitle,subject,date);
 
 INSERT INTO messages VALUES (nextval(''public.user_id_seq''::text),
              id_user,id_victim ,entitle2,subject2,date);
 
END'
    LANGUAGE plpgsql;


SET SESSION AUTHORIZATION 'pbouvier';

--
-- TOC entry 188 (OID 994123)
-- Name: equip(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION equip(integer, integer) RETURNS boolean
    AS 'DECLARE mimi ALIAS FOR $1;
DECLARE equipement ALIAS FOR $2;
 DECLARE equipUsePA INTEGER;
 DECLARE objLimit INTEGER;
 DECLARE equipUsed INTEGER;
 DECLARE equipable BOOLEAN;
 DECLARE pa INTEGER;
 DECLARE maxi INTEGER;
 DECLARE nb INTEGER;
 DECLARE eq BOOLEAN;
 DECLARE dateEq TIMESTAMP;
 DECLARE date_act TIMESTAMP;
 DECLARE diff_date TIMESTAMP;
 DECLARE tempObj TIME;

BEGIN

SELECT obj_equip FROM equipements WHERE equip_id=equipement INTO eq;

SELECT char_pa,char_equipmax FROM characters WHERE char_id=mimi INTO pa,maxi;

SELECT count(equip_id) FROM equipements WHERE char_id=mimi and obj_equip=TRUE INTO nb;

SELECT objects.obj_equip,equipements.equip_used  ,equipements.obj_date  ,objects.obj_use_limit ,objects.obj_equip_pa,objects.obj_duration  FROM equipements,objects WHERE equip_id=equipement and objects.obj_id=equipements.obj_id INTO equipable,equipUsed,dateEq,objLimit,equipUsePA,tempObj;


date_act=CURRENT_TIMESTAMP;
diff_date=dateEq+tempObj;


if ((equipable = TRUE)AND (equipUsed<objLimit) AND (pa>equipUsePA) AND (nb<maxi) AND (eq=FALSE) AND (date_act<=diff_date)) then

UPDATE equipements SET obj_equip = TRUE WHERE equip_id = equipement;
UPDATE characters SET char_pa = (char_pa-equipUsePA) WHERE char_id = mimi;

return TRUE;
else
return FALSE;
end if;



END
 


 '
    LANGUAGE plpgsql;


--
-- TOC entry 35 (OID 994220)
-- Name: ground_types; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE ground_types (
    type_id serial NOT NULL,
    type_name name,
    type_infos character varying,
    type_move_pa smallint DEFAULT 1,
    type_defense smallint DEFAULT 0,
    type_take_pa smallint DEFAULT 50
);


--
-- TOC entry 42 (OID 994249)
-- Name: mines; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE mines (
    mine_id serial NOT NULL,
    ground_id integer,
    mine_name name,
    mine_infos character varying,
    mine_capacity integer,
    mine_graph name,
    mine_take_pa smallint DEFAULT 80
);


--
-- TOC entry 50 (OID 994267)
-- Name: town_services; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE town_services (
    service_id serial NOT NULL,
    service_name name,
    service_infos character varying,
    service_pa smallint,
    service_cp smallint
);


--
-- TOC entry 56 (OID 994277)
-- Name: town_services_links; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE town_services_links (
    link_id serial NOT NULL,
    town_id integer,
    service_id integer
);


--
-- TOC entry 60 (OID 994286)
-- Name: towns; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE towns (
    town_id serial NOT NULL,
    ground_id integer,
    town_name name,
    town_infos character varying,
    town_take_pa smallint DEFAULT 100,
    town_coeff smallint DEFAULT 100
);


--
-- TOC entry 67 (OID 994307)
-- Name: grounds; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE grounds (
    ground_id serial NOT NULL,
    type_id integer DEFAULT 1,
    ground_pos point,
    user_id integer DEFAULT 0,
    ground_build smallint DEFAULT 0 NOT NULL,
    deco_id integer,
    obj_id integer
);


--
-- TOC entry 74 (OID 1021977)
-- Name: decorations; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE decorations (
    deco_id serial NOT NULL,
    deco_name name,
    deco_infos character varying,
    deco_obstacle boolean DEFAULT false,
    deco_graph name
);


--
-- TOC entry 258 (OID 1022089)
-- Name: tests_take_ground(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION tests_take_ground(integer) RETURNS integer
    AS 'DECLARE id_mimichant ALIAS FOR $1;
DECLARE id_utilisateur INTEGER;
DECLARE NbPa INTEGER;
DECLARE Proprietaire INTEGER;
DECLARE Alliance CHARACTER VARYING;
DECLARE AllianceProprietaire CHARACTER VARYING;
DECLARE AdversairesSurCase INTEGER;
DECLARE NbAdv INTEGER;
DECLARE rep INTEGER;
DECLARE coutTerrain INTEGER;
DECLARE id_ground INTEGER;

BEGIN

        SELECT ground_id INTO id_ground FROM grounds JOIN characters ON ground_pos ~= char_pos WHERE char_id=id_mimichant;
    	SELECT * into coutTerrain FROM ground_price(id_ground);
	SELECT user_id INTO id_utilisateur FROM characters WHERE char_id = id_mimichant;
	SELECT char_pa INTO NbPa FROM characters WHERE char_id = id_mimichant;
	IF (nbPa = 0) THEN 
                RETURN 0;
        ELSE 
                SELECT user_id INTO Proprietaire FROM grounds WHERE ground_id=id_ground;	
                IF (Proprietaire IS NOT NULL)THEN
			SELECT user_guild_master_id INTO Alliance FROM users WHERE user_id = id_utilisateur;
			SELECT user_guild_master_id INTO AllianceProprietaire FROM users WHERE user_id = 

Proprietaire;
                        IF (Alliance = AllianceProprietaire)THEN
				RETURN 1;
			ELSE
				SELECT char_id INTO AdversairesSurCase FROM(SELECT user_id FROM users WHERE 

user_guild_master_id = AllianceProprietaire)AS temp JOIN characters ON temp.user_id=characters.user_id WHERE 

char_pos[0] = x AND char_pos[1]=y AND char_pv > 0 ;
				                       
                                IF (AdversairesSurCase IS NULL) THEN
				     
                                      SELECT * INTO rep FROM take_ground(id_mimichant,coutTerrain);
                                      RETURN 3;
				ELSE
				      RETURN 2;
                                END IF;
                        END IF;
                RETURN 1;
                ELSE 
                     
                     SELECT * INTO rep FROM take_ground(id_mimichant,coutTerrain);
                     RETURN 4;
                END IF;
        END IF;
END;'
    LANGUAGE plpgsql;


SET SESSION AUTHORIZATION 'tjeyarat';

--
-- TOC entry 166 (OID 1022303)
-- Name: update_pa(integer); Type: FUNCTION; Schema: public; Owner: tjeyarat
--

CREATE FUNCTION update_pa(integer) RETURNS integer
    AS 'DECLARE 
date_current TIMESTAMP;
minute_current INTEGER;
second_current DOUBLE PRECISION;
second_current_casted INTEGER;
perso_id ALIAS FOR $1;

BEGIN

date_current = CURRENT_TIMESTAMP;

minute_current := extract(minute from date_current);

second_current := extract(second from date_current);

SELECT CAST (second_current AS INTEGER)INTO second_current_casted; 

if (minute_current=23 )
then
 
 return 1;
else
 return 0;
end if;



END;'
    LANGUAGE plpgsql;


--
-- TOC entry 167 (OID 1022308)
-- Name: update_pa_hour(integer); Type: FUNCTION; Schema: public; Owner: tjeyarat
--

CREATE FUNCTION update_pa_hour(integer) RETURNS integer
    AS 'DECLARE 
perso_id ALIAS FOR $1;
perso_pa INTEGER;
perso_papm INTEGER;
perso_pamax INTEGER;

BEGIN


SELECT char_pa FROM characters WHERE char_id = perso_id INTO perso_pa ;
SELECT char_papm FROM characters WHERE char_id = perso_id INTO perso_papm ;
/*
SELECT char_pamax FROM characters WHERE char_id = perso_id INTO perso_pamax ;
*/


/* 
if ( perso_pa + perso_papm > perso_pamax )
then
UPDATE characters SET char_pa = perso_pamax WHERE char_id = perso_id; 

else
*/

perso_pa := perso_pa + perso_papm;
UPDATE characters SET char_pa = perso_pa WHERE char_id = perso_id; 

/*
end if;
*/

END;'
    LANGUAGE plpgsql;


SET SESSION AUTHORIZATION 'pbouvier';

--
-- TOC entry 168 (OID 1022524)
-- Name: move(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION "move"(integer, integer, integer) RETURNS integer
    AS 'DECLARE 

id_char ALIAS FOR $1;
x_dir   ALIAS FOR $2;
y_dir   ALIAS FOR $3;

x_dest  INTEGER;
y_dest  INTEGER;

pos_dest POINT;
pos_char POINT;
pa_char  INTEGER;

ground_dest_type INTEGER;
ground_dest_pa   INTEGER;

new_pa_char INTEGER;

BEGIN
	SELECT char_pos FROM characters WHERE char_id = id_char INTO pos_char;
	
	IF (x_dir >= -1 AND x_dir <= 1) THEN x_dest = pos_char[0] + x_dir; END IF;
	IF (y_dir >= -1 AND y_dir <= 1) THEN y_dest = pos_char[1] + y_dir; END IF;
	
	pos_dest := POINT ( x_dest, y_dest );
	IF (pos_dest ~= pos_char) THEN
		RETURN 1;
	END IF;
	
	SELECT type_id FROM grounds WHERE ( ground_pos ~= pos_dest ) INTO ground_dest_type;
	IF (ground_dest_type = 0) THEN
		RETURN 2;
	END IF;

	SELECT type_move_pa FROM ground_types WHERE type_id = ground_dest_type INTO ground_dest_pa;
	
	SELECT char_pa FROM characters WHERE char_id = id_char INTO pa_char;
	
	new_pa_char := pa_char - ground_dest_pa;
	
	IF ( new_pa_char > 0 ) THEN
		UPDATE characters SET char_pos = pos_dest, char_pa = new_pa_char WHERE char_id = id_char;
		RETURN 0;
	END IF;
	RETURN 3;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 172 (OID 1022576)
-- Name: minage(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION minage(integer, integer, integer) RETURNS boolean
    AS '/* MINAGE(ground_id, char_id, nb_pa_depense) */



DECLARE


coef_comp INTEGER;
strength INTEGER;
const INTEGER;
dext INTEGER;
pa INTEGER;
pa_tot INTEGER;
mine_cap INTEGER;
nb_pa_depense INTEGER;
cp INTEGER;

BEGIN 

nb_pa_depense := $3;

/* pa : nb de pa du character */
SELECT char_pa 
INTO pa
FROM characters
WHERE char_id=$2;

/* mine_cap : nb de cp que contient la mine */
SELECT mine_capacity 
INTO mine_cap
FROM mines
WHERE ground_id = $1;

	
SELECT char_strength, char_const, char_dext 
INTO strength, const, dext 
FROM characters 
WHERE char_id=$2;

coef_comp := (strength + const/2 + dext)/20;

/* si l''utilisateur veut dépenser plus de PA qu''il n''en a */
IF (nb_pa_depense < pa) THEN

	cp := nb_pa_depense*coef_comp;

	/*si le nombre de choco de la mine est suffisant*/
	IF cp < mine_cap THEN
	
		UPDATE characters
		SET char_money = char_money + cp
		WHERE char_id=$2;

		UPDATE characters
		SET char_pa = char_pa - nb_pa_depense
		WHERE char_id=$2;

		UPDATE mines
		SET mine_capacity =  mine_capacity - cp
		WHERE ground_id = $1;

	/*si le nombre de choco de la mine n''est pas suffisant*/
	ELSE

		UPDATE characters
		SET char_money = char_money + mine_cap
		WHERE char_id=$2;

		UPDATE characters
		SET char_pa = char_pa - nb_pa_depense
		WHERE char_id=$2;

		/* on renouvelle la mine vide */
		SELECT * FROM chargement_mine($1);
	
	END IF;

/* si le nombre de PA du character est insuffisant */
ELSE 

	pa_tot := pa;
	cp := pa_tot*coef_comp;

	/*si le nombre de choco de la mine est suffisant*/
	IF (cp < mine_cap) THEN

		UPDATE characters
		SET char_money = char_money + cp
		WHERE char_id=$2;

		UPDATE characters
		SET char_pa = char_pa - nb_pa_depense
		WHERE char_id=$2;
		
		UPDATE mines
		SET mine_capacity =  mine_capacity - cp
		WHERE ground_id = $1;

	/*si le nombre de choco de la mine n''est pas suffisant*/
	ELSE

		UPDATE characters
		SET char_money = char_money + mine_cap
		WHERE char_id=$2;
		
		UPDATE characters
		SET char_pa = char_pa - nb_pa_depense
		WHERE char_id=$2;
		
		/* on renouvelle la mine vide */
		SELECT * FROM chargement_mine($1);
		
	END IF;
END IF;


return true;

/* fin du begin */
END;









'
    LANGUAGE plpgsql;


--
-- TOC entry 229 (OID 1022577)
-- Name: chargement_mine(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION chargement_mine(integer) RETURNS boolean
    AS '/* CHARGEMENT_MINE(ground_id) */


/*
donner aléatoirement un nb de choco à la mine (de 50 à 100 par ex)
compter les grounds
donner aleatoirement la position de la mine : pas sur terrier, eau, autre mine, ni obstacle
changer le ground_build du ground choisi à 2=mine
*/

DECLARE
nb_choco INTEGER;
nb_ground INTEGER;
ground_selected INTEGER;
builded INTEGER;
type_ground INTEGER;
obstacle BOOLEAN;
rd DOUBLE PRECISION;

BEGIN

rd := random();
nb_choco := round(rd*50 +50);
/*nb_choco := 80;*/


UPDATE mines
SET mine_capacity =  nb_choco
WHERE ground_id = $1;

SELECT MAX(ground_id)
INTO nb_ground
FROM grounds;

LOOP
    ground_selected := round(rd *(nb_ground-1)+1);
    /*ground_selected := 10;*/

	/* case vide ou non */
	SELECT ground_build 
	INTO builded
	FROM grounds
	WHERE ground_id = ground_selected;
	
	/* type du terrain, eau=4 */
	SELECT type_id 
	INTO type_ground
	FROM grounds
	WHERE ground_id = ground_selected;
	
	/* obstacle sur le terrain *
	SELECT deco_obstacle 
	INTO obstacle
	FROM decorations d JOIN grounds g
	WHERE ( d.deco_id = g.deco_id AND g.ground_id = ground_selected );
	
	/* si le terrain est correct on sort du loop */
    IF ( builded =0 AND type_ground!=4 AND  obstacle=false ) THEN
        EXIT;  
    END IF;
	/* sinon on choisit un nouveau terrain */
	
END LOOP;

/* on attribue le nouveau ground à la mine */
UPDATE mines
SET ground_id =  ground_selected
WHERE ground_id = $1;

/* on rajoute la mine au ground */
UPDATE grounds
SET ground_build =  2
WHERE ground_id = ground_selected;

return true;

END;



'
    LANGUAGE plpgsql;


--
-- TOC entry 80 (OID 1022601)
-- Name: quests; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE quests (
    quest_id serial NOT NULL,
    quest_dial_begin character varying,
    quest_dial_end character varying,
    obj_id_get integer,
    obj_id_win integer,
    "quest_nbCP_win" integer,
    quest_nbobj_win integer,
    obj_ground_type integer,
    quest_dial_middle character varying
);


--
-- TOC entry 170 (OID 1022662)
-- Name: quest_has_object(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION quest_has_object(integer, integer) RETURNS boolean
    AS 'DECLARE
id_mimichant ALIAS FOR $1;
id_pnj ALIAS FOR $2;
obj_identifier INTEGER;
nb_objects INTEGER;

BEGIN

/* On récupère l''identifiant de l''objet d''après la quête du PNJ */
SELECT obj_id_get FROM quests q, pnjs p WHERE pnj_id=id_pnj AND p.quest_id=q.quest_id INTO obj_identifier;

/* On cherche l''objet dans l''équipement */
SELECT COUNT(equip_id) FROM equipements WHERE char_id=id_mimichant AND equip_id=obj_identifier INTO nb_objects;

IF(nb_objects = 0) THEN
  RETURN FALSE;
ELSE
  RETURN TRUE;
END IF;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 91 (OID 1022665)
-- Name: pnjs; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE pnjs (
    pnj_id serial NOT NULL,
    pnj_type boolean,
    quest_id integer,
    pnj_dialon character varying,
    pnj_dialshop character varying,
    pnj_sell boolean,
    pnj_buy integer,
    pnj_graph character varying,
    pnj_pos point
);


--
-- TOC entry 102 (OID 1022707)
-- Name: towntemp; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE towntemp (
    ground_id integer
);


--
-- TOC entry 103 (OID 1022725)
-- Name: messages; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE messages (
    message_id serial NOT NULL,
    user_send_id integer,
    message_subject character varying,
    message_text text,
    message_read boolean,
    message_date timestamp without time zone
);


--
-- TOC entry 105 (OID 1022767)
-- Name: send; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE send (
    message_id integer,
    user_receive_id integer,
    message_read boolean
);


--
-- TOC entry 107 (OID 1022779)
-- Name: grounds_visibles; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE grounds_visibles (
    vis_id serial NOT NULL,
    ground_id integer,
    user_id integer
);


--
-- TOC entry 109 (OID 1022786)
-- Name: eggs; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE eggs (
    id_egg serial NOT NULL,
    id_char_mum integer,
    id_char_dad integer,
    id_object integer,
    date_copulation timestamp without time zone
);


--
-- TOC entry 111 (OID 1022806)
-- Name: characters_sold; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE characters_sold (
    sold_id serial NOT NULL,
    char_id integer,
    sold_price integer,
    ground_id integer
);


--
-- TOC entry 113 (OID 1022815)
-- Name: skills; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE skills (
    skill_id serial NOT NULL,
    skill_name character varying(50),
    skill_infos character varying(255),
    skill_up_dodge integer,
    skill_up_impact_melee integer,
    skill_up_impact_distance integer,
    skill_up_training integer,
    skill_up_hurt integer,
    skill_up_psy integer,
    skill_up_fight_melee integer,
    skill_up_fight_distance integer,
    skill_up_move integer
);


--
-- TOC entry 115 (OID 1022824)
-- Name: events; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE events (
    event_id serial NOT NULL,
    user_id integer,
    char_id integer,
    event_name character varying(255),
    event_infos text,
    event_date timestamp without time zone
);


--
-- TOC entry 116 (OID 1022838)
-- Name: characters_skills; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE characters_skills (
    char_skill_id serial NOT NULL,
    char_id integer,
    skill_id integer,
    skill_lvl integer
);


--
-- TOC entry 118 (OID 1022847)
-- Name: townindices; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE townindices (
    ground_id integer
);


--
-- TOC entry 173 (OID 1023451)
-- Name: add_visible_grounds(); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION add_visible_grounds() RETURNS "trigger"
    AS 'DECLARE

vision INTEGER;

BEGIN
    
    IF NEW.char_pos[0] != OLD.char_pos[0] OR NEW.char_pos[1] != OLD.char_pos[1] THEN 
        
        vision := 1+(NEW.char_psy+NEW.char_dext);

        IF vision > 5 THEN
                vision := 5;
        END IF;

        INSERT INTO grounds_visibles(ground_id,user_id)
	SELECT grounds.ground_id, OLD.user_id  FROM grounds
	WHERE  grounds.ground_pos @ circle( NEW.char_pos, vision )
	EXCEPT
	SELECT gd.ground_id, OLD.user_id FROM grounds AS gd
	JOIN (
	SELECT gv.ground_id  FROM grounds_visibles gv
	   WHERE gv.user_id=OLD.user_id) AS gvn
	ON (gd.ground_id=gvn.ground_id)
	WHERE  grounds.ground_pos @ circle( NEW.char_pos, vision );

    END IF;

RETURN NEW;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 179 (OID 1024209)
-- Name: depose(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION depose(integer, integer) RETURNS boolean
    AS 'DECLARE

 mimi ALIAS FOR $1;
 equipement ALIAS FOR $2;
 ground INTEGER;
 obj INTEGER;
 objet INTEGER;
 ground_construction INTEGER;
 posX INTEGER;
 posY INTEGER;

BEGIN

/*trouver le ground où est le perso*/
SELECT char_pos[0],char_pos[1] FROM characters WHERE char_id=mimi INTO posX,posY;
SELECT ground_id,obj_id,ground_build FROM grounds WHERE ground_pos[0]=posX and ground_pos[1]=posY INTO ground,obj,ground_construction;
 
if(obj!=0 or ground_construction!=0) then
return FALSE;
else
/*calcul de lid de lobjet que lon veut déposer*/
SELECT obj_id FROM equipements WHERE equip_id=equipement INTO objet;
/* dépot de lobjet sur le ground*/
UPDATE grounds SET obj_id = objet WHERE ground_id=ground;
/*suppression de léquipement*/
DELETE  FROM equipements where equip_id=equipement;

end if;

return TRUE;

END'
    LANGUAGE plpgsql;


--
-- TOC entry 177 (OID 1024210)
-- Name: get_object_from_ground(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION get_object_from_ground(integer) RETURNS boolean
    AS 'DECLARE

mimi ALIAS FOR $1;
ground INTEGER;
obj INTEGER;
posX INTEGER;
posY INTEGER;
dat DATE;
maxi INTEGER;
nb INTEGER;

BEGIN
 
SELECT char_pos[0],char_pos[1] FROM characters WHERE char_id=mimi INTO posX,posY;
SELECT char_inventmax FROM characters WHERE char_id=mimi INTO maxi;
SELECT ground_id,obj_id FROM grounds WHERE ground_pos[0]=posX and ground_pos[1]=posY INTO ground,obj;
SELECT count(equip_id) FROM equipements WHERE char_id=mimi INTO nb;

/* si la case est vide?*/

if(obj=NULL or nb=maxi)then 
return FALSE;
else
/*on le met dans lequipement*/
dat=CURRENT_TIMESTAMP;
INSERT INTO equipements VALUES (nextval(''public.equipements_equip_id_seq''::text),
              mimi,obj ,dat,false,0,0);
UPDATE grounds SET obj_id=NULL WHERE ground_id=ground;

end if;
return TRUE;

END'
    LANGUAGE plpgsql;


--
-- TOC entry 237 (OID 1026205)
-- Name: ground_price(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION ground_price(integer) RETURNS integer
    AS 'DECLARE id ALIAS FOR $1;
DECLARE pos POINT;
DECLARE nbPa INTEGER;
DECLARE construct INTEGER;
DECLARE coutTerrain INTEGER;
DECLARE coutVille INTEGER;
DECLARE coutMine INTEGER;

BEGIN

SELECT ground_pos INTO pos 
FROM grounds WHERE ground_id=id;

SELECT ground_build INTO construct 
FROM grounds WHERE ground_id=id;

SELECT type_take_pa 
INTO coutTerrain 
FROM ground_types 
JOIN grounds ON grounds.type_id  = ground_types.type_id 
WHERE ground_id=id;

SELECT char_pa 
INTO nbPa 
FROM characters 
WHERE char_pos~= pos;

IF(construct=1) THEN
SELECT town_take_pa
INTO coutVille
FROM towns
WHERE ground_id=id;
coutTerrain:=coutTerrain+coutVille;
RETURN coutTerrain;
END IF;

IF(construct=2) THEN
SELECT mine_take_pa
INTO coutMine
FROM mines
WHERE ground_id=id;
coutTerrain:=coutTerrain+coutMine;
RETURN coutTerrain;


ELSE
RETURN coutTerrain;
END IF;

END;
'
    LANGUAGE plpgsql;


--
-- TOC entry 119 (OID 1026405)
-- Name: coutterrain; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE coutterrain (
    ground_price integer
);


--
-- TOC entry 120 (OID 1026407)
-- Name: construct; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE construct (
    ground_build smallint
);


--
-- TOC entry 121 (OID 1026410)
-- Name: nbpa; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE nbpa (
    char_pa integer
);


--
-- TOC entry 176 (OID 1027191)
-- Name: newpvmax(); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION newpvmax() RETURNS "trigger"
    AS 'DECLARE newpvmax INTEGER;
BEGIN
   
    IF NEW.char_const != OLD.char_const THEN 
        newpvmax := NEW.char_const *5;
        NEW.char_pvmax := newpvmax;

        IF NEW.char_pv > newpvmax THEN
            NEW.char_pv := newpvmax;

END IF;
END IF;

RETURN NEW;

END;
'
    LANGUAGE plpgsql;


--
-- TOC entry 175 (OID 1027192)
-- Name: newequipmax(); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION newequipmax() RETURNS "trigger"
    AS 'DECLARE newmax INTEGER;
BEGIN
   
    IF NEW.char_strength != OLD.char_strength THEN 
  
        newmax := 3 + NEW.char_strength*2;
        NEW.char_equipmax := newmax;

END IF;
RETURN NEW;
END;
'
    LANGUAGE plpgsql;


--
-- TOC entry 181 (OID 1027264)
-- Name: unequip(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION unequip(integer, integer) RETURNS boolean
    AS 'DECLARE 
mimi ALIAS FOR $1;
equipement ALIAS FOR $2;
pa INTEGER;
equipPa INTEGER;
eq BOOLEAN;
 
BEGIN

SELECT obj_equip FROM equipements WHERE equip_id=equipement INTO eq;
 
SELECT char_pa FROM characters WHERE char_id=mimi INTO pa;
 

SELECT obj_equip_pa FROM equipements e JOIN objects o ON e.obj_id=o.obj_id WHERE equip_id=equipement INTO equipPa;

if((pa<equipPa) or (eq=FALSE))  then 
return FALSE;
end if; 

UPDATE equipements SET obj_equip = FALSE WHERE equip_id = equipement;
UPDATE characters SET char_pa = (char_pa-equipPa) WHERE char_id = mimi;
return TRUE;

END
 
 '
    LANGUAGE plpgsql;


--
-- TOC entry 244 (OID 1027379)
-- Name: sell_obj(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION sell_obj(integer, integer, integer) RETURNS boolean
    AS 'DECLARE Boutique ALIAS FOR $1;
DECLARE Vendeur ALIAS FOR $2;
DECLARE EqpVend ALIAS FOR $3;
DECLARE PA integer;
DECLARE Prix integer;
DECLARE Proprietaire integer;
DECLARE ObjVend integer;
DECLARE perso_pa integer;
DECLARE perso_money integer;
DECLARE money integer;
DECLARE idObj integer;

BEGIN

/* Retrait des points d''action acheteur ou vendeur */

SELECT service_pa 
INTO PA
FROM Town_services
WHERE service_id = Boutique;

SELECT char_pa 
INTO perso_pa
FROM characters
WHERE char_id = Vendeur;


SELECT char_money 
INTO perso_money
FROM characters
WHERE char_id = Vendeur;

IF(perso_pa < PA) THEN
  RETURN FALSE;

ELSE
  UPDATE characters SET char_pa = perso_pa - PA WHERE char_id = Vendeur;

  /* Recherche du propriétaire du mimichant vendeur */
  SELECT user_id INTO Proprietaire FROM characters WHERE char_id = Vendeur;

  /* Récupérer le prix de base des objets */
  SELECT obj_id INTO idObj FROM equipements WHERE equip_id = EqpVend;
  SELECT obj_base_cost INTO Prix FROM objects WHERE obj_id = idObj;

  money = perso_money + Prix/2;
 
  /* Enlever l''objet vendu de l''inventaire du mimichant vendeur */
  DELETE FROM equipements WHERE equip_id = EqpVend AND char_id=Vendeur;

  /* Créditer le compte du propriétaire du mimichant vendeur */
  /*UPDATE characters SET char_money = perso_money + Prix/2 WHERE char_id = Vendeur;*/
  UPDATE characters SET char_money = money WHERE char_id = Vendeur;

  RETURN TRUE;

END IF;

END'
    LANGUAGE plpgsql;


--
-- TOC entry 242 (OID 1027381)
-- Name: buy_obj(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION buy_obj(integer, integer, integer) RETURNS boolean
    AS 'DECLARE Boutique ALIAS FOR $1;
DECLARE Acheteur ALIAS FOR $2;
DECLARE ObjAch ALIAS FOR $3;
DECLARE PA integer;
DECLARE Prix integer;
DECLARE Proprietaire integer;
DECLARE Solde integer;
DECLARE perso_pa integer;
DECLARE date_achat TIMESTAMP;


BEGIN

date_achat = CURRENT_TIMESTAMP;

/* Retrait des points d''action du mimichant acheteur */

SELECT service_pa INTO PA FROM Town_services WHERE service_id = Boutique;

SELECT char_pa INTO perso_pa FROM characters WHERE char_id = Acheteur;

IF (perso_pa >= PA) THEN

  UPDATE characters SET char_pa = char_pa - PA WHERE char_id = Acheteur;

  /* Recherche du propriétaire du mimichant acheteur */
  SELECT user_id INTO Proprietaire FROM characters WHERE char_id = Acheteur;

  /* Récupérer le prix de base des objets */
  SELECT obj_base_cost INTO Prix FROM objects WHERE obj_id = ObjAch;

  /* Récupérer le solde du mimichant acheteur */
  SELECT char_money INTO Solde FROM characters WHERE char_id = Acheteur;

  /* Debiter le compte du mimichant acheteur */
  if (Solde > Prix) then 
    UPDATE characters 
    SET char_money = char_money - Prix
    WHERE char_id = Acheteur;

    /* Ajouter l''objet acheté à l''inventaire du mimichant */

    INSERT INTO equipements(char_id, obj_id,obj_equip,equip_duration,obj_date)
    VALUES (Acheteur, ObjAch,TRUE,20, date_achat);
    RETURN TRUE;
  end if;

END IF;

 RETURN FALSE;

END'
    LANGUAGE plpgsql;


--
-- TOC entry 122 (OID 1029816)
-- Name: propretaire; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE propretaire (
    user_id integer
);


--
-- TOC entry 123 (OID 1029947)
-- Name: echanges; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE echanges (
    ech_id serial NOT NULL,
    equip_id integer NOT NULL,
    obj_id integer NOT NULL
);


--
-- TOC entry 183 (OID 1030529)
-- Name: checkpa(integer, boolean); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION checkpa(integer, boolean) RETURNS boolean
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE sex ALIAS FOR $2;
DECLARE pa INTEGER;

BEGIN
SELECT char_pa INTO pa FROM characters WHERE char_id=mimichant;
    IF (sex = TRUE) THEN
        IF (pa > 11) THEN
           return TRUE;
        END IF;
    ELSE
        IF (pa > 23) THEN
           return TRUE;
        END IF;
    END IF;
return FALSE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 241 (OID 1030534)
-- Name: isheteronotpregnant(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION isheteronotpregnant(integer, integer) RETURNS boolean
    AS 'DECLARE mimichant1 ALIAS FOR $1;
DECLARE mimichant2 ALIAS FOR $2;
DECLARE mimichant1_sex BOOLEAN;
DECLARE mimichant2_sex BOOLEAN;
DECLARE mimichant_female INTEGER;
DECLARE user1 INTEGER;
DECLARE user2 INTEGER;
DECLARE pregnant INTEGER;

BEGIN
SELECT user_id INTO user1 FROM characters WHERE char_id=mimichant1;
SELECT user_id INTO user2 FROM characters WHERE char_id=mimichant2;
    IF (user1 = user2) THEN
        return FALSE;
    END IF;

SELECT char_sex INTO mimichant1_sex FROM characters WHERE char_id=mimichant1;
SELECT char_sex INTO mimichant2_sex FROM characters WHERE char_id=mimichant2;
    IF (mimichant1_sex = mimichant2_sex) THEN
        return FALSE;
    END IF;
    IF (mimichant1_sex= FALSE) THEN
        mimichant_female= mimichant1;
    ELSE mimichant_female= mimichant2;
    END IF;
SELECT count(id_char_mum) INTO pregnant FROM eggs WHERE id_char_mum=mimichant_female;
    IF (pregnant =1) THEN
        return FALSE;
    END IF;
return TRUE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 252 (OID 1030546)
-- Name: checkeggandcheckenoughspace(); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION checkeggandcheckenoughspace() RETURNS boolean
    AS 'DECLARE date_act TIMESTAMP;
DECLARE diff_date TIMESTAMP;
DECLARE i eggs;
DECLARE date_act2 TIMESTAMP;
DECLARE diff_date2 TIMESTAMP;
DECLARE e equipements;
DECLARE user INTEGER;
DECLARE mimi_dad INTEGER;
DECLARE nbgrounds INTEGER;
DECLARE nbmimi INTEGER;
DECLARE coeffgrounds INTEGER;
DECLARE createBool BOOLEAN;
DECLARE strength INTEGER;
DECLARE const INTEGER;
DECLARE dext INTEGER;
DECLARE psy INTEGER;
DECLARE name_mimi character varying(50);
DECLARE infos_mimi text; 
DECLARE id_mum INTEGER;
DECLARE id_user1 INTEGER;
DECLARE id_user2 INTEGER;
DECLARE entitle character varying(255);
DECLARE subject text; 
DECLARE date TIMESTAMP; 

BEGIN
FOR i IN SELECT *  FROM eggs
          LOOP
          date_act=CURRENT_TIMESTAMP;
          diff_date=i.date_copulation+ time ''23:59'';
          IF (date_act>diff_date) THEN
          SELECT char_strength,char_const,char_psy,char_dext INTO strength,const,psy,dext FROM characters where char_id=i.id_char_dad; 
          UPDATE characters SET char_strength = strength+10 WHERE char_id = i.id_char_dad; 
          UPDATE characters SET char_const= const+10 WHERE char_id = i.id_char_dad;
          UPDATE characters SET char_psy= psy+10 WHERE char_id = i.id_char_dad;
          UPDATE characters SET char_dext= dext+10 WHERE char_id = i.id_char_dad;
          INSERT INTO equipements(char_id,obj_id,obj_date,obj_equip,egg_id_dad)VALUES(i.id_char_mum,1,date_act,false,i.id_char_dad);
          DELETE FROM eggs WHERE id_char_mum=i.id_char_mum;    
          END IF; 
   
      
        END LOOP;


FOR e IN SELECT * FROM equipements WHERE obj_id=1
          LOOP
          date_act2=CURRENT_TIMESTAMP;
          diff_date2=e.obj_date+ time ''23:59''+ time ''23:59'';
          IF (date_act2>diff_date2) THEN
          SELECT user_id INTO user FROM characters WHERE char_id=e.char_id;
          SELECT COUNT(ground_id) INTO nbgrounds FROM grounds WHERE user_id=user;
          SELECT COUNT(char_id) INTO nbmimi FROM characters WHERE user_id=user;
          coeffgrounds=nbgrounds/nbmimi;
          SELECT char_strength,char_const,char_dext,char_psy INTO strength,const, dext, psy FROM characters WHERE char_id=e.char_id;

            /* IF(coeffgrounds>=2) THEN*/
             /* creation_mimichant*/ 
             name_mimi:= ''bebe_mimichant '';
             infos_mimi:= ''nouveau mimichant'';
             id_mum=e.char_id;
             mimi_dad = e.egg_id_dad;
          
             SELECT *  INTO createBool  FROM                   create_mimichant_by_repro(user,name_mimi,infos_mimi,id_mum,mimi_dad,strength,const,dext,psy);
             /*END IF;*/
             /* sinon : envoi d''un message */
             /* SELECT user_id INTO id_user FROM characters WHERE char_id=id_mum;
              entitle:= ''pas assez d\\''espace '';
              subject:= ''tu n\\''as pas assez de terrain pour abriter un nouveau mimichant '';
              date := CURRENT_TIMESTAMP;
              INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),
              id_user,entitle,subject,FALSE,date);*/

/* sinon : envoi d''un message */
SELECT user_id INTO id_user1 FROM characters WHERE char_id=id_mum;
SELECT user_id INTO id_user2 FROM characters WHERE char_id=mimi_dad;
entitle:= ''creation mimichant'';
subject:= ''mimichant eclos'';
date := CURRENT_TIMESTAMP;
INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),id_user1,entitle,subject,FALSE,date);
SELECT message_id INTO id_msg FROM messages WHERE message_date=date;
INSERT INTO send VALUES (id_msg,id_user1,TRUE);
           DELETE FROM equipements WHERE char_id=e.char_id;  
         END IF;
        END LOOP;

return TRUE;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 256 (OID 1030548)
-- Name: copulation(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION copulation(integer, integer) RETURNS boolean
    AS 'DECLARE mimichant1 ALIAS FOR $1;
DECLARE mimichant2 ALIAS FOR $2;
DECLARE pa INTEGER;
DECLARE checkpachar BOOLEAN;
DECLARE checkheteronotpregnant BOOLEAN;
DECLARE sex1 BOOLEAN;
DECLARE sex2 BOOLEAN;
DECLARE id_mimifemale INTEGER;
DECLARE id_user1 INTEGER;
DECLARE id_user2 INTEGER;
DECLARE id_mimimale INTEGER;
DECLARE date_copul TIMESTAMP;
DECLARE entitle1 character varying(255);
DECLARE subject1 text; 
DECLARE date1 TIMESTAMP; 
DECLARE date2 TIMESTAMP; 
DECLARE date3 TIMESTAMP; 
DECLARE entitle2 character varying(255);
DECLARE subject2 text; 
DECLARE entitle3 character varying(255);
DECLARE subject3 text; 
DECLARE id_msg1 INTEGER; 
DECLARE id_msg2 INTEGER; 
DECLARE id_msg3 INTEGER; 

BEGIN


SELECT char_sex INTO sex1 FROM characters WHERE char_id=mimichant1;
SELECT * INTO checkpachar from checkpa(mimichant1,sex1);
IF (checkpachar = FALSE) THEN
SELECT user_id INTO id_user1 FROM characters WHERE char_id=mimichant1;
entitle1:= ''pas assez de pa'';
subject1:= ''tu n\\''as pas assez de pa pour copuler '';
date1 := CURRENT_TIMESTAMP;
INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),id_user1,entitle1,subject1,FALSE,date1);
SELECT message_id INTO id_msg1 FROM messages WHERE message_date=date1;
INSERT INTO send VALUES (id_msg1,id_user1,TRUE);
return FALSE;
END IF;

SELECT char_sex INTO sex2 FROM characters WHERE char_id=mimichant2;
SELECT * INTO checkpachar from checkpa(mimichant2,sex2);
IF (checkpachar = FALSE) THEN
return FALSE;
END IF;

SELECT * INTO checkheteronotpregnant from isheteronotpregnant(mimichant1,mimichant2);
IF (checkheteronotpregnant = FALSE) THEN
/* sinon : envoi d''un message */
SELECT user_id INTO id_user1 FROM characters WHERE char_id=mimichant1;
entitle2:= ''couple pas hetero ou femelle enceinte ou mimichants du meme user'';
subject2:= ''tu ne reunis pas les conditions pour copuler '';
date2 := CURRENT_TIMESTAMP;
INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),id_user1,entitle2,subject2,FALSE,date2);
SELECT message_id INTO id_msg2 FROM messages WHERE message_date=date2;
INSERT INTO send VALUES (id_msg2,id_user1,TRUE);
return FALSE;
END IF;

date_copul=CURRENT_TIMESTAMP;
IF(sex1 = TRUE) THEN
id_mimimale=mimichant1;
UPDATE characters SET char_pa=char_pa/2 WHERE char_id=mimichant1;
ELSE UPDATE characters SET char_pa=0 WHERE char_id=mimichant1;
id_mimifemale=mimichant1;
END IF;

IF(sex2 = TRUE) THEN
id_mimimale=mimichant2;
UPDATE characters SET char_pa=char_pa/2 WHERE char_id=mimichant2;
ELSE UPDATE characters SET char_pa=0 WHERE char_id=mimichant2;
id_mimifemale=mimichant2;
END IF;

/*envoi messages pour dire que la copulation est ok*/
SELECT user_id INTO id_user1 FROM characters WHERE char_id=mimichant1;
subject3:= ''la copulation s\\''est bien deroulee'';
entitle3:= '' copulation '';
date3 := CURRENT_TIMESTAMP;
INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),id_user1,entitle3,subject3,FALSE,date3);
SELECT message_id INTO id_msg3 FROM messages WHERE message_date=date3;
INSERT INTO send VALUES (id_msg3,id_user1,TRUE);


INSERT INTO eggs(id_char_mum,id_char_dad,date_copulation,id_object) VALUES(id_mimifemale,id_mimimale,date_copul,1) ;

return TRUE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 254 (OID 1030551)
-- Name: create_mimichant_by_repro(integer, character varying, text, integer, integer, integer, integer, integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION create_mimichant_by_repro(integer, character varying, text, integer, integer, integer, integer, integer, integer) RETURNS boolean
    AS '/*ARGUMENTS*/
/*user_id,char_name,char_infos,charIdMum,charIdDad,char_strength,char_const,char_dext,char_psy*/

/*identifiant du mimichant*/
DECLARE charId INTEGER;

/*Parent*/
DECLARE charIdMum ALIAS FOR $4;
DECLARE charPosMum POINT;
DECLARE charIdDad ALIAS FOR $5;

/*Couleur et graphisme des parents*/
DECLARE ColorMum character varying(6);
DECLARE HeadMum INT;
DECLARE EarsMum INT;
DECLARE CorpsMum INT;
DECLARE TailMum INT;

DECLARE ColorDad character varying(6);
DECLARE HeadDad INT;
DECLARE EarsDad INT;
DECLARE CorpsDad INT;
DECLARE TailDad INT;


/*aleatoire*/
DECLARE alea FLOAT;
DECLARE pv INT;
DECLARE pa INT;
DECLARE papm INT;
DECLARE inventmax INT;
DECLARE equipmax INT;
DECLARE sexe FLOAT;

DECLARE bebe_sex BOOLEAN;
DECLARE bebe_skincolor CHARACTER VARYING (20);
DECLARE bebe_head INTEGER;
DECLARE bebe_ears INTEGER;
DECLARE bebe_corps INTEGER;
DECLARE bebe_tail INTEGER;
DECLARE bebe_pvmax INTEGER;
DECLARE bebe_inshop BOOLEAN; 

BEGIN

/*CARACTERISTIQUE COMMUNE*/
charId:=nextval(''public.characters_char_id_seq''::text);
SELECT round(random()*50+30) INTO pv;
SELECT round(random()*50+50) INTO
 pa;
SELECT round(random()*50+50) INTO
 inventmax;
SELECT round(random()*9+1) INTO papm;
SELECT round(random()*20+10) INTO equipmax;


  /*Couleur et graphisme des parents*/
  SELECT char_skincolor INTO ColorMum from characters WHERE char_id=charIdMum;
  SELECT char_skincolor INTO ColorDad from characters WHERE char_id=charIdDad;
  SELECT char_head INTO HeadMum from characters WHERE char_id=charIdMum;
  SELECT char_head INTO HeadDad from characters WHERE char_id=charIdDad;
  SELECT
 char_ears from characters WHERE char_id=charIdMum INTO EarsMum;
  SELECT char_ears  INTO EarsDad from characters WHERE char_id=charIdDad;
  SELECT char_corps INTO CorpsMum from characters WHERE char_id=charIdMum;
  SELECT char_corps INTO CorpsDad from characters WHERE char_id=charIdDad;
  SELECT char_tail INTO TailMum from characters WHERE char_id=charIdMum;
  SELECT char_tail INTO TailDad from characters WHERE char_id=charIdDad;

  SELECT char_pvmax INTO bebe_pvmax from characters WHERE char_id=charIdMum;
  SELECT char_inshop INTO bebe_inshop from characters WHERE char_id=charIdMum;


  
  /*sexe*/
  SELECT random() INTO sexe;
  IF(sexe>0.5)THEN bebe_sex=FALSE; 
  ELSE bebe_sex=TRUE;
  END IF ;

  /*Couleur et graphisme de enfant*/
  /*Couleur*/
  SELECT random() INTO alea;
  IF(alea>0.5)THEN bebe_skincolor=ColorMum;
  ELSE bebe_skincolor=ColorDad;
  END IF;
  
  /*Tete*/
  SELECT random() INTO alea;
  IF(alea>0.5)THEN bebe_head=HeadMum;
  ELSE bebe_head=HeadDad;
  END IF;
  
  /*Oreilles*/
  SELECT random() INTO alea;
 
 IF(alea>0.5)THEN bebe_ears=EarsMum;
  ELSE bebe_ears=EarsDad;
  END IF;
  
  /*Corps*/
  SELECT random() INTO alea;
  IF(alea>0.5)THEN bebe_corps=CorpsMum;
  ELSE bebe_corps=CorpsDad;
  END IF;
  
 
 /*Queue*/
  SELECT random() INTO alea;
  IF(alea>0.5)THEN bebe_tail=TailMum;
  ELSE bebe_tail=TailDad;
  END IF;


/*position de la mere=position de oeuf*/
SELECT char_pos from characters WHERE char_id=charIdMum INTO charPosMum;
INSERT INTO characters(char_id,user_id,char_pos,char_name,char_infos,char_sex,char_money,char_pv,char_pa,char_papm,char_equipmax,char_inventmax,char_head, char_ears, char_corps, char_tail,char_strength,char_const,char_dext,char_psy,char_id_dad, char_id_mum, char_pvmax, char_skincolor, char_inshop)
VALUES(charId,$1,charPosMum,$2,$3,bebe_sex,200,pv,pa,papm,equipmax,inventmax,bebe_head,bebe_ears,bebe_corps,bebe_tail,$6,$7,$8,$9,$5,$4,bebe_pvmax,bebe_skincolor,bebe_inshop);

RETURN TRUE;
END ;'
    LANGUAGE plpgsql;


SET SESSION AUTHORIZATION 'tjeyarat';

--
-- TOC entry 184 (OID 1030554)
-- Name: update_cp_hour_user_connected(integer); Type: FUNCTION; Schema: public; Owner: tjeyarat
--

CREATE FUNCTION update_cp_hour_user_connected(integer) RETURNS boolean
    AS 'DECLARE 
    userMoney INTEGER;
BEGIN
    SELECT user_money FROM users WHERE user_id =$1 INTO userMoney ;
    userMoney = userMoney + 1;
    UPDATE users SET user_money = userMoney WHERE user_id = $1; 
    return true;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 186 (OID 1030667)
-- Name: update_cp_hour_user_disconnected(integer); Type: FUNCTION; Schema: public; Owner: tjeyarat
--

CREATE FUNCTION update_cp_hour_user_disconnected(integer) RETURNS boolean
    AS 'DECLARE 
    date_last TIMESTAMP ;
    date_current TIMESTAMP ;
    difference DOUBLE PRECISION;
    last DOUBLE PRECISION;
    current DOUBLE PRECISION;
    userMoney  INTEGER;
    caste INTEGER; 
BEGIN
    date_current= CURRENT_TIMESTAMP;
    SELECT user_last_connect FROM users WHERE user_id=$1 INTO date_last;
    SELECT  date_part(''epoch'',date_current)INTO current;
    SELECT  date_part(''epoch'',date_last)INTO last;
    difference = current - last;
    difference = difference/3600; 
    SELECT CAST (difference AS INTEGER)INTO caste; 
    SELECT user_money FROM users WHERE user_id =$1 INTO userMoney ;
    userMoney = userMoney + caste;
    UPDATE users SET user_money = userMoney WHERE user_id = $1; 
    return true;
END;'
    LANGUAGE plpgsql;


SET SESSION AUTHORIZATION 'pbouvier';

--
-- TOC entry 124 (OID 1031246)
-- Name: news; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE news (
    id serial NOT NULL,
    date timestamp without time zone,
    message text,
    auteur character varying(50)
);


--
-- TOC entry 190 (OID 1031256)
-- Name: guild_owner(); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION guild_owner() RETURNS integer
    AS 'DECLARE position_mimi POINT;
DECLARE nb_mimi INTEGER;
DECLARE i INTEGER;
DECLARE chara characters;
DECLARE proprietaire INTEGER;
DECLARE chef INTEGER;
 
BEGIN


SELECT char_pos INTO position_mimi FROM characters WHERE char_id=6;
SELECT user_id INTO proprietaire FROM grounds WHERE ground_pos[0] = position_mimi[0] AND ground_pos[1] = position_mimi[1];
SELECT user_guild_master_id INTO chef FROM users WHERE user_id=proprietaire;
RETURN chef;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 126 (OID 1031257)
-- Name: position_mimi; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE position_mimi (
    char_pos point
);


--
-- TOC entry 5 (OID 1031294)
-- Name: infoscase; Type: TYPE; Schema: public; Owner: pbouvier
--

CREATE TYPE infoscase AS (
	login character varying,
	guildname character varying,
	guildemblem character varying,
	masterid integer
);


--
-- TOC entry 191 (OID 1031295)
-- Name: getinfoscase(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION getinfoscase(integer) RETURNS infoscase
    AS '
DECLARE res infosCase%rowtype;
DECLARE res2 infosCase%rowtype;

BEGIN
	SELECT INTO res user_login, user_guild_name, user_guild_emblem, user_guild_master_id FROM users WHERE user_id=$1;

	IF (res.masterId = $1) THEN
		RETURN res;
	ELSE
		SELECT INTO res2 user_login, user_guild_name, user_guild_emblem, user_guild_master_id FROM users WHERE user_id=res.masterId;
		res2.login = res.login;
		RETURN res2;
	END IF;
END;
'
    LANGUAGE plpgsql;


--
-- TOC entry 230 (OID 1031351)
-- Name: firedfromguild(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION firedfromguild(integer, integer) RETURNS boolean
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_victim ALIAS FOR $2;
DECLARE user_name character varying(255);
DECLARE user_guild character varying(255);
DECLARE entitle character varying(255);
DECLARE subject text; 
DECLARE msg_id integer; 
DECLARE date TIMESTAMP;  



BEGIN
 SELECT user_login FROM users WHERE user_id=$1 INTO user_name;
 SELECT user_guild_name FROM users WHERE user_id=$1 INTO user_guild;
 
 entitle:= ''tu as été viré de ta guilde '';
 subject:= user_name || '' t\\''as viré de la guilde '' || user_guild;
 date := CURRENT_TIMESTAMP;

 INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),
              id_user,entitle,subject,FALSE,date);

 SELECT max(message_id) FROM messages WHERE user_send_id=id_user INTO msg_id;
 INSERT INTO send VALUES (msg_id, id_victim);

 UPDATE users SET user_guild_master_id = id_victim WHERE user_id = id_victim;
 RETURN TRUE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 196 (OID 1031389)
-- Name: inviteIntoGuild(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION "inviteIntoGuild"(integer, integer) RETURNS boolean
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_victim ALIAS FOR $2;
DECLARE user_name character varying(255);
DECLARE user_guild character varying(255);
DECLARE test_master BOOLEAN;  
DECLARE entitle character varying(255);
DECLARE subject text; 
DECLARE d_date TIMESTAMP; 
DECLARE msg_id INTEGER; 

BEGIN
 SELECT user_login FROM users WHERE user_id=$1 INTO user_name;
 SELECT user_guild_name FROM users WHERE user_id=$1 INTO user_guild;
 entitle=''proposition d\\''alliance'';
 SELECT * INTO test_master FROM is_master(id_victim);
 
IF (test_master = TRUE) then
subject:= user_name || '' vous propose de faire partie de l\\''alliance '' || user_guild ||'' :
<a href="resAlliance.php?boul=1&u=''|| $1 ||''&v=''|| $2 ||''"> ! Accepter !</a> | <a href="resAlliance.php?boul=0&u=''|| $1 ||''&v=''|| $2 ||''"> ! Refuser !</a>'';

else 
subject:= user_name || '' vous propose de faire partie de l\\''alliance '' || user_guild ||'', mais vous faite deja partie d\\''une alliance. Voulez vous la quitter pour rejoindre l\\''alliance '' || user_guild ||'' :
 <a href="resAlliance.php?boul=2&u=''|| $1 ||''&v=''|| $2 ||''"> ! Quitter et rejoindre la nouvel alliance !</a> | <a href="resAlliance.php?boul=0&u=''|| $1 ||''&v=''|| $2 ||''"> ! Refuser !</a>'';
END IF;
 
d_date := CURRENT_TIMESTAMP;
INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),
              id_user ,entitle,subject,FALSE,d_date);
SELECT max(message_id) FROM messages WHERE user_send_id=id_user INTO msg_id;
INSERT INTO send VALUES (msg_id, id_victim);

RETURN TRUE;
END'
    LANGUAGE plpgsql;


--
-- TOC entry 192 (OID 1031390)
-- Name: joinGuild (integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION "joinGuild "(integer, integer) RETURNS boolean
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_victim ALIAS FOR $2;
DECLARE user_name character varying(255);
DECLARE victim_name character varying(255);
DECLARE user_guild character varying(255);
DECLARE entitle character varying(255);
DECLARE entitle2 character varying(255);
DECLARE subject text; 
DECLARE subject2 text;
DECLARE date TIMESTAMP; 
DECLARE msg_id INTEGER;
DECLARE msg_id2 INTEGER; 


BEGIN
 SELECT user_login FROM users WHERE user_id=id_user INTO user_name;
 SELECT user_login FROM users WHERE user_id=id_victim INTO victim_name;
 SELECT user_guild_name FROM users WHERE user_id=id_user INTO user_guild;
 
 entitle =''Acceptation d\\''alliance'';
 entitle2 =''Accusé de reception'';
 subject = victim_name || '' a accepté de faire partie de votre alliance'' ||user_guild;
 subject2 = user_name || '' a bien recu l\\''acceptiration de faire partie de votre alliance'' ||user_guild;
 date = CURRENT_TIMESTAMP;
 
 INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),
              id_user,entitle,subject,FALSE,date);
SELECT max(message_id) FROM messages WHERE user_send_id=id_user INTO msg_id;
INSERT INTO send VALUES (msg_id, id_victim);

 INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),
              id_victim,entitle2,subject2,FALSE,date);
SELECT max(message_id) FROM messages WHERE user_send_id=id_victim INTO msg_id2;
INSERT INTO send VALUES (msg_id2, id_user);

 UPDATE users SET user_guild_master_id = id_user WHERE user_id = id_victim OR user_guild_master_id= id_victim;
RETURN TRUE;

END'
    LANGUAGE plpgsql;


--
-- TOC entry 235 (OID 1031391)
-- Name: quitguild (integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION "quitguild "(integer) RETURNS boolean
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_master INTEGER;
DECLARE user_name character varying(255);
DECLARE entitle character varying(255);
DECLARE subject text; 
DECLARE date TIMESTAMP;  



BEGIN
 SELECT user_login FROM users WHERE user_id=$1 INTO user_name;
 SELECT user_guild_master_id FROM users WHERE user_id=id_user INTO id_master;
 
 entitle :=''Un membre de votre alliance est parti est parti vers d\\''autres cieux'';
 subject := user_name || '' a quitté ta guilde de mimichants pas beau'';
 date := CURRENT_TIMESTAMP;

 INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),
              id_master,entitle,subject,FALSE,date);

 UPDATE users SET user_guild_master_id = id_user WHERE user_id = id_user;

RETURN TRUE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 194 (OID 1031392)
-- Name: refuseGuild (integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION "refuseGuild "(integer, integer) RETURNS boolean
    AS 'DECLARE id_user ALIAS FOR $1;
DECLARE id_victim ALIAS FOR $2;
DECLARE user_name character varying(255);
DECLARE victim_name character varying(255);
DECLARE user_guild character varying(255);
DECLARE entitle character varying(255);
DECLARE entitle2 character varying(255);
DECLARE subject text;
DECLARE subject2 text; 
DECLARE date TIMESTAMP;  
DECLARE msg_id INTEGER;  
DECLARE msg_id2 INTEGER; 

BEGIN
 SELECT user_login FROM users WHERE user_id=$1 INTO user_name;
 SELECT user_login FROM users WHERE user_id=$2 INTO victim_name;
 SELECT user_guild_name FROM users WHERE user_id=$1 INTO user_guild;
 
 entitle=''Refus d\\''alliance'';
 entitle2=''Accusé de reception'';
 subject:= victim_name || '' a refusé de faire partie de votre alliance'' ||user_guild;
 subject2:= user_name || '' a bien recu votre refus de faire partie de votre alliance'' ||user_guild;
 date := CURRENT_TIMESTAMP;
 
 INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),
              id_victim,entitle,subject,FALSE,date);
 SELECT max(message_id) FROM messages WHERE user_send_id=id_user INTO msg_id;
 INSERT INTO send VALUES (msg_id, id_user); 


 INSERT INTO messages VALUES (nextval(''public.messages_message_id_seq''::text),
              id_user,entitle2,subject2,FALSE,date);
 SELECT max(message_id) FROM messages WHERE user_send_id=id_user INTO msg_id2;
 INSERT INTO send VALUES (msg_id2, id_victim); 

 RETURN TRUE;
END'
    LANGUAGE plpgsql;


--
-- TOC entry 198 (OID 1031527)
-- Name: ismaster(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION ismaster(integer) RETURNS boolean
    AS 'DECLARE master_id INTEGER;


BEGIN

SELECT user_guild_master_id from users where user_id= $1 INTO master_id;

 if( master_id=0 OR master_id=$1) then
     RETURN TRUE;
   else
     RETURN FALSE; 
   end if;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 200 (OID 1031818)
-- Name: accepter_echange(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION accepter_echange(integer, integer, integer) RETURNS boolean
    AS 'DECLARE mimichantY ALIAS FOR $1;
DECLARE echange ALIAS FOR $2;
DECLARE equipementY ALIAS FOR $3;
DECLARE mimichantX INTEGER;
DECLARE equipementX INTEGER;
DECLARE objetX INTEGER;
DECLARE objetY INTEGER;

BEGIN

  SELECT equip_id INTO equipementX FROM echanges WHERE ech_id=echange;
  SELECT obj_id INTO objetX FROM echanges WHERE ech_id=echange;
  SELECT obj_id INTO objetY FROM equipements WHERE equip_id=equipementY;
  SELECT char_id INTO mimichantX FROM equipements WHERE equip_id=equipementX;

  IF (objetX=objetY) THEN
    UPDATE equipements SET char_id=mimichantY WHERE equip_id=equipementX;
    UPDATE equipements SET char_id=mimichantX WHERE equip_id=equipementY;
    DELETE FROM echanges WHERE ech_id=echange;
    RETURN TRUE;
  END IF;

  RETURN FALSE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 202 (OID 1031819)
-- Name: buy_mimichant(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION buy_mimichant(integer, integer) RETURNS boolean
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE mimiAcheteur ALIAS FOR $2;
DECLARE acheteur INTEGER;
DECLARE vendeur INTEGER;
DECLARE terrier INTEGER;
DECLARE argent INTEGER;
DECLARE prix INTEGER;
DECLARE paEnleves INTEGER;
DECLARE pa INTEGER;
DECLARE nbgrounds INTEGER;
DECLARE nbmimi INTEGER;
DECLARE coeffgrounds INTEGER;

BEGIN
    paEnleves = 10;
    
    SELECT user_id INTO vendeur FROM characters WHERE char_id=mimichant;
    SELECT user_id INTO acheteur FROM characters WHERE char_id=mimiAcheteur;
    SELECT char_pa INTO pa FROM characters WHERE char_id=mimiAcheteur;
    SELECT user_money INTO argent FROM users WHERE user_id=acheteur;
    SELECT sold_price INTO prix FROM characters_sold WHERE char_id=mimichant;
    SELECT ground_id INTO terrier FROM characters_sold WHERE char_id=mimichant;
    
    SELECT COUNT(ground_id) INTO nbgrounds FROM grounds WHERE user_id=acheteur;
    SELECT COUNT(char_id) INTO nbmimi FROM characters WHERE user_id=acheteur;
    coeffgrounds=nbgrounds/nbmimi;
    
    IF(coeffgrounds>=2) THEN
        IF (pa>paEnleves) THEN
            IF (argent>prix) THEN
                /*retirer des PA*/
                UPDATE characters SET char_pa=char_pa-paEnleves WHERE char_id=mimiAcheteur;
                
                /*transférer des chocopépites*/
                UPDATE users SET user_money=argent-prix WHERE user_id=acheteur;
                UPDATE users SET user_money=user_money+prix WHERE user_id=vendeur;
                
                /*mettre le mimi en mode actif*/
                UPDATE characters SET char_inshop=false WHERE char_id=mimichant;
                
                /*  transferer le mimi */
                UPDATE characters SET user_id=acheteur WHERE char_id=mimichant;
                
                /* le mimi nest plus a vendre */
                DELETE FROM characters_sold WHERE char_id=mimichant;
                
                return TRUE;
            END IF;
        END IF;
    END IF;
        return FALSE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 204 (OID 1031820)
-- Name: depot(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION depot(integer, integer) RETURNS boolean
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE utilisateur INTEGER;
DECLARE montant ALIAS FOR $2;
DECLARE liquide INTEGER;
BEGIN
SELECT user_id INTO utilisateur FROM characters WHERE char_id=mimichant;
SELECT char_money INTO liquide FROM characters WHERE char_id=mimichant;
    IF (liquide > montant) THEN
        UPDATE users SET user_money=user_money+montant-(montant*0.05) WHERE user_id=utilisateur;
        UPDATE characters SET char_money=char_money-montant WHERE char_id=mimichant;
        return TRUE;
END IF;
    return FALSE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 206 (OID 1031821)
-- Name: enlever_echange(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION enlever_echange(integer, integer) RETURNS boolean
    AS 'DECLARE equipement ALIAS FOR $1;
DECLARE mimichant ALIAS FOR $2;
DECLARE mimichant2 INTEGER;

BEGIN

  SELECT char_id INTO mimichant2 FROM equipements WHERE equip_id=equipement;

  IF (mimichant=mimichant2) THEN
    DELETE FROM echanges WHERE equip_id=equipement;
    RETURN TRUE;
  END IF;

  RETURN FALSE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 208 (OID 1031822)
-- Name: lower_price(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION lower_price(integer, integer, integer) RETURNS boolean
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE prix ALIAS FOR $2;
DECLARE mimichantCourant ALIAS FOR $3;
DECLARE utilisateur INTEGER;
DECLARE utilisateur2 INTEGER;
DECLARE ancienPrix INTEGER;
DECLARE paEnleves INTEGER;
DECLARE pa INTEGER;

BEGIN
    paEnleves = 10;
    
    SELECT user_id INTO utilisateur FROM characters WHERE char_id=mimichant;
    SELECT user_id INTO utilisateur2 FROM characters WHERE char_id=mimichantCourant;
    SELECT char_pa INTO pa FROM characters WHERE char_id=mimichantCourant;
    SELECT sold_price INTO ancienPrix FROM characters_sold WHERE char_id=mimichant;
    
    IF (utilisateur=utilisateur2) THEN
        IF (pa>paEnleves) THEN
            IF (prix<ancienPrix) THEN
                UPDATE characters SET char_pa=char_pa-paEnleves WHERE char_id=mimichantCourant;
                UPDATE characters_sold SET sold_price=prix WHERE char_id=mimichant;
                return TRUE;
            END IF;
        END IF;
    END IF;
        return FALSE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 210 (OID 1031823)
-- Name: mimi_envente(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION mimi_envente(integer, integer) RETURNS SETOF characters
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE utilisateur INTEGER;
DECLARE terrier ALIAS FOR $2;
DECLARE retour characters;
DECLARE mimi characters_sold;

BEGIN

    SELECT user_id INTO utilisateur FROM characters WHERE char_id=mimichant;
    
    FOR mimi IN SELECT * FROM characters_sold WHERE ground_id=terrier
	LOOP
                SELECT * INTO retour FROM characters WHERE (user_id=utilisateur AND char_id=mimi.char_id);
		RETURN NEXT retour;
	END LOOP;
	RETURN ;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 212 (OID 1031824)
-- Name: mimi_envente(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION mimi_envente(integer) RETURNS SETOF characters
    AS 'DECLARE terrier ALIAS FOR $1;
DECLARE retour characters;
DECLARE mimi characters_sold;

BEGIN
    FOR mimi IN SELECT * FROM characters_sold WHERE ground_id=terrier
	LOOP
                SELECT * INTO retour FROM characters WHERE char_id=mimi.char_id;
		RETURN NEXT retour;
	END LOOP;
	RETURN ;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 214 (OID 1031826)
-- Name: proposer_echange(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION proposer_echange(integer, integer) RETURNS boolean
    AS 'DECLARE equipement ALIAS FOR $1;
DECLARE objet ALIAS FOR $2;
BEGIN
  INSERT INTO echanges(equip_id,obj_id) VALUES (equipement,objet);
  RETURN TRUE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 215 (OID 1031827)
-- Name: retrait(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION retrait(integer, integer) RETURNS boolean
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE utilisateur INTEGER;
DECLARE montant ALIAS FOR $2;
DECLARE solde INTEGER;
BEGIN
SELECT user_id INTO utilisateur FROM characters WHERE char_id=mimichant;
SELECT user_money INTO solde FROM users WHERE user_id=utilisateur;
    IF (solde > montant) THEN
        UPDATE users SET user_money=user_money-montant WHERE user_id=utilisateur;
        UPDATE characters SET char_money=char_money+montant WHERE char_id=mimichant;
        return TRUE;
END IF;
    return FALSE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 217 (OID 1031828)
-- Name: sell_mimichant(integer, integer, integer, integer, boolean); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION sell_mimichant(integer, integer, integer, integer, boolean) RETURNS boolean
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE mimichantCourant ALIAS FOR $2;
DECLARE utilisateur INTEGER;
DECLARE terrier ALIAS FOR $3;
DECLARE prix ALIAS FOR $4;
DECLARE venteEquipement ALIAS FOR $5;
DECLARE paEnleves INTEGER;
DECLARE pa INTEGER;
DECLARE taxe INTEGER;
DECLARE argent INTEGER;

BEGIN
    /* apparemment ici faudra récupérer ça dans des tables, mais ça existe pas ?! */
    paEnleves = 10;
    taxe = 20;
    
    IF(mimichant <> mimichantCourant) THEN    

    SELECT user_id INTO utilisateur FROM characters WHERE char_id=mimichant;
    SELECT char_pa INTO pa FROM characters WHERE char_id=mimichantCourant;
    SELECT user_money INTO argent FROM users WHERE user_id=utilisateur;
    
    argent = argent -prix*taxe/100;
    
    IF (pa>paEnleves) THEN
        IF (argent>0) THEN
            /*retirer des PA*/
            UPDATE characters SET char_pa=char_pa-paEnleves WHERE char_id=mimichantCourant;
            
            /*retirer des chocopépites*/
            UPDATE users SET user_money=argent WHERE user_id=utilisateur;
            
            /*mettre le mimi en mode non actif*/
            UPDATE characters SET char_inshop=true WHERE char_id=mimichant;
            UPDATE characters SET char_money=0 WHERE char_id=mimichant;
            
            /* supprimer les armes si non vendues avec */
            IF (venteEquipement=false) THEN
                DELETE FROM equipements WHERE char_id=mimichant;
            END IF;
            
            INSERT INTO characters_sold(char_id,sold_price,ground_id) VALUES (mimichant,prix,terrier);
            
            return TRUE;
        END IF;
    END IF;
    END IF;
        return FALSE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 219 (OID 1031829)
-- Name: solde_character(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION solde_character(integer) RETURNS integer
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE solde INTEGER;
BEGIN
SELECT char_money INTO solde FROM characters WHERE char_id=mimichant;
return SOLDE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 221 (OID 1031830)
-- Name: solde_user(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION solde_user(integer) RETURNS integer
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE utilisateur INTEGER;
DECLARE solde INTEGER;
BEGIN
SELECT user_id INTO utilisateur FROM characters WHERE char_id=mimichant;
SELECT user_money INTO solde FROM users WHERE user_id=utilisateur;
return SOLDE;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 223 (OID 1031832)
-- Name: voir_echanges(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION voir_echanges(integer) RETURNS SETOF echanges
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE retour echanges;
DECLARE equipement equipments;

BEGIN
  FOR equipement IN SELECT * FROM equipements WHERE char_id=mimichant
	LOOP
                SELECT * INTO retour FROM echanges WHERE obj_id=equipement.obj_id;
		RETURN NEXT retour;
	END LOOP;
  RETURN ;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 225 (OID 1031833)
-- Name: voir_ses_echanges(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION voir_ses_echanges(integer) RETURNS SETOF echanges
    AS 'DECLARE mimichant ALIAS FOR $1;
DECLARE retour echanges;
DECLARE equipement equipments;

BEGIN
    FOR equipement IN SELECT * FROM equipements WHERE char_id=mimichant
	LOOP
                SELECT * INTO retour FROM echanges WHERE equip_id=equipement.equip_id;
		RETURN NEXT retour;
	END LOOP;
	RETURN ;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 127 (OID 1031989)
-- Name: announces; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE announces (
    announce_id serial NOT NULL,
    announce_msg text,
    announce_date_depot timestamp without time zone,
    char_id integer,
    ground_id integer
);


--
-- TOC entry 227 (OID 1032078)
-- Name: setannounce(integer, integer, text); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION setannounce(integer, integer, text) RETURNS SETOF announces
    AS 'DECLARE user INTEGER;
    DECLARE userGuild INTEGER; 
    DECLARE userProprioId INTEGER; 
    DECLARE userProprioGuild INTEGER;
    DECLARE charMoney bigint;
    DECLARE stop BOOLEAN;
    DECLARE retour BOOLEAN; 
    DECLARE date TIMESTAMP;   

BEGIN
 date = CURRENT_TIMESTAMP;
 stop := 0;
 retour := 0;
 SELECT user_id from characters where char_id= $1 INTO user;
 SELECT user_guild_master_id from users where user_id = user INTO userGuild;
 SELECT user_id from grounds where ground_id =$2 INTO userProprioId;
 SELECT user_guild_master_id from users where user_id = userProprioId INTO userProprioGuild;
 if(userGuild != userProprioGuild) then
   SELECT char_money from characters where char_id =$1 INTO charMoney;
   charMoney = charMoney -5;
   if(charMoney>=0) then
     UPDATE characters set char_money = charMoney where char_id =$1;
   else
     stop := 1;   
   end if;
 end if;
 
 if(stop = false) then
   INSERT INTO announces (announce_msg,announce_date_depot,char_id,ground_id) VALUES ($3,date,$1,$2);
   retour := 1; 
 end if;
RETURN retour;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 228 (OID 1032082)
-- Name: readannounce(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION readannounce(integer) RETURNS SETOF announces
    AS 'DECLARE retour announces;
 
BEGIN
	FOR retour IN SELECT * from announces where ground_id= $1 
	LOOP
		RETURN NEXT retour;
	END LOOP;
	RETURN ;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 128 (OID 1032355)
-- Name: contact; Type: TABLE; Schema: public; Owner: pbouvier
--

CREATE TABLE contact (
    user_id integer,
    user_id_contact integer,
    contact_infos character varying
);


--
-- TOC entry 232 (OID 1032578)
-- Name: random(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION random(integer, integer) RETURNS integer
    AS 'BEGIN
 return round(random()*($2-$1))+$1;
END'
    LANGUAGE plpgsql;


--
-- TOC entry 233 (OID 1032580)
-- Name: resurrection_town(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION resurrection_town(integer) RETURNS boolean
    AS 'DECLARE nbTowns INTEGER;
DECLARE i INTEGER;
DECLARE x_mimi INTEGER;
DECLARE y_mimi INTEGER;
DECLARE dist INTEGER;
DECLARE distTemp INTEGER;
DECLARE town_clother INTEGER;
DECLARE id_mimichant ALIAS FOR $1;
DECLARE gp POINT;
DECLARE view RECORD;

BEGIN

SELECT count(ground_id) INTO nbTowns FROM towns;

SELECT char_pos[0] , char_pos[1] INTO x_mimi , y_mimi FROM characters WHERE char_id = 

id_mimichant;

distTemp:=10000000;
i=0;

FOR view IN SELECT grounds.ground_id, ground_pos FROM
        grounds
        JOIN 
        towns
        ON grounds.ground_id = towns.ground_id
LOOP
	dist:=point(x_mimi,y_mimi) <-> point(view.ground_pos[0],view.ground_pos[1]); 
        IF(dist<distTemp)THEN
                distTemp:=dist;
		town_clother:=view.ground_id;
        END IF;
END LOOP;	

SELECT ground_pos INTO gp FROM grounds WHERE ground_id = town_clother;

UPDATE characters SET char_pos = gp WHERE char_id = id_mimichant;
UPDATE characters SET char_money = 0 WHERE char_id = id_mimichant;
UPDATE characters SET char_pv = 1 WHERE char_id = id_mimichant;

return true;
END;'
    LANGUAGE plpgsql;


--
-- TOC entry 239 (OID 1032658)
-- Name: take_ground(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION take_ground(integer, integer) RETURNS integer
    AS 'DECLARE id_mimichant ALIAS FOR $1;
DECLARE coutTerrain ALIAS FOR $2;

DECLARE userI INTEGER;
DECLARE nbPa INTEGER;
DECLARE pos_char POINT;
DECLARE gi INTEGER;

BEGIN
  	SELECT char_pa 
	INTO nbPa 
	FROM characters 
	WHERE char_id=id_mimichant;
	
	SELECT user_id 
	INTO userI 
	FROM characters 
	WHERE char_id = id_mimichant;

        SELECT char_pos 
	INTO pos_char 
	FROM characters 
	WHERE char_id = id_mimichant;

        SELECT ground_id 
	INTO gi 
	FROM grounds 
	WHERE ground_pos ~= pos_char;
       
	nbPa:=nbPa-coutTerrain;
	UPDATE characters SET char_pa = nbPa WHERE char_id = id_mimichant;
	
	UPDATE grounds SET user_id = userI WHERE ground_id=gi;
	return nbPa;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 240 (OID 1033018)
-- Name: object_use(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION object_use(integer, integer, integer) RETURNS boolean
    AS 'DECLARE
 id_mimiATT ALIAS FOR $1;
 id_mimiDEF ALIAS FOR $2;
 equipement ALIAS FOR $3;


 id_obj2 INTEGER;
 id_obj INTEGER;
 dead INTEGER;
 trou BOOLEAN;
 coutPA INTEGER;
 currentPAatt INTEGER;
 currentPVatt INTEGER;
 currentPAdef INTEGER;
 posATT POINT;
 posDEF POINT;
 distMaxObj INTEGER;
 distMinObj INTEGER;
 attBonusStr INTEGER;
 attBonusCst INTEGER;
 attBonusPsy INTEGER;
 attBonusDex INTEGER;
 attCurrentStr INTEGER;
 attCurrentCst INTEGER;
 attCurrentPsy INTEGER;
 attCurrentDex INTEGER;
 defCurrentStr INTEGER;
 defCurrentCst INTEGER;
 defCurrentDex INTEGER;
 defCurrentPsy INTEGER;
 attTotalStr INTEGER;
 attTotalCst INTEGER;
 attTotalPsy INTEGER;
 attTotalDex INTEGER;
 defBonusGround INTEGER;
 typeGround INTEGER;
 groundBonus INTEGER;
 defObj INTEGER;
 defBuild INTEGER;
 defTotalDef INTEGER;
 selfPA INTEGER;
 targetPA INTEGER;
 coutPV INTEGER;
 newValue INTEGER;
 newValue2 INTEGER;
 coeffParadeATT INTEGER;
 coeffParadeDEF INTEGER;
 tabWeap RECORD;
 up INTEGER;
 upStr INTEGER;
 upDxt INTEGER;
 upPsy INTEGER;
 upCst INTEGER;
 riposte INTEGER;
 impactMax INTEGER;
 impactMin INTEGER;
 impactPV INTEGER;
 degats INTEGER;
 degatsEffectif INTEGER; 
 impactPAtg INTEGER;
 dist INTEGER;
 usure INTEGER;
 build INTEGER;
 objUse INTEGER;


BEGIN

SELECT obj_id FROM equipements WHERE equip_id=equipement INTO id_obj2;  

riposte := 0;

id_obj := id_obj2;

/*Choix de l objet de riposte*/
IF (id_obj = -10) THEN
 
  SELECT COUNT (id_obj) FROM equipements WHERE char_id = id_mimiATT AND obj_equip= TRUE INTO newValue;
    
  SELECT random (0, newvalue) INTO newvalue2;

  SELECT id_obj FROM equipements WHERE char_id = id_mimiATT AND obj_equip= TRUE LIMIT 1 INTO newvalue2; 
 
  id_obj := newvalue2;
  riposte := 1 ;

END IF;

SELECT char_pv, char_pos, char_pa, char_psy, char_dext, char_const, char_strength FROM characters WHERE char_id = id_mimiDEF INTO dead, posDEF, currentPAdef, defCurrentPsy,  defCurrentDex, defCurrentCst, defCurrentStr ;

SELECT obj_use_dead, obj_use_pa_self, obj_use_dist_max, obj_use_dist_min, obj_impact_pa_self, obj_impact_pa_target, obj_use_pa_target,  obj_use_pv_self, obj_impact_pv_self, obj_impact_dam_min, obj_impact_dam_max, obj_use_limit FROM objects WHERE obj_id = id_obj INTO trou, coutPA, distMaxObj, distMinObj, selfPA,  impactPAtg , targetPA, coutPV, impactPV, impactMin, impactMax, usure;

SELECT char_pa, char_pos, char_strength, char_const, char_dext,char_psy, char_pv FROM characters WHERE char_id = id_mimiATT INTO currentPAatt, posATT, attCurrentStr, attCurrentCst, attCurrentDex, attCurrentPsy, currentPVatt;

IF (dead = 0 AND trou=FALSE) THEN
  RETURN FALSE;
END IF;

IF (coutPA > currentPAatt) THEN
  RETURN FALSE;
END IF;

IF (posATT@circle(posDEF,distMaxObj)=FALSE) THEN 
  RETURN FALSE;
END IF;

IF (posATT@circle(posDEF,distMinObj)) THEN 
  RETURN FALSE;
END IF;

SELECT sum(O.obj_equip_bonus_strength)
FROM objects O
LEFT JOIN equipements E
ON E.obj_id = O.obj_id
WHERE E.char_id = id_mimiATT AND E.obj_equip = TRUE 
INTO attBonusStr;

SELECT sum(O.obj_equip_bonus_const)
FROM objects O
LEFT JOIN equipements E
ON E.obj_id = O.obj_id
WHERE E.char_id = id_mimiATT AND E.obj_equip = TRUE 
INTO attBonusCst;

SELECT sum(O.obj_equip_bonus_psy)
FROM objects O
LEFT JOIN equipements E
ON E.obj_id = O.obj_id
WHERE E.char_id = id_mimiATT AND E.obj_equip = TRUE 
INTO attBonusPsy;

SELECT sum(O.obj_equip_bonus_dext)
FROM objects O
LEFT JOIN equipements E
ON E.obj_id = O.obj_id
WHERE E.char_id = id_mimiATT AND E.obj_equip = TRUE 
INTO attBonusDex;

 attTotalStr := attCurrentStr + attBonusStr ;
 attTotalCst := attCurrentCst + attBonusCst ;
 attTotalPsy := attCurrentPsy + attBonusPsy ;
 attTotalDex := attCurrentDex + attBonusDex ;

SELECT type_id, ground_build FROM grounds WHERE ground_pos[0] = posDEF[0] AND ground_pos[1] = posDEF[1] INTO typeGround, build;
SELECT type_defense FROM ground_types WHERE type_id = typeGround INTO groundBonus;

SELECT sum(O.obj_equip_defense)
FROM objects O
LEFT JOIN equipements E
ON E.obj_id = O.obj_id
WHERE E.char_id = id_mimiDEF AND E.obj_equip = TRUE 
INTO defObj;

defBuild := 20;
defBuild := defBuild - build * 7;

defTotalDef := groundBonus + defObj + defBuild ;

 newValue := currentPAatt - selfPA - coutPA;
 newValue2 := currentPVatt - coutPV; 

UPDATE characters SET char_pa = newValue, char_pv = newValue2 WHERE char_id = id_mimiATT;

 newValue := currentPAdef - targetPA; 

UPDATE characters SET char_pa = newValue WHERE char_id= id_mimiDEF;

 coeffParadeATT := attCurrentDex + attCurrentStr + attCurrentCst ;
 coeffParadeDEF := defCurrentDex + defCurrentStr + defCurrentCst ;

SELECT random ( 0 , coeffParadeATT ) INTO newValue ;
SELECT random ( 0 , coeffParadeDEF ) INTO newValue2;

IF (newValue < newValue2) THEN

   SELECT skill_up_dodge FROM skills WHERE skill_id = 4 INTO up;
   upStr := up + defCurrentStr ;
   SELECT skill_up_dodge FROM skills WHERE skill_id = 3 INTO up;
   upCst := up + defCurrentCst ;
   SELECT skill_up_dodge FROM skills WHERE skill_id = 1 INTO up;
   upDxt := up + defCurrentDex ;
   SELECT skill_up_dodge FROM skills WHERE skill_id = 2 INTO up;
   upPsy := up + defCurrentPsy ;

   UPDATE characters SET char_strength = upStr , char_const = upCst, char_dext = upDxt, char_psy = upPsy WHERE char_id= id_mimiDEF;

   IF (riposte = 0) THEN
      PERFORM object_use(id_mimiDEF, id_mimiATT , -10 );
   END IF;

   SELECT equip_used FROM equimements WHERE equip_id=equipement INTO objUse;

   IF (objUse=usure-1) THEN
      DELETE * FROM equipements WHERE equip_id=equipement;
   ELSE
      UPDATE equipements SET equip_used = equip_used +1  WHERE equip_id= equipement;
   END IF ;

    RETURN FALSE;

END IF;

SELECT random ( impactMin, impactMax ) INTO degats; 
dist := posDEF<-> posATT;

newvalue := (-1/(distMinObj+1))* dist ;
newvalue2 := attTotalStr/(attTotalStr-1) ;


degatsEffectif := degats * (newvalue + newvalue2) + ((1/ distMaxObj)*dist+(1/ (attTotalDex-1)))/2- defTotalDef;

UPDATE characters SET char_pv = char_pv - degatsEffectif, char_pa  = char_pa + impactPAtg  WHERE char_id= id_mimiDEF;

UPDATE characters SET char_pa = char_pa + selfPA , char_pv = char_pv + impactPV WHERE char_id= id_mimiATT;

IF (dist < 2) THEN

   SELECT skill_up_impact_melee FROM skills WHERE skill_id = 4 INTO up;
   upStr := up + attCurrentStr ;
   SELECT skill_up_impact_melee FROM skills WHERE skill_id = 3 INTO up;
   upCst := up + attCurrentCst ;
   SELECT skill_up_impact_melee FROM skills WHERE skill_id = 1 INTO up;
   upDxt := up + attCurrentDex ;
   SELECT skill_up_impact_melee FROM skills WHERE skill_id = 2 INTO up;
   upPsy := up + attCurrentPsy ;

ELSE 

   SELECT skill_up_impact_distance FROM skills WHERE skill_id = 4 INTO up;
   upStr := up + attCurrentStr ;
   SELECT skill_up_impact_distance FROM skills WHERE skill_id = 3 INTO up;
   upCst := up + attCurrentCst ;
   SELECT skill_up_impact_distance FROM skills WHERE skill_id = 1 INTO up;
   upDxt := up + attCurrentDex ;
   SELECT skill_up_impact_distance FROM skills WHERE skill_id = 2 INTO up;
   upPsy := up + attCurrentPsy ;

END IF ;

UPDATE characters SET char_strength = upStr , char_const = upCst, char_dext = upDxt, char_psy = upPsy WHERE char_id= id_mimiATT;

SELECT equip_used FROM equimements WHERE equip_id=equipement INTO objUse;

IF (objUse=usure-1) THEN
   DELETE * FROM equipements WHERE equip_id=equipement;
ELSE
   UPDATE equipements SET equip_used = equip_used +1  WHERE equip_id= equipement;
END IF ;

IF (riposte = 0) THEN
   PERFORM object_use(id_mimiDEF, id_mimiATT , -10 );
END IF;

RETURN TRUE;

END;

'
    LANGUAGE plpgsql;


--
-- TOC entry 246 (OID 1033392)
-- Name: nb_colocs(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION nb_colocs(integer) RETURNS integer
    AS 'DECLARE 

id_char ALIAS FOR $1;

pos_char 	POINT;
nb_colocs  	INTEGER;
cpt		INTEGER;

BEGIN
	SELECT char_pos FROM characters WHERE char_id = id_char INTO pos_char;
	
	SELECT COUNT(*) FROM characters WHERE char_pos ~= pos_char INTO cpt;

	nb_colocs = cpt - 1;

	RETURN nb_colocs;
END;
	
'
    LANGUAGE plpgsql;


--
-- TOC entry 248 (OID 1033731)
-- Name: nb_neighbors(integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION nb_neighbors(integer) RETURNS integer
    AS 'DECLARE 

id_char ALIAS FOR $1;

pos_char 			POINT;
nb_neighbors  			INTEGER;
nb_neighbors_left		INTEGER;
nb_neighbors_up_left		INTEGER;
nb_neighbors_up			INTEGER;
nb_neighbors_up_right		INTEGER;
nb_neighbors_right		INTEGER;
nb_neighbors_bottom_right	INTEGER;
nb_neighbors_bottom		INTEGER;
nb_neighbors_bottom_left	INTEGER;
x				INTEGER;
y				INTEGER;

BEGIN
	SELECT char_pos FROM characters WHERE char_id = id_char INTO pos_char;
	
	x = pos_char[0];
	y = pos_char[1];
	
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x-1 AND char_pos[1] = y INTO nb_neighbors_left;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x-1 AND char_pos[1] = y+1 INTO nb_neighbors_up_left;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x AND char_pos[1] = y+1 INTO nb_neighbors_up;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x+1 AND char_pos[1] = y+1 INTO nb_neighbors_up_right;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x+1 AND char_pos[1] = y INTO nb_neighbors_right;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x+1 AND char_pos[1] = y-1 INTO nb_neighbors_bottom_right;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x AND char_pos[1] = y-1 INTO nb_neighbors_bottom;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x-1 AND char_pos[1] = y-1 INTO nb_neighbors_bottom_left;

	nb_neighbors = nb_neighbors_left + nb_neighbors_up_left + nb_neighbors_up + nb_neighbors_up_right + nb_neighbors_bottom_right + nb_neighbors_bottom + nb_neighbors_bottom_left;

	RETURN nb_neighbors;

END;'
    LANGUAGE plpgsql;


--
-- TOC entry 250 (OID 1033817)
-- Name: nb_neighbors_case(integer, integer); Type: FUNCTION; Schema: public; Owner: pbouvier
--

CREATE FUNCTION nb_neighbors_case(integer, integer) RETURNS integer
    AS 'DECLARE 

id_char ALIAS FOR $1;
numero_case ALIAS FOR $2;

pos_char 			POINT;
nb_neighbors_left		INTEGER;
nb_neighbors_up_left		INTEGER;
nb_neighbors_up			INTEGER;
nb_neighbors_up_right		INTEGER;
nb_neighbors_right		INTEGER;
nb_neighbors_bottom_right	INTEGER;
nb_neighbors_bottom		INTEGER;
nb_neighbors_bottom_left	INTEGER;
x				INTEGER;
y				INTEGER;

BEGIN
	SELECT char_pos FROM characters WHERE char_id = id_char INTO pos_char;
	
	x = pos_char[0];
	y = pos_char[1];
	
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x-1 AND char_pos[1] = y INTO nb_neighbors_left;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x-1 AND char_pos[1] = y+1 INTO nb_neighbors_up_left;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x AND char_pos[1] = y+1 INTO nb_neighbors_up;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x+1 AND char_pos[1] = y+1 INTO nb_neighbors_up_right;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x+1 AND char_pos[1] = y INTO nb_neighbors_right;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x+1 AND char_pos[1] = y-1 INTO nb_neighbors_bottom_right;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x AND char_pos[1] = y-1 INTO nb_neighbors_bottom;
	SELECT COUNT(*) FROM characters WHERE char_pos[0] = x-1 AND char_pos[1] = y-1 INTO nb_neighbors_bottom_left;

	
IF(numero_case=1)    THEN RETURN nb_neighbors_left; END IF;
IF(numero_case=2)    THEN RETURN nb_neighbors_up_left; END IF;
IF(numero_case=3)    THEN RETURN nb_neighbors_up; END IF;
IF(numero_case=4)    THEN RETURN nb_neighbors_up_right; END IF;
IF(numero_case=5)    THEN RETURN nb_neighbors_right; END IF;
IF(numero_case=6)    THEN RETURN nb_neighbors_bottom_right; END IF;
IF(numero_case=7)    THEN RETURN nb_neighbors_bottom; END IF;
IF(numero_case=8)    THEN RETURN nb_neighbors_bottom_left; END IF;





END;'
    LANGUAGE plpgsql;


--
-- TOC entry 129 (OID 978593)
-- Name: characters_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY characters
    ADD CONSTRAINT characters_pkey PRIMARY KEY (char_id);


--
-- TOC entry 131 (OID 978599)
-- Name: objects_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY objects
    ADD CONSTRAINT objects_pkey PRIMARY KEY (obj_id);


--
-- TOC entry 130 (OID 978605)
-- Name: equipements_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY equipements
    ADD CONSTRAINT equipements_pkey PRIMARY KEY (equip_id);


--
-- TOC entry 133 (OID 994239)
-- Name: ground_types_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY ground_types
    ADD CONSTRAINT ground_types_pkey PRIMARY KEY (type_id);


--
-- TOC entry 134 (OID 994259)
-- Name: mines_ground_id_key; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY mines
    ADD CONSTRAINT mines_ground_id_key UNIQUE (ground_id);


--
-- TOC entry 135 (OID 994261)
-- Name: mines_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY mines
    ADD CONSTRAINT mines_pkey PRIMARY KEY (mine_id);


--
-- TOC entry 136 (OID 994273)
-- Name: town_services_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY town_services
    ADD CONSTRAINT town_services_pkey PRIMARY KEY (service_id);


--
-- TOC entry 137 (OID 994280)
-- Name: town_services_links_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY town_services_links
    ADD CONSTRAINT town_services_links_pkey PRIMARY KEY (link_id);


--
-- TOC entry 138 (OID 994282)
-- Name: town_services_links_town_id_key; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY town_services_links
    ADD CONSTRAINT town_services_links_town_id_key UNIQUE (town_id, service_id);


--
-- TOC entry 139 (OID 994301)
-- Name: towns_ground_id_key; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY towns
    ADD CONSTRAINT towns_ground_id_key UNIQUE (ground_id);


--
-- TOC entry 140 (OID 994303)
-- Name: towns_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY towns
    ADD CONSTRAINT towns_pkey PRIMARY KEY (town_id);


--
-- TOC entry 141 (OID 1021973)
-- Name: grounds_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY grounds
    ADD CONSTRAINT grounds_pkey PRIMARY KEY (ground_id);


--
-- TOC entry 142 (OID 1022022)
-- Name: decorations_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY decorations
    ADD CONSTRAINT decorations_pkey PRIMARY KEY (deco_id);


--
-- TOC entry 143 (OID 1022731)
-- Name: messages_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY messages
    ADD CONSTRAINT messages_pkey PRIMARY KEY (message_id);


--
-- TOC entry 132 (OID 1022765)
-- Name: users_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY users
    ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);


--
-- TOC entry 144 (OID 1022782)
-- Name: grounds_visibles_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY grounds_visibles
    ADD CONSTRAINT grounds_visibles_pkey PRIMARY KEY (vis_id);


--
-- TOC entry 145 (OID 1022818)
-- Name: eggs_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY eggs
    ADD CONSTRAINT eggs_pkey PRIMARY KEY (id_egg);


--
-- TOC entry 146 (OID 1022820)
-- Name: skills_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY skills
    ADD CONSTRAINT skills_pkey PRIMARY KEY (skill_id);


--
-- TOC entry 147 (OID 1022830)
-- Name: events_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY events
    ADD CONSTRAINT events_pkey PRIMARY KEY (event_id);


--
-- TOC entry 148 (OID 1022841)
-- Name: characters_skills_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY characters_skills
    ADD CONSTRAINT characters_skills_pkey PRIMARY KEY (char_skill_id);


--
-- TOC entry 149 (OID 1029950)
-- Name: echanges_pkey; Type: CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY echanges
    ADD CONSTRAINT echanges_pkey PRIMARY KEY (ech_id);


--
-- TOC entry 260 (OID 978595)
-- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY equipements
    ADD CONSTRAINT "$1" FOREIGN KEY (char_id) REFERENCES characters(char_id);


--
-- TOC entry 261 (OID 978601)
-- Name: $2; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY equipements
    ADD CONSTRAINT "$2" FOREIGN KEY (obj_id) REFERENCES objects(obj_id);


--
-- TOC entry 262 (OID 1022769)
-- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY send
    ADD CONSTRAINT "$1" FOREIGN KEY (message_id) REFERENCES messages(message_id);


--
-- TOC entry 263 (OID 1022773)
-- Name: $2; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY send
    ADD CONSTRAINT "$2" FOREIGN KEY (user_receive_id) REFERENCES users(user_id);


--
-- TOC entry 264 (OID 1022832)
-- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY events
    ADD CONSTRAINT "$1" FOREIGN KEY (user_id) REFERENCES users(user_id);


--
-- TOC entry 265 (OID 1022843)
-- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY characters_skills
    ADD CONSTRAINT "$1" FOREIGN KEY (skill_id) REFERENCES skills(skill_id);


--
-- TOC entry 266 (OID 1029952)
-- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY echanges
    ADD CONSTRAINT "$1" FOREIGN KEY (equip_id) REFERENCES equipements(equip_id);


--
-- TOC entry 267 (OID 1029956)
-- Name: $2; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY echanges
    ADD CONSTRAINT "$2" FOREIGN KEY (obj_id) REFERENCES objects(obj_id);


--
-- TOC entry 268 (OID 1031995)
-- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY announces
    ADD CONSTRAINT "$1" FOREIGN KEY (char_id) REFERENCES characters(char_id);


--
-- TOC entry 269 (OID 1032360)
-- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY contact
    ADD CONSTRAINT "$1" FOREIGN KEY (user_id) REFERENCES users(user_id);


--
-- TOC entry 270 (OID 1032364)
-- Name: $2; Type: FK CONSTRAINT; Schema: public; Owner: pbouvier
--

ALTER TABLE ONLY contact
    ADD CONSTRAINT "$2" FOREIGN KEY (user_id_contact) REFERENCES users(user_id);


--
-- TOC entry 273 (OID 1024207)
-- Name: charactermoved; Type: TRIGGER; Schema: public; Owner: pbouvier
--

CREATE TRIGGER charactermoved
    AFTER UPDATE ON characters
    FOR EACH ROW
    EXECUTE PROCEDURE add_visible_grounds();


--
-- TOC entry 272 (OID 1027195)
-- Name: calcpvmax; Type: TRIGGER; Schema: public; Owner: pbouvier
--

CREATE TRIGGER calcpvmax
    BEFORE UPDATE ON characters
    FOR EACH ROW
    EXECUTE PROCEDURE newpvmax();


--
-- TOC entry 271 (OID 1027196)
-- Name: calcequipmax; Type: TRIGGER; Schema: public; Owner: pbouvier
--

CREATE TRIGGER calcequipmax
    BEFORE UPDATE ON characters
    FOR EACH ROW
    EXECUTE PROCEDURE newequipmax();


SET SESSION AUTHORIZATION 'postgres';

--
-- TOC entry 3 (OID 2200)
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres
--

COMMENT ON SCHEMA public IS 'Standard public schema';


SET SESSION AUTHORIZATION 'pbouvier';

--
-- TOC entry 8 (OID 978572)
-- Name: TABLE characters; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE characters IS 'pour les mimichants';


--
-- TOC entry 9 (OID 978572)
-- Name: COLUMN characters.char_pvmax; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN characters.char_pvmax IS 'points de vies max';


--
-- TOC entry 10 (OID 978572)
-- Name: COLUMN characters.char_skincolor; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN characters.char_skincolor IS 'Valeur Hexa de la couleur de pelage';


--
-- TOC entry 11 (OID 978572)
-- Name: COLUMN characters.char_inshop; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN characters.char_inshop IS 'si true, le mimichant est en vente et donc inactif';


--
-- TOC entry 13 (OID 978577)
-- Name: TABLE equipements; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE equipements IS 'les équipements';


--
-- TOC entry 14 (OID 978577)
-- Name: COLUMN equipements.equip_used; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN equipements.equip_used IS 'nombre d''utilisation en cours';


--
-- TOC entry 15 (OID 978577)
-- Name: COLUMN equipements.egg_id_dad; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN equipements.egg_id_dad IS '0 si ce n''est pas un oeuf, ';


--
-- TOC entry 17 (OID 978590)
-- Name: TABLE objects; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE objects IS 'les objets du jeu';


--
-- TOC entry 18 (OID 978590)
-- Name: COLUMN objects.obj_base_cost; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN objects.obj_base_cost IS 'prix de base de l''objet';


--
-- TOC entry 20 (OID 993720)
-- Name: TABLE users; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE users IS 'Information sur l''utilisateur';


--
-- TOC entry 21 (OID 993720)
-- Name: COLUMN users.user_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_id IS 'id du joueur';


--
-- TOC entry 22 (OID 993720)
-- Name: COLUMN users.user_login; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_login IS 'login du joueur';


--
-- TOC entry 23 (OID 993720)
-- Name: COLUMN users.user_pass; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_pass IS 'mot de passe du joueur';


--
-- TOC entry 24 (OID 993720)
-- Name: COLUMN users.user_mail; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_mail IS 'mail du joueur';


--
-- TOC entry 25 (OID 993720)
-- Name: COLUMN users.user_guild_name; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_guild_name IS 'nom de la guilde du joueur';


--
-- TOC entry 26 (OID 993720)
-- Name: COLUMN users.user_guild_color; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_guild_color IS 'couleur de la guilde du joueur';


--
-- TOC entry 27 (OID 993720)
-- Name: COLUMN users.user_guild_infos; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_guild_infos IS 'infos sur la guilde du joueur';


--
-- TOC entry 28 (OID 993720)
-- Name: COLUMN users.user_guild_master_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_guild_master_id IS 'id du joueur maitre de la guilde dont dépend le joueur';


--
-- TOC entry 29 (OID 993720)
-- Name: COLUMN users.user_date_crea_old; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_date_crea_old IS 'OBSOLETE : NE PAS UTILISER';


--
-- TOC entry 30 (OID 993720)
-- Name: COLUMN users.user_last_connect_old; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_last_connect_old IS 'OBSOLETE : NE PAS UTILISER';


--
-- TOC entry 31 (OID 993720)
-- Name: COLUMN users.user_money; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_money IS 'argent du joueur';


--
-- TOC entry 32 (OID 993720)
-- Name: COLUMN users.user_guild_emblem; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_guild_emblem IS 'url (ABSOLUE!) du fichier image de l''embleme du joueur';


--
-- TOC entry 33 (OID 993720)
-- Name: COLUMN users.user_last_connect; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_last_connect IS 'date de derniere connexion du joueur';


--
-- TOC entry 34 (OID 993720)
-- Name: COLUMN users.user_date_crea; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN users.user_date_crea IS 'date de creation du joueur';


SET SESSION AUTHORIZATION 'lmoineau';

--
-- TOC entry 155 (OID 993756)
-- Name: FUNCTION fired_from_guild(integer, integer); Type: COMMENT; Schema: public; Owner: lmoineau
--

COMMENT ON FUNCTION fired_from_guild(integer, integer) IS 'When you want to fire someone from your guild, use that process. Parameter 1 is the id of your mimichant & parameter 2 is the id of the mimichant you have chosen';


--
-- TOC entry 165 (OID 993757)
-- Name: FUNCTION is_master(integer); Type: COMMENT; Schema: public; Owner: lmoineau
--

COMMENT ON FUNCTION is_master(integer) IS 'If you want to check if the user is the master of his guild, use that process. The parameter is the id of your mimichant';


--
-- TOC entry 157 (OID 993758)
-- Name: FUNCTION invite_into_guild(integer, integer); Type: COMMENT; Schema: public; Owner: lmoineau
--

COMMENT ON FUNCTION invite_into_guild(integer, integer) IS 'When you want to invite someone into your guild, use that process.Parameter 1 is the id of your mimichant & parameter 2 is the id of the mimichant you have chosen';


--
-- TOC entry 159 (OID 993759)
-- Name: FUNCTION join_guild(integer, integer); Type: COMMENT; Schema: public; Owner: lmoineau
--

COMMENT ON FUNCTION join_guild(integer, integer) IS 'When someone accept to join your guild, use that process. That process update datas of user and ex-guild''s members if the user were a master. Parameter 1 is the id of your mimichant & parameter 2 is the id of the mimichant you have chosen.';


--
-- TOC entry 161 (OID 993760)
-- Name: FUNCTION quit_guild(integer, integer); Type: COMMENT; Schema: public; Owner: lmoineau
--

COMMENT ON FUNCTION quit_guild(integer, integer) IS 'When you want to quit the guild, use that process. Parameter 1 is the id of your mimichant & parameter 2 is the id of the mimichant you have chosen';


--
-- TOC entry 163 (OID 993761)
-- Name: FUNCTION refuse_guild(integer, integer); Type: COMMENT; Schema: public; Owner: lmoineau
--

COMMENT ON FUNCTION refuse_guild(integer, integer) IS 'When someone refuse to join your guild, use that process. Parameter 1 is the id of your mimichant & parameter 2 is the id of the mimichant you have chosen.';


SET SESSION AUTHORIZATION 'pbouvier';

--
-- TOC entry 189 (OID 994123)
-- Name: FUNCTION equip(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION equip(integer, integer) IS 'equipe un objet de son equipement, paramètre : chat_id et equip_id';


--
-- TOC entry 36 (OID 994220)
-- Name: TABLE ground_types; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE ground_types IS 'Types disponibles pour chaque case de terrain';


--
-- TOC entry 37 (OID 994220)
-- Name: COLUMN ground_types.type_name; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN ground_types.type_name IS 'nom du type de terrain (herbe, etc...)';


--
-- TOC entry 38 (OID 994220)
-- Name: COLUMN ground_types.type_infos; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN ground_types.type_infos IS 'informations sur ce type de terrain';


--
-- TOC entry 39 (OID 994220)
-- Name: COLUMN ground_types.type_move_pa; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN ground_types.type_move_pa IS 'Coût en Points d''Action (PA) lors du déplacement';


--
-- TOC entry 40 (OID 994220)
-- Name: COLUMN ground_types.type_defense; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN ground_types.type_defense IS 'Bonus ou Malus de défense pour le ou les Mimichants';


--
-- TOC entry 41 (OID 994220)
-- Name: COLUMN ground_types.type_take_pa; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN ground_types.type_take_pa IS 'Coût en Points d''Action (PA) pour prendre la case';


--
-- TOC entry 43 (OID 994249)
-- Name: TABLE mines; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE mines IS 'Définition des mines';


--
-- TOC entry 44 (OID 994249)
-- Name: COLUMN mines.ground_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN mines.ground_id IS 'Case sur laquelle la mine se trouve. Une seule mine maximum par case.';


--
-- TOC entry 45 (OID 994249)
-- Name: COLUMN mines.mine_name; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN mines.mine_name IS 'Nom de la mine';


--
-- TOC entry 46 (OID 994249)
-- Name: COLUMN mines.mine_infos; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN mines.mine_infos IS 'Informations sur la mine (description, etc)';


--
-- TOC entry 47 (OID 994249)
-- Name: COLUMN mines.mine_capacity; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN mines.mine_capacity IS 'Nombre de CP de la mine. Lorsqu''il n''y a plus de CP, la mine disparait et une autre est recréée ailleur aléatoirement';


--
-- TOC entry 48 (OID 994249)
-- Name: COLUMN mines.mine_graph; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN mines.mine_graph IS 'Nom du fichier (sans extension ni chemin) de la représentation graphique de la mine';


--
-- TOC entry 49 (OID 994249)
-- Name: COLUMN mines.mine_take_pa; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN mines.mine_take_pa IS 'Coût en Points d''Action (PA) pour capturer';


--
-- TOC entry 51 (OID 994267)
-- Name: TABLE town_services; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE town_services IS 'Définition des différents services disponibles dans un terrier.';


--
-- TOC entry 52 (OID 994267)
-- Name: COLUMN town_services.service_name; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN town_services.service_name IS 'Nom du service';


--
-- TOC entry 53 (OID 994267)
-- Name: COLUMN town_services.service_infos; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN town_services.service_infos IS 'Informations du service';


--
-- TOC entry 54 (OID 994267)
-- Name: COLUMN town_services.service_pa; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN town_services.service_pa IS 'Prix d''accès au service en PA';


--
-- TOC entry 55 (OID 994267)
-- Name: COLUMN town_services.service_cp; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN town_services.service_cp IS 'Prix d''accès au service en CP';


--
-- TOC entry 57 (OID 994277)
-- Name: TABLE town_services_links; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE town_services_links IS 'Lien entre "towns" et "town_services" afin de savoir quelles villes a quels services.';


--
-- TOC entry 58 (OID 994277)
-- Name: COLUMN town_services_links.town_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN town_services_links.town_id IS 'ID de la ville';


--
-- TOC entry 59 (OID 994277)
-- Name: COLUMN town_services_links.service_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN town_services_links.service_id IS 'ID du service disponible';


--
-- TOC entry 61 (OID 994286)
-- Name: TABLE towns; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE towns IS 'Définition d''un Terrier (ville de Mimichants)';


--
-- TOC entry 62 (OID 994286)
-- Name: COLUMN towns.ground_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN towns.ground_id IS 'Case sur laquelle la ville se trouve. Une seule ville maximum par case.';


--
-- TOC entry 63 (OID 994286)
-- Name: COLUMN towns.town_name; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN towns.town_name IS 'Nom de la ville';


--
-- TOC entry 64 (OID 994286)
-- Name: COLUMN towns.town_infos; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN towns.town_infos IS 'Informations sur la ville (histoire, etc)';


--
-- TOC entry 65 (OID 994286)
-- Name: COLUMN towns.town_take_pa; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN towns.town_take_pa IS 'PA nécessaires pour la prise de cette ville';


--
-- TOC entry 66 (OID 994286)
-- Name: COLUMN towns.town_coeff; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN towns.town_coeff IS 'Coefficient pourcentage des prix d''accès aux services';


--
-- TOC entry 68 (OID 994307)
-- Name: TABLE grounds; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE grounds IS 'Cases du plateau de jeu (terrain)';


--
-- TOC entry 69 (OID 994307)
-- Name: COLUMN grounds.type_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN grounds.type_id IS 'id du type de terrain (lien vers la table ground_types)';


--
-- TOC entry 70 (OID 994307)
-- Name: COLUMN grounds.ground_pos; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN grounds.ground_pos IS 'Position X,Y de la case dans la carte';


--
-- TOC entry 71 (OID 994307)
-- Name: COLUMN grounds.user_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN grounds.user_id IS 'id du joueur propriétaire de la case';


--
-- TOC entry 72 (OID 994307)
-- Name: COLUMN grounds.ground_build; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN grounds.ground_build IS 'Construction sur la case (0=rien, 1=terrier, 2=mine)';


--
-- TOC entry 73 (OID 994307)
-- Name: COLUMN grounds.deco_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN grounds.deco_id IS 'Décoration de la case';


--
-- TOC entry 75 (OID 1021977)
-- Name: TABLE decorations; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE decorations IS 'Décorations pouvant être placées sur les cases du terrain.';


--
-- TOC entry 76 (OID 1021977)
-- Name: COLUMN decorations.deco_name; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN decorations.deco_name IS 'nom de la décoration';


--
-- TOC entry 77 (OID 1021977)
-- Name: COLUMN decorations.deco_infos; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN decorations.deco_infos IS 'informations sur la décoration';


--
-- TOC entry 78 (OID 1021977)
-- Name: COLUMN decorations.deco_obstacle; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN decorations.deco_obstacle IS 'décoration obstacle ou non';


--
-- TOC entry 79 (OID 1021977)
-- Name: COLUMN decorations.deco_graph; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN decorations.deco_graph IS 'Nom du fichier (sans extension ni chemin) de la représentation graphique du décor';


--
-- TOC entry 259 (OID 1022089)
-- Name: FUNCTION tests_take_ground(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION tests_take_ground(integer) IS 'Tests pour savoir si vous pouvez capturer le terrain. Retourne 1 si vous n''avez pas assez de points, 2 si vous devez d''abord combattre les propriétaire se trouvant sur le terrain, 3 si c''est OK (les propriétaires ne sont pas là) et 4  si c''est OK(le terrain n''appartient à personne). Pour ces 2 cas "OK" la fonction appelle ensuite elle meme la fonction take_ground.
';


--
-- TOC entry 169 (OID 1022524)
-- Name: FUNCTION "move"(integer, integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION "move"(integer, integer, integer) IS 'move ( char_id, x_dir, y_dir ) : moves a character to a neighbor ground, after checking the destination ground, the distance and the PA cost (updated). The x_dir and y_dir parameters could be -1, 0 or 1. Returns 1 if the parameters are wrong, 2 if destination ground is undefined, 3 if the character doesn t have enough PA. And 0 if everything is OK.';


--
-- TOC entry 81 (OID 1022601)
-- Name: TABLE quests; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE quests IS 'Quêtes proposées par les PNJ';


--
-- TOC entry 82 (OID 1022601)
-- Name: COLUMN quests.quest_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests.quest_id IS 'Identifiant de la quête';


--
-- TOC entry 83 (OID 1022601)
-- Name: COLUMN quests.quest_dial_begin; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests.quest_dial_begin IS 'Texte de début de quête - lorsque le joueur n''a pas l''objet';


--
-- TOC entry 84 (OID 1022601)
-- Name: COLUMN quests.quest_dial_end; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests.quest_dial_end IS 'Texte de remerciement lorsque le joueur a donné l''objet';


--
-- TOC entry 85 (OID 1022601)
-- Name: COLUMN quests.obj_id_get; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests.obj_id_get IS 'Identifiant de l''objet recherché';


--
-- TOC entry 86 (OID 1022601)
-- Name: COLUMN quests.obj_id_win; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests.obj_id_win IS 'Identifiant de l''objet offert';


--
-- TOC entry 87 (OID 1022601)
-- Name: COLUMN quests."quest_nbCP_win"; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests."quest_nbCP_win" IS 'Nombre de chocopépites gagnées';


--
-- TOC entry 88 (OID 1022601)
-- Name: COLUMN quests.quest_nbobj_win; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests.quest_nbobj_win IS 'Quantité du même objet offert';


--
-- TOC entry 89 (OID 1022601)
-- Name: COLUMN quests.obj_ground_type; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests.obj_ground_type IS 'Type de terrain sur lequel se trouve l''objet';


--
-- TOC entry 90 (OID 1022601)
-- Name: COLUMN quests.quest_dial_middle; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN quests.quest_dial_middle IS 'Dialogue quand le joueur a l''objet désiré par le PNJ';


--
-- TOC entry 171 (OID 1022662)
-- Name: FUNCTION quest_has_object(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION quest_has_object(integer, integer) IS 'quest_has_object(id_mimichant,id_pnj). Retourne true si le mimichant possède l''objet de la quête, false sinon.';


--
-- TOC entry 92 (OID 1022665)
-- Name: TABLE pnjs; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE pnjs IS 'Personnages non joueurs';


--
-- TOC entry 93 (OID 1022665)
-- Name: COLUMN pnjs.pnj_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.pnj_id IS 'Identifiant du PNJ';


--
-- TOC entry 94 (OID 1022665)
-- Name: COLUMN pnjs.pnj_type; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.pnj_type IS 'Type de PNJ';


--
-- TOC entry 95 (OID 1022665)
-- Name: COLUMN pnjs.quest_id; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.quest_id IS 'id de la quête. 0 = pas de quête';


--
-- TOC entry 96 (OID 1022665)
-- Name: COLUMN pnjs.pnj_dialon; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.pnj_dialon IS 'Dialogue d''accueil';


--
-- TOC entry 97 (OID 1022665)
-- Name: COLUMN pnjs.pnj_dialshop; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.pnj_dialshop IS 'Dialogue boutique';


--
-- TOC entry 98 (OID 1022665)
-- Name: COLUMN pnjs.pnj_sell; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.pnj_sell IS 'si le PNJ vend ou pas';


--
-- TOC entry 99 (OID 1022665)
-- Name: COLUMN pnjs.pnj_buy; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.pnj_buy IS 'Valeur maximale achetée. <0 : n''achète rien. 0 : prend gratuit';


--
-- TOC entry 100 (OID 1022665)
-- Name: COLUMN pnjs.pnj_graph; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.pnj_graph IS 'Image du PNJ';


--
-- TOC entry 101 (OID 1022665)
-- Name: COLUMN pnjs.pnj_pos; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON COLUMN pnjs.pnj_pos IS 'Position du PNJ';


--
-- TOC entry 104 (OID 1022725)
-- Name: TABLE messages; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE messages IS 'Table messagerie';


--
-- TOC entry 106 (OID 1022767)
-- Name: TABLE send; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE send IS 'Table lié a la messagerie';


--
-- TOC entry 108 (OID 1022779)
-- Name: TABLE grounds_visibles; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE grounds_visibles IS 'cases visibles pour chaque utilisateur';


--
-- TOC entry 110 (OID 1022786)
-- Name: TABLE eggs; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE eggs IS 'pour la copulation';


--
-- TOC entry 112 (OID 1022806)
-- Name: TABLE characters_sold; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE characters_sold IS 'pour la vente de mimichants';


--
-- TOC entry 114 (OID 1022815)
-- Name: TABLE skills; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE skills IS 'pour les competences';


--
-- TOC entry 117 (OID 1022838)
-- Name: TABLE characters_skills; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE characters_skills IS 'table de liaison entre les characters et les skills';


--
-- TOC entry 174 (OID 1023451)
-- Name: FUNCTION add_visible_grounds(); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION add_visible_grounds() IS 'Met à jour la table "grounds_visibles" en fonction du champ de vision du personnage qui viens de bouger ou d''être créé';


--
-- TOC entry 180 (OID 1024209)
-- Name: FUNCTION depose(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION depose(integer, integer) IS 'fonction qui permet le dépot d''objet sur le ground ou est le mimichant : paramètre id_equip et char_id';


--
-- TOC entry 178 (OID 1024210)
-- Name: FUNCTION get_object_from_ground(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION get_object_from_ground(integer) IS 'prend l''objet de la case ground dans son equipement si il y en a : paramètre char_id';


--
-- TOC entry 238 (OID 1026205)
-- Name: FUNCTION ground_price(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION ground_price(integer) IS 'Returns the price of a ground depending on his type';


--
-- TOC entry 182 (OID 1027264)
-- Name: FUNCTION unequip(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION unequip(integer, integer) IS 'desequipe un objet : paramètre mimichant et equipement';


--
-- TOC entry 245 (OID 1027379)
-- Name: FUNCTION sell_obj(integer, integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION sell_obj(integer, integer, integer) IS 'Fonction permettant de vendre un objet dans une boutique Bric à brac. Cette fonction prend en parametre l''od de la boutique où on veut vendre l''objet, l''id du mimichant qui veut vendre l''objet et l''id de l''objet que le mimichant veut vendre.';


--
-- TOC entry 243 (OID 1027381)
-- Name: FUNCTION buy_obj(integer, integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION buy_obj(integer, integer, integer) IS 'Fonction permettant d''acheter un objet dans une boutique Bric à brac.';


--
-- TOC entry 253 (OID 1030546)
-- Name: FUNCTION checkeggandcheckenoughspace(); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION checkeggandcheckenoughspace() IS 'a appeler en continu. regarde si il y a des oeufs à éclore, et si oui, crée le mimichant 48h après copulation';


--
-- TOC entry 257 (OID 1030548)
-- Name: FUNCTION copulation(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION copulation(integer, integer) IS 'fonction de copulation. Appelle checkpa et isheteronotpregnant pour faire les vérifications nécessaires avant la copulation. arg1 et arg2 sont les id des futurs parents';


--
-- TOC entry 255 (OID 1030551)
-- Name: FUNCTION create_mimichant_by_repro(integer, character varying, text, integer, integer, integer, integer, integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION create_mimichant_by_repro(integer, character varying, text, integer, integer, integer, integer, integer, integer) IS 'appelé par le procédure checkeggandcheckenoughspace pour créer un mimichant issu de la reproduction';


SET SESSION AUTHORIZATION 'tjeyarat';

--
-- TOC entry 185 (OID 1030554)
-- Name: FUNCTION update_cp_hour_user_connected(integer); Type: COMMENT; Schema: public; Owner: tjeyarat
--

COMMENT ON FUNCTION update_cp_hour_user_connected(integer) IS 'uopdate the cp (cp/hour) where user connected';


--
-- TOC entry 187 (OID 1030667)
-- Name: FUNCTION update_cp_hour_user_disconnected(integer); Type: COMMENT; Schema: public; Owner: tjeyarat
--

COMMENT ON FUNCTION update_cp_hour_user_disconnected(integer) IS 'update cp (cp/hour) where user disconnected';


SET SESSION AUTHORIZATION 'pbouvier';

--
-- TOC entry 125 (OID 1031246)
-- Name: TABLE news; Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON TABLE news IS 'Pour les news sur la page d''accueil';


--
-- TOC entry 231 (OID 1031351)
-- Name: FUNCTION firedfromguild(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION firedfromguild(integer, integer) IS 'utilisez cette procédure , l''autre est fausse. procédure pour virer les gens, le premier parametre est celui du maitre, le 2e est celui du mec de ta guilde que tu veut virer
';


--
-- TOC entry 197 (OID 1031389)
-- Name: FUNCTION "inviteIntoGuild"(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION "inviteIntoGuild"(integer, integer) IS 'Utilisez cette procédure, l''autre est fausse. Procédure a utiliser quand on veut inviter quelqu''un dans une guilde. Premier parametre est l''id maitre de guilde, l''autre est l''id de celui qui se fait inviter. Le processus de guilde necessite l''utilisation d''un page resAlliance qui sert a gérer le resultat de l''invitation. la variable $u est l''id du maitre, $v = id de la cible de l''invitation.';


--
-- TOC entry 193 (OID 1031390)
-- Name: FUNCTION "joinGuild "(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION "joinGuild "(integer, integer) IS 'Utilisez cette fonction, l''autre est fausse. Cette fonction sert a rejoindre une guilde. on l''utilise apres l''invitation dans une guilde. le premier parametre est l''id du maitre de guilde, le 2e parametre est l''idée du mec qui rejoint la guilde.';


--
-- TOC entry 236 (OID 1031391)
-- Name: FUNCTION "quitguild "(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION "quitguild "(integer) IS 'Comme d''hab, cette procedure est la bonne, l''autre est fausse. Le seul parametre est l''id du mimichant qui veut se tirer de sa guilde. ';


--
-- TOC entry 195 (OID 1031392)
-- Name: FUNCTION "refuseGuild "(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION "refuseGuild "(integer, integer) IS 'vrai procédure, l''autre fausse. A utiliser quand on veut notifier le refus a une invitation d''alliance. premier parametre = le maitre de guilde, 2e parametre = le mec qui refuse';


--
-- TOC entry 199 (OID 1031527)
-- Name: FUNCTION ismaster(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION ismaster(integer) IS 'Bonne procedure pour verifier si on est maitre de guilde. Conditionne si on peut virer quelqu''un ou conditionne les invitations';


--
-- TOC entry 201 (OID 1031818)
-- Name: FUNCTION accepter_echange(integer, integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION accepter_echange(integer, integer, integer) IS 'accepter un échange d''objet';


--
-- TOC entry 203 (OID 1031819)
-- Name: FUNCTION buy_mimichant(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION buy_mimichant(integer, integer) IS 'acheter un mimichant en vente';


--
-- TOC entry 205 (OID 1031820)
-- Name: FUNCTION depot(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION depot(integer, integer) IS 'déposer de l''argent à la banque';


--
-- TOC entry 207 (OID 1031821)
-- Name: FUNCTION enlever_echange(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION enlever_echange(integer, integer) IS 'retirer une proposition d''échange d''objet';


--
-- TOC entry 209 (OID 1031822)
-- Name: FUNCTION lower_price(integer, integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION lower_price(integer, integer, integer) IS 'baisser le prix d''un mimichant en vente';


--
-- TOC entry 211 (OID 1031823)
-- Name: FUNCTION mimi_envente(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION mimi_envente(integer, integer) IS 'tous les mimichants en vente d''un utilisateur dans une foire précise';


--
-- TOC entry 213 (OID 1031824)
-- Name: FUNCTION mimi_envente(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION mimi_envente(integer) IS 'tous les mimichants en vente dans une foire précise';


--
-- TOC entry 216 (OID 1031827)
-- Name: FUNCTION retrait(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION retrait(integer, integer) IS 'retirer de l''argent de la banque';


--
-- TOC entry 218 (OID 1031828)
-- Name: FUNCTION sell_mimichant(integer, integer, integer, integer, boolean); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION sell_mimichant(integer, integer, integer, integer, boolean) IS 'mettre un mimichant à la vente dans une foire';


--
-- TOC entry 220 (OID 1031829)
-- Name: FUNCTION solde_character(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION solde_character(integer) IS 'argent porté par un mimichant';


--
-- TOC entry 222 (OID 1031830)
-- Name: FUNCTION solde_user(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION solde_user(integer) IS 'argent sur le compte du propriétaire du mimichant';


--
-- TOC entry 224 (OID 1031832)
-- Name: FUNCTION voir_echanges(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION voir_echanges(integer) IS 'voir toutes les propositions d''échanges qui concernent le mimichant courant';


--
-- TOC entry 226 (OID 1031833)
-- Name: FUNCTION voir_ses_echanges(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION voir_ses_echanges(integer) IS 'voir les propositions d''échanges du mimichant courant';


--
-- TOC entry 234 (OID 1032580)
-- Name: FUNCTION resurrection_town(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION resurrection_town(integer) IS 'Envoie le mimichant KO (qui n''a plus de point de vie) dans la ville la plus proche. Il récupère un point de vie.';


--
-- TOC entry 247 (OID 1033392)
-- Name: FUNCTION nb_colocs(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION nb_colocs(integer) IS 'Retourne le nombre de Mimichants qui sont sur la même case qu''un Mimichant donné.';


--
-- TOC entry 249 (OID 1033731)
-- Name: FUNCTION nb_neighbors(integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION nb_neighbors(integer) IS 'Retourne le nombre de voisins du Mimichant (case voisine = en 8-connexité avec la case du Mimichant)';


--
-- TOC entry 251 (OID 1033817)
-- Name: FUNCTION nb_neighbors_case(integer, integer); Type: COMMENT; Schema: public; Owner: pbouvier
--

COMMENT ON FUNCTION nb_neighbors_case(integer, integer) IS 'Retourne le nombre de Mimichants sur une case donnée en 8-connexité autour de notre Mimichant';


