Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 26th, 2011  |  syntax: PHP  |  size: 4.09 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. class Wordlist {
  4.     // Basic CRUD etc. here!
  5.     // Other sample methods:
  6.     public function wordlistCount($user_id) {} // Returns count of how many wordlists a user has
  7.     public function getAll($user_id) {} // Returns all wordlists of a user
  8. }
  9.  
  10. // -----------------------------------------------------------------------------------
  11.  
  12. class Word {
  13.     // Basic CRUD etc. here!
  14.     // Other sample methods:
  15.     public function wordCount($wordlist_id) {} // Returns count of words in a wordlist
  16.     public function getAll($wordlist_id) {} // Returns all words from a wordlist
  17.     public function getWordInfo($word_id) {} // Returns information about a word
  18. }
  19.  
  20. // -----------------------------------------------------------------------------------
  21.  
  22. class Wordpicker {
  23.    
  24.     // The class needs to know which words and wordlists to exclude
  25.     protected $_used_words = array();
  26.     protected $_used_wordlists = array();
  27.    
  28.     // Wordlists to pick words from
  29.     protected $_wordlists = array();
  30.    
  31.     /* Public Methods */
  32.     public function setWordlists($wordlists = array()) {}
  33.     public function setUsedWords($used_words = array()) {}
  34.     public function setUsedWordlists($used_wordlists = array()) {}
  35.    
  36.     public function getRandomWord() {} // COUPLING POINT! Will most likely need to communicate with both the Wordlist and Word classes
  37.    
  38.     /* Protected Methods */
  39.     protected function _checkAvailableWordlists() {} // COUPLING POINT! Might need to check if wordlists are deleted etc.
  40.     protected function _checkAvailableWords() {} // COUPLING POINT! Method needs to get all words in a wordlist from the Word class
  41.    
  42. }
  43.  
  44. // -----------------------------------------------------------------------------------
  45.  
  46. class Game {
  47.    
  48.     protected $_session_id; // The ID of a game session which gets stored in the database along with game details
  49.     protected $_game_info = array();
  50.    
  51.     // Game instantiation
  52.     public function __construct($user_id) {
  53.         if (! $this->_session_id = $this->_gameExists($user_id)) {
  54.             // New game
  55.         } else {
  56.             // Resume game
  57.         }
  58.     }
  59.    
  60.     // This is the method I tried to make flexible by using abstract classes etc.
  61.     // Does it even belong in this class at all?
  62.     public function checkWord($answer, $native_word, $translation) {} // This method checks the answer against the native word / translation word, depending on game mode
  63.    
  64.     public function getGameInfo() {} // Returns information about a game session, or creates it if it does not exist
  65.     public function deleteSession($session_id) {} // Deletes a game session from the database
  66.    
  67.     // Methods dealing with game session information
  68.     protected function _gameExists($user_id) {}
  69.     protected function _getProgress($session_id) {}
  70.     protected function _updateProgress($game_info = array()) {}
  71.    
  72. }
  73.  
  74. // -----------------------------------------------------------------------------------
  75.  
  76. /* CONTROLLER */
  77. /* "Guess the word" page */
  78.  
  79. // User input
  80. $game_type = $_POST['game_type']; // Chosen with radio buttons etc.
  81. $wordlists = $_POST['wordlists']; // Chosen with checkboxes etc.
  82.  
  83. // Starts a new game or resumes one from the database
  84. $game = new Game($_SESSION['user_id']);
  85. $game_info = $game->getGameInfo();
  86.  
  87. // Instantiates a new Wordpicker
  88. $wordpicker = new Wordpicker();
  89.  
  90. $wordpicker->setWordlists((isset($game_info['wordlists'])) ? $game_info['wordlists'] : $wordlists);
  91. $wordpicker->setUsedWordlists((isset($game_info['used_wordlists'])) ? $game_info['used_wordlists'] : NULL);
  92. $wordpicker->setUsedWords((isset($game_info['used_words'])) ? $game_info['used_words'] : NULL);
  93.  
  94. // Fetches an available word
  95. if (! $word_id = $wordpicker->getRandomWord()) {
  96.    
  97.     // No more words left - game over!
  98.     $game->deleteSession($game_info['id']);
  99.     redirect();
  100.    
  101. } else {
  102.    
  103.     // Presents word details to the user
  104.     $word = new Word();
  105.     $word_info = $word->getWordInfo($word_id);
  106.    
  107. }
  108.  
  109. // -----------------------------------------------------------------------------------
  110.  
  111. /* CONTROLLER */
  112. /* "Check the answer" page */
  113.  
  114. // ??????????????????
clone this paste RAW Paste Data