SHARE
TWEET

Untitled

a guest Aug 18th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity^0.5.2;
  2.  
  3. contract RoomManager{
  4.      
  5.   Room[] private rooms; // lists all rooms
  6.   User[] private users; // lists all users
  7.   address[] private adr; // used to convert address to [address]
  8.   string[] private name; // same as above, both containing all adr and names
  9.   uint[] private prn; // same as above, to hold all random number
  10.   uint private winningNumber;
  11.  
  12.   address payable winner;
  13.  
  14.   struct Room{                               // to store all the bid info
  15.       string[] names;           // array of players (just names - can have duplicate)
  16.       address[] accounts;       // array of addresses playing (to cross check some requirements)
  17.       uint cap;                 // the amount of players playing
  18.       uint cost;                // the cost of the bid / the amount paid by instantiater
  19.       uint timer;               // time before possible winner
  20.       uint[] APRNG;               // stores ALL the numbers
  21.       uint timeStamp;           // to keep track of time if needed in requirements
  22.       bool status;              // if the room is still accessible or ON
  23.   }
  24.  
  25.   struct User{                              // to store users data privately
  26.       string name;
  27.       address payable account;
  28.   }
  29.  
  30.   constructor () public {
  31.       rooms.length = 1;
  32.       users.length = 1;
  33.   }
  34.  
  35.   mapping (address => uint) addressToUserID;    // to get access to users info by their address
  36.  
  37.   mapping (address => uint) addressToRoomID;    // to get access to users rooms by their address
  38.   mapping (uint => uint) costToRoomID;          // to get access to rooms by their cost
  39.   mapping (bool => uint) statusToRoomID;        // to get all the active rooms
  40.  
  41.  
  42.   // internal function to get rooms by users address
  43.   function _roomID() view internal returns (uint) {
  44.       uint ID = addressToRoomID[msg.sender];
  45.       return ID;
  46.   }
  47.  
  48.   // internal function to get users details by users address
  49.   function _userID() view internal returns (uint) {
  50.       uint ID = addressToUserID[msg.sender];
  51.       return ID;
  52.   }
  53.  
  54.   // internal function to get rooms by cost
  55.   function _roomIDC() view internal returns(uint) {
  56.       uint ID = costToRoomID[msg.value];
  57.       return ID;
  58.   }
  59.  
  60.   // public function to create an user with a nickname
  61.   function addUser(string memory _name) public {
  62.      
  63.       require(bytes(_name).length != 0 && bytes(_name).length <= 16,
  64.       'Name must not be empthy or greater than 16 letters long. Reverting');
  65.      
  66.       require(msg.sender != address(0),
  67.       'Invalid address, please provide a valid ETH address. Reverting');
  68.      
  69.        //gets a number for each user
  70.       uint userIndex = users.length;
  71.      
  72.       // links it to the public key
  73.       addressToUserID[msg.sender] = userIndex;
  74.  
  75.       // adds it to the users array
  76.       users.push(User(                          
  77.       _name,
  78.       msg.sender
  79.       ));
  80.   }
  81.  
  82.   // public function to get private details of your user
  83.   function getUser() view public returns (string memory) {
  84.      
  85.       require(users[_userID()].account != address(0),
  86.       'requires an User, create one calling addUser(string _nickname). Reverting');
  87.      
  88.       return users[_userID()].name;
  89.   }
  90.  
  91.   function addRoom(uint cap, uint timer, uint PRNG) public payable {
  92.      
  93.     require(PRNG != 0, 'no PRNG detected. Reverting');
  94.    
  95.     require(users[_userID()].account.balance >= msg.value && msg.value >= 0.01 ether,
  96.     'Not the right amount of Ether was sent or you either missing an User. Min 0.01 Ether. Reverting');
  97.    
  98.     require(cap != 0 && timer != 0,
  99.     'Neither max partecipants nor timer can be empthy. Revering');
  100.    
  101.     require(now < (timer + now) && timer >= 10,
  102.     'timer has to be at least 10 seconds. Reverting');
  103.    
  104.     require(cap >= 3,
  105.     'at least 3 partecipants are required. Reverting');
  106.    
  107.     // converting some values to be stored in arrays
  108.     name.push(users[_userID()].name);
  109.     adr.push(msg.sender);
  110.     prn.push(PRNG);
  111.    
  112.     //giving a room id number
  113.     uint roomIndex = rooms.length;
  114.     addressToRoomID[msg.sender] = roomIndex;
  115.     costToRoomID[msg.value] = roomIndex;
  116.     statusToRoomID[true] = roomIndex;
  117.    
  118.    
  119.     //adding room to rooms array
  120.     rooms.push(Room(
  121.      name,
  122.      adr,
  123.      cap,
  124.      msg.value,
  125.      timer,
  126.      prn,
  127.      now,
  128.      true
  129.     ));  
  130.   }
  131.  
  132.   function getRoom() view public returns (address[] memory, uint, uint, uint) {
  133.     Room memory room = rooms[statusToRoomID[true]];
  134.     return (room.accounts,
  135.             room.cap,
  136.             room.cost,
  137.             room.timer
  138.     );
  139.   }
  140.  
  141.  
  142.  function exists() view public returns (bool) {
  143.     return addressToRoomID[msg.sender] != 0;
  144.   }
  145.  
  146.     function joinRoom (uint PRNG) public payable {
  147.         Room storage room = rooms[statusToRoomID[true]];  
  148.        
  149.         require(msg.sender == users[_userID()].account,
  150.         'no user was found, please create one. Reverting');
  151.        
  152.         require(room.cap >= room.accounts.length-1,
  153.         'max partecipants reached, try another room. Reverting');
  154.        
  155.         require(room.timer + room.timeStamp >= now || room.status == true,
  156.         'bid is over. Reverting');
  157.        
  158.         require(msg.value == room.cost,
  159.         'sent ether is not equal to bid value. Reverting');
  160.  
  161.         room.APRNG.push(PRNG); // to check if actually updates the value
  162.         room.names.push(users[_userID()].name); // adds name to room
  163.         room.accounts.push(users[_userID()].account); // adds account to addresses of room
  164.    }
  165.  
  166.     function close() public {
  167.         Room memory room = rooms[statusToRoomID[true]];
  168.             if(room.status == true && room.cap == room.names.length || room.status == true && room.timer + room.timeStamp <= now) {
  169.                 getWinningNumber();
  170.             }
  171.         }
  172.    
  173.    
  174.     function getWinningNumber() internal {
  175.         Room storage room = rooms[statusToRoomID[true]];
  176.         uint CPRNG = 0;
  177.        
  178.         for(uint i = 0; i <= room.APRNG.length; i++) {
  179.             CPRNG += room.APRNG[i];
  180.            
  181.             if(i == room.APRNG.length) {
  182.                 extractWinner(CPRNG);
  183.             }
  184.         }
  185.     }            
  186.        
  187.     function extractWinner(uint _CPRNG) internal {
  188.         Room storage room = rooms[statusToRoomID[true]];
  189.         uint x = 0;
  190.         for(uint y=0; y <= _CPRNG; y++) {
  191.             if(x <= room.accounts.length) {
  192.                x++;
  193.             }
  194.             else{x=0;}
  195.             if(y == _CPRNG) {
  196.                 winner == room.accounts[x];
  197.                 winner.transfer(address(this).balance);
  198.                 room.status = false;
  199.             }
  200.         }
  201.     }
  202. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top