Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Wordlist {
- // Basic CRUD etc. here!
- // Other sample methods:
- public function wordlistCount($user_id) {} // Returns count of how many wordlists a user has
- public function getAll($user_id) {} // Returns all wordlists of a user
- }
- // -----------------------------------------------------------------------------------
- class Word {
- // Basic CRUD etc. here!
- // Other sample methods:
- public function wordCount($wordlist_id) {} // Returns count of words in a wordlist
- public function getAll($wordlist_id) {} // Returns all words from a wordlist
- public function getWordInfo($word_id) {} // Returns information about a word
- }
- // -----------------------------------------------------------------------------------
- class Wordpicker {
- // The class needs to know which words and wordlists to exclude
- protected $_used_words = array();
- protected $_used_wordlists = array();
- // Wordlists to pick words from
- protected $_wordlists = array();
- /* Public Methods */
- public function setWordlists($wordlists = array()) {}
- public function setUsedWords($used_words = array()) {}
- public function setUsedWordlists($used_wordlists = array()) {}
- public function getRandomWord() {} // COUPLING POINT! Will most likely need to communicate with both the Wordlist and Word classes
- /* Protected Methods */
- protected function _checkAvailableWordlists() {} // COUPLING POINT! Might need to check if wordlists are deleted etc.
- protected function _checkAvailableWords() {} // COUPLING POINT! Method needs to get all words in a wordlist from the Word class
- }
- // -----------------------------------------------------------------------------------
- class Game {
- protected $_session_id; // The ID of a game session which gets stored in the database along with game details
- protected $_game_info = array();
- // Game instantiation
- public function __construct($user_id) {
- if (! $this->_session_id = $this->_gameExists($user_id)) {
- // New game
- } else {
- // Resume game
- }
- }
- // This is the method I tried to make flexible by using abstract classes etc.
- // Does it even belong in this class at all?
- public function checkWord($answer, $native_word, $translation) {} // This method checks the answer against the native word / translation word, depending on game mode
- public function getGameInfo() {} // Returns information about a game session, or creates it if it does not exist
- public function deleteSession($session_id) {} // Deletes a game session from the database
- // Methods dealing with game session information
- protected function _gameExists($user_id) {}
- protected function _getProgress($session_id) {}
- protected function _updateProgress($game_info = array()) {}
- }
- // -----------------------------------------------------------------------------------
- /* CONTROLLER */
- /* "Guess the word" page */
- // User input
- $game_type = $_POST['game_type']; // Chosen with radio buttons etc.
- $wordlists = $_POST['wordlists']; // Chosen with checkboxes etc.
- // Starts a new game or resumes one from the database
- $game = new Game($_SESSION['user_id']);
- $game_info = $game->getGameInfo();
- // Instantiates a new Wordpicker
- $wordpicker = new Wordpicker();
- $wordpicker->setWordlists((isset($game_info['wordlists'])) ? $game_info['wordlists'] : $wordlists);
- $wordpicker->setUsedWordlists((isset($game_info['used_wordlists'])) ? $game_info['used_wordlists'] : NULL);
- $wordpicker->setUsedWords((isset($game_info['used_words'])) ? $game_info['used_words'] : NULL);
- // Fetches an available word
- if (! $word_id = $wordpicker->getRandomWord()) {
- // No more words left - game over!
- $game->deleteSession($game_info['id']);
- redirect();
- } else {
- // Presents word details to the user
- $word = new Word();
- $word_info = $word->getWordInfo($word_id);
- }
- // -----------------------------------------------------------------------------------
- /* CONTROLLER */
- /* "Check the answer" page */
- // ??????????????????
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement