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;
     }
 }
Ejemplo n.º 2
0
 public function updateLives($playerID, $payload)
 {
     Log::add('updateLives called with player: ' . $playerID . ' and payload: ' . $payload);
     if ($data = NinjaHash::decode($payload)) {
         $player = $this->getPlayerData($playerID);
         Validate::hash($player, $data->key);
         Log::add('update lives payload contained: ' . print_r($data, true));
         if ($data->lifeCount != '') {
             $value = min(self::MAX_LIVES, (int) $player->lives + (int) $data->lifeCount);
             Log::add('updating lives with value: ' . $value . ' player lives: ' . (int) $player->lives . ' lives = ' . (int) $data->lifeCount);
             Log::add('max lives: ' . self::MAX_LIVES);
             $this->update('lives', $value, $playerID);
         }
         if ($data->freelife == "true") {
             Log::add('Free life was true!');
             $this->update('last_life_bonus', date('Y-m-d H:i:s'), $playerID);
         }
         return array('lives' => $value, 'requestCount' => 0);
     } else {
         Log::add('Decoding uploadLive payload failed');
         return false;
     }
 }