/******************************************************


    TABELLE STUPIDE


*/


create table t_e_mime_type(

    t_mime_type varchar(255) not null,

    t_extension varchar(10) not null,

    t_image varchar(50) not null,

    b_enabled char(1) default '0' not null,

    PRIMARY KEY(t_mime_type, t_extension)

);


create table t_e_param (

    t_param varchar(20) not null,

    t_value varchar(255) not null,

    t_description blob,

    primary key(t_param)

);


create table t_r_emoticon (

    i_emoticon_id integer not null,

    t_file_name varchar(255) not null,

    t_key_sequences varchar(1024) not null,

    i_order integer not null,

    primary key (i_emoticon_id)

);


CREATE TABLE t_a_file (

  i_file_id integer NOT NULL,

  t_mime_type varchar(255) NOT NULL,

  t_name varchar(255) NOT NULL,

  t_content blob,

  i_size integer NOT NULL,

  t_store_path varchar(4096) NOT NULL,

  t_thumb_path varchar(4096) ,

  i_downloads integer  default 0 not null,

  t_type varchar(10) not null,

  PRIMARY KEY  (i_file_id)

);


CREATE TABLE t_e_lang (

  t_lang char(3) NOT NULL,

  PRIMARY KEY  (t_lang)

);


CREATE TABLE t_e_result_view (

  t_result_view char(1) NOT NULL,

  t_description varchar(50) NOT NULL,

  PRIMARY KEY  (t_result_view),

  UNIQUE(t_description)

);


CREATE TABLE t_e_game (

  t_game char(10) NOT NULL,

  t_description varchar(255) NOT NULL,

  PRIMARY KEY  (t_game)

);


CREATE TABLE t_e_match_player_status (

  t_match_player_status char(2) NOT NULL,

  t_description varchar(255) NOT NULL,

  PRIMARY KEY  (t_match_player_status)

);


/* ******************************************************


    STILI


*/


-- forum, topic, post

CREATE TABLE t_e_style_level (

  t_style_level char(1) NOT NULL,

  t_description varchar(255) NOT NULL,

  PRIMARY KEY  (t_style_level)

);


-- normal, sticky, etc

CREATE TABLE t_e_style_type (

  t_style_type char(5) NOT NULL,

  t_description varchar(255) NOT NULL,

  i_order integer default 0 not null,

  PRIMARY KEY  (t_style_type)

);


CREATE TABLE t_e_style (

  t_style char(17) NOT NULL,

  t_description varchar(255) NOT NULL,

  t_class varchar(15) NOT NULL,

  t_css_name varchar(255),

  t_style_level char(1) NOT NULL,

  t_style_type char(5) ,

  t_style_definition blob,

  t_post_style char(17),    -- stile del post se questo e' di livello topic

  PRIMARY KEY  (t_style),

  UNIQUE (t_class, t_style_level)

);


-- stile forum => stili topic

-- uno stile topic ha un solo stile post legato

create table t_p_forum_topic_style (        

    i_forum_topic_style_id integer not null primary key,

    t_forum_style char(17) not null,

    t_topic_style char(17) not null,

    b_w char(1) default '0' not null,

    b_mod char(1) default '0' not null,

    unique(t_forum_style, t_topic_style)

);


/* ******************************************************


    SERVERS


*/


create table t_e_qstat (

    t_qstat char(10) not null primary key,

    t_label varchar(50), -- questa viene pre-pesa al nome del server

    t_description varchar(50) not null

);


create table t_e_xfire (

    t_xfire char(10) not null primary key,

    t_label varchar(50) not null

);


create table t_a_machine(

    i_machine_id integer not null primary key,

    t_ip varchar(15) not null,

    t_name varchar(50) not null,

    t_login varchar(50) not null,

    e_password varchar(255) not null,

    unique(t_ip), unique(t_name)

);


create table t_a_server (

    i_server_id integer not null primary key,

    t_qstat char(10),

    t_xfire char(10),

    t_game char(10),

    t_script varchar(255),

    t_log varchar(255),

    t_label varchar(50),    -- pre-pesa al nome del server

    t_name varchar(255) not null,   -- nome del server

                                    -- se up, il nome e' dato da t_label.t_qname; se t_label e' null, viene usata la t_label della qstat

                                    -- se down, il nome e' dato dalla label della qstat +  t_name

    t_sysfiles varchar(50),    -- nome per la png e il db rrd

    t_ip varchar(15) not null,     -- ip

    t_port varchar(5) not null,         -- porta


    t_qname varchar(255),

    i_ping integer,

    i_max_players integer,    

    i_players integer,

    b_up char(1),

    r_update timestamp,                 -- no trigger, update a mano

    i_user_admin_id integer,

    i_group_id integer,

    b_private char(1) default '0' not null,

    UNIQUE(t_ip, t_port)

);


create table t_a_ts_server(

    i_ts_server_id integer not null primary key,

    t_address varchar(50) not null,

    t_port varchar(5) not null default '8767',

    t_query_port varchar(5) not null default '51234',

    t_name varchar(255) not null,

    i_group_id integer,

    unique(t_address, t_port)

);


/* ******************************************************


    ACCESSO


    ci sono diritti generici sulle varie parti dell'applicazione

    che ne so, backoffice, bo-accesso, bo-forum, bo-stile, home-page, etc

    devo ancora definirle, servono per la sicurezza


    gruppi predefiniti: guests, utenti registrati, gods

        (vedere i valori in system/parametri.ini)


    cmq ci sono utenti, gruppi e queste sezioni logiche

    un utente puo' appartenere a + gruppi (anche nessuno)

    di base eredita la somma dei diritti dei gruppi cui appartiene,

    ma puo' fare l'override

    i diritti sono: R, W, RW, D

    in realta' credo che le sezioni saranno pochissime e gestiremo

    tutto coi diritti sul forum.


    a livello di forum la logica e' la stessa, solo che

    i diritti sono R e W per il gruppo, ed R, W, MOD per l'utente.

    i diritti non si ereditano dai forum genitori (si possono pero' copiare)

    nei forum che non permettono di postare, possono farlo solo i moderatori di quel forum.

    il moderatore puo' modificare l'accesso al singolo topic: di base e' lettura - scrittura,

    che lui puo' modificare, e in pi specificare per un singolo utente cosa quello puo'

    fare sul topic (R, W).

    quindi tipo dentro un contenitore metti dei topic con le info o che cazzo vuoi, in sola

    lettura, e magari lo modifichi te che sei il moderatore del forum e l'altro.


*/


CREATE TABLE t_a_section (

  i_section_id integer NOT NULL,

  s_name varchar(255) NOT NULL,

  t_description varchar(255) ,

  PRIMARY KEY  (i_section_id)

);


CREATE TABLE t_a_group (

  i_group_id integer NOT NULL,

  t_name varchar(255) NOT NULL,

  t_description varchar(255) NOT NULL,

  i_clan_forum_id integer,

  PRIMARY KEY  (i_group_id)

);


create table t_e_contact_visibility (

    t_contact_visibility char(1) not null primary key,

    t_description varchar(50) not null

);

insert into t_e_contact_visibility values('C', 'Clan');

insert into t_e_contact_visibility values('R', 'Utenti registrati');

insert into t_e_contact_visibility values('A', 'Tutti');

insert into t_e_contact_visibility values('Z', 'Nessuno');


CREATE TABLE t_a_user (

  i_user_id integer NOT NULL,

  t_name varchar(255) NOT NULL,

  t_surname varchar(255) NOT NULL,

  t_login varchar(192) NOT NULL,

  c_password varchar(255) ,

  t_email varchar(255) ,

  t_signature blob,

  t_description blob,

  t_lang char(3) default 'ITA' NOT NULL,

    t_custom_home varchar(4096),

  i_avatar_file_id integer ,        /* vedi files.php */  

  i_guestbook_topic_id integer ,    /* lo creo e lo tolgo a mano da users.php */

  i_pm_forum_id integer ,    /* lo creo e lo tolgo a mano da users.php */

  i_post_count integer default 0 not null,


    t_cnt_skype varchar(255),

    t_cnt_xfire varchar(255),

    t_cnt_icq varchar(255),

    t_cnt_msn varchar(255),

    t_cnt_irc varchar(255),

    t_vis_skype char(1) default 'Z' not null,

    t_vis_xfire char(1) default 'Z' not null,

    t_vis_icq char(1) default 'Z' not null,

    t_vis_msn char(1) default 'Z' not null,

    t_vis_irc char(1) default 'Z' not null,

    t_vis_email char(1) default 'Z' not null,


    b_logged char(1) default '0' not null,

    r_last_login timestamp,

    i_browsing_forum_id integer,

    i_browsing_topic_id integer,

    d_join date default current_date not null,

b_banned char(1) default '0' not null,

i_recent_days integer default 5 not null,

t_result_view char(1) default 'G' not null,


  PRIMARY KEY  (i_user_id),

  UNIQUE (t_login)

);


create table t_s_user_login(

    i_user_login_id integer not null primary key,

    i_user_id integer not null,

    r_timestamp timestamp default current_timestamp not null,

    t_type char(1) not null,

    s_ip varchar(15),

    check(t_type in ('P', 'C'))

);

create table t_a_banned_ip(

    i_banned_ip_id integer not null primary key,

    s_ip varchar(15) not null

);

create table t_s_user_feed(

    i_user_feed_id integer not null primary key,

    i_user_id integer not null,

    t_link varchar(4096) not null,

    t_concrete_link varchar(4096) not null,

    i_ps_thumb integer default 3 not null,

    i_ps_home integer default 10 not null,

    b_active char(1) default '1' not null,

    t_name varchar(255),

    t_description varchar(1024),

    i_order integer not null

);


create table t_s_user_forum_bmk(

    i_user_forum_bmk_id integer not null primary key,

    i_user_id integer not null,

    i_forum_id integer not null,

    i_order integer not null,

    unique(i_user_id, i_forum_id)

);

 

/*  diritti */


CREATE TABLE t_p_user_group (

  i_user_group_id integer NOT NULL,

  i_user_id integer NOT NULL,

  i_group_id integer NOT NULL,

  b_mod char(1) default '0' NOT NULL,

  PRIMARY KEY  (i_user_group_id),

  UNIQUE (i_user_id,i_group_id)

);


CREATE TABLE t_p_group_section (

  i_group_section_id integer NOT NULL,

  b_r char(1) default '0' NOT NULL ,

  b_w char(1) default '0' NOT NULL ,

  b_rw char(1) default '0' NOT NULL ,

  b_d char(1) default '0' NOT NULL ,

  i_group_id integer NOT NULL,

  i_section_id integer NOT NULL,

  PRIMARY KEY  (i_group_section_id),

  UNIQUE (i_group_id,i_section_id)

);


CREATE TABLE t_p_user_section (

  i_user_section_id integer NOT NULL,

  b_r char(1) default '0' NOT NULL ,

  b_w char(1) default '0' NOT NULL ,

  b_rw char(1) default '0' NOT NULL ,

  b_d char(1) default '0' NOT NULL ,

  i_user_id integer NOT NULL,

  i_section_id integer NOT NULL,

  PRIMARY KEY  (i_user_section_id),

  UNIQUE (i_user_id,i_section_id)

);


/*  diritti forum


    un gruppo ha dei diritti di base sui vari forum (lettura, scrittura)

    un utente eredita i diritti dei gruppi cui appartiene, e puo' fare l'override

    per le viste vedi sotto al forum

*/


CREATE TABLE t_p_group_forum_access (

  i_group_forum_access_id integer NOT NULL,

  i_forum_id integer NOT NULL,

  i_group_id integer NOT NULL,

  b_R char(1) default '0' NOT NULL ,

  b_W char(1) default '0' NOT NULL ,    -- new topic

  b_RW char(1) default '0' NOT NULL ,   -- reply

  PRIMARY KEY  (i_group_forum_access_id),

  UNIQUE(i_forum_id, i_group_id)

);


CREATE TABLE t_p_user_forum_access (

  i_user_forum_access_id integer NOT NULL,

  i_forum_id integer NOT NULL,

  i_user_id integer NOT NULL,

  b_R char(1) default '0' NOT NULL ,

  b_W char(1) default '0' NOT NULL ,

  b_RW char(1) default '0' NOT NULL ,

  b_MOD char(1) default '0' NOT NULL ,

  PRIMARY KEY  (i_user_forum_access_id),

  UNIQUE (i_forum_id,i_user_id)

 

);


/* ******************************************************


    FORUM


*/


CREATE TABLE t_a_forum (

  i_forum_id integer NOT NULL,

  t_name varchar(192) NOT NULL,

  t_description blob,

  t_style char(17) NOT NULL,

  t_custom_home varchar(255),

  i_parent_forum_id integer,

  i_order integer NOT NULL,

  b_allow_post char(1) default '1' not null ,    /* se a 0 non si puo' postare nel forum ed e' un contenitore quindi */

  b_can_join char(1) default '0' NOT NULL ,      /*  se si puo' entrare / uscire  */

    b_show_feed char(1) default '1' not null,

    b_show_bmk char(1) default '1' not null,

    b_show_readers char(1) default '1' not null,

  i_topic_count integer default 0 not null ,     /* trigger insert / delete su topic */

  i_post_count integer default 0 not null ,      /* trigger insert / delete su post */

  i_hit_count integer default 0 not null ,

  i_readers integer default 0 not null ,

  i_writers integer default 0 not null ,

  i_last_post_id integer ,

  r_last_post timestamp,

  PRIMARY KEY  (i_forum_id)

  --unique(t_name)

);


CREATE TABLE t_s_topic (

  i_topic_id integer NOT NULL,

  i_forum_id integer NOT NULL,

  t_style char(10) NOT NULL,

  i_user_id integer ,

  t_guest varchar(50) ,

  r_insert timestamp default CURRENT_TIMESTAMP NOT NULL ,

  i_post_count integer default 0 NOT NULL ,    /* trigger insert / delete su post */

  i_hit_count integer default 0 NOT NULL,     /* a mano da general.php */

  i_last_post_id integer,          /* trigger insert / delete su post */

  r_last_post timestamp,

  d_auto_prune date ,

  t_title varchar(255) NOT NULL,

  t_description varchar(255) ,

  d_from date ,

  d_to date ,

  i_order integer ,     /* questa per ora non e' gestita */

  b_R char(1) default '1' NOT NULL ,

  b_RW char(1) default '1' NOT NULL , -- solo reply

  t_moved_to varchar(255) ,

  PRIMARY KEY  (i_topic_id)

);


CREATE TABLE t_s_post (

  i_post_id integer NOT NULL,

  i_topic_id integer NOT NULL,

  t_style char(10) NOT NULL,

  i_user_id integer ,

  t_guest varchar(50) ,

  r_insert timestamp default CURRENT_TIMESTAMP NOT NULL ,

  r_edit_last timestamp ,                       /* trigger update */

  i_edit_count integer default 0 NOT NULL ,    /* trigger update */

  i_edit_user_id integer ,

  t_title varchar(255) ,

  t_description varchar(255) ,

  t_post_source blob NOT NULL,

  t_censored_source blob,

    i_post_reply_id integer,

    b_mod_att char(1) default '0' not null,

   i_mod_att_user_id integer,

    r_mod_att timestamp,

    s_ip varchar(15)

  PRIMARY KEY  (i_post_id)

);


CREATE TABLE t_s_post_attach (

  i_post_attach_id integer NOT NULL,

  i_post_id integer NOT NULL,

  i_file_id integer NOT NULL,

  t_description blob,

  PRIMARY KEY  (i_post_attach_id),

  UNIQUE (i_post_id, i_file_id)

);


/*  se c'e' una riga, ignoro i nuovi post nel forum */

create table t_p_user_forum_ignore(

  i_user_forum_ignore_id integer NOT NULL,

  i_forum_id integer NOT NULL,

  i_user_id integer NOT NULL,

  PRIMARY KEY  (i_user_forum_ignore_id),

  UNIQUE (i_forum_id,i_user_id)

);


/*  se c'e' una riga, ho joinato un forum joinabile */

create table t_p_user_forum_join(

  i_user_forum_join_id integer NOT NULL,

  i_forum_id integer NOT NULL,

  i_user_id integer NOT NULL,

  PRIMARY KEY  (i_user_forum_join_id),

  UNIQUE (i_forum_id,i_user_id)

);


/*

    bookmark, da leggere, notifica

    il 'new' e' in base all'ignore

    sarebbe da gestire con i forum in cui vedo roba, con conseguente update su cambio di diritti ... da trigger ?

*/

CREATE TABLE t_p_user_topic (

  i_user_topic_id integer NOT NULL,

  i_topic_id integer NOT NULL,

  i_user_id integer NOT NULL,

  r_date timestamp default CURRENT_TIMESTAMP NOT NULL ,

  b_bookmark char(1) default '0' NOT NULL ,

  b_new char(1) default '1' NOT NULL ,

  b_notify char(1) default '0' NOT NULL ,

  i_last_read_post_id integer,

  PRIMARY KEY  (i_user_topic_id),

  UNIQUE (i_topic_id,i_user_id)

);


create table t_s_topic_hit
(

i_topic_hit_id integer not null primary key,

i_topic_id integer not null,
i_user_id integer not null,
r_hit timestamp default current_timestamp not null);



/* ha solo le righe che interessano */

CREATE TABLE t_p_user_topic_access (

  i_user_topic_access_id integer NOT NULL,

  i_topic_id integer NOT NULL,

  i_user_id integer NOT NULL,

  b_R char(1) default '0' NOT NULL ,

  b_RW char(1) default '0' NOT NULL ,

  b_MOD char(1) default '0' NOT NULL ,

  PRIMARY KEY  (i_user_topic_access_id),

  UNIQUE (i_topic_id,i_user_id)

);





/* ******************************************************


    GIOCATORI E TEAM


    ci sono i giochi, le coppe (di un gioco)

    ogni giocatore puo' farsi la scheda per ogni gioco (config, menate)

    ogni gruppo puo' creare vari team di un gioco, in cui mettere e togliere

        giocatori del gruppo (per quel gioco)

    il team ha una tag e un war arranger

    il player specifica la sua tag per ogni team cui appartiene

    il war arranger gestisce le partite del suo team

    la partita ha i suoi vari dati e la formazione (giocatore del team col suo stato)

    

*/


CREATE TABLE t_r_match_cup (

  i_match_cup_id integer NOT NULL,

  t_description varchar(255) NOT NULL,

  t_game char(10) NOT NULL,

  PRIMARY KEY  (i_match_cup_id)

);


/*  uno user puo' giocare a tanta roba, ma ha al massimo una scheda per ogni gioco   */

CREATE TABLE t_p_player (

  i_player_id integer NOT NULL,

  t_game char(10) NOT NULL,

  i_user_id integer NOT NULL,

  t_role varchar(255) ,

  t_exp blob,

  t_config blob,

  b_active char(1) default '0' NOT NULL ,

  PRIMARY KEY  (i_player_id),

  UNIQUE (i_user_id, t_game)

);


/*  un gruppo puo' avere piu' team per ogni gioco */

CREATE TABLE t_a_team (

  i_team_id integer NOT NULL,

  i_group_id integer NOT NULL,

  t_game char(10) NOT NULL,

    t_description BLOB,

  i_war_arranger_id integer ,   /* potrebbe puntare a player anziche' a user */

  t_tag varchar(50) ,

  PRIMARY KEY  (i_team_id)

);


CREATE TABLE t_p_team_player (

  i_team_player_id integer NOT NULL,

  i_team_id integer NOT NULL,

  i_player_id integer NOT NULL,

  t_nick varchar(50) ,

  PRIMARY KEY  (i_team_player_id),

  UNIQUE (i_team_id,i_player_id)

);


/*  PARTITE */


CREATE TABLE t_a_match (

  i_match_id integer NOT NULL,

  i_team_id integer NOT NULL,

  i_match_cup_id integer ,

  d_date date NOT NULL,

  h_time time NOT NULL,

  t_opponent varchar(255) ,

  i_team_score integer ,

  i_opponent_score integer ,

  i_points integer ,

  t_note blob,

  i_topic_id integer ,

  PRIMARY KEY  (i_match_id)

);


CREATE TABLE t_p_match_player (

  i_match_player_id integer NOT NULL,

  i_match_id integer NOT NULL,

  i_player_id integer NOT NULL,

  t_match_player_status char(2) NOT NULL,

  PRIMARY KEY  (i_match_player_id),

  UNIQUE (i_match_id,i_player_id)

);