Ejemplo n.º 1
0
 public function saveLevelScore($payload = null, $playerID = null)
 {
     Validate::Player($playerID);
     if ($payload == null) {
         return false;
     }
     if ($decodedPayload = NinjaHash::decode($payload)) {
         $player = new PlayerModel();
         $p = $player->loadPlayer($this->playerID);
         $updates = false;
         $this->db->select('*');
         $this->db->from('level_scores');
         $this->db->where('level_id', $decodedPayload->level);
         $this->db->where('player_id', $this->playerID);
         $query = $this->db->get();
         $tmpObject = $query->row();
         $levelScoreExists = $query->num_rows();
         if ($levelScoreExists) {
             if ($decodedPayload->total_moves < $tmpObject->total_moves) {
                 Log::add('total moves was less then ' . $decodedPayload->total_moves . ' : ' . $tmpObject->total_moves);
                 $updates = true;
                 $dbData['total_moves'] = $decodedPayload->total_moves;
             }
             if (strlen($decodedPayload->largest_word) > strlen($tmpObject->largest_word)) {
                 Log::add('largest word was bigger ' . $decodedPayload->total_moves . ' : ' . $tmpObject->total_moves);
                 $updates = true;
                 $dbData['largest_word'] = $decodedPayload->largest_word;
             }
             if ($decodedPayload->time_elapsed < $tmpObject->time_elapsed || $tmpObject->time_elapsed == '') {
                 Log::add('time elapsed was less then ' . $decodedPayload->total_moves . ' : ' . $tmpObject->total_moves);
                 $updates = true;
                 $dbData['time_elapsed'] = $decodedPayload->time_elapsed;
             }
             if ($decodedPayload->score > $tmpObject->score) {
                 Log::add('score was more than ' . $decodedPayload->score . ' : ' . $tmpObject->score);
                 $dbData['score_level'] = $this->getScoreLevel($decodedPayload->score, $decodedPayload->level);
                 $updates = true;
                 $dbData['score'] = $decodedPayload->score;
             }
             //update play_count
             $playCount = $tmpObject->play_count;
             $playCount = $playCount + 1;
             $dbData['play_count'] = $playCount;
             if ($updates) {
                 $this->db->where('level_id', $decodedPayload->level);
                 $this->db->where('player_id', $this->playerID);
                 $this->db->update('level_scores', $dbData);
                 Log::add('updating player level score - last query: ' . $this->db->last_query());
             }
         } else {
             $dbData['total_moves'] = $decodedPayload->total_moves;
             $dbData['largest_word'] = $decodedPayload->largest_word;
             $dbData['time_elapsed'] = $decodedPayload->time_elapsed;
             $dbData['score'] = $decodedPayload->score;
             $dbData['date_added'] = date('Y-m-d H:i:a');
             $dbData['level_id'] = $decodedPayload->level;
             $dbData['player_id'] = $this->playerID;
             $dbData['play_count'] = 1;
             $dbData['score_level'] = $this->getScoreLevel($decodedPayload->score, $decodedPayload->level);
             $this->db->insert('level_scores', $dbData);
             Log::add('inserting player level score - last query: ' . $this->db->last_query());
         }
         //check if leveled up
         $newLives = $decodedPayload->completed == 'true' ? $player->data->lives + 1 : $player->data->lives;
         if ($decodedPayload->completed == 'true' && $player->data->level == $decodedPayload->level) {
             $player->levelUp($playerID, $decodedPayload->score, $newLives);
         }
         return $player->data;
     } else {
         return false;
     }
 }