SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity >= 0.4.22 < 0.6.0;
  2. pragma experimental ABIEncoderV2;
  3.  
  4. contract WakeUpEarly {
  5.    
  6.    
  7.     // Ranking schema
  8.     enum Stars { NaN , bronze , silver, gold , platinum, diamond }
  9.    
  10.  
  11.     // Estrutura de um dorminhoco
  12.     struct Sleeper {
  13.         bool awake;
  14.         uint lastDay;
  15.         uint initMoney;
  16.         uint accumulatedMoney;
  17.         uint accumulatedDays;
  18.         uint howManyDays;
  19.         uint howHour;
  20.         Stars rank;
  21.        
  22.     }
  23.  
  24.     // Endereço do atual Usuario do contrato
  25.     address chairperson;
  26.     mapping(address => Sleeper) sleepers;
  27.    
  28.    
  29.  
  30.  
  31.     // Evento para imutalibilizar no ABI pra acessar no JS
  32.     event WakeUp(Sleeper _person);
  33.  
  34.  
  35.  
  36.     constructor() public payable{
  37.            
  38.         chairperson = msg.sender;
  39.         sleepers[chairperson].initMoney = msg.value;
  40.         sleepers[chairperson].rank = Stars.bronze;
  41.        
  42.     }
  43.    
  44.    
  45.    
  46.    
  47.     function setWakeUp() public {
  48.        
  49.         sleepers[chairperson].awake = true;
  50.         emit WakeUp(sleepers[chairperson]);
  51.         sleepers[chairperson].awake = false;
  52.     }
  53.    
  54.     function getWakeUp() public view returns (bool){
  55.         return sleepers[chairperson].awake;
  56.        
  57.     }
  58.  
  59.     modifier onlyOwner {
  60.         require(msg.sender == chairperson);
  61.         _;
  62.     }
  63.  
  64.     function setHowManyDays(uint _howManyDays) public {
  65.         sleepers[chairperson].howManyDays = _howManyDays;
  66.     }
  67.    
  68.     function setHourToWakeUp(uint _howHour) public {
  69.         sleepers[chairperson].howHour = _howHour;
  70.  
  71.     }
  72.    
  73.    
  74.     // Funcionando!
  75.     function getInitMoney() public view returns (uint){
  76.         return sleepers[chairperson].initMoney;
  77.     }
  78.    
  79.        
  80.     // Funcionando!
  81.     function getAccumulatedMoney() public view returns (uint){
  82.         return sleepers[chairperson].accumulatedMoney;
  83.     }
  84.  
  85.      // Funcionando!
  86.     function getAccumulatedDays() public view returns (uint){
  87.         return sleepers[chairperson].accumulatedDays;
  88.     }
  89.    
  90.     // Funcionando!
  91.     function getHowManyDays() public view returns (uint) {
  92.         return sleepers[chairperson].howManyDays;
  93.     }
  94.  
  95.     // Funcionando!
  96.     function getHowHours() public view returns(uint){
  97.         return sleepers[chairperson].howHour;
  98.     }
  99.    
  100.     // Como retorna o Enum Type?
  101.     function getRank() public view returns(Stars){
  102.         return sleepers[chairperson].rank;
  103.     }
  104.    
  105.     function setBronze() internal {
  106.         sleepers[chairperson].rank = Stars.bronze;
  107.     }
  108.  
  109.     function setSilver() internal {
  110.         sleepers[chairperson].rank = Stars.silver;
  111.        
  112.     }
  113.    
  114.     function setGold() internal {
  115.         sleepers[chairperson].rank = Stars.gold;
  116.     }
  117.  
  118.     function setPlatinum() internal {
  119.         sleepers[chairperson].rank = Stars.platinum;
  120.        
  121.     }
  122.  
  123.     function setDiamond() internal {
  124.         sleepers[chairperson].rank = Stars.diamond;
  125.     }
  126.    
  127.    
  128.  
  129.    
  130.     function checkAndUpdateRank() internal {
  131.         if( sleepers[chairperson].accumulatedDays > 7) {
  132.             setBronze();
  133.         }
  134.         else if (sleepers[chairperson].accumulatedDays > 30) {
  135.             setSilver();
  136.         }
  137.         else if (sleepers[chairperson].accumulatedDays > 90) {
  138.             setGold();
  139.         }
  140.         else if (sleepers[chairperson].accumulatedDays > 180) {
  141.             setPlatinum();
  142.         }
  143.         else if (sleepers[chairperson].accumulatedDays > 360) {
  144.             setDiamond();
  145.         }
  146.     }
  147.    
  148.    
  149.    // Funcionando
  150.     function getMidnightHourBasedInThatDay() internal view  returns (uint){
  151.         return (now / 1 days * 1 days);
  152.    }
  153.  
  154.    // Funcionando
  155.  
  156.     function checkHour(uint _howHour) internal view returns (bool){
  157.         if (now - getMidnightHourBasedInThatDay() < _howHour *  1 hours){
  158.             return true;
  159.         }
  160.         return false;
  161.  
  162.     }
  163.    
  164.    
  165.     function getTest() public view returns (uint){
  166.     return (now - getMidnightHourBasedInThatDay());
  167.     }
  168.    
  169.     function getTest2() public view returns (uint){
  170.     return (1 / sleepers[chairperson].howManyDays);
  171.     }
  172.    
  173.    
  174.  
  175.  
  176. // Não funcionado
  177.     function alarmButton() public {
  178.         sleepers[chairperson].accumulatedMoney = sleepers[chairperson].initMoney;
  179.        
  180.         checkAndUpdateRank();
  181.        
  182.         if(checkHour(sleepers[chairperson].howHour) == true){
  183.             sleepers[chairperson].accumulatedDays = sleepers[chairperson].accumulatedDays + 1;
  184.         }else{
  185.  
  186.             sleepers[chairperson].accumulatedMoney = sleepers[chairperson].accumulatedMoney - (
  187.             ((sleepers[chairperson].accumulatedMoney / 1) * (1 / sleepers[chairperson].howManyDays)) );
  188.  
  189.            
  190.         }
  191.  
  192.  
  193.     }
  194.  
  195.  
  196. }
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