pragma solidity ^0.4.13;

contract Database
{
    address public m_Owner;
    address public m_Owner2;
    address public m_Creator;
    AbstractRandom m_RandomGen = AbstractRandom(0x3936fba4dc8cf1e2746423a04f5c6b4ade033e81);
    BitGuildToken public tokenContract = BitGuildToken(0x7E43581b19ab509BCF9397a2eFd1ab10233f27dE); // Predefined PLAT token address
    mapping(address => mapping(uint256 => mapping(uint256 => bytes32))) public m_Data;
    mapping(address => bool)  public trustedContracts;

    modifier OnlyOwnerAndContracts()
    {
        require(msg.sender == m_Owner || msg.sender == m_Owner2 || msg.sender== m_Creator || trustedContracts[msg.sender]);
        _;
    }

    function ChangeRandomGen(address rg) public OnlyOwnerAndContracts(){
        m_RandomGen = AbstractRandom(rg);
    }

    function() public payable
    {

    }

    function Database() public
    {
        m_Owner = address(0);
        m_Owner2 = address(0);
        m_Creator = msg.sender;
    }

    function ChangeOwner(address new_owner) OnlyOwnerAndContracts() public
    {
        require(msg.sender == m_Owner || msg.sender == m_Creator || msg.sender == m_Owner2);

        m_Owner = new_owner;
    }

    function ChangeOwner2(address new_owner2) OnlyOwnerAndContracts() public
    {
        require(msg.sender == m_Owner || msg.sender == m_Creator || msg.sender == m_Owner2);

        m_Owner2 = new_owner2;
    }

    function ChangeAddressTrust(address contract_address,bool trust_flag) public OnlyOwnerAndContracts()
    {
        trustedContracts[contract_address] = trust_flag;
    }

    function Store(address user, uint256 category, uint256 index, bytes32 data) public OnlyOwnerAndContracts()
    {
        m_Data[user][category][index] = data;
    }

    function Load(address user, uint256 category, uint256 index) public view returns (bytes32)
    {
        return m_Data[user][category][index];
    }

    function TransferFunds(address target, uint256 transfer_amount) public OnlyOwnerAndContracts()
    {
        tokenContract.transfer(target,transfer_amount);
    }

    function getRandom(uint256 _upper, uint8 _seed) public OnlyOwnerAndContracts() returns (uint256 number){
        number = m_RandomGen.random(_upper,_seed);

        return number;
    }
 
    

}
contract BitGuildToken{
    function transfer(address _to, uint256 _value) public;
}
contract AbstractRandom
{
    function random(uint256 upper, uint8 seed) public returns (uint256 number);
}