Exemplo n.º 1
0
 /**
  * Returns a get_messages() style array of new or updated conversations in $message1
  * In other words, any messages in $message1 that are not in $message2 are returned
  * 
  * @param mixed $message1 The newer messages
  * @param mixed $message2 The older messages
  */
 static function diff_messages($message1, $message2)
 {
     $new = array();
     // Hold return value
     foreach ($message1 as $first) {
         $newconv = true;
         foreach ($message2 as $second) {
             if ($first['id'] == $second['id']) {
                 $newconv = false;
                 $messages = array_diff_recursive($first['messages'], $second['messages']);
                 if (count($messages) != 0) {
                     $new2 = $first;
                     // Copy
                     $new2['messages'] = $messages;
                     // Overwrite
                     $new[] = $new2;
                 }
             }
         }
         if ($newconv) {
             $new[] = $first;
         }
     }
     return $new;
 }
function array_diff_recursive($array1, $array2)
{
    //Compared two arrays recursively to find differences.
    $output = array();
    foreach ($array1 as $nKey => $nValue) {
        if (array_key_exists($nKey, $array2)) {
            if (is_array($nValue)) {
                $recursiveDiff = array_diff_recursive($nValue, $array2[$nKey]);
                if (count($recursiveDiff)) {
                    $output[$nKey] = $recursiveDiff;
                } else {
                    if ($nValue != $array2[$nKey]) {
                        $output[$nKey] = $nValue;
                    }
                }
            }
        } else {
            $output[$nKey] = $nValue;
        }
    }
    return $output;
}
Exemplo n.º 3
0
 /**
  * Add an array to the log queue
  * @param $array
  * @param $oldArray (optional) - when included, only the changes between the arrays is saved. 
  * @param $type (optional) default: LOG_MESSAGE_NODISPLAY. or E_MESSAGE_WARNING, E_MESSAGE_NOTICE, E_MESSAGE_SUCCESS
  */
 public function addArray($array, $oldArray = null, $type = LOG_MESSAGE_NODISPLAY, $session = false)
 {
     if (is_array($oldArray)) {
         $text = array_diff_recursive($array, $oldArray);
         // Located in core_functions.php
         if (count($text) < 1) {
             $text = "No differences found";
         }
     } else {
         $text = $array;
     }
     return $this->logMessage($text, $type, $type, $session);
 }
Exemplo n.º 4
0
/**
 * Runs array_diff recursively
 *
 * @param array $array1
 * @param array $array2
 *
 * @return array of elements in $array1 that are different from $array2
 */
function array_diff_recursive($array1, $array2)
{
    $diff = array();
    foreach ($array1 as $key => $value) {
        if (array_key_exists($key, $array2)) {
            if (is_array($value)) {
                $recursive_diff = array_diff_recursive($value, $array2[$key]);
                if (count($recursive_diff)) {
                    $diff[$key] = $recursive_diff;
                }
            } elseif ($value != $array2[$key]) {
                // change to !== to use exact match
                $diff[$key] = $value;
            }
        } else {
            $diff[$key] = $value;
        }
    }
    return $diff;
}
Exemplo n.º 5
0
/**
 * Return an array with changes between 2 arrays. 
 */
function array_diff_recursive($array1, $array2)
{
    $ret = array();
    foreach ($array1 as $key => $val) {
        if (array_key_exists($key, $array2)) {
            if (is_array($val)) {
                $diff = array_diff_recursive($val, $array2[$key]);
                if (count($diff)) {
                    $ret[$key] = $diff;
                }
            } else {
                if ($val != $array2[$key]) {
                    $ret[$key] = $val;
                }
            }
        } else {
            $ret[$key] = $val;
        }
    }
    return $ret;
}
Exemplo n.º 6
0
 /** protected function _save
  *		Saves all changed data to the database
  *
  * @param void
  *
  * @action saves the game data
  *
  * @return void
  * @throws MyException
  */
 protected function _save()
 {
     call(__METHOD__);
     if (!$this->id) {
         return;
     }
     $Mysql = Mysql::get_instance();
     // send an email if we have to
     if ($this->_risk->new_player) {
         Email::send('turn', $this->_risk->current_player, array('game_id' => $this->id, 'name' => $this->name));
     }
     // make sure we don't have a MySQL error here, it may be causing the issues
     $run_once = false;
     do {
         if ($run_once) {
             // pause for 3 seconds, then try again
             sleep(3);
         }
         // update the game data
         $query = "\n\t\t\t\tSELECT extra_info\n\t\t\t\t\t, state\n\t\t\t\t\t, modify_date\n\t\t\t\tFROM " . self::GAME_TABLE . "\n\t\t\t\tWHERE game_id = '{$this->id}'\n\t\t\t";
         $game = $Mysql->fetch_assoc($query);
         // make sure we don't have a MySQL error here, it may be causing the issues
         $error = $Mysql->error;
         $errno = preg_replace('/(\\d+)/', '$1', $error);
         $run_once = true;
     } while (2006 == $errno || 2013 == $errno);
     $update_modified = false;
     if (!$game) {
         throw new MyException(__METHOD__ . ': Game data not found for game #' . $this->id);
     }
     // test the modified date and make sure we still have valid data
     call($this->modify_date);
     call(strtotime($game['modify_date']));
     if ($this->modify_date != strtotime($game['modify_date'])) {
         $this->_log('== FAILED == DATA SAVE: #' . $this->id . ' @ ' . time() . "\n" . ' - ' . $this->modify_date . "\n" . ' - ' . strtotime($game['modify_date']));
         throw new MyException(__METHOD__ . ': Trying to save game (#' . $this->id . ') with out of sync data');
     }
     $update_game = false;
     if ($game['state'] != $this->state) {
         $update_game['state'] = $this->state;
     }
     $update_game['extra_info'] = array_diff_recursive($this->_extra_info, self::$_EXTRA_INFO_DEFAULTS);
     ksort($update_game['extra_info']);
     $update_game['extra_info'] = json_encode($update_game['extra_info']);
     if ('[]' == $update_game['extra_info']) {
         $update_game['extra_info'] = null;
     }
     if (0 === strcmp($game['extra_info'], $update_game['extra_info'])) {
         unset($update_game['extra_info']);
     }
     if ($update_game) {
         $update_modified = true;
         $Mysql->insert(self::GAME_TABLE, $update_game, " WHERE game_id = '{$this->id}' ");
     }
     // update the player's data
     $query = "\n\t\t\tSELECT *\n\t\t\tFROM " . self::GAME_PLAYER_TABLE . "\n\t\t\tWHERE game_id = '{$this->id}'\n\t\t";
     $db_players = $Mysql->fetch_array($query);
     // add missing players
     $db_player_ids = array_shrink($db_players, 'player_id');
     if (!$db_player_ids) {
         $db_player_ids = array();
     }
     $game_player_ids = array_keys($this->_players);
     $new_players = array_diff($game_player_ids, $db_player_ids);
     foreach ($new_players as $new_player_id) {
         $update_player = array('game_id' => $this->id, 'player_id' => $new_player_id, 'color' => $this->_players[$new_player_id]['color'], 'order_num' => $this->_risk->players[$new_player_id]['order_num'], 'cards' => $this->_risk->players[$new_player_id]['cards'], 'armies' => $this->_risk->players[$new_player_id]['armies'], 'state' => $this->_risk->players[$new_player_id]['state'], 'move_date' => null);
         $update_player['cards'] = implode(',', $update_player['cards']);
         $update_player['extra_info'] = array_diff_assoc($this->_risk->players[$new_player_id]['extra_info'], self::$_PLAYER_EXTRA_INFO_DEFAULTS);
         ksort($update_player['extra_info']);
         $update_player['extra_info'] = json_encode($update_player['extra_info']);
         if ('[]' == $update_player['extra_info']) {
             $update_player['extra_info'] = null;
         }
         call($update_player);
         $Mysql->insert(self::GAME_PLAYER_TABLE, $update_player);
         $update_modified = true;
     }
     // check the player parts
     foreach ($db_players as $db_player) {
         $update_player = array();
         $player_id = $db_player['player_id'];
         $risk_player = $this->_risk->players[$player_id];
         array_trim($db_player['cards'], 'int');
         foreach ($db_player['cards'] as $key => $card_id) {
             if (0 === $card_id) {
                 unset($db_player['cards'][$key]);
             }
         }
         sort($db_player['cards']);
         $cards = $risk_player['cards'];
         sort($cards);
         if (count($db_player['cards']) != count($cards) || array_diff($db_player['cards'], $cards)) {
             $update_player['cards'] = implode(',', $cards);
         }
         if ($db_player['armies'] != $risk_player['armies']) {
             $update_player['armies'] = (int) $risk_player['armies'];
         }
         if ($db_player['state'] != $risk_player['state']) {
             $update_player['state'] = $risk_player['state'];
         }
         if ($db_player['order_num'] != $risk_player['order_num']) {
             $update_player['order_num'] = $risk_player['order_num'];
         }
         $risk_player['extra_info'] = array_diff_assoc($risk_player['extra_info'], self::$_PLAYER_EXTRA_INFO_DEFAULTS);
         ksort($risk_player['extra_info']);
         $risk_player['extra_info'] = json_encode($risk_player['extra_info']);
         if ('[]' == $risk_player['extra_info']) {
             $risk_player['extra_info'] = null;
         }
         if (0 !== strcmp($db_player['extra_info'], $risk_player['extra_info'])) {
             $update_player['extra_info'] = $risk_player['extra_info'];
         }
         // game was started, reset all player's move dates to now
         if (array_key_exists('move_date', $risk_player)) {
             $update_player['move_date'] = null;
         }
         if (count($update_player)) {
             $update_modified = true;
             $Mysql->insert(self::GAME_PLAYER_TABLE, $update_player, array('game_id' => $this->id, 'player_id' => $player_id));
         }
     }
     if ('Waiting' != $this->state) {
         // update the land data
         $query = "\n\t\t\t\tSELECT *\n\t\t\t\tFROM `" . self::GAME_LAND_TABLE . "`\n\t\t\t\tWHERE game_id = :game_id\n\t\t\t";
         $params = array(':game_id' => $this->id);
         $db_lands = $Mysql->fetch_array($query, $params);
         if (!$db_lands) {
             $board = $this->_risk->board;
             foreach ($board as $land_id => $land) {
                 $land['game_id'] = $this->id;
                 $land['land_id'] = $land_id;
                 $Mysql->insert(self::GAME_LAND_TABLE, $land);
             }
             $update_modified = true;
         } else {
             foreach ($db_lands as $db_land) {
                 $update_land = array();
                 $land_id = $db_land['land_id'];
                 $rland = $this->_risk->board[$land_id];
                 if ($db_land['player_id'] != $rland['player_id']) {
                     $update_land['player_id'] = $rland['player_id'];
                 }
                 if ($db_land['armies'] != $rland['armies']) {
                     $update_land['armies'] = $rland['armies'];
                 }
                 if ($update_land) {
                     $update_modified = true;
                     $Mysql->insert(self::GAME_LAND_TABLE, $update_land, " WHERE game_id = '{$this->id}' AND land_id = '{$land_id}' ");
                 }
             }
         }
     }
     // update the game modified date
     if ($update_modified) {
         $Mysql->insert(self::GAME_TABLE, array('modify_date' => NULL), " WHERE game_id = '{$this->id}' ");
     }
 }