Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity^0.5.2;
- contract RoomManager{
- Room[] private rooms; // lists all rooms
- User[] private users; // lists all users
- address[] private adr; // used to convert address to [address]
- string[] private name; // same as above, both containing all adr and names
- uint[] private prn; // same as above, to hold all random number
- uint private winningNumber;
- address payable winner;
- struct Room{ // to store all the bid info
- string[] names; // array of players (just names - can have duplicate)
- address[] accounts; // array of addresses playing (to cross check some requirements)
- uint cap; // the amount of players playing
- uint cost; // the cost of the bid / the amount paid by instantiater
- uint timer; // time before possible winner
- uint[] APRNG; // stores ALL the numbers
- uint timeStamp; // to keep track of time if needed in requirements
- bool status; // if the room is still accessible or ON
- }
- struct User{ // to store users data privately
- string name;
- address payable account;
- }
- constructor () public {
- rooms.length = 1;
- users.length = 1;
- }
- mapping (address => uint) addressToUserID; // to get access to users info by their address
- mapping (address => uint) addressToRoomID; // to get access to users rooms by their address
- mapping (uint => uint) costToRoomID; // to get access to rooms by their cost
- mapping (bool => uint) statusToRoomID; // to get all the active rooms
- // internal function to get rooms by users address
- function _roomID() view internal returns (uint) {
- uint ID = addressToRoomID[msg.sender];
- return ID;
- }
- // internal function to get users details by users address
- function _userID() view internal returns (uint) {
- uint ID = addressToUserID[msg.sender];
- return ID;
- }
- // internal function to get rooms by cost
- function _roomIDC() view internal returns(uint) {
- uint ID = costToRoomID[msg.value];
- return ID;
- }
- // public function to create an user with a nickname
- function addUser(string memory _name) public {
- require(bytes(_name).length != 0 && bytes(_name).length <= 16,
- 'Name must not be empthy or greater than 16 letters long. Reverting');
- require(msg.sender != address(0),
- 'Invalid address, please provide a valid ETH address. Reverting');
- //gets a number for each user
- uint userIndex = users.length;
- // links it to the public key
- addressToUserID[msg.sender] = userIndex;
- // adds it to the users array
- users.push(User(
- _name,
- msg.sender
- ));
- }
- // public function to get private details of your user
- function getUser() view public returns (string memory) {
- require(users[_userID()].account != address(0),
- 'requires an User, create one calling addUser(string _nickname). Reverting');
- return users[_userID()].name;
- }
- function addRoom(uint cap, uint timer, uint PRNG) public payable {
- require(PRNG != 0, 'no PRNG detected. Reverting');
- require(users[_userID()].account.balance >= msg.value && msg.value >= 0.01 ether,
- 'Not the right amount of Ether was sent or you either missing an User. Min 0.01 Ether. Reverting');
- require(cap != 0 && timer != 0,
- 'Neither max partecipants nor timer can be empthy. Revering');
- require(now < (timer + now) && timer >= 10,
- 'timer has to be at least 10 seconds. Reverting');
- require(cap >= 3,
- 'at least 3 partecipants are required. Reverting');
- // converting some values to be stored in arrays
- name.push(users[_userID()].name);
- adr.push(msg.sender);
- prn.push(PRNG);
- //giving a room id number
- uint roomIndex = rooms.length;
- addressToRoomID[msg.sender] = roomIndex;
- costToRoomID[msg.value] = roomIndex;
- statusToRoomID[true] = roomIndex;
- //adding room to rooms array
- rooms.push(Room(
- name,
- adr,
- cap,
- msg.value,
- timer,
- prn,
- now,
- true
- ));
- }
- function getRoom() view public returns (address[] memory, uint, uint, uint) {
- Room memory room = rooms[statusToRoomID[true]];
- return (room.accounts,
- room.cap,
- room.cost,
- room.timer
- );
- }
- function exists() view public returns (bool) {
- return addressToRoomID[msg.sender] != 0;
- }
- function joinRoom (uint PRNG) public payable {
- Room storage room = rooms[statusToRoomID[true]];
- require(msg.sender == users[_userID()].account,
- 'no user was found, please create one. Reverting');
- require(room.cap >= room.accounts.length-1,
- 'max partecipants reached, try another room. Reverting');
- require(room.timer + room.timeStamp >= now || room.status == true,
- 'bid is over. Reverting');
- require(msg.value == room.cost,
- 'sent ether is not equal to bid value. Reverting');
- room.APRNG.push(PRNG); // to check if actually updates the value
- room.names.push(users[_userID()].name); // adds name to room
- room.accounts.push(users[_userID()].account); // adds account to addresses of room
- }
- function close() public {
- Room memory room = rooms[statusToRoomID[true]];
- if(room.status == true && room.cap == room.names.length || room.status == true && room.timer + room.timeStamp <= now) {
- getWinningNumber();
- }
- }
- function getWinningNumber() internal {
- Room storage room = rooms[statusToRoomID[true]];
- uint CPRNG = 0;
- for(uint i = 0; i <= room.APRNG.length; i++) {
- CPRNG += room.APRNG[i];
- if(i == room.APRNG.length) {
- extractWinner(CPRNG);
- }
- }
- }
- function extractWinner(uint _CPRNG) internal {
- Room storage room = rooms[statusToRoomID[true]];
- uint x = 0;
- for(uint y=0; y <= _CPRNG; y++) {
- if(x <= room.accounts.length) {
- x++;
- }
- else{x=0;}
- if(y == _CPRNG) {
- winner == room.accounts[x];
- winner.transfer(address(this).balance);
- room.status = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement