Contract Overview
Balance: 0 Ether
Ether Value: $0
Transactions: 80 txns
Misc:
Address Watch: Add To Watch List
Contract Creator: 0x6589adf7720a5b5f80bd391c0bbf2148d00be5aeat txn 0xea41b2c6a5cfd8ae9f7dd1d53fbd36100e1bebfd196bf3566828263762712d7f
 Latest 25 transactions from a total of 80 transactions

TxHash Age From To Value [TxFee]
0x7a88df9330a3a9bdf2ead5adee73e615f68c77d4002c3f13d6bf3db776ff357512 days 9 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00107523262
0xa1b934ecae6bb139a2bc34999ff4a55a972351d88d38ab86f0b165435f20494312 days 11 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00122829289
0xade51cfd3fac9044ef5ad0efdac98bea88d9b42f7b588db8df5f3b425f33048312 days 12 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.0009149536
0x8fa539613dbf3c67cf187b7e6276d54ed85e7b1c5b942f4bd62e1b63fc82052812 days 12 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00122698372
0x6e41e289437948ace75ae0e42edcf0436dfdeaa1f80cc6bfad3418801cfcc30c12 days 13 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00095141332
0x0d2766bd87e38d0a5fc7e7b43b34eb5f4a239864ed2d72e559f65d91a4081cd112 days 14 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00095183803
0x52c63ee7ba2d56a633ab18c0a3dc7654d08c816cc9682fc95f726d8f67e97ec112 days 14 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00093636213
0x1bac4f3e75fd1c0621e0d5eec11cf412b6870e97a01ca5c7bf608be5533042f512 days 15 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00101295392
0x3cbe33f3deb38d937c514ab41c98c9edc04c8932bae1a7ea88715152bae8dc2412 days 15 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00102574483
0x076c2c1c4329aa1ffe5d16300d39db6a3d18fc7f2e7b6ca8d5b73c8ab26dd00812 days 15 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00095730602
0xce6ee7c0f415cbd8452aa94febb03473ace007a034029ca3cbc79ab30fa416e112 days 16 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00088768383
0xc0d64c4a8e58de085eaa313fff6c66e7802ad549705c27efc4a86d6de4271d1712 days 16 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00093636576
0xa0139765e573dbc5cb76d5f140df6d0a32c86cd66ad3b30314d8715788fe739512 days 17 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00116809649
0xf188f702d2574d5847d38783ee1bb5d3899ff2b74800eed086c972095ef106af12 days 17 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00116864336
0x1a11327b6ca3a055e88880d5780bec6fc30ac557d523d3092893861ed44269d512 days 17 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00128387599
0x1bbd2cc34161dd52f8df2a2e0168e365e2854205c99c8180212f9a6d931597a012 days 18 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00118739152
0xfa4050aab10581db82e53be7b09aab9baa7ada56f04422514c678019f94060c012 days 18 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.002440917
0xb2b4480a042a27b5fc8ed80d4235e9eccb31476ffac261e973ebeb61aea63ad012 days 18 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00122925023
0x4407b7355744d1fa74678df1e31b977eb59a50fbfac55a40f1345f32e43d22c212 days 19 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.0012537681
0xb8fe11343ae49981cc54e5337c4a68e9ff041c04e70e221003479642d4ead32612 days 19 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00113894166
0xc9e9e60aad4bbaa4abe84bb09e5aa953479da228aa8eb85777c9f5a884c0b20412 days 19 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00124222566
0x389b95209eb68c887e49590110a3794c964d1ff8bcf57cd18fc9627e71434ec912 days 20 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00118960518
0xdd8dafe7f9d094439fb89097affa7948812232bc8b857ad1a0a7f793209a871d12 days 20 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00138779268
0x89caacf5e8779ce7684bb68bd4ef997b8e387245bd3b855ab1637993b0da0bfb12 days 20 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00118709254
0x0d2255a718ff0c9b3d41b515a15d553793ef235c7f76f37d02dbe66c1632da2e13 days 6 hrs ago0xd1baaa1da5143f09434af0fd66918a250930baea  IN   0x0c69ea8a12c6ca8acc500751251e2ea9440e73df0 Ether0.00118911141
[ Download CSV Export  ] 
 Internal Transactions as a result of Contract Execution
View All
ParentTxHash Block Age From To Value
Warning: The Compiled Contract might be susceptible to ExpExponentCleanup (medium/high-severity), EventStructWrongData (very low-severity), NestedArrayFunctionCallDecoder (medium-severity) SolidityCompiler Bugs.

Contract Source Code Verified (Similar Match)
Note: Displaying Similar Match Verified Source Code At Contract 0x7e8a2e5876453ea41936c64678de81ae40522bb3(Excluding Constructor Arguments if any)
Contract Name: CryptoSagaDungeonVer1
Compiler Text: v0.4.18+commit.9cf6e910
Optimization Enabled: Yes
Runs (Optimiser):  200



  Contract Source Code   Find Similiar Contracts

pragma solidity ^0.4.18;


/**
 * @title SafeMath
 * @dev Math operations with safety checks that throw on error
 */
library SafeMath {

  /**
  * @dev Multiplies two numbers, throws on overflow.
  */
  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
      return 0;
    }
    uint256 c = a * b;
    assert(c / a == b);
    return c;
  }

  /**
  * @dev Integer division of two numbers, truncating the quotient.
  */
  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    // assert(b > 0); // Solidity automatically throws when dividing by 0
    uint256 c = a / b;
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    return c;
  }

  /**
  * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  */
  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  /**
  * @dev Adds two numbers, throws on overflow.
  */
  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;
  }
}

/**
 * @title Ownable
 * @dev The Ownable contract has an owner address, and provides basic authorization control
 * functions, this simplifies the implementation of "user permissions".
 */
contract Ownable {
  address public owner;


  event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);


  /**
   * @dev The Ownable constructor sets the original `owner` of the contract to the sender
   * account.
   */
  function Ownable() public {
    owner = msg.sender;
  }

  /**
   * @dev Throws if called by any account other than the owner.
   */
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }

  /**
   * @dev Allows the current owner to transfer control of the contract to a newOwner.
   * @param newOwner The address to transfer ownership to.
   */
  function transferOwnership(address newOwner) public onlyOwner {
    require(newOwner != address(0));
    OwnershipTransferred(owner, newOwner);
    owner = newOwner;
  }

}


/**
 * @title Claimable
 * @dev Extension for the Ownable contract, where the ownership needs to be claimed.
 * This allows the new owner to accept the transfer.
 */
contract Claimable is Ownable {
  address public pendingOwner;

  /**
   * @dev Modifier throws if called by any account other than the pendingOwner.
   */
  modifier onlyPendingOwner() {
    require(msg.sender == pendingOwner);
    _;
  }

  /**
   * @dev Allows the current owner to set the pendingOwner address.
   * @param newOwner The address to transfer ownership to.
   */
  function transferOwnership(address newOwner) onlyOwner public {
    pendingOwner = newOwner;
  }

  /**
   * @dev Allows the pendingOwner address to finalize the transfer.
   */
  function claimOwnership() onlyPendingOwner public {
    OwnershipTransferred(owner, pendingOwner);
    owner = pendingOwner;
    pendingOwner = address(0);
  }
}


/**
 * @title Pausable
 * @dev Base contract which allows children to implement an emergency stop mechanism.
 */
contract Pausable is Ownable {
  event Pause();
  event Unpause();

  bool public paused = false;


  /**
   * @dev Modifier to make a function callable only when the contract is not paused.
   */
  modifier whenNotPaused() {
    require(!paused);
    _;
  }

  /**
   * @dev Modifier to make a function callable only when the contract is paused.
   */
  modifier whenPaused() {
    require(paused);
    _;
  }

  /**
   * @dev called by the owner to pause, triggers stopped state
   */
  function pause() onlyOwner whenNotPaused public {
    paused = true;
    Pause();
  }

  /**
   * @dev called by the owner to unpause, returns to normal state
   */
  function unpause() onlyOwner whenPaused public {
    paused = false;
    Unpause();
  }
}


/**
 * @title ERC721 interface
 * @dev see https://github.com/ethereum/eips/issues/721
 */
contract ERC721 {
  event Transfer(address indexed _from, address indexed _to, uint256 _tokenId);
  event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId);

  function balanceOf(address _owner) public view returns (uint256 _balance);
  function ownerOf(uint256 _tokenId) public view returns (address _owner);
  function transfer(address _to, uint256 _tokenId) public;
  function approve(address _to, uint256 _tokenId) public;
  function takeOwnership(uint256 _tokenId) public;
}


/**
 * @title ERC721Token
 * Generic implementation for the required functionality of the ERC721 standard
 */
contract ERC721Token is ERC721 {
  using SafeMath for uint256;

  // Total amount of tokens
  uint256 private totalTokens;

  // Mapping from token ID to owner
  mapping (uint256 => address) private tokenOwner;

  // Mapping from token ID to approved address
  mapping (uint256 => address) private tokenApprovals;

  // Mapping from owner to list of owned token IDs
  mapping (address => uint256[]) private ownedTokens;

  // Mapping from token ID to index of the owner tokens list
  mapping(uint256 => uint256) private ownedTokensIndex;

  /**
  * @dev Guarantees msg.sender is owner of the given token
  * @param _tokenId uint256 ID of the token to validate its ownership belongs to msg.sender
  */
  modifier onlyOwnerOf(uint256 _tokenId) {
    require(ownerOf(_tokenId) == msg.sender);
    _;
  }

  /**
  * @dev Gets the total amount of tokens stored by the contract
  * @return uint256 representing the total amount of tokens
  */
  function totalSupply() public view returns (uint256) {
    return totalTokens;
  }

  /**
  * @dev Gets the balance of the specified address
  * @param _owner address to query the balance of
  * @return uint256 representing the amount owned by the passed address
  */
  function balanceOf(address _owner) public view returns (uint256) {
    return ownedTokens[_owner].length;
  }

  /**
  * @dev Gets the list of tokens owned by a given address
  * @param _owner address to query the tokens of
  * @return uint256[] representing the list of tokens owned by the passed address
  */
  function tokensOf(address _owner) public view returns (uint256[]) {
    return ownedTokens[_owner];
  }

  /**
  * @dev Gets the owner of the specified token ID
  * @param _tokenId uint256 ID of the token to query the owner of
  * @return owner address currently marked as the owner of the given token ID
  */
  function ownerOf(uint256 _tokenId) public view returns (address) {
    address owner = tokenOwner[_tokenId];
    require(owner != address(0));
    return owner;
  }

  /**
   * @dev Gets the approved address to take ownership of a given token ID
   * @param _tokenId uint256 ID of the token to query the approval of
   * @return address currently approved to take ownership of the given token ID
   */
  function approvedFor(uint256 _tokenId) public view returns (address) {
    return tokenApprovals[_tokenId];
  }

  /**
  * @dev Transfers the ownership of a given token ID to another address
  * @param _to address to receive the ownership of the given token ID
  * @param _tokenId uint256 ID of the token to be transferred
  */
  function transfer(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
    clearApprovalAndTransfer(msg.sender, _to, _tokenId);
  }

  /**
  * @dev Approves another address to claim for the ownership of the given token ID
  * @param _to address to be approved for the given token ID
  * @param _tokenId uint256 ID of the token to be approved
  */
  function approve(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
    address owner = ownerOf(_tokenId);
    require(_to != owner);
    if (approvedFor(_tokenId) != 0 || _to != 0) {
      tokenApprovals[_tokenId] = _to;
      Approval(owner, _to, _tokenId);
    }
  }

  /**
  * @dev Claims the ownership of a given token ID
  * @param _tokenId uint256 ID of the token being claimed by the msg.sender
  */
  function takeOwnership(uint256 _tokenId) public {
    require(isApprovedFor(msg.sender, _tokenId));
    clearApprovalAndTransfer(ownerOf(_tokenId), msg.sender, _tokenId);
  }

  /**
  * @dev Mint token function
  * @param _to The address that will own the minted token
  * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  */
  function _mint(address _to, uint256 _tokenId) internal {
    require(_to != address(0));
    addToken(_to, _tokenId);
    Transfer(0x0, _to, _tokenId);
  }

  /**
  * @dev Burns a specific token
  * @param _tokenId uint256 ID of the token being burned by the msg.sender
  */
  function _burn(uint256 _tokenId) onlyOwnerOf(_tokenId) internal {
    if (approvedFor(_tokenId) != 0) {
      clearApproval(msg.sender, _tokenId);
    }
    removeToken(msg.sender, _tokenId);
    Transfer(msg.sender, 0x0, _tokenId);
  }

  /**
   * @dev Tells whether the msg.sender is approved for the given token ID or not
   * This function is not private so it can be extended in further implementations like the operatable ERC721
   * @param _owner address of the owner to query the approval of
   * @param _tokenId uint256 ID of the token to query the approval of
   * @return bool whether the msg.sender is approved for the given token ID or not
   */
  function isApprovedFor(address _owner, uint256 _tokenId) internal view returns (bool) {
    return approvedFor(_tokenId) == _owner;
  }

  /**
  * @dev Internal function to clear current approval and transfer the ownership of a given token ID
  * @param _from address which you want to send tokens from
  * @param _to address which you want to transfer the token to
  * @param _tokenId uint256 ID of the token to be transferred
  */
  function clearApprovalAndTransfer(address _from, address _to, uint256 _tokenId) internal {
    require(_to != address(0));
    require(_to != ownerOf(_tokenId));
    require(ownerOf(_tokenId) == _from);

    clearApproval(_from, _tokenId);
    removeToken(_from, _tokenId);
    addToken(_to, _tokenId);
    Transfer(_from, _to, _tokenId);
  }

  /**
  * @dev Internal function to clear current approval of a given token ID
  * @param _tokenId uint256 ID of the token to be transferred
  */
  function clearApproval(address _owner, uint256 _tokenId) private {
    require(ownerOf(_tokenId) == _owner);
    tokenApprovals[_tokenId] = 0;
    Approval(_owner, 0, _tokenId);
  }

  /**
  * @dev Internal function to add a token ID to the list of a given address
  * @param _to address representing the new owner of the given token ID
  * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
  */
  function addToken(address _to, uint256 _tokenId) private {
    require(tokenOwner[_tokenId] == address(0));
    tokenOwner[_tokenId] = _to;
    uint256 length = balanceOf(_to);
    ownedTokens[_to].push(_tokenId);
    ownedTokensIndex[_tokenId] = length;
    totalTokens = totalTokens.add(1);
  }

  /**
  * @dev Internal function to remove a token ID from the list of a given address
  * @param _from address representing the previous owner of the given token ID
  * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
  */
  function removeToken(address _from, uint256 _tokenId) private {
    require(ownerOf(_tokenId) == _from);

    uint256 tokenIndex = ownedTokensIndex[_tokenId];
    uint256 lastTokenIndex = balanceOf(_from).sub(1);
    uint256 lastToken = ownedTokens[_from][lastTokenIndex];

    tokenOwner[_tokenId] = 0;
    ownedTokens[_from][tokenIndex] = lastToken;
    ownedTokens[_from][lastTokenIndex] = 0;
    // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
    // be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
    // the lastToken to the first position, and then dropping the element placed in the last position of the list

    ownedTokens[_from].length--;
    ownedTokensIndex[_tokenId] = 0;
    ownedTokensIndex[lastToken] = tokenIndex;
    totalTokens = totalTokens.sub(1);
  }
}


/**
 * @title ERC20Basic
 * @dev Simpler version of ERC20 interface
 * @dev see https://github.com/ethereum/EIPs/issues/179
 */
contract ERC20Basic {
  function totalSupply() public view returns (uint256);
  function balanceOf(address who) public view returns (uint256);
  function transfer(address to, uint256 value) public returns (bool);
  event Transfer(address indexed from, address indexed to, uint256 value);
}


/**
 * @title ERC20 interface
 * @dev see https://github.com/ethereum/EIPs/issues/20
 */
contract ERC20 is ERC20Basic {
  function allowance(address owner, address spender) public view returns (uint256);
  function transferFrom(address from, address to, uint256 value) public returns (bool);
  function approve(address spender, uint256 value) public returns (bool);
  event Approval(address indexed owner, address indexed spender, uint256 value);
}


/**
 * @title Basic token
 * @dev Basic version of StandardToken, with no allowances.
 */
contract BasicToken is ERC20Basic {
  using SafeMath for uint256;

  mapping(address => uint256) balances;

  uint256 totalSupply_;

  /**
  * @dev total number of tokens in existence
  */
  function totalSupply() public view returns (uint256) {
    return totalSupply_;
  }

  /**
  * @dev transfer token for a specified address
  * @param _to The address to transfer to.
  * @param _value The amount to be transferred.
  */
  function transfer(address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[msg.sender]);

    // SafeMath.sub will throw if there is not enough balance.
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    Transfer(msg.sender, _to, _value);
    return true;
  }

  /**
  * @dev Gets the balance of the specified address.
  * @param _owner The address to query the the balance of.
  * @return An uint256 representing the amount owned by the passed address.
  */
  function balanceOf(address _owner) public view returns (uint256 balance) {
    return balances[_owner];
  }

}


/**
 * @title Standard ERC20 token
 *
 * @dev Implementation of the basic standard token.
 * @dev https://github.com/ethereum/EIPs/issues/20
 * @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
 */
contract StandardToken is ERC20, BasicToken {

  mapping (address => mapping (address => uint256)) internal allowed;


  /**
   * @dev Transfer tokens from one address to another
   * @param _from address The address which you want to send tokens from
   * @param _to address The address which you want to transfer to
   * @param _value uint256 the amount of tokens to be transferred
   */
  function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[_from]);
    require(_value <= allowed[_from][msg.sender]);

    balances[_from] = balances[_from].sub(_value);
    balances[_to] = balances[_to].add(_value);
    allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
    Transfer(_from, _to, _value);
    return true;
  }

  /**
   * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
   *
   * Beware that changing an allowance with this method brings the risk that someone may use both the old
   * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
   * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
   * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
   * @param _spender The address which will spend the funds.
   * @param _value The amount of tokens to be spent.
   */
  function approve(address _spender, uint256 _value) public returns (bool) {
    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
    return true;
  }

  /**
   * @dev Function to check the amount of tokens that an owner allowed to a spender.
   * @param _owner address The address which owns the funds.
   * @param _spender address The address which will spend the funds.
   * @return A uint256 specifying the amount of tokens still available for the spender.
   */
  function allowance(address _owner, address _spender) public view returns (uint256) {
    return allowed[_owner][_spender];
  }

  /**
   * @dev Increase the amount of tokens that an owner allowed to a spender.
   *
   * approve should be called when allowed[_spender] == 0. To increment
   * allowed value is better to use this function to avoid 2 calls (and wait until
   * the first transaction is mined)
   * From MonolithDAO Token.sol
   * @param _spender The address which will spend the funds.
   * @param _addedValue The amount of tokens to increase the allowance by.
   */
  function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
    allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
    Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

  /**
   * @dev Decrease the amount of tokens that an owner allowed to a spender.
   *
   * approve should be called when allowed[_spender] == 0. To decrement
   * allowed value is better to use this function to avoid 2 calls (and wait until
   * the first transaction is mined)
   * From MonolithDAO Token.sol
   * @param _spender The address which will spend the funds.
   * @param _subtractedValue The amount of tokens to decrease the allowance by.
   */
  function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
    uint oldValue = allowed[msg.sender][_spender];
    if (_subtractedValue > oldValue) {
      allowed[msg.sender][_spender] = 0;
    } else {
      allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
    }
    Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

}


/**
 * @title AccessDeposit
 * @dev Adds grant/revoke functions to the contract.
 */
contract AccessDeposit is Claimable {

  // Access for adding deposit.
  mapping(address => bool) private depositAccess;

  // Modifier for accessibility to add deposit.
  modifier onlyAccessDeposit {
    require(msg.sender == owner || depositAccess[msg.sender] == true);
    _;
  }

  // @dev Grant acess to deposit heroes.
  function grantAccessDeposit(address _address)
    onlyOwner
    public
  {
    depositAccess[_address] = true;
  }

  // @dev Revoke acess to deposit heroes.
  function revokeAccessDeposit(address _address)
    onlyOwner
    public
  {
    depositAccess[_address] = false;
  }

}


/**
 * @title AccessDeploy
 * @dev Adds grant/revoke functions to the contract.
 */
contract AccessDeploy is Claimable {

  // Access for deploying heroes.
  mapping(address => bool) private deployAccess;

  // Modifier for accessibility to deploy a hero on a location.
  modifier onlyAccessDeploy {
    require(msg.sender == owner || deployAccess[msg.sender] == true);
    _;
  }

  // @dev Grant acess to deploy heroes.
  function grantAccessDeploy(address _address)
    onlyOwner
    public
  {
    deployAccess[_address] = true;
  }

  // @dev Revoke acess to deploy heroes.
  function revokeAccessDeploy(address _address)
    onlyOwner
    public
  {
    deployAccess[_address] = false;
  }

}

/**
 * @title AccessMint
 * @dev Adds grant/revoke functions to the contract.
 */
contract AccessMint is Claimable {

  // Access for minting new tokens.
  mapping(address => bool) private mintAccess;

  // Modifier for accessibility to define new hero types.
  modifier onlyAccessMint {
    require(msg.sender == owner || mintAccess[msg.sender] == true);
    _;
  }

  // @dev Grant acess to mint heroes.
  function grantAccessMint(address _address)
    onlyOwner
    public
  {
    mintAccess[_address] = true;
  }

  // @dev Revoke acess to mint heroes.
  function revokeAccessMint(address _address)
    onlyOwner
    public
  {
    mintAccess[_address] = false;
  }

}


/**
 * @title Gold
 * @dev ERC20 Token that can be minted.
 */
contract Gold is StandardToken, Claimable, AccessMint {

  string public constant name = "Gold";
  string public constant symbol = "G";
  uint8 public constant decimals = 18;

  // Event that is fired when minted.
  event Mint(
    address indexed _to,
    uint256 indexed _tokenId
  );

  // @dev Mint tokens with _amount to the address.
  function mint(address _to, uint256 _amount) 
    onlyAccessMint
    public 
    returns (bool) 
  {
    totalSupply_ = totalSupply_.add(_amount);
    balances[_to] = balances[_to].add(_amount);
    Mint(_to, _amount);
    Transfer(address(0), _to, _amount);
    return true;
  }

}


/**
 * @title CryptoSaga Card
 * @dev ERC721 Token that repesents CryptoSaga's cards.
 *  Buy consuming a card, players of CryptoSaga can get a heroe.
 */
contract CryptoSagaCard is ERC721Token, Claimable, AccessMint {

  string public constant name = "CryptoSaga Card";
  string public constant symbol = "CARD";

  // Rank of the token.
  mapping(uint256 => uint8) public tokenIdToRank;

  // The number of tokens ever minted.
  uint256 public numberOfTokenId;

  // The converter contract.
  CryptoSagaCardSwap private swapContract;

  // Event that should be fired when card is converted.
  event CardSwap(address indexed _by, uint256 _tokenId, uint256 _rewardId);

  // @dev Set the address of the contract that represents CryptoSaga Cards.
  function setCryptoSagaCardSwapContract(address _contractAddress)
    public
    onlyOwner
  {
    swapContract = CryptoSagaCardSwap(_contractAddress);
  }

  function rankOf(uint256 _tokenId) 
    public view
    returns (uint8)
  {
    return tokenIdToRank[_tokenId];
  }

  // @dev Mint a new card.
  function mint(address _beneficiary, uint256 _amount, uint8 _rank)
    onlyAccessMint
    public
  {
    for (uint256 i = 0; i < _amount; i++) {
      _mint(_beneficiary, numberOfTokenId);
      tokenIdToRank[numberOfTokenId] = _rank;
      numberOfTokenId ++;
    }
  }

  // @dev Swap this card for reward.
  //  The card will be burnt.
  function swap(uint256 _tokenId)
    onlyOwnerOf(_tokenId)
    public
    returns (uint256)
  {
    require(address(swapContract) != address(0));

    var _rank = tokenIdToRank[_tokenId];
    var _rewardId = swapContract.swapCardForReward(this, _rank);
    CardSwap(ownerOf(_tokenId), _tokenId, _rewardId);
    _burn(_tokenId);
    return _rewardId;
  }

}


/**
 * @title The interface contract for Card-For-Hero swap functionality.
 * @dev With this contract, a card holder can swap his/her CryptoSagaCard for reward.
 *  This contract is intended to be inherited by CryptoSagaCardSwap implementation contracts.
 */
contract CryptoSagaCardSwap is Ownable {

  // Card contract.
  address internal cardAddess;

  // Modifier for accessibility to define new hero types.
  modifier onlyCard {
    require(msg.sender == cardAddess);
    _;
  }
  
  // @dev Set the address of the contract that represents ERC721 Card.
  function setCardContract(address _contractAddress)
    public
    onlyOwner
  {
    cardAddess = _contractAddress;
  }

  // @dev Convert card into reward.
  //  This should be implemented by CryptoSagaCore later.
  function swapCardForReward(address _by, uint8 _rank)
    onlyCard
    public 
    returns (uint256);

}


/**
 * @title CryptoSagaHero
 * @dev The token contract for the hero.
 *  Also a superset of the ERC721 standard that allows for the minting
 *  of the non-fungible tokens.
 */
contract CryptoSagaHero is ERC721Token, Claimable, Pausable, AccessMint, AccessDeploy, AccessDeposit {

  string public constant name = "CryptoSaga Hero";
  string public constant symbol = "HERO";
  
  struct HeroClass {
    // ex) Soldier, Knight, Fighter...
    string className;
    // 0: Common, 1: Uncommon, 2: Rare, 3: Heroic, 4: Legendary.
    uint8 classRank;
    // 0: Human, 1: Celestial, 2: Demon, 3: Elf, 4: Dark Elf, 5: Yogoe, 6: Furry, 7: Dragonborn, 8: Undead, 9: Goblin, 10: Troll, 11: Slime, and more to come.
    uint8 classRace;
    // How old is this hero class? 
    uint32 classAge;
    // 0: Fighter, 1: Rogue, 2: Mage.
    uint8 classType;

    // Possible max level of this class.
    uint32 maxLevel; 
    // 0: Water, 1: Fire, 2: Nature, 3: Light, 4: Darkness.
    uint8 aura; 

    // Base stats of this hero type. 
    // 0: ATK	1: DEF 2: AGL	3: LUK 4: HP.
    uint32[5] baseStats;
    // Minimum IVs for stats. 
    // 0: ATK	1: DEF 2: AGL	3: LUK 4: HP.
    uint32[5] minIVForStats;
    // Maximum IVs for stats.
    // 0: ATK	1: DEF 2: AGL	3: LUK 4: HP.
    uint32[5] maxIVForStats;
    
    // Number of currently instanced heroes.
    uint32 currentNumberOfInstancedHeroes;
  }
    
  struct HeroInstance {
    // What is this hero's type? ex) John, Sally, Mark...
    uint32 heroClassId;
    
    // Individual hero's name.
    string heroName;
    
    // Current level of this hero.
    uint32 currentLevel;
    // Current exp of this hero.
    uint32 currentExp;

    // Where has this hero been deployed? (0: Never depolyed ever.) ex) Dungeon Floor #1, Arena #5...
    uint32 lastLocationId;
    // When a hero is deployed, it takes time for the hero to return to the base. This is in Unix epoch.
    uint256 availableAt;

    // Current stats of this hero. 
    // 0: ATK	1: DEF 2: AGL	3: LUK 4: HP.
    uint32[5] currentStats;
    // The individual value for this hero's stats. 
    // This will affect the current stats of heroes.
    // 0: ATK	1: DEF 2: AGL	3: LUK 4: HP.
    uint32[5] ivForStats;
  }

  // Required exp for level up will increase when heroes level up.
  // This defines how the value will increase.
  uint32 public requiredExpIncreaseFactor = 100;

  // Required Gold for level up will increase when heroes level up.
  // This defines how the value will increase.
  uint256 public requiredGoldIncreaseFactor = 1000000000000000000;

  // Existing hero classes.
  mapping(uint32 => HeroClass) public heroClasses;
  // The number of hero classes ever defined.
  uint32 public numberOfHeroClasses;

  // Existing hero instances.
  // The key is _tokenId.
  mapping(uint256 => HeroInstance) public tokenIdToHeroInstance;
  // The number of tokens ever minted. This works as the serial number.
  uint256 public numberOfTokenIds;

  // Gold contract.
  Gold public goldContract;

  // Deposit of players (in Gold).
  mapping(address => uint256) public addressToGoldDeposit;

  // Random seed.
  uint32 private seed = 0;

  // Event that is fired when a hero type defined.
  event DefineType(
    address indexed _by,
    uint32 indexed _typeId,
    string _className
  );

  // Event that is fired when a hero is upgraded.
  event LevelUp(
    address indexed _by,
    uint256 indexed _tokenId,
    uint32 _newLevel
  );

  // Event that is fired when a hero is deployed.
  event Deploy(
    address indexed _by,
    uint256 indexed _tokenId,
    uint32 _locationId,
    uint256 _duration
  );

  // @dev Get the class's entire infomation.
  function getClassInfo(uint32 _classId)
    external view
    returns (string className, uint8 classRank, uint8 classRace, uint32 classAge, uint8 classType, uint32 maxLevel, uint8 aura, uint32[5] baseStats, uint32[5] minIVs, uint32[5] maxIVs) 
  {
    var _cl = heroClasses[_classId];
    return (_cl.className, _cl.classRank, _cl.classRace, _cl.classAge, _cl.classType, _cl.maxLevel, _cl.aura, _cl.baseStats, _cl.minIVForStats, _cl.maxIVForStats);
  }

  // @dev Get the class's name.
  function getClassName(uint32 _classId)
    external view
    returns (string)
  {
    return heroClasses[_classId].className;
  }

  // @dev Get the class's rank.
  function getClassRank(uint32 _classId)
    external view
    returns (uint8)
  {
    return heroClasses[_classId].classRank;
  }

  // @dev Get the heroes ever minted for the class.
  function getClassMintCount(uint32 _classId)
    external view
    returns (uint32)
  {
    return heroClasses[_classId].currentNumberOfInstancedHeroes;
  }

  // @dev Get the hero's entire infomation.
  function getHeroInfo(uint256 _tokenId)
    external view
    returns (uint32 classId, string heroName, uint32 currentLevel, uint32 currentExp, uint32 lastLocationId, uint256 availableAt, uint32[5] currentStats, uint32[5] ivs, uint32 bp)
  {
    HeroInstance memory _h = tokenIdToHeroInstance[_tokenId];
    var _bp = _h.currentStats[0] + _h.currentStats[1] + _h.currentStats[2] + _h.currentStats[3] + _h.currentStats[4];
    return (_h.heroClassId, _h.heroName, _h.currentLevel, _h.currentExp, _h.lastLocationId, _h.availableAt, _h.currentStats, _h.ivForStats, _bp);
  }

  // @dev Get the hero's class id.
  function getHeroClassId(uint256 _tokenId)
    external view
    returns (uint32)
  {
    return tokenIdToHeroInstance[_tokenId].heroClassId;
  }

  // @dev Get the hero's name.
  function getHeroName(uint256 _tokenId)
    external view
    returns (string)
  {
    return tokenIdToHeroInstance[_tokenId].heroName;
  }

  // @dev Get the hero's level.
  function getHeroLevel(uint256 _tokenId)
    external view
    returns (uint32)
  {
    return tokenIdToHeroInstance[_tokenId].currentLevel;
  }
  
  // @dev Get the hero's location.
  function getHeroLocation(uint256 _tokenId)
    external view
    returns (uint32)
  {
    return tokenIdToHeroInstance[_tokenId].lastLocationId;
  }

  // @dev Get the time when the hero become available.
  function getHeroAvailableAt(uint256 _tokenId)
    external view
    returns (uint256)
  {
    return tokenIdToHeroInstance[_tokenId].availableAt;
  }

  // @dev Get the hero's BP.
  function getHeroBP(uint256 _tokenId)
    public view
    returns (uint32)
  {
    var _tmp = tokenIdToHeroInstance[_tokenId].currentStats;
    return (_tmp[0] + _tmp[1] + _tmp[2] + _tmp[3] + _tmp[4]);
  }

  // @dev Get the hero's required gold for level up.
  function getHeroRequiredGoldForLevelUp(uint256 _tokenId)
    public view
    returns (uint256)
  {
    return (uint256(2) ** (tokenIdToHeroInstance[_tokenId].currentLevel / 10)) * requiredGoldIncreaseFactor;
  }

  // @dev Get the hero's required exp for level up.
  function getHeroRequiredExpForLevelUp(uint256 _tokenId)
    public view
    returns (uint32)
  {
    return ((tokenIdToHeroInstance[_tokenId].currentLevel + 2) * requiredExpIncreaseFactor);
  }

  // @dev Get the deposit of gold of the player.
  function getGoldDepositOfAddress(address _address)
    external view
    returns (uint256)
  {
    return addressToGoldDeposit[_address];
  }

  // @dev Get the token id of the player's #th token.
  function getTokenIdOfAddressAndIndex(address _address, uint256 _index)
    external view
    returns (uint256)
  {
    return tokensOf(_address)[_index];
  }

  // @dev Get the total BP of the player.
  function getTotalBPOfAddress(address _address)
    external view
    returns (uint32)
  {
    var _tokens = tokensOf(_address);
    uint32 _totalBP = 0;
    for (uint256 i = 0; i < _tokens.length; i ++) {
      _totalBP += getHeroBP(_tokens[i]);
    }
    return _totalBP;
  }

  // @dev Set the hero's name.
  function setHeroName(uint256 _tokenId, string _name)
    onlyOwnerOf(_tokenId)
    public
  {
    tokenIdToHeroInstance[_tokenId].heroName = _name;
  }

  // @dev Set the address of the contract that represents ERC20 Gold.
  function setGoldContract(address _contractAddress)
    onlyOwner
    public
  {
    goldContract = Gold(_contractAddress);
  }

  // @dev Set the required golds to level up a hero.
  function setRequiredExpIncreaseFactor(uint32 _value)
    onlyOwner
    public
  {
    requiredExpIncreaseFactor = _value;
  }

  // @dev Set the required golds to level up a hero.
  function setRequiredGoldIncreaseFactor(uint256 _value)
    onlyOwner
    public
  {
    requiredGoldIncreaseFactor = _value;
  }

  // @dev Contructor.
  function CryptoSagaHero(address _goldAddress)
    public
  {
    require(_goldAddress != address(0));

    // Assign Gold contract.
    setGoldContract(_goldAddress);

    // Initial heroes.
    // Name, Rank, Race, Age, Type, Max Level, Aura, Stats.
    defineType("Archangel", 4, 1, 13540, 0, 99, 3, [uint32(74), 75, 57, 99, 95], [uint32(8), 6, 8, 5, 5], [uint32(8), 10, 10, 6, 6]);
    defineType("Shadowalker", 3, 4, 134, 1, 75, 4, [uint32(45), 35, 60, 80, 40], [uint32(3), 2, 10, 4, 5], [uint32(5), 5, 10, 7, 5]);
    defineType("Pyromancer", 2, 0, 14, 2, 50, 1, [uint32(50), 28, 17, 40, 35], [uint32(5), 3, 2, 3, 3], [uint32(8), 4, 3, 4, 5]);
    defineType("Magician", 1, 3, 224, 2, 30, 0, [uint32(35), 15, 25, 25, 30], [uint32(3), 1, 2, 2, 2], [uint32(5), 2, 3, 3, 3]);
    defineType("Farmer", 0, 0, 59, 0, 15, 2, [uint32(10), 22, 8, 15, 25], [uint32(1), 2, 1, 1, 2], [uint32(1), 3, 1, 2, 3]);
  }

  // @dev Define a new hero type (class).
  function defineType(string _className, uint8 _classRank, uint8 _classRace, uint32 _classAge, uint8 _classType, uint32 _maxLevel, uint8 _aura, uint32[5] _baseStats, uint32[5] _minIVForStats, uint32[5] _maxIVForStats)
    onlyOwner
    public
  {
    require(_classRank < 5);
    require(_classType < 3);
    require(_aura < 5);
    require(_minIVForStats[0] <= _maxIVForStats[0] && _minIVForStats[1] <= _maxIVForStats[1] && _minIVForStats[2] <= _maxIVForStats[2] && _minIVForStats[3] <= _maxIVForStats[3] && _minIVForStats[4] <= _maxIVForStats[4]);

    HeroClass memory _heroType = HeroClass({
      className: _className,
      classRank: _classRank,
      classRace: _classRace,
      classAge: _classAge,
      classType: _classType,
      maxLevel: _maxLevel,
      aura: _aura,
      baseStats: _baseStats,
      minIVForStats: _minIVForStats,
      maxIVForStats: _maxIVForStats,
      currentNumberOfInstancedHeroes: 0
    });

    // Save the hero class.
    heroClasses[numberOfHeroClasses] = _heroType;

    // Fire event.
    DefineType(msg.sender, numberOfHeroClasses, _heroType.className);

    // Increment number of hero classes.
    numberOfHeroClasses ++;

  }

  // @dev Mint a new hero, with _heroClassId.
  function mint(address _owner, uint32 _heroClassId)
    onlyAccessMint
    public
    returns (uint256)
  {
    require(_owner != address(0));
    require(_heroClassId < numberOfHeroClasses);

    // The information of the hero's class.
    var _heroClassInfo = heroClasses[_heroClassId];

    // Mint ERC721 token.
    _mint(_owner, numberOfTokenIds);

    // Build random IVs for this hero instance.
    uint32[5] memory _ivForStats;
    uint32[5] memory _initialStats;
    for (uint8 i = 0; i < 5; i++) {
      _ivForStats[i] = (random(_heroClassInfo.maxIVForStats[i] + 1, _heroClassInfo.minIVForStats[i]));
      _initialStats[i] = _heroClassInfo.baseStats[i] + _ivForStats[i];
    }

    // Temporary hero instance.
    HeroInstance memory _heroInstance = HeroInstance({
      heroClassId: _heroClassId,
      heroName: "",
      currentLevel: 1,
      currentExp: 0,
      lastLocationId: 0,
      availableAt: now,
      currentStats: _initialStats,
      ivForStats: _ivForStats
    });

    // Save the hero instance.
    tokenIdToHeroInstance[numberOfTokenIds] = _heroInstance;

    // Increment number of token ids.
    // This will only increment when new token is minted, and will never be decemented when the token is burned.
    numberOfTokenIds ++;

     // Increment instanced number of heroes.
    _heroClassInfo.currentNumberOfInstancedHeroes ++;

    return numberOfTokenIds - 1;
  }

  // @dev Set where the heroes are deployed, and when they will return.
  //  This is intended to be called by Dungeon, Arena, Guild contracts.
  function deploy(uint256 _tokenId, uint32 _locationId, uint256 _duration)
    onlyAccessDeploy
    public
    returns (bool)
  {
    // The hero should be possessed by anybody.
    require(ownerOf(_tokenId) != address(0));

    var _heroInstance = tokenIdToHeroInstance[_tokenId];

    // The character should be avaiable. 
    require(_heroInstance.availableAt <= now);

    _heroInstance.lastLocationId = _locationId;
    _heroInstance.availableAt = now + _duration;

    // As the hero has been deployed to another place, fire event.
    Deploy(msg.sender, _tokenId, _locationId, _duration);
  }

  // @dev Add exp.
  //  This is intended to be called by Dungeon, Arena, Guild contracts.
  function addExp(uint256 _tokenId, uint32 _exp)
    onlyAccessDeploy
    public
    returns (bool)
  {
    // The hero should be possessed by anybody.
    require(ownerOf(_tokenId) != address(0));

    var _heroInstance = tokenIdToHeroInstance[_tokenId];

    var _newExp = _heroInstance.currentExp + _exp;

    // Sanity check to ensure we don't overflow.
    require(_newExp == uint256(uint128(_newExp)));

    _heroInstance.currentExp += _newExp;

  }

  // @dev Add deposit.
  //  This is intended to be called by Dungeon, Arena, Guild contracts.
  function addDeposit(address _to, uint256 _amount)
    onlyAccessDeposit
    public
  {
    // Increment deposit.
    addressToGoldDeposit[_to] += _amount;
  }

  // @dev Level up the hero with _tokenId.
  //  This function is called by the owner of the hero.
  function levelUp(uint256 _tokenId)
    onlyOwnerOf(_tokenId) whenNotPaused
    public
  {

    // Hero instance.
    var _heroInstance = tokenIdToHeroInstance[_tokenId];

    // The character should be avaiable. (Should have already returned from the dungeons, arenas, etc.)
    require(_heroInstance.availableAt <= now);

    // The information of the hero's class.
    var _heroClassInfo = heroClasses[_heroInstance.heroClassId];

    // Hero shouldn't level up exceed its max level.
    require(_heroInstance.currentLevel < _heroClassInfo.maxLevel);

    // Required Exp.
    var requiredExp = getHeroRequiredExpForLevelUp(_tokenId);

    // Need to have enough exp.
    require(_heroInstance.currentExp >= requiredExp);

    // Required Gold.
    var requiredGold = getHeroRequiredGoldForLevelUp(_tokenId);

    // Owner of token.
    var _ownerOfToken = ownerOf(_tokenId);

    // Need to have enough Gold balance.
    require(addressToGoldDeposit[_ownerOfToken] >= requiredGold);

    // Increase Level.
    _heroInstance.currentLevel += 1;

    // Increase Stats.
    for (uint8 i = 0; i < 5; i++) {
      _heroInstance.currentStats[i] = _heroClassInfo.baseStats[i] + (_heroInstance.currentLevel - 1) * _heroInstance.ivForStats[i];
    }
    
    // Deduct exp.
    _heroInstance.currentExp -= requiredExp;

    // Deduct gold.
    addressToGoldDeposit[_ownerOfToken] -= requiredGold;

    // Fire event.
    LevelUp(msg.sender, _tokenId, _heroInstance.currentLevel);
  }

  // @dev Transfer deposit (with the allowance pattern.)
  function transferDeposit(uint256 _amount)
    whenNotPaused
    public
  {
    require(goldContract.allowance(msg.sender, this) >= _amount);

    // Send msg.sender's Gold to this contract.
    if (goldContract.transferFrom(msg.sender, this, _amount)) {
       // Increment deposit.
      addressToGoldDeposit[msg.sender] += _amount;
    }
  }

  // @dev Withdraw deposit.
  function withdrawDeposit(uint256 _amount)
    public
  {
    require(addressToGoldDeposit[msg.sender] >= _amount);

    // Send deposit of Golds to msg.sender. (Rather minting...)
    if (goldContract.transfer(msg.sender, _amount)) {
      // Decrement deposit.
      addressToGoldDeposit[msg.sender] -= _amount;
    }
  }

  // @dev return a pseudo random number between lower and upper bounds
  function random(uint32 _upper, uint32 _lower)
    private
    returns (uint32)
  {
    require(_upper > _lower);

    seed = uint32(keccak256(keccak256(block.blockhash(block.number), seed), now));
    return seed % (_upper - _lower) + _lower;
  }

}


/**
 * @title CryptoSagaCorrectedHeroStats
 * @dev Corrected hero stats is needed to fix the bug in hero stats.
 */
contract CryptoSagaCorrectedHeroStats {

  // The hero contract.
  CryptoSagaHero private heroContract;

  // @dev Constructor.
  function CryptoSagaCorrectedHeroStats(address _heroContractAddress)
    public
  {
    heroContract = CryptoSagaHero(_heroContractAddress);
  }

  // @dev Get the hero's stats and some other infomation.
  function getCorrectedStats(uint256 _tokenId)
    external view
    returns (uint32 currentLevel, uint32 currentExp, uint32[5] currentStats, uint32[5] ivs, uint32 bp)
  {
    var (, , _currentLevel, _currentExp, , , _currentStats, _ivs, ) = heroContract.getHeroInfo(_tokenId);
    
    if (_currentLevel != 1) {
      for (uint8 i = 0; i < 5; i ++) {
        _currentStats[i] += _ivs[i];
      }
    }

    var _bp = _currentStats[0] + _currentStats[1] + _currentStats[2] + _currentStats[3] + _currentStats[4];
    return (_currentLevel, _currentExp, _currentStats, _ivs, _bp);
  }

  // @dev Get corrected total BP of the address.
  function getCorrectedTotalBPOfAddress(address _address)
    external view
    returns (uint32)
  {
    var _balance = heroContract.balanceOf(_address);

    uint32 _totalBP = 0;

    for (uint256 i = 0; i < _balance; i ++) {
      var (, , _currentLevel, , , , _currentStats, _ivs, ) = heroContract.getHeroInfo(heroContract.getTokenIdOfAddressAndIndex(_address, i));
      if (_currentLevel != 1) {
        for (uint8 j = 0; j < 5; j ++) {
          _currentStats[j] += _ivs[j];
        }
      }
      _totalBP += (_currentStats[0] + _currentStats[1] + _currentStats[2] + _currentStats[3] + _currentStats[4]);
    }

    return _totalBP;
  }

  // @dev Get corrected total BP of the address.
  function getCorrectedTotalBPOfTokens(uint256[] _tokens)
    external view
    returns (uint32)
  {
    uint32 _totalBP = 0;

    for (uint256 i = 0; i < _tokens.length; i ++) {
      var (, , _currentLevel, , , , _currentStats, _ivs, ) = heroContract.getHeroInfo(_tokens[i]);
      if (_currentLevel != 1) {
        for (uint8 j = 0; j < 5; j ++) {
          _currentStats[j] += _ivs[j];
        }
      }
      _totalBP += (_currentStats[0] + _currentStats[1] + _currentStats[2] + _currentStats[3] + _currentStats[4]);
    }

    return _totalBP;
  }
}


/**
 * @title CryptoSagaDungeonProgress
 * @dev Storage contract for progress of dungeons.
 */
contract CryptoSagaDungeonProgress is Claimable, AccessDeploy {

  // The progress of the player in dungeons.
  mapping(address => uint32[25]) public addressToProgress;

  // @dev Get progress.
  function getProgressOfAddressAndId(address _address, uint32 _id)
    external view
    returns (uint32)
  {
    var _progressList = addressToProgress[_address];
    return _progressList[_id];
  }

  // @dev Increment progress.
  function incrementProgressOfAddressAndId(address _address, uint32 _id)
    onlyAccessDeploy
    public
  {
    var _progressList = addressToProgress[_address];
    _progressList[_id]++;
    addressToProgress[_address] = _progressList;
  }
}


/**
 * @title CryptoSagaDungeonVer1
 * @dev The actual gameplay is done by this contract. Version 1.0.1.
 */
contract CryptoSagaDungeonVer1 is Claimable, Pausable {

  struct EnemyCombination {
    // Is non-default combintion?
    bool isPersonalized;
    // Enemy slots' class Id.
    uint32[4] enemySlotClassIds;
  }

  struct PlayRecord {
    // This is needed for reconstructing the record.
    uint32 initialSeed;
    // The progress of the dugoeon when this play record made.
    uint32 progress;
    // Hero's token ids.
    uint256[4] tokenIds;
    // Unit's class ids. 0 ~ 3: Heroes. 4 ~ 7: Mobs.
    uint32[8] unitClassIds;
    // Unit's levels. 0 ~ 3: Heroes. 4 ~ 7: Mobs.
    uint32[8] unitLevels;
    // Exp reward given.
    uint32 expReward;
    // Gold Reward given.
    uint256 goldReward;
  }

  // This information can be reconstructed with seed and dateTime.
  // In order for the optimization this won't be really used.
  struct TurnInfo {
    // Number of turns before a team was vanquished.
    uint8 turnLength;
    // Turn order of units.
    uint8[8] turnOrder;
    // Defender list. (The unit that is attacked.)
    uint8[24] defenderList;
    // Damage list. (The damage given to the defender.)
    uint32[24] damageList;
    // Heroes' original Exps.
    uint32[4] originalExps;
  }

  // Progress contract.
  CryptoSagaDungeonProgress private progressContract;

  // The hero contract.
  CryptoSagaHero private heroContract;

  // Corrected hero stats contract.
  CryptoSagaCorrectedHeroStats private correctedHeroContract;

  // Gold contract.
  Gold public goldContract;

  // Card contract.
  CryptoSagaCard public cardContract;

  // The location Id of this contract.
  // Will be used when calling deploy function of hero contract.
  uint32 public locationId = 0;

  // The dungeon cooldown time. (Default value: 15 mins.)
  uint256 public coolDungeon = 900;

  // Hero cooldown time. (Default value: 60 mins.)
  uint256 public coolHero = 3600;

  // The exp reward when clearing this dungeon.
  uint32 public expReward = 100;

  // The Gold reward when clearing this dungeon.
  uint256 public goldReward = 1000000000000000000;

  // The previous dungeon that should be cleared.
  uint32 public previousDungeonId;

  // The progress of the previous dungeon that should be cleared.
  uint32 public requiredProgressOfPreviousDungeon;

  // Turn data save.
  bool public isTurnDataSaved = true;

  // The enemies in this dungeon for the player.
  mapping(address => EnemyCombination) public addressToEnemyCombination;

  // Last game's play datetime.
  mapping(address => uint256) public addressToPlayRecordDateTime;

  // Last game's record of the player.
  mapping(address => PlayRecord) public addressToPlayRecord;

  // Additional information on last game's record of the player.
  mapping(address => TurnInfo) public addressToTurnInfo;

  // List of the Mobs possibly appear in this dungeon.
  uint32[] public possibleMobClasses;

  // Initial enemy combination.
  // This will be shown when there's no play record.
  EnemyCombination public initialEnemyCombination;

  // Random seed.
  uint32 private seed = 0;

  // Event that is fired when a player try to clear this dungeon.
  event TryDungeon(
    address indexed _by,
    uint32 _tryingProgress,
    uint32 _progress,
    bool _isSuccess
  );

  // @dev Get enemy combination.
  function getEnemyCombinationOfAddress(address _address)
    external view
    returns (uint32[4])
  {
    // Retrieve enemy information.
    // Instead of null check, isPersonalized check will tell the personalized mobs for this player exist.
    var _enemyCombination = addressToEnemyCombination[_address];
    if (_enemyCombination.isPersonalized == false) {
      // Then let's use default value.
      _enemyCombination = initialEnemyCombination;
    }
    return _enemyCombination.enemySlotClassIds;
  }

  // @dev Get initial enemy combination.
  function getInitialEnemyCombination()
    external view
    returns (uint32[4])
  {
    return initialEnemyCombination.enemySlotClassIds;
  }

  // @dev Get play record's datetime.
  function getLastPlayDateTime(address _address)
    external view
    returns (uint256 dateTime)
  {
    return addressToPlayRecordDateTime[_address];
  }

  // @dev Get previous game record.
  function getPlayRecord(address _address)
    external view
    returns (uint32 initialSeed, uint32 progress, uint256[4] heroTokenIds, uint32[8] uintClassIds, uint32[8] unitLevels, uint32 expRewardGiven, uint256 goldRewardGiven, uint8 turnLength, uint8[8] turnOrder, uint8[24] defenderList, uint32[24] damageList)
  {
    PlayRecord memory _p = addressToPlayRecord[_address];
    TurnInfo memory _t = addressToTurnInfo[_address];
    return (_p.initialSeed, _p.progress, _p.tokenIds, _p.unitClassIds, _p.unitLevels, _p.expReward, _p.goldReward, _t.turnLength, _t.turnOrder, _t.defenderList, _t.damageList);
  }

  // @dev Get previous game record.
  function getPlayRecordNoTurnData(address _address)
    external view
    returns (uint32 initialSeed, uint32 progress, uint256[4] heroTokenIds, uint32[8] uintClassIds, uint32[8] unitLevels, uint32 expRewardGiven, uint256 goldRewardGiven)
  {
    PlayRecord memory _p = addressToPlayRecord[_address];
    return (_p.initialSeed, _p.progress, _p.tokenIds, _p.unitClassIds, _p.unitLevels, _p.expReward, _p.goldReward);
  }

  // @dev Set location id.
  function setLocationId(uint32 _value)
    onlyOwner
    public
  {
    locationId = _value;
  }

  // @dev Set cooldown of this dungeon.
  function setCoolDungeon(uint32 _value)
    onlyOwner
    public
  {
    coolDungeon = _value;
  }

  // @dev Set cooldown of heroes entered this dungeon.
  function setCoolHero(uint32 _value)
    onlyOwner
    public
  {
    coolHero = _value;
  }

  // @dev Set the Exp given to the player when clearing this dungeon.
  function setExpReward(uint32 _value)
    onlyOwner
    public
  {
    expReward = _value;
  }

  // @dev Set the Golds given to the player when clearing this dungeon.
  function setGoldReward(uint256 _value)
    onlyOwner
    public
  {
    goldReward = _value;
  }

  // @dev Set wether the turn data saved or not.
  function setIsTurnDataSaved(bool _value)
    onlyOwner
    public
  {
    isTurnDataSaved = _value;
  }

  // @dev Set initial enemy combination.
  function setInitialEnemyCombination(uint32[4] _enemySlotClassIds)
    onlyOwner
    public
  {
    initialEnemyCombination.isPersonalized = false;
    initialEnemyCombination.enemySlotClassIds = _enemySlotClassIds;
  }

  // @dev Set previous dungeon.
  function setPreviousDungeoonId(uint32 _dungeonId)
    onlyOwner
    public
  {
    previousDungeonId = _dungeonId;
  }

  // @dev Set required progress of previous dungeon.
  function setRequiredProgressOfPreviousDungeon(uint32 _progress)
    onlyOwner
    public
  {
    requiredProgressOfPreviousDungeon = _progress;
  }

  // @dev Set possible mobs in this dungeon.
  function setPossibleMobs(uint32[] _classIds)
    onlyOwner
    public
  {
    possibleMobClasses = _classIds;
  }

  // @dev Constructor.
  function CryptoSagaDungeonVer1(address _progressAddress, address _heroContractAddress, address _correctedHeroContractAddress, address _cardContractAddress, address _goldContractAddress, uint32 _locationId, uint256 _coolDungeon, uint256 _coolHero, uint32 _expReward, uint256 _goldReward, uint32 _previousDungeonId, uint32 _requiredProgressOfPreviousDungeon, uint32[4] _enemySlotClassIds, bool _isTurnDataSaved)
    public
  {
    progressContract = CryptoSagaDungeonProgress(_progressAddress);
    heroContract = CryptoSagaHero(_heroContractAddress);
    correctedHeroContract = CryptoSagaCorrectedHeroStats(_correctedHeroContractAddress);
    cardContract = CryptoSagaCard(_cardContractAddress);
    goldContract = Gold(_goldContractAddress);
    
    locationId = _locationId;
    coolDungeon = _coolDungeon;
    coolHero = _coolHero;
    expReward = _expReward;
    goldReward = _goldReward;

    previousDungeonId = _previousDungeonId;
    requiredProgressOfPreviousDungeon = _requiredProgressOfPreviousDungeon;

    initialEnemyCombination.isPersonalized = false;
    initialEnemyCombination.enemySlotClassIds = _enemySlotClassIds;
    
    isTurnDataSaved = _isTurnDataSaved;
  }
  
  // @dev Enter this dungeon.
  function enterDungeon(uint256[4] _tokenIds, uint32 _tryingProgress)
    whenNotPaused
    public
  {
    // Each hero should be different ids.
    require(_tokenIds[0] == 0 || (_tokenIds[0] != _tokenIds[1] && _tokenIds[0] != _tokenIds[2] && _tokenIds[0] != _tokenIds[3]));
    require(_tokenIds[1] == 0 || (_tokenIds[1] != _tokenIds[0] && _tokenIds[1] != _tokenIds[2] && _tokenIds[1] != _tokenIds[3]));
    require(_tokenIds[2] == 0 || (_tokenIds[2] != _tokenIds[0] && _tokenIds[2] != _tokenIds[1] && _tokenIds[2] != _tokenIds[3]));
    require(_tokenIds[3] == 0 || (_tokenIds[3] != _tokenIds[0] && _tokenIds[3] != _tokenIds[1] && _tokenIds[3] != _tokenIds[2]));

    // Check the previous dungeon's progress.
    if (requiredProgressOfPreviousDungeon != 0) {
      require(progressContract.getProgressOfAddressAndId(msg.sender, previousDungeonId) >= requiredProgressOfPreviousDungeon);
    }

    // 1 is the minimum prgress.
    require(_tryingProgress > 0);

    // Only up to 'progress + 1' is allowed.
    require(_tryingProgress <= progressContract.getProgressOfAddressAndId(msg.sender, locationId) + 1);

    // Check dungeon availability.
    require(addressToPlayRecordDateTime[msg.sender] + coolDungeon <= now);

    // Check ownership and availability check.
    require(checkOwnershipAndAvailability(msg.sender, _tokenIds));

    // Set play record datetime.
    addressToPlayRecordDateTime[msg.sender] = now;

    // Set seed.
    seed += uint32(now);

    // Define play record here.
    PlayRecord memory _playRecord;
    _playRecord.initialSeed = seed;
    _playRecord.progress = _tryingProgress;
    _playRecord.tokenIds[0] = _tokenIds[0];
    _playRecord.tokenIds[1] = _tokenIds[1];
    _playRecord.tokenIds[2] = _tokenIds[2];
    _playRecord.tokenIds[3] = _tokenIds[3];

    // The information that can give additional information.
    TurnInfo memory _turnInfo;

    // Step 1: Retrieve Hero information (0 ~ 3) & Enemy information (4 ~ 7).

    uint32[5][8] memory _unitStats; // Stats of units for given levels and class ids.
    uint8[2][8] memory _unitTypesAuras; // 0: Types of units for given levels and class ids. 1: Auras of units for given levels and class ids.

    // Retrieve deployed hero information.
    if (_tokenIds[0] != 0) {
      _playRecord.unitClassIds[0] = heroContract.getHeroClassId(_tokenIds[0]);
      (_playRecord.unitLevels[0], _turnInfo.originalExps[0], _unitStats[0], , ) = correctedHeroContract.getCorrectedStats(_tokenIds[0]);
      (, , , , _unitTypesAuras[0][0], , _unitTypesAuras[0][1], , , ) = heroContract.getClassInfo(_playRecord.unitClassIds[0]);
    }
    if (_tokenIds[1] != 0) {
      _playRecord.unitClassIds[1] = heroContract.getHeroClassId(_tokenIds[1]);
      (_playRecord.unitLevels[1], _turnInfo.originalExps[1], _unitStats[1], , ) = correctedHeroContract.getCorrectedStats(_tokenIds[1]);
      (, , , , _unitTypesAuras[1][0], , _unitTypesAuras[1][1], , , ) = heroContract.getClassInfo(_playRecord.unitClassIds[1]);
    }
    if (_tokenIds[2] != 0) {
      _playRecord.unitClassIds[2] = heroContract.getHeroClassId(_tokenIds[2]);
      (_playRecord.unitLevels[2], _turnInfo.originalExps[2], _unitStats[2], , ) = correctedHeroContract.getCorrectedStats(_tokenIds[2]);
      (, , , , _unitTypesAuras[2][0], , _unitTypesAuras[2][1], , , ) = heroContract.getClassInfo(_playRecord.unitClassIds[2]);
    }
    if (_tokenIds[3] != 0) {
      _playRecord.unitClassIds[3] = heroContract.getHeroClassId(_tokenIds[3]);
      (_playRecord.unitLevels[3], _turnInfo.originalExps[3], _unitStats[3], , ) = correctedHeroContract.getCorrectedStats(_tokenIds[3]);
      (, , , , _unitTypesAuras[3][0], , _unitTypesAuras[3][1], , , ) = heroContract.getClassInfo(_playRecord.unitClassIds[3]);
    }

    // Retrieve enemy information.
    // Instead of null check, isPersonalized check will tell the personalized mobs for this player exist.
    var _enemyCombination = addressToEnemyCombination[msg.sender];
    if (_enemyCombination.isPersonalized == false) {
      // Then let's use default value.
      _enemyCombination = initialEnemyCombination;
    }

    uint32[5][8] memory _tmpEnemyBaseStatsAndIVs; // 0 ~ 3: Temp value for getting enemy base stats. 4 ~ 7: Temp value for getting enemy IVs.

    // Retrieve mobs' class information. 
    (, , , , _unitTypesAuras[4][0], , _unitTypesAuras[4][1], _tmpEnemyBaseStatsAndIVs[0], _tmpEnemyBaseStatsAndIVs[4], ) = heroContract.getClassInfo(_enemyCombination.enemySlotClassIds[0]);
    (, , , , _unitTypesAuras[5][0], , _unitTypesAuras[5][1], _tmpEnemyBaseStatsAndIVs[1], _tmpEnemyBaseStatsAndIVs[5], ) = heroContract.getClassInfo(_enemyCombination.enemySlotClassIds[1]);
    (, , , , _unitTypesAuras[6][0], , _unitTypesAuras[6][1], _tmpEnemyBaseStatsAndIVs[2], _tmpEnemyBaseStatsAndIVs[6], ) = heroContract.getClassInfo(_enemyCombination.enemySlotClassIds[2]);
    (, , , , _unitTypesAuras[7][0], , _unitTypesAuras[7][1], _tmpEnemyBaseStatsAndIVs[3], _tmpEnemyBaseStatsAndIVs[7], ) = heroContract.getClassInfo(_enemyCombination.enemySlotClassIds[3]);

    _playRecord.unitClassIds[4] = _enemyCombination.enemySlotClassIds[0];
    _playRecord.unitClassIds[5] = _enemyCombination.enemySlotClassIds[1];
    _playRecord.unitClassIds[6] = _enemyCombination.enemySlotClassIds[2];
    _playRecord.unitClassIds[7] = _enemyCombination.enemySlotClassIds[3];
    
    // Set level for enemies.
    _playRecord.unitLevels[4] = _tryingProgress;
    _playRecord.unitLevels[5] = _tryingProgress;
    _playRecord.unitLevels[6] = _tryingProgress;
    _playRecord.unitLevels[7] = _tryingProgress;

    // With _tryingProgress, _tmpEnemyBaseStatsAndIVs, we can get the current stats of mobs.
    for (uint8 i = 0; i < 5; i ++) {
      _unitStats[4][i] = _tmpEnemyBaseStatsAndIVs[0][i] + _playRecord.unitLevels[4] * _tmpEnemyBaseStatsAndIVs[4][i];
      _unitStats[5][i] = _tmpEnemyBaseStatsAndIVs[1][i] + _playRecord.unitLevels[5] * _tmpEnemyBaseStatsAndIVs[5][i];
      _unitStats[6][i] = _tmpEnemyBaseStatsAndIVs[2][i] + _playRecord.unitLevels[6] * _tmpEnemyBaseStatsAndIVs[6][i];
      _unitStats[7][i] = _tmpEnemyBaseStatsAndIVs[3][i] + _playRecord.unitLevels[7] * _tmpEnemyBaseStatsAndIVs[7][i];
    }

    // Step 2. Run the battle logic.
    
    // Firstly, we need to assign the unit's turn order with AGLs of the units.
    uint32[8] memory _unitAGLs;
    for (i = 0; i < 8; i ++) {
      _unitAGLs[i] = _unitStats[i][2];
    }
    _turnInfo.turnOrder = getOrder(_unitAGLs);
    
    // Fight for 24 turns. (8 units x 3 rounds.)
    _turnInfo.turnLength = 24;
    for (i = 0; i < 24; i ++) {
      if (_unitStats[4][4] == 0 && _unitStats[5][4] == 0 && _unitStats[6][4] == 0 && _unitStats[7][4] == 0) {
        _turnInfo.turnLength = i;
        break;
      } else if (_unitStats[0][4] == 0 && _unitStats[1][4] == 0 && _unitStats[2][4] == 0 && _unitStats[3][4] == 0) {
        _turnInfo.turnLength = i;
        break;
      }
      
      var _slotId = _turnInfo.turnOrder[(i % 8)];
      if (_slotId < 4 && _tokenIds[_slotId] == 0) {
        // This means the slot is empty.
        // Defender should be default value.
        _turnInfo.defenderList[i] = 127;
      } else if (_unitStats[_slotId][4] == 0) {
        // This means the unit on this slot is dead.
        // Defender should be default value.
        _turnInfo.defenderList[i] = 128;
      } else {
        // 1) Check number of attack targets that are alive.
        uint8 _targetSlotId = 255;
        if (_slotId < 4) {
          if (_unitStats[4][4] > 0)
            _targetSlotId = 4;
          else if (_unitStats[5][4] > 0)
            _targetSlotId = 5;
          else if (_unitStats[6][4] > 0)
            _targetSlotId = 6;
          else if (_unitStats[7][4] > 0)
            _targetSlotId = 7;
        } else {
          if (_unitStats[0][4] > 0)
            _targetSlotId = 0;
          else if (_unitStats[1][4] > 0)
            _targetSlotId = 1;
          else if (_unitStats[2][4] > 0)
            _targetSlotId = 2;
          else if (_unitStats[3][4] > 0)
            _targetSlotId = 3;
        }
        
        // Target is the defender.
        _turnInfo.defenderList[i] = _targetSlotId;
        
        // Base damage. (Attacker's ATK * 1.5 - Defender's DEF).
        uint32 _damage = 10;
        if ((_unitStats[_slotId][0] * 150 / 100) > _unitStats[_targetSlotId][1])
          _damage = max((_unitStats[_slotId][0] * 150 / 100) - _unitStats[_targetSlotId][1], 10);
        else
          _damage = 10;

        // Check miss / success.
        if ((_unitStats[_slotId][3] * 150 / 100) > _unitStats[_targetSlotId][2]) {
          if (min(max(((_unitStats[_slotId][3] * 150 / 100) - _unitStats[_targetSlotId][2]), 75), 99) <= random(100, 0))
            _damage = _damage * 0;
        }
        else {
          if (75 <= random(100, 0))
            _damage = _damage * 0;
        }

        // Is the attack critical?
        if (_unitStats[_slotId][3] > _unitStats[_targetSlotId][3]) {
          if (min(max((_unitStats[_slotId][3] - _unitStats[_targetSlotId][3]), 5), 75) > random(100, 0))
            _damage = _damage * 150 / 100;
        }
        else {
          if (5 > random(100, 0))
            _damage = _damage * 150 / 100;
        }

        // Is attacker has the advantageous Type?
        if (_unitTypesAuras[_slotId][0] == 0 && _unitTypesAuras[_targetSlotId][0] == 1) // Fighter > Rogue
          _damage = _damage * 125 / 100;
        else if (_unitTypesAuras[_slotId][0] == 1 && _unitTypesAuras[_targetSlotId][0] == 2) // Rogue > Mage
          _damage = _damage * 125 / 100;
        else if (_unitTypesAuras[_slotId][0] == 2 && _unitTypesAuras[_targetSlotId][0] == 0) // Mage > Fighter
          _damage = _damage * 125 / 100;

        // Is attacker has the advantageous Aura?
        if (_unitTypesAuras[_slotId][1] == 0 && _unitTypesAuras[_targetSlotId][1] == 1) // Water > Fire
          _damage = _damage * 150 / 100;
        else if (_unitTypesAuras[_slotId][1] == 1 && _unitTypesAuras[_targetSlotId][1] == 2) // Fire > Nature
          _damage = _damage * 150 / 100;
        else if (_unitTypesAuras[_slotId][1] == 2 && _unitTypesAuras[_targetSlotId][1] == 0) // Nature > Water
          _damage = _damage * 150 / 100;
        else if (_unitTypesAuras[_slotId][1] == 3 && _unitTypesAuras[_targetSlotId][1] == 4) // Light > Darkness
          _damage = _damage * 150 / 100;
        else if (_unitTypesAuras[_slotId][1] == 4 && _unitTypesAuras[_targetSlotId][1] == 3) // Darkness > Light
          _damage = _damage * 150 / 100;
        
        // Apply damage so that reduce hp of defender.
        if(_unitStats[_targetSlotId][4] > _damage)
          _unitStats[_targetSlotId][4] -= _damage;
        else
          _unitStats[_targetSlotId][4] = 0;

        // Save damage to play record.
        _turnInfo.damageList[i] = _damage;
      }
    }
    
    // Step 3. Apply the result of this battle.

    // Set heroes deployed.
    if (_tokenIds[0] != 0)
      heroContract.deploy(_tokenIds[0], locationId, coolHero);
    if (_tokenIds[1] != 0)
      heroContract.deploy(_tokenIds[1], locationId, coolHero);
    if (_tokenIds[2] != 0)
      heroContract.deploy(_tokenIds[2], locationId, coolHero);
    if (_tokenIds[3] != 0)
      heroContract.deploy(_tokenIds[3], locationId, coolHero);

    uint8 _deadEnemies = 0;

    // Check result.
    if (_unitStats[4][4] == 0)
      _deadEnemies ++;
    if (_unitStats[5][4] == 0)
      _deadEnemies ++;
    if (_unitStats[6][4] == 0)
      _deadEnemies ++;
    if (_unitStats[7][4] == 0)
      _deadEnemies ++;
      
    if (_deadEnemies == 4) {
      // Fire TryDungeon event.
      TryDungeon(msg.sender, _tryingProgress, progressContract.getProgressOfAddressAndId(msg.sender, locationId), true);
      
      // Check for progress.
      if (_tryingProgress == progressContract.getProgressOfAddressAndId(msg.sender, locationId) + 1) {
        // Increment progress.
        progressContract.incrementProgressOfAddressAndId(msg.sender, locationId);
        // Rewards.
        (_playRecord.expReward, _playRecord.goldReward) = giveReward(_tokenIds, _tryingProgress, _deadEnemies, false, _turnInfo.originalExps);
        // For every 10th floor(progress), Dungeon Chest card is given.
        if (_tryingProgress % 10 == 0) {
          cardContract.mint(msg.sender, 1, 3);
        }
      } else {
        // Rewards for already cleared dungeon.
        (_playRecord.expReward, _playRecord.goldReward) = giveReward(_tokenIds, _tryingProgress, _deadEnemies, true, _turnInfo.originalExps);
      }

      // New enemy combination for the player.
      createNewCombination(msg.sender);
    }
    else {
      // Fire TryDungeon event.
      TryDungeon(msg.sender, _tryingProgress, progressContract.getProgressOfAddressAndId(msg.sender, locationId), false);

      // Rewards.
      (_playRecord.expReward, _playRecord.goldReward) = giveReward(_tokenIds, _tryingProgress, _deadEnemies, false, _turnInfo.originalExps);
    }

    // Save the result of this gameplay.
    addressToPlayRecord[msg.sender] = _playRecord;

    // Save the turn data.
    // This is commented as this information can be reconstructed with intitial seed and date time.
    // By commenting this, we can reduce about 400k gas.
    if (isTurnDataSaved) {
      addressToTurnInfo[msg.sender] = _turnInfo;
    }
  }

  // @dev Check ownership.
  function checkOwnershipAndAvailability(address _playerAddress, uint256[4] _tokenIds)
    private view
    returns(bool)
  {
    if ((_tokenIds[0] == 0 || heroContract.ownerOf(_tokenIds[0]) == _playerAddress) && (_tokenIds[1] == 0 || heroContract.ownerOf(_tokenIds[1]) == _playerAddress) && (_tokenIds[2] == 0 || heroContract.ownerOf(_tokenIds[2]) == _playerAddress) && (_tokenIds[3] == 0 || heroContract.ownerOf(_tokenIds[3]) == _playerAddress)) {
      
      // Retrieve avail time of heroes.
      uint256[4] memory _heroAvailAts;
      if (_tokenIds[0] != 0)
        ( , , , , , _heroAvailAts[0], , , ) = heroContract.getHeroInfo(_tokenIds[0]);
      if (_tokenIds[1] != 0)
        ( , , , , , _heroAvailAts[1], , , ) = heroContract.getHeroInfo(_tokenIds[1]);
      if (_tokenIds[2] != 0)
        ( , , , , , _heroAvailAts[2], , , ) = heroContract.getHeroInfo(_tokenIds[2]);
      if (_tokenIds[3] != 0)
        ( , , , , , _heroAvailAts[3], , , ) = heroContract.getHeroInfo(_tokenIds[3]);

      if (_heroAvailAts[0] <= now && _heroAvailAts[1] <= now && _heroAvailAts[2] <= now && _heroAvailAts[3] <= now) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }

  // @dev New combination of mobs.
  //  The combination is personalized by players, and refreshed when the dungeon cleared.
  function createNewCombination(address _playerAddress)
    private
  {
    EnemyCombination memory _newCombination;
    _newCombination.isPersonalized = true;
    for (uint8 i = 0; i < 4; i++) {
      _newCombination.enemySlotClassIds[i] = possibleMobClasses[random(uint32(possibleMobClasses.length), 0)];
    }
    addressToEnemyCombination[_playerAddress] = _newCombination;
  }

  // @dev Give rewards.
  function giveReward(uint256[4] _heroes, uint32 _progress, uint8 _numberOfKilledEnemies, bool _isClearedBefore, uint32[4] _originalExps)
    private
    returns (uint32 expRewardGiven, uint256 goldRewardGiven)
  {
    uint256 _goldRewardGiven;
    uint32 _expRewardGiven;
    if (_numberOfKilledEnemies != 4) {
      // In case lost.
      // Give baseline gold reward.
      _goldRewardGiven = goldReward / 25 * sqrt(_progress);
      _expRewardGiven = expReward * _numberOfKilledEnemies / 4 / 5 * sqrt(_progress / 4 + 1);
    } else if (_isClearedBefore == true) {
      // Did win, but this progress has been already cleared before.
      _goldRewardGiven = goldReward / 5 * sqrt(_progress);
      _expRewardGiven = expReward / 5 * sqrt(_progress / 4 + 1);
    } else {
      // Firstly cleared the progress.
      _goldRewardGiven = goldReward * sqrt(_progress);
      _expRewardGiven = expReward * sqrt(_progress / 4 + 1);
    }

    // Give reward Gold.
    goldContract.mint(msg.sender, _goldRewardGiven);
    
    // Give reward EXP.
    if(_heroes[0] != 0)
      heroContract.addExp(_heroes[0], uint32(2)**32 - _originalExps[0] + _expRewardGiven);
    if(_heroes[1] != 0)
      heroContract.addExp(_heroes[1], uint32(2)**32 - _originalExps[1] + _expRewardGiven);
    if(_heroes[2] != 0)
      heroContract.addExp(_heroes[2], uint32(2)**32 - _originalExps[2] + _expRewardGiven);
    if(_heroes[3] != 0)
      heroContract.addExp(_heroes[3], uint32(2)**32 - _originalExps[3] + _expRewardGiven);

    return (_expRewardGiven, _goldRewardGiven);
  }

  // @dev Return a pseudo random number between lower and upper bounds
  function random(uint32 _upper, uint32 _lower)
    private
    returns (uint32)
  {
    require(_upper > _lower);

    seed = seed % uint32(1103515245) + 12345;
    return seed % (_upper - _lower) + _lower;
  }

  // @dev Retreive order based on given array _by.
  function getOrder(uint32[8] _by)
    private pure
    returns (uint8[8])
  {
    uint8[8] memory _order = [uint8(0), 1, 2, 3, 4, 5, 6, 7];
    for (uint8 i = 0; i < 8; i ++) {
      for (uint8 j = i + 1; j < 8; j++) {
        if (_by[i] < _by[j]) {
          uint32 tmp1 = _by[i];
          _by[i] = _by[j];
          _by[j] = tmp1;
          uint8 tmp2 = _order[i];
          _order[i] = _order[j];
          _order[j] = tmp2;
        }
      }
    }
    return _order;
  }

  // @return Bigger value of two uint32s.
  function max(uint32 _value1, uint32 _value2)
    private pure
    returns (uint32)
  {
    if(_value1 >= _value2)
      return _value1;
    else
      return _value2;
  }

  // @return Bigger value of two uint32s.
  function min(uint32 _value1, uint32 _value2)
    private pure
    returns (uint32)
  {
    if(_value2 >= _value1)
      return _value1;
    else
      return _value2;
  }

  // @return Square root of the given value.
  function sqrt(uint32 _value) 
    private pure
    returns (uint32) 
  {
    uint32 z = (_value + 1) / 2;
    uint32 y = _value;
    while (z < y) {
      y = z;
      z = (_value / z + z) / 2;
    }
    return y;
  }

}

    Contract ABI  
[{"constant":true,"inputs":[],"name":"previousDungeonId","outputs":[{"name":"","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_classIds","type":"uint32[]"}],"name":"setPossibleMobs","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getLastPlayDateTime","outputs":[{"name":"dateTime","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"requiredProgressOfPreviousDungeon","outputs":[{"name":"","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"addressToPlayRecord","outputs":[{"name":"initialSeed","type":"uint32"},{"name":"progress","type":"uint32"},{"name":"expReward","type":"uint32"},{"name":"goldReward","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"bool"}],"name":"setIsTurnDataSaved","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_progress","type":"uint32"}],"name":"setRequiredProgressOfPreviousDungeon","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"addressToTurnInfo","outputs":[{"name":"turnLength","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint32"}],"name":"setCoolDungeon","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"isTurnDataSaved","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint32"}],"name":"setExpReward","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_dungeonId","type":"uint32"}],"name":"setPreviousDungeoonId","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"claimOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"possibleMobClasses","outputs":[{"name":"","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"goldReward","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"cardContract","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"setGoldReward","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_enemySlotClassIds","type":"uint32[4]"}],"name":"setInitialEnemyCombination","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getEnemyCombinationOfAddress","outputs":[{"name":"","type":"uint32[4]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getPlayRecord","outputs":[{"name":"initialSeed","type":"uint32"},{"name":"progress","type":"uint32"},{"name":"heroTokenIds","type":"uint256[4]"},{"name":"uintClassIds","type":"uint32[8]"},{"name":"unitLevels","type":"uint32[8]"},{"name":"expRewardGiven","type":"uint32"},{"name":"goldRewardGiven","type":"uint256"},{"name":"turnLength","type":"uint8"},{"name":"turnOrder","type":"uint8[8]"},{"name":"defenderList","type":"uint8[24]"},{"name":"damageList","type":"uint32[24]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"addressToEnemyCombination","outputs":[{"name":"isPersonalized","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"coolDungeon","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint32"}],"name":"setLocationId","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"coolHero","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"initialEnemyCombination","outputs":[{"name":"isPersonalized","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getInitialEnemyCombination","outputs":[{"name":"","type":"uint32[4]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"pendingOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"locationId","outputs":[{"name":"","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getPlayRecordNoTurnData","outputs":[{"name":"initialSeed","type":"uint32"},{"name":"progress","type":"uint32"},{"name":"heroTokenIds","type":"uint256[4]"},{"name":"uintClassIds","type":"uint32[8]"},{"name":"unitLevels","type":"uint32[8]"},{"name":"expRewardGiven","type":"uint32"},{"name":"goldRewardGiven","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"expReward","outputs":[{"name":"","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_tokenIds","type":"uint256[4]"},{"name":"_tryingProgress","type":"uint32"}],"name":"enterDungeon","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint32"}],"name":"setCoolHero","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"goldContract","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"addressToPlayRecordDateTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_progressAddress","type":"address"},{"name":"_heroContractAddress","type":"address"},{"name":"_correctedHeroContractAddress","type":"address"},{"name":"_cardContractAddress","type":"address"},{"name":"_goldContractAddress","type":"address"},{"name":"_locationId","type":"uint32"},{"name":"_coolDungeon","type":"uint256"},{"name":"_coolHero","type":"uint256"},{"name":"_expReward","type":"uint32"},{"name":"_goldReward","type":"uint256"},{"name":"_previousDungeonId","type":"uint32"},{"name":"_requiredProgressOfPreviousDungeon","type":"uint32"},{"name":"_enemySlotClassIds","type":"uint32[4]"},{"name":"_isTurnDataSaved","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_by","type":"address"},{"indexed":false,"name":"_tryingProgress","type":"uint32"},{"indexed":false,"name":"_progress","type":"uint32"},{"indexed":false,"name":"_isSuccess","type":"bool"}],"name":"TryDungeon","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"}]

  Contract Creation Code Switch To Opcodes View
60606040526001805460a060020a60ff02191690556006805460a060020a63ffffffff0219169055610384600755610e106008556009805463ffffffff19908116606417909155670de0b6b3a7640000600a55600b8054604060020a60ff0219166801000000000000000017905560138054909116905534156200008257600080fd5b6040516102208062004e6983398101604052808051919060200180519190602001805191906020018051919060200180519190602001805191906020018051919060200180519190602001805191906020018051919060200180519190602001805191602082019160a0018051906020019091905050336000806101000a815481600160a060020a030219169083600160a060020a031602179055508d600260006101000a815481600160a060020a030219169083600160a060020a031602179055508c600360006101000a815481600160a060020a030219169083600160a060020a031602179055508b600460006101000a815481600160a060020a030219169083600160a060020a031602179055508a600660006101000a815481600160a060020a030219169083600160a060020a0316021790555089600560006101000a815481600160a060020a030219169083600160a060020a0316021790555088600660146101000a81548163ffffffff021916908363ffffffff160217905550876007819055508660088190555085600960006101000a81548163ffffffff021916908363ffffffff16021790555084600a8190555083600b60006101000a81548163ffffffff021916908363ffffffff16021790555082600b60046101000a81548163ffffffff021916908363ffffffff1602179055506000601160000160006101000a81548160ff021916908315150217905550816011600101906004620002ae929190620002e7565b50600b80549115156801000000000000000002604060020a60ff021990921691909117905550620003b19b505050505050505050505050565b600183019183908215620003785791602002820160005b838211156200034457835183826101000a81548163ffffffff021916908363ffffffff1602179055509260200192600401602081600301049283019260010302620002fe565b8015620003765782816101000a81549063ffffffff021916905560040160208160030104928301926001030262000344565b505b50620003869291506200038a565b5090565b620003ae91905b808211156200038657805463ffffffff1916815560010162000391565b90565b614aa880620003c16000396000f3006060604052600436106101c95763ffffffff60e060020a600035041663040177b781146101ce578063063ca521146101fa5780630e0304f71461024b5780630ec89f2b1461027c5780630faac0e21461028f5780632b99f3e1146102e157806334eb3200146102f95780633789ddd2146103155780633872b3ec1461034a5780633f4ba83a1461036657806345fd3666146103795780634a5e4fa8146103a05780634cc78983146103bc5780634e71e0c8146103d8578063510c72ae146103eb5780635c975abb1461040157806360e587f914610414578063693bd2d0146104275780638456cb5914610456578063862eb9c01461046957806387a977521461047f5780638da5cb5b146104b75780638f3a8982146104ca5780639686898a14610521578063be25a61c14610688578063c3fb31a4146106a7578063c4e6aaa6146106ba578063d9c76d61146106d6578063da8300d2146106e9578063db7f1e6b146106fc578063e30c39781461070f578063e8aadc3f14610722578063eeb8491014610735578063f019c5da14610814578063f2fde38b14610827578063fa99d7ae14610846578063fc36cc9d14610886578063fc996557146108a2578063fd07e1fd146108b5575b600080fd5b34156101d957600080fd5b6101e16108d4565b60405163ffffffff909116815260200160405180910390f35b341561020557600080fd5b61024960046024813581810190830135806020818102016040519081016040528093929190818152602001838360200280828437509496506108e095505050505050565b005b341561025657600080fd5b61026a600160a060020a0360043516610912565b60405190815260200160405180910390f35b341561028757600080fd5b6101e161092d565b341561029a57600080fd5b6102ae600160a060020a0360043516610941565b60405163ffffffff9485168152928416602084015292166040808301919091526060820192909252608001905180910390f35b34156102ec57600080fd5b6102496004351515610976565b341561030457600080fd5b61024963ffffffff600435166109b9565b341561032057600080fd5b610334600160a060020a03600435166109fc565b60405160ff909116815260200160405180910390f35b341561035557600080fd5b61024963ffffffff60043516610a11565b341561037157600080fd5b610249610a37565b341561038457600080fd5b61038c610ab6565b604051901515815260200160405180910390f35b34156103ab57600080fd5b61024963ffffffff60043516610acb565b34156103c757600080fd5b61024963ffffffff60043516610b02565b34156103e357600080fd5b610249610b39565b34156103f657600080fd5b6101e1600435610bc7565b341561040c57600080fd5b61038c610bff565b341561041f57600080fd5b61026a610c0f565b341561043257600080fd5b61043a610c15565b604051600160a060020a03909116815260200160405180910390f35b341561046157600080fd5b610249610c24565b341561047457600080fd5b610249600435610ca8565b341561048a57600080fd5b61024960046084818060806040519081016040529190828260808082843750939550610cc8945050505050565b34156104c257600080fd5b61043a610cfa565b34156104d557600080fd5b6104e9600160a060020a0360043516610d09565b6040518082608080838360005b8381101561050e5780820151838201526020016104f6565b5050505090500191505060405180910390f35b341561052c57600080fd5b610540600160a060020a0360043516610d9c565b60405163ffffffff808d1682528b166020820152604081018a608080838360005b83811015610579578082015183820152602001610561565b5050505090500189600860200280838360005b838110156105a457808201518382015260200161058c565b5050505090500188600860200280838360005b838110156105cf5780820151838201526020016105b7565b5050505063ffffffff8a169201918252506020810187905260ff861660408201526060018461010080838360005b838110156106155780820151838201526020016105fd565b5050505090500183601860200280838360005b83811015610640578082015183820152602001610628565b5050505090500182601860200280838360005b8381101561066b578082015183820152602001610653565b505050509050019b50505050505050505050505060405180910390f35b341561069357600080fd5b61038c600160a060020a0360043516611185565b34156106b257600080fd5b61026a61119a565b34156106c557600080fd5b61024963ffffffff600435166111a0565b34156106e157600080fd5b61026a6111f2565b34156106f457600080fd5b61038c6111f8565b341561070757600080fd5b6104e9611201565b341561071a57600080fd5b61043a61126a565b341561072d57600080fd5b6101e1611279565b341561074057600080fd5b610754600160a060020a036004351661128c565b60405163ffffffff8089168252871660208201526040810186608080838360005b8381101561078d578082015183820152602001610775565b5050505090500185600860200280838360005b838110156107b85780820151838201526020016107a0565b5050505090500184600860200280838360005b838110156107e35780820151838201526020016107cb565b505050509050018363ffffffff1663ffffffff16815260200182815260200197505050505050505060405180910390f35b341561081f57600080fd5b6101e1611450565b341561083257600080fd5b610249600160a060020a036004351661145c565b341561085157600080fd5b610249600460848180608060405190810160405291908282608080828437509395505050913563ffffffff1691506114a69050565b341561089157600080fd5b61024963ffffffff6004351661390b565b34156108ad57600080fd5b61043a613931565b34156108c057600080fd5b61026a600160a060020a0360043516613940565b600b5463ffffffff1681565b60005433600160a060020a039081169116146108fb57600080fd5b601081805161090e929160200190614632565b5050565b600160a060020a03166000908152600d602052604090205490565b600b54640100000000900463ffffffff1681565b600e6020526000908152604090208054600782015460089092015463ffffffff80831693640100000000909304811692169084565b60005433600160a060020a0390811691161461099157600080fd5b600b8054911515680100000000000000000268ff000000000000000019909216919091179055565b60005433600160a060020a039081169116146109d457600080fd5b600b805463ffffffff9092166401000000000267ffffffff0000000019909216919091179055565b600f6020526000908152604090205460ff1681565b60005433600160a060020a03908116911614610a2c57600080fd5b63ffffffff16600755565b60005433600160a060020a03908116911614610a5257600080fd5b60015460a060020a900460ff161515610a6a57600080fd5b6001805474ff0000000000000000000000000000000000000000191690557f7805862f689e2f13df9f062ff482ad3ad112aca9e0847911ed832e158c525b3360405160405180910390a1565b600b5468010000000000000000900460ff1681565b60005433600160a060020a03908116911614610ae657600080fd5b6009805463ffffffff191663ffffffff92909216919091179055565b60005433600160a060020a03908116911614610b1d57600080fd5b600b805463ffffffff191663ffffffff92909216919091179055565b60015433600160a060020a03908116911614610b5457600080fd5b600154600054600160a060020a0391821691167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a3600180546000805473ffffffffffffffffffffffffffffffffffffffff19908116600160a060020a03841617909155169055565b6010805482908110610bd557fe5b9060005260206000209060089182820401919006600402915054906101000a900463ffffffff1681565b60015460a060020a900460ff1681565b600a5481565b600654600160a060020a031681565b60005433600160a060020a03908116911614610c3f57600080fd5b60015460a060020a900460ff1615610c5657600080fd5b6001805474ff0000000000000000000000000000000000000000191660a060020a1790557f6985a02210a168e66602d3235cb6db0e70f92b3ba4d376a33c0f3d9434bff62560405160405180910390a1565b60005433600160a060020a03908116911614610cc357600080fd5b600a55565b60005433600160a060020a03908116911614610ce357600080fd5b6011805460ff1916905561090e60128260046146e1565b600054600160a060020a031681565b610d1161473a565b600160a060020a0382166000908152600c60205260409020805460ff161515610d38575060115b6001810160046080604051908101604052919060808301826000855b82829054906101000a900463ffffffff1663ffffffff1681526020019060040190602082600301049283019260010382029150808411610d5457509498975050505050505050565b600080610da7614762565b610daf614789565b610db7614789565b6000806000610dc4614789565b610dcc6147a4565b610dd46147a4565b610ddc6147bf565b610de461480f565b600160a060020a038e166000908152600e6020526040908190209060e090519081016040908152825463ffffffff80821684526401000000009091041660208301529091908083019060018301906004906080905190810160405291906080830182845b815481526020019060010190808311610e4857505050918352505060200160058201600861010060405190810160405291906101008301826000855b82829054906101000a900463ffffffff1663ffffffff1681526020019060040190602082600301049283019260010382029150808411610e84579050505050918352505060200160068201600861010060405190810160405291906101008301826000855b82829054906101000a900463ffffffff1663ffffffff1681526020019060040190602082600301049283019260010382029150808411610ee9579050505050505081526020016007820160009054906101000a900463ffffffff1663ffffffff1663ffffffff1681526020016008820154815250509150600f60008f600160a060020a0316600160a060020a0316815260200190815260200160002060a06040519081016040908152825460ff16825290919060208301906001830190600890610100905190810160405291906101008301826000855b825461010083900a900460ff16815260206001928301818104948501949093039092029101808411610fc0579050505050918352505060200160028201601861030060405190810160405291906103008301826000855b825461010083900a900460ff16815260206001928301818104948501949093039092029101808411611017579050505050918352505060200160038201601861030060405190810160405291906103008301826000855b82829054906101000a900463ffffffff1663ffffffff168152602001906004019060208260030104928301926001038202915080841161106e57905050505091835250506020016006820160046080604051908101604052919060808301826000855b82829054906101000a900463ffffffff1663ffffffff16815260200190600401906020826003010492830192600103820291508084116110d15790505050505050815250509050816000015182602001518360400151846060015185608001518660a001518760c001518751886020015189604001518a606001518898508797508696508292508191508090509c509c509c509c509c509c509c509c509c509c509c50505091939597999b90929496989a50565b600c6020526000908152604090205460ff1681565b60075481565b60005433600160a060020a039081169116146111bb57600080fd5b6006805463ffffffff90921660a060020a0277ffffffff000000000000000000000000000000000000000019909216919091179055565b60085481565b60115460ff1681565b61120961473a565b601260046080604051908101604052919060808301826000855b82829054906101000a900463ffffffff1663ffffffff1681526020019060040190602082600301049283019260010382029150808411611223579050505050505090505b90565b600154600160a060020a031681565b60065460a060020a900463ffffffff1681565b600080611297614762565b61129f614789565b6112a7614789565b6000806112b26147bf565b600160a060020a0389166000908152600e6020526040908190209060e090519081016040908152825463ffffffff80821684526401000000009091041660208301529091908083019060018301906004906080905190810160405291906080830182845b81548152602001906001019080831161131657505050918352505060200160058201600861010060405190810160405291906101008301826000855b82829054906101000a900463ffffffff1663ffffffff1681526020019060040190602082600301049283019260010382029150808411611352579050505050918352505060200160068201600861010060405190810160405291906101008301826000855b82829054906101000a900463ffffffff1663ffffffff16815260200190600401906020826003010492830192600103820291508084116113b757505050928452505050600782015463ffffffff1660208201526008909101546040909101529050805181602001518260400151836060015184608001518560a001518660c00151959f949e50929c50909a509850965090945092505050565b60095463ffffffff1681565b60005433600160a060020a0390811691161461147757600080fd5b6001805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0392909216919091179055565b6114ae6147bf565b6114b661480f565b6114be614857565b6114c6614885565b60006114d0614857565b60006114da614789565b60015460009081908190819060a060020a900460ff16156114fa57600080fd5b8d51158061152e575060208e01518e511415801561151d575060408e01518e5114155b801561152e575060608e01518e5114155b151561153957600080fd5b60208e0151158061157657508d5160208f015114158015611562575060408e015160208f015114155b8015611576575060608e015160208f015114155b151561158157600080fd5b60408e015115806115be57508d5160408f0151141580156115aa575060208e015160408f015114155b80156115be575060608e015160408f015114155b15156115c957600080fd5b60608e0151158061160657508d5160608f0151141580156115f2575060208e015160608f015114155b8015611606575060408e015160608f015114155b151561161157600080fd5b600b54640100000000900463ffffffff16156116d057600b5460025463ffffffff6401000000008304811692600160a060020a039092169163e156c5939133911660006040516020015260405163ffffffff84811660e060020a028252600160a060020a0393909316600482015291166024820152604401602060405180830381600087803b15156116a257600080fd5b6102c65a03f115156116b357600080fd5b5050506040518051905063ffffffff16101515156116d057600080fd5b600063ffffffff8e16116116e357600080fd5b600254600654600160a060020a039091169063e156c59390339060a060020a900463ffffffff1660006040516020015260405163ffffffff84811660e060020a028252600160a060020a0393909316600482015291166024820152604401602060405180830381600087803b151561175a57600080fd5b6102c65a03f1151561176b57600080fd5b5050506040518051905060010163ffffffff168d63ffffffff161115151561179257600080fd5b600754600160a060020a0333166000908152600d602052604090205442910111156117bc57600080fd5b6117c6338f613952565b15156117d157600080fd5b600160a060020a0333166000908152600d6020908152604090912042908190556013805463ffffffff808216909301831663ffffffff19909116179081905581168e528e16908d01528d518c604001515260208e01518c604001516020015260408e01518c604001516040015260608e01518c60400151606001528d5115611a4157600354600160a060020a031663d1f699028f5160006040516020015260405160e060020a63ffffffff84160281526004810191909152602401602060405180830381600087803b15156118a557600080fd5b6102c65a03f115156118b657600080fd5b505050604051805190508c6060015163ffffffff919091169052600454600160a060020a031663763901448f5160006040516101a0015260405160e060020a63ffffffff841602815260048101919091526024016101a060405180830381600087803b151561192457600080fd5b6102c65a03f1151561193557600080fd5b5050506040518051906020018051906020018060a0018060a0018051506020016040525060808f015160808f0151918e5263ffffffff92831690915291169052600354600160a060020a0316636ccd5cbe60608e01515160006040516102c0015260405163ffffffff83811660e060020a0282529190911660048201526024016102c060405180830381600087803b15156119cf57600080fd5b6102c65a03f115156119e057600080fd5b50505060405180519060200180519060200180519060200180519060200180519060200180519060200180516102009091016040529195509093508d925060009150611a299050565b60200201518b5160ff92831660209190910152911690525b60208e015115611c5e57600354600160a060020a031663d1f699028f6001602002015160006040516020015260405160e060020a63ffffffff84160281526004810191909152602401602060405180830381600087803b1515611aa357600080fd5b6102c65a03f11515611ab457600080fd5b505050604051805190508c60600151600163ffffffff9092166020929092020152600454600160a060020a031663763901448f6001602002015160006040516101a0015260405160e060020a63ffffffff841602815260048101919091526024016101a060405180830381600087803b1515611b2f57600080fd5b6102c65a03f11515611b4057600080fd5b5050506040518051906020018051906020018060a0018060a0018051506020016040525060808f015160200160808f015160208f81019390935263ffffffff93841692019190915291169052600354600160a060020a0316636ccd5cbe60608e01516020015160006040516102c0015260405163ffffffff83811660e060020a0282529190911660048201526024016102c060405180830381600087803b1515611be957600080fd5b6102c65a03f11515611bfa57600080fd5b50505060405180519060200180519060200180519060200180519060200180519060200180519060200180516102009091016040529195509093508d925060019150611c439050565b602002015160208c015160ff92831660209190910152911690525b60408e015115611e7b57600354600160a060020a031663d1f699028f6002602002015160006040516020015260405160e060020a63ffffffff84160281526004810191909152602401602060405180830381600087803b1515611cc057600080fd5b6102c65a03f11515611cd157600080fd5b505050604051805190508c60600151600263ffffffff9092166020929092020152600454600160a060020a031663763901448f6002602002015160006040516101a0015260405160e060020a63ffffffff841602815260048101919091526024016101a060405180830381600087803b1515611d4c57600080fd5b6102c65a03f11515611d5d57600080fd5b5050506040518051906020018051906020018060a0018060a0018051506020016040525060808f015160400160808f015160408f81019390935263ffffffff93841692019190915291169052600354600160a060020a0316636ccd5cbe60608e01516040015160006040516102c0015260405163ffffffff83811660e060020a0282529190911660048201526024016102c060405180830381600087803b1515611e0657600080fd5b6102c65a03f11515611e1757600080fd5b50505060405180519060200180519060200180519060200180519060200180519060200180519060200180516102009091016040529195509093508d925060029150611e609050565b602002015160408c015160ff92831660209190910152911690525b60608e0151156120bc57600360009054906101000a9004600160a060020a0316600160a060020a031663d1f699028f6003600481101515611eb857fe5b602002015160006040516020015260405160e060020a63ffffffff84160281526004810191909152602401602060405180830381600087803b1515611efc57600080fd5b6102c65a03f11515611f0d57600080fd5b505050604051805190508c60600151600363ffffffff9092166020929092020152600454600160a060020a031663763901448f6003602002015160006040516101a0015260405160e060020a63ffffffff841602815260048101919091526024016101a060405180830381600087803b1515611f8857600080fd5b6102c65a03f11515611f9957600080fd5b5050506040518051906020018051906020018060a0018060a0018051506020016040525060808f015160600160808f01516003602002018e6003602002019290925263ffffffff92831690915291169052600354600160a060020a0316636ccd5cbe60608e01516060015160006040516102c0015260405163ffffffff83811660e060020a0282529190911660048201526024016102c060405180830381600087803b151561204757600080fd5b6102c65a03f1151561205857600080fd5b50505060405180519060200180519060200180519060200180519060200180519060200180519060200180516102009091016040529195509093508d9250600391506120a19050565b602002015160608c015160ff92831660209190910152911690525b600160a060020a0333166000908152600c60205260409020805490985060ff1615156120e757601197505b6003546001890154600160a060020a0390911690636ccd5cbe9063ffffffff1660006040516102c0015260405163ffffffff83811660e060020a0282529190911660048201526024016102c060405180830381600087803b151561214a57600080fd5b6102c65a03f1151561215b57600080fd5b505050604051805190602001805190602001805190602001805190602001805190602001805190602001805161020082016040529296509194505060208101925060c001905060808d015160808e015160808d0192909252918b5260ff928316602090910152911690526003546001890154600160a060020a0390911690636ccd5cbe90640100000000900463ffffffff1660006040516102c0015260405163ffffffff83811660e060020a0282529190911660048201526024016102c060405180830381600087803b151561223057600080fd5b6102c65a03f1151561224157600080fd5b505050604051805190602001805190602001805190602001805190602001805190602001805190602001805161020082016040529296509194505060208101925060c001905060a08d015160a08e015160a08d019290925260208c81019390935260ff9384169190920152911690526003546001890154600160a060020a0390911690636ccd5cbe9063ffffffff680100000000000000009091041660006040516102c0015260405163ffffffff83811660e060020a0282529190911660048201526024016102c060405180830381600087803b151561232057600080fd5b6102c65a03f1151561233157600080fd5b505050604051805190602001805190602001805190602001805190602001805190602001805190602001805161020082016040529296509194505060208101925060c090810191508d015160c08e015160c08d01929092526040808d019390935260ff9384166020909201919091529290911690915260035460018a0154600160a060020a0390911691636ccd5cbe916c01000000000000000000000000900463ffffffff1690600090516102c0015260405163ffffffff83811660e060020a0282529190911660048201526024016102c060405180830381600087803b151561241a57600080fd5b6102c65a03f1151561242b57600080fd5b505050604051805190602001805190602001805190602001805190602001805190602001805190602001805161020082016040529296509194505060208101925060c001905060e08d015160e08e015160e08d01929092526060808d019390935260ff93841660209092019190915292909116909152600189015463ffffffff16908d015163ffffffff91821660809190910152600189015464010000000090041660608d015163ffffffff91821660a0919091015260018901546801000000000000000090041660608d015163ffffffff91821660c0919091015260018901546c0100000000000000000000000090041660608d015163ffffffff9190911660e0909101528c60808d015163ffffffff919091166080918201528d908d015163ffffffff9190911660a0909101528c60808d015163ffffffff9190911660c0909101528c60808d015163ffffffff9190911660e090910152600095505b60058660ff16101561271557608087015160ff8716600581106125a857fe5b60200201518c608001516080015102875160ff8816600581106125c757fe5b60200201510160808b015160ff8816600581106125e057fe5b63ffffffff929092166020909202015260a087015160ff87166005811061260357fe5b60200201518c6080015160a0015102602088015160ff88166005811061262557fe5b60200201510160a08b015160ff88166005811061263e57fe5b63ffffffff929092166020909202015260c087015160ff87166005811061266157fe5b60200201518c6080015160c0015102604088015160ff88166005811061268357fe5b60200201510160c08b015160ff88166005811061269c57fe5b63ffffffff929092166020909202015260e087015160ff8716600581106126bf57fe5b60200201518c6080015160e0015102606088015160ff8816600581106126e157fe5b60200201510160e08b015160ff8816600581106126fa57fe5b63ffffffff9092166020929092020152600190950194612589565b600095505b60088660ff161015612768578960ff87166008811061273557fe5b6020020151604001518560ff88166008811061274d57fe5b63ffffffff909216602092909202015260019095019461271a565b61277185613f01565b60208c015260188b52600095505b60188660ff1610156130225760808a01516080015163ffffffff161580156127b3575060a08a01516080015163ffffffff16155b80156127cb575060c08a01516080015163ffffffff16155b80156127e3575060e08a01516080015163ffffffff16155b156127f35760ff86168b52613022565b89516080015163ffffffff16158015612818575060208a01516080015163ffffffff16155b8015612830575060408a01516080015163ffffffff16155b8015612848575060608a01516080015163ffffffff16155b156128585760ff86168b52613022565b8a60200151600860ff88160660ff1660088110151561287357fe5b6020020151935060048460ff1610801561289e57508d60ff85166004811061289757fe5b6020020151155b156128ca57607f8b6040015160ff8816601881106128b857fe5b60ff9092166020929092020152613017565b8960ff8516600881106128d957fe5b60200201516080015163ffffffff1615156129035760808b6040015160ff8816601881106128b857fe5b60ff925060048460ff16101561299457600060808b01516080015163ffffffff161115612933576004925061298f565b600060a08b01516080015163ffffffff161115612953576005925061298f565b600060c08b01516080015163ffffffff161115612973576006925061298f565b600060e08b01516080015163ffffffff16111561298f57600792505b612a0d565b60008a516080015163ffffffff1611156129b15760009250612a0d565b600060208b01516080015163ffffffff1611156129d15760019250612a0d565b600060408b01516080015163ffffffff1611156129f15760029250612a0d565b600060608b01516080015163ffffffff161115612a0d57600392505b828b6040015160ff881660188110612a2157fe5b60ff9283166020919091029190910152600a92508a90841660088110612a4357fe5b60200201516020015163ffffffff1660648b60ff871660088110612a6357fe5b60200201515160960263ffffffff16811515612a7b57fe5b0463ffffffff161115612adc57612ad58a60ff851660088110612a9a57fe5b60200201516020015160648c60ff881660088110612ab457fe5b60200201515160960263ffffffff16811515612acc57fe5b0403600a6140ad565b9150612ae1565b600a91505b8960ff841660088110612af057fe5b60200201516040015163ffffffff1660648b60ff871660088110612b1057fe5b60200201516060015160960263ffffffff16811515612b2b57fe5b0463ffffffff161115612bb957612b44606460006140cf565b63ffffffff16612ba4612b9d8c60ff871660088110612b5f57fe5b60200201516040015160648e60ff8a1660088110612b7957fe5b60200201516060015160960263ffffffff16811515612b9457fe5b0403604b6140ad565b6063614133565b63ffffffff1611612bb457600091505b612bd7565b612bc5606460006140cf565b63ffffffff16604b11612bd757600091505b8960ff841660088110612be657fe5b60200201516060015163ffffffff168a60ff861660088110612c0457fe5b60200201516060015163ffffffff161115612c9157612c25606460006140cf565b63ffffffff16612c70612c698c60ff871660088110612c4057fe5b6020020151606001518d60ff891660088110612c5857fe5b6020020151606001510360056140ad565b604b614133565b63ffffffff161115612c8c57606463ffffffff60968402160491505b612cbb565b612c9d606460006140cf565b63ffffffff1660051115612cbb57606463ffffffff60968402160491505b8860ff851660088110612cca57fe5b60200201515160ff16158015612cf757508860ff841660088110612cea57fe5b60200201515160ff166001145b15612d1157606463ffffffff607d8402165b049150612db6565b8860ff851660088110612d2057fe5b60200201515160ff166001148015612d4f57508860ff841660088110612d4257fe5b60200201515160ff166002145b15612d6557606463ffffffff607d840216612d09565b8860ff851660088110612d7457fe5b60200201515160ff166002148015612da157508860ff841660088110612d9657fe5b60200201515160ff16155b15612db657606463ffffffff607d8402160491505b8860ff851660088110612dc557fe5b60200201516020015160ff16158015612df857508860ff841660088110612de857fe5b60200201516020015160ff166001145b15612e1257606463ffffffff60968402165b049150612f77565b8860ff851660088110612e2157fe5b60200201516020015160ff166001148015612e5657508860ff841660088110612e4657fe5b60200201516020015160ff166002145b15612e6c57606463ffffffff6096840216612e0a565b8860ff851660088110612e7b57fe5b60200201516020015160ff166002148015612eae57508860ff841660088110612ea057fe5b60200201516020015160ff16155b15612ec457606463ffffffff6096840216612e0a565b8860ff851660088110612ed357fe5b60200201516020015160ff166003148015612f0857508860ff841660088110612ef857fe5b60200201516020015160ff166004145b15612f1e57606463ffffffff6096840216612e0a565b8860ff851660088110612f2d57fe5b60200201516020015160ff166004148015612f6257508860ff841660088110612f5257fe5b60200201516020015160ff166003145b15612f7757606463ffffffff60968402160491505b63ffffffff82168a60ff851660088110612f8d57fe5b60200201516080015163ffffffff161115612fcc57818a60ff851660088110612fb257fe5b60200201516080018181510363ffffffff16905250612ff2565b60008a60ff851660088110612fdd57fe5b602002015163ffffffff919091166080909101525b818b6060015160ff88166018811061300657fe5b63ffffffff90921660209290920201525b60019095019461277f565b8d51156130c957600354600160a060020a031663284fb3638f51600660149054906101000a900463ffffffff166008546000604051602001526040518463ffffffff1660e060020a028152600401808481526020018363ffffffff1663ffffffff1681526020018281526020019350505050602060405180830381600087803b15156130ad57600080fd5b6102c65a03f115156130be57600080fd5b505050604051805150505b60208e01511561317957600354600160a060020a031663284fb3638f60016020020151600660149054906101000a900463ffffffff166008546000604051602001526040518463ffffffff1660e060020a028152600401808481526020018363ffffffff1663ffffffff1681526020018281526020019350505050602060405180830381600087803b151561315d57600080fd5b6102c65a03f1151561316e57600080fd5b505050604051805150505b60408e01511561322957600354600160a060020a031663284fb3638f60026020020151600660149054906101000a900463ffffffff166008546000604051602001526040518463ffffffff1660e060020a028152600401808481526020018363ffffffff1663ffffffff1681526020018281526020019350505050602060405180830381600087803b151561320d57600080fd5b6102c65a03f1151561321e57600080fd5b505050604051805150505b60608e0151156132f857600360009054906101000a9004600160a060020a0316600160a060020a031663284fb3638f600360048110151561326657fe5b6020020151600660149054906101000a900463ffffffff166008546000604051602001526040518463ffffffff1660e060020a028152600401808481526020018363ffffffff1663ffffffff1681526020018281526020019350505050602060405180830381600087803b15156132dc57600080fd5b6102c65a03f115156132ed57600080fd5b505050604051805150505b50600060808a01516080015163ffffffff161515613314576001015b60a08a01516080015163ffffffff16151561332d576001015b60c08a01516080015163ffffffff161515613346576001015b60e08a01516080015163ffffffff16151561335f576001015b8060ff16600414156136745733600160a060020a03167f899a544c07bbb626fdb7f36c63b7cffee3869aaad13a9b770dce9de2f79919b48e600260009054906101000a9004600160a060020a0316600160a060020a031663e156c59333600660149054906101000a900463ffffffff1660006040516020015260405163ffffffff84811660e060020a028252600160a060020a0393909316600482015291166024820152604401602060405180830381600087803b151561341f57600080fd5b6102c65a03f1151561343057600080fd5b50505060405180519050600160405163ffffffff93841681529190921660208201529015156040808301919091526060909101905180910390a2600254600654600160a060020a039091169063e156c59390339060a060020a900463ffffffff1660006040516020015260405163ffffffff84811660e060020a028252600160a060020a0393909316600482015291166024820152604401602060405180830381600087803b15156134e157600080fd5b6102c65a03f115156134f257600080fd5b5050506040518051905060010163ffffffff168d63ffffffff16141561364357600254600654600160a060020a039091169063a40156b890339060a060020a900463ffffffff1660405163ffffffff84811660e060020a028252600160a060020a0393909316600482015291166024820152604401600060405180830381600087803b151561358057600080fd5b6102c65a03f1151561359157600080fd5b5050506135a68e8e8360008f6080015161414c565b60c08e015263ffffffff90811660a08e0152600a8e82160616151561363e57600654600160a060020a0316636112e8ac336001600360405160e060020a63ffffffff8616028152600160a060020a039093166004840152602483019190915260ff166044820152606401600060405180830381600087803b151561362957600080fd5b6102c65a03f1151561363a57600080fd5b5050505b613666565b6136558e8e8360018f6080015161414c565b60c08e015263ffffffff1660a08d01525b61366f336144fe565b613796565b33600160a060020a03167f899a544c07bbb626fdb7f36c63b7cffee3869aaad13a9b770dce9de2f79919b48e600260009054906101000a9004600160a060020a0316600160a060020a031663e156c59333600660149054906101000a900463ffffffff1660006040516020015260405163ffffffff84811660e060020a028252600160a060020a0393909316600482015291166024820152604401602060405180830381600087803b151561372857600080fd5b6102c65a03f1151561373957600080fd5b50505060405180519050600060405163ffffffff93841681529190921660208201529015156040808301919091526060909101905180910390a26137858e8e8360008f6080015161414c565b60c08e015263ffffffff1660a08d01525b600160a060020a0333166000908152600e602052604090208c908151815463ffffffff191663ffffffff919091161781556020820151815463ffffffff919091166401000000000267ffffffff0000000019909116178155604082015161380390600183019060046148b3565b50606082015161381990600583019060086146e1565b50608082015161382f90600683019060086146e1565b5060a082015160078201805463ffffffff191663ffffffff9290921691909117905560c082015160089091015550600b5460ff6801000000000000000090910416156138fb57600160a060020a0333166000908152600f602052604090208b908151815460ff191660ff9190911617815560208201516138b590600183019060086148ed565b5060408201516138cb90600283019060186148ed565b5060608201516138e1906003830190601861497c565b5060808201516138f790600683019060046146e1565b5050505b5050505050505050505050505050565b60005433600160a060020a0390811691161461392657600080fd5b63ffffffff16600855565b600554600160a060020a031681565b600d6020526000908152604090205481565b600061395c614762565b825115806139e15750600354600160a060020a038581169116636352211e855160006040516020015260405160e060020a63ffffffff84160281526004810191909152602401602060405180830381600087803b15156139bb57600080fd5b6102c65a03f115156139cc57600080fd5b50505060405180519050600160a060020a0316145b8015613a73575060208301511580613a735750600354600160a060020a038581169116636352211e602086015160006040516020015260405160e060020a63ffffffff84160281526004810191909152602401602060405180830381600087803b1515613a4d57600080fd5b6102c65a03f11515613a5e57600080fd5b50505060405180519050600160a060020a0316145b8015613b05575060408301511580613b055750600354600160a060020a038581169116636352211e604086015160006040516020015260405160e060020a63ffffffff84160281526004810191909152602401602060405180830381600087803b1515613adf57600080fd5b6102c65a03f11515613af057600080fd5b50505060405180519050600160a060020a0316145b8015613b97575060608301511580613b975750600354600160a060020a038581169116636352211e606086015160006040516020015260405160e060020a63ffffffff84160281526004810191909152602401602060405180830381600087803b1515613b7157600080fd5b6102c65a03f11515613b8257600080fd5b50505060405180519050600160a060020a0316145b15613ef557825115613c5d57600354600160a060020a03166375e39f2684516000604051610220015260405160e060020a63ffffffff8416028152600481019190915260240161022060405180830381600087803b1515613bf757600080fd5b6102c65a03f11515613c0857600080fd5b5050506040518051906020018051906020018051906020018051906020018051906020018051906020018060a0018060a0018051506020016040525090955086945060009350613c5792505050565b60200201525b602083015115613d2457600354600160a060020a03166375e39f2660208501516000604051610220015260405160e060020a63ffffffff8416028152600481019190915260240161022060405180830381600087803b1515613cbe57600080fd5b6102c65a03f11515613ccf57600080fd5b5050506040518051906020018051906020018051906020018051906020018051906020018051906020018060a0018060a0018051506020016040525090955086945060019350613d1e92505050565b60200201525b604083015115613deb57600354600160a060020a03166375e39f2660408501516000604051610220015260405160e060020a63ffffffff8416028152600481019190915260240161022060405180830381600087803b1515613d8557600080fd5b6102c65a03f11515613d9657600080fd5b5050506040518051906020018051906020018051906020018051906020018051906020018051906020018060a0018060a0018051506020016040525090955086945060029350613de592505050565b60200201525b606083015115613eb257600354600160a060020a03166375e39f2660608501516000604051610220015260405160e060020a63ffffffff8416028152600481019190915260240161022060405180830381600087803b1515613e4c57600080fd5b6102c65a03f11515613e5d57600080fd5b5050506040518051906020018051906020018051906020018051906020018051906020018051906020018060a0018060a0018051506020016040525090955086945060039350613eac92505050565b60200201525b42815111158015613ec7575042602082015111155b8015613ed7575042604082015111155b8015613ee7575042606082015111155b15613ef55760019150613efa565b600091505b5092915050565b613f09614789565b613f11614789565b60008060008061010060405190810160409081526000808352600160208401526002918301919091526003606083015260046080830152600560a0830152600660c0830152600760e083015290955093505b60088460ff1610156140a2578360010192505b60088360ff161015614097578660ff841660088110613f9157fe5b602002015163ffffffff168760ff861660088110613fab57fe5b602002015163ffffffff16101561408c578660ff851660088110613fcb57fe5b602002015191508660ff841660088110613fe157fe5b60200201518760ff861660088110613ff557fe5b63ffffffff9092166020929092020152818760ff85166008811061401557fe5b63ffffffff90921660209290920201528460ff85166008811061403457fe5b602002015190508460ff84166008811061404a57fe5b60200201518560ff86166008811061405e57fe5b60ff92831660209190910291909101528190869085166008811061407e57fe5b60ff90921660209290920201525b600190920191613f76565b600190930192613f63565b509295945050505050565b600063ffffffff808316908416106140c65750816140c9565b50805b92915050565b600063ffffffff808316908416116140e657600080fd5b6013546341c64e6d9063ffffffff166013805463ffffffff1916929091066130390163ffffffff908116929092179081905583918286038116911681151561412a57fe5b06019392505050565b600063ffffffff808416908316106140c65750816140c9565b6000808080600460ff8816146141b657614165886145d0565b63ffffffff166019600a5481151561417957fe5b04029150614193600463ffffffff8a165b046001016145d0565b600954600590600460ff8b1663ffffffff92831602821604165b0402905061423b565b60018615151415614204576141ca886145d0565b63ffffffff166005600a548115156141de57fe5b040291506141f3600463ffffffff8a1661418a565b60095460059063ffffffff166141ad565b61420d886145d0565b63ffffffff16600a5402915061422e60048963ffffffff1681151561418a57fe5b60095463ffffffff160290505b600554600160a060020a03166340c10f19338460006040516020015260405160e060020a63ffffffff8516028152600160a060020a0390921660048301526024820152604401602060405180830381600087803b151561429a57600080fd5b6102c65a03f115156142ab57600080fd5b505050604051805150899050511561433e57600354600160a060020a0316631debbe2f8a51838851640100000000030160006040516020015260405163ffffffff84811660e060020a028252600482019390935291166024820152604401602060405180830381600087803b151561432257600080fd5b6102c65a03f1151561433357600080fd5b505050604051805150505b6020890151156143cf57600354600160a060020a0316631debbe2f60208b0151836020890151640100000000030160006040516020015260405163ffffffff84811660e060020a028252600482019390935291166024820152604401602060405180830381600087803b15156143b357600080fd5b6102c65a03f115156143c457600080fd5b505050604051805150505b60408901511561446057600354600160a060020a0316631debbe2f60408b0151836040890151640100000000030160006040516020015260405163ffffffff84811660e060020a028252600482019390935291166024820152604401602060405180830381600087803b151561444457600080fd5b6102c65a03f1151561445557600080fd5b505050604051805150505b6060890151156144f157600354600160a060020a0316631debbe2f60608b0151836060890151640100000000030160006040516020015260405163ffffffff84811660e060020a028252600482019390935291166024820152604401602060405180830381600087803b15156144d557600080fd5b6102c65a03f115156144e657600080fd5b505050604051805150505b9890975095505050505050565b6145066149d5565b6001815260005b60048160ff16101561458b57601080546145289060006140cf565b63ffffffff1681548110151561453a57fe5b90600052602060002090600891828204019190066004029054906101000a900463ffffffff16826020015160ff83166004811061457357fe5b63ffffffff909216602092909202015260010161450d565b600160a060020a0383166000908152600c6020526040902082908151815460ff191690151517815560208201516145c890600183019060046146e1565b505050505050565b600060026001830163ffffffff1604825b8063ffffffff168263ffffffff16101561462b578190506002828363ffffffff168663ffffffff1681151561461257fe5b040163ffffffff1681151561462357fe5b0491506145e1565b9392505050565b828054828255906000526020600020906007016008900481019282156146d15791602002820160005b8382111561469f57835183826101000a81548163ffffffff021916908363ffffffff160217905550926020019260040160208160030104928301926001030261465b565b80156146cf5782816101000a81549063ffffffff021916905560040160208160030104928301926001030261469f565b505b506146dd9291506149f0565b5090565b6001830191839082156146d15791602002820160008382111561469f57835183826101000a81548163ffffffff021916908363ffffffff160217905550926020019260040160208160030104928301926001030261465b565b60806040519081016040526004815b6000815260001990910190602001816147495790505090565b60806040519081016040526004815b60008152602001906001900390816147715790505090565b61010060405190810160405260008152600760208201614749565b61030060405190810160405260008152601760208201614749565b61030060405190810160409081526000808352602083015281016147e1614762565b81526020016147ee614789565b81526020016147fb614789565b815260006020820181905260409091015290565b6107a0604051908101604052600081526020810161482b614789565b81526020016148386147a4565b81526020016148456147a4565b815260200161485261473a565b905290565b6105006040519081016040526008815b61486f614a11565b8152602001906001900390816148675790505090565b6102006040519081016040526008815b61489d614a2b565b8152602001906001900390816148955790505090565b82600481019282156148e1579160200282015b828111156148e15782518255916020019190600101906148c6565b506146dd929150614a44565b6001830191839082156149705791602002820160005b8382111561494157835183826101000a81548160ff021916908360ff1602179055509260200192600101602081600001049283019260010302614903565b801561496e5782816101000a81549060ff0219169055600101602081600001049283019260010302614941565b505b506146dd929150614a5e565b6003830191839082156146d15791602002820160008382111561469f57835183826101000a81548163ffffffff021916908363ffffffff160217905550926020019260040160208160030104928301926001030261465b565b60a0604051908101604052600081526020810161485261473a565b61126791905b808211156146dd57805463ffffffff191681556001016149f6565b60a060405190810160405260008152600460208201614749565b6040805190810160405260008152600160208201614749565b61126791905b808211156146dd5760008155600101614a4a565b61126791905b808211156146dd57805460ff19168155600101614a645600a165627a7a7230582028f8dae12b06c20afd4d538e3320145a50dced46de19906e10e576dc8c456ed60029000000000000000000000000e7b704652787ff7654b9975ea99c3d1ca04bcf42000000000000000000000000abc7e6c01237e8eef355bba2bf925a730b714d5f0000000000000000000000001f6f71e1e6a56dc348f1ec9a22b200ac44459fe40000000000000000000000001b5242794288b45831ce069c9934a29b89af019700000000000000000000000059bcded9c87ce46ec97c13640bfc0390ceb00e990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012c000000000000000000000000000000000000000000000000000000000000038400000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000004563918244f400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000260000000000000000000000000000000000000000000000000000000000000026000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000260000000000000000000000000000000000000000000000000000000000000000

   Swarm Source:
bzzr://28f8dae12b06c20afd4d538e3320145a50dced46de19906e10e576dc8c456ed6

 

View All
Block Age transaction Difficulty GasUsed Reward
View All
Block Age UncleNumber Difficulty GasUsed Reward
Make sure to use the "Vote Down" button for any spammy posts, and the "Vote Up" for interesting conversations.