From 2d796d48df6f4371111bcbc776ea781e4f45c831 Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Sat, 8 Jul 2017 11:11:53 -0400 Subject: Expand on query and result, begin sql design --- private/Model/Connection.php | 41 +++++++++++++++++ private/Model/Query.php | 71 +++++++++++++++++++++++++++++ private/Model/Result.php | 52 ++++++++++++++++++++++ private/Model/creds.php | 11 +++++ private/Model/fresh.sql | 103 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 278 insertions(+) create mode 100644 private/Model/Connection.php create mode 100644 private/Model/Query.php create mode 100644 private/Model/Result.php create mode 100644 private/Model/creds.php create mode 100644 private/Model/fresh.sql (limited to 'private/Model') diff --git a/private/Model/Connection.php b/private/Model/Connection.php new file mode 100644 index 0000000..48cf4fb --- /dev/null +++ b/private/Model/Connection.php @@ -0,0 +1,41 @@ +host . + (($cred->port !== '') ? ";port=" . $cred->port : '') . + ";dbname=" . $cred->dbName, + $cred->login, + $cred->password + ); + // we destroy $cred as quickly as possible + $cred = null; + } catch (\PDOException $err) { + // we destroy $cred as quickly as possible + $cred = null; + die(json_encode(array("Result-Type" => "ERROR", "Content" => array($err->getMessage())))); + } + return $this; + } +} diff --git a/private/Model/Query.php b/private/Model/Query.php new file mode 100644 index 0000000..df05b24 --- /dev/null +++ b/private/Model/Query.php @@ -0,0 +1,71 @@ +path = $path; + $this->request = $request; + $this->content = $content; + + return $this; + } + + /** + * Returns the request path made by the client. + * + * @return array + */ + public function getPath (): array + { + return $this->path; + } + + /** + * Returns the request type made by the client. + * + * @return string + */ + public function get_request (): string + { + return $this->request; + } + + /** + * Returns the information that is built from outside the request path. + * + * @return array + */ + public function getContent (): array + { + return $this->content; + } +} diff --git a/private/Model/Result.php b/private/Model/Result.php new file mode 100644 index 0000000..ac08821 --- /dev/null +++ b/private/Model/Result.php @@ -0,0 +1,52 @@ +resultType = $resultType; + $this->result = $result; + + return $this; + } + + /** + * Returns the result type of the Result. + * + * @return string + */ + public function getResultType (): string + { + return $this->resultType; + } + + /** + * Returns the result array of the Result. + * + * @return array + */ + public function getResult (): array + { + return $this->result; + } +} \ No newline at end of file diff --git a/private/Model/creds.php b/private/Model/creds.php new file mode 100644 index 0000000..d7b62f1 --- /dev/null +++ b/private/Model/creds.php @@ -0,0 +1,11 @@ +host = "localhost"; +$cred->port = "5432"; +$cred->dbName = "august"; +$cred->login = "smallg"; +$cred->password = "abc"; + diff --git a/private/Model/fresh.sql b/private/Model/fresh.sql new file mode 100644 index 0000000..f39b360 --- /dev/null +++ b/private/Model/fresh.sql @@ -0,0 +1,103 @@ + -- fresh.sql: SQL script that creates the tables used by AO + -- please drop all tables in db before initiating unless told otherwise + + -- uncomment the following two lines to automatically drop all tables +--DROP SCHEMA "public" CASCADE; +--CREATE SCHEMA "public"; + +CREATE OR REPLACE FUNCTION public.GETDATE() RETURNS TIMESTAMPTZ + STABLE LANGUAGE SQL AS 'SELECT NOW()'; + + -- base table for users +CREATE TABLE users ( + userid SERIAL, + email VARCHAR(40) UNIQUE NOT NULL, + firstname VARCHAR(20) NOT NULL, + lastname VARCHAR(20) NOT NULL, + password VARCHAR(255) NOT NULL, -- intend to use bcrypt hash through PHP + games INTEGER DEFAULT 0, -- simple tracking statistics + wins INTEGER DEFAULT 0, -- " + joindate DATE DEFAULT GETDATE(), + activated BIT(1) DEFAULT B'0', -- see activation_keys table + PRIMARY KEY (userid) +); + + -- when activation is completed, key gets deleted and activated bit for user gets flipped to 1. +CREATE TABLE activation_keys ( + code VARCHAR(128), + userid SERIAL, + PRIMARY KEY (code), + CONSTRAINT activation_keys_userid + FOREIGN KEY (userid) REFERENCES users(userid) +); + + -- base table for games +CREATE TABLE games ( + gameid SERIAL, + title VARCHAR(140) NOT NULL, + gametypeid SERIAL NOT NULL, -- future proofing: suppose different game types + players SMALLINT NOT NULL, -- number of players in a game + waitfor INTEGER NOT NULL, -- time in seconds to wait for player to complete a turn + lastturn DATE DEFAULT GETDATE(), + gamestate INTEGER DEFAULT 1, + PRIMARY KEY (gameid) +); + + -- associative table: many users, many games +CREATE TABLE allegiances ( + gameid SERIAL, + userid SERIAL, + allegiance VARCHAR(140), -- important/unimportant role for user (depends on gametype) + ordernum SMALLINT NOT NULL, -- user # spot in game + playing SMALLINT DEFAULT 1, -- default is binary: 0 means player OOP + PRIMARY KEY (gameid, userid), + CONSTRAINT users_games_gameid + FOREIGN KEY (gameid) REFERENCES games(gameid), + CONSTRAINT users_games_userid + FOREIGN KEY (userid) REFERENCES users(userid) +); + + -- Regions are composed of nationstates +CREATE TABLE regions ( + regionid SERIAL, + name VARCHAR(64) NOT NULL, + abbreviation CHAR(2), + bonus INTEGER NOT NULL, + PRIMARY KEY (regionid) +); + + -- base table for nationstates +CREATE TABLE nationstates ( + nationid SERIAL, + regionid SERIAL, + name VARCHAR(64) NOT NULL, + abbreviation CHAR(4), + PRIMARY KEY (nationid), + CONSTRAINT nationstates_regionid + FOREIGN KEY (regionid) REFERENCES regions(regionid) +); + + -- nationstates border nationstates +CREATE TABLE borders ( + nationid SERIAL, + borderid SERIAL, + PRIMARY KEY (nationid, borderid), + CONSTRAINT borders_nationid + FOREIGN KEY (nationid) REFERENCES nationstates(nationid), + CONSTRAINT borders_borderid + FOREIGN KEY (borderid) REFERENCES nationstates(nationid) +); + + -- associative table: many games, many nationstates. Users control them. +CREATE TABLE games_nationstates ( + gameid SERIAL, + nationid SERIAL, + userid SERIAL, -- owner of nationstate for particular game + PRIMARY KEY (gameid, nationid), + CONSTRAINT games_nationstates_gameid + FOREIGN KEY (gameid) REFERENCES games(gameid), + CONSTRAINT games_nationstates_nationid + FOREIGN KEY (nationid) REFERENCES nationstates(nationid), + CONSTRAINT games_nationstates_userid + FOREIGN KEY (userid) REFERENCES users(userid) +); -- cgit