Created
June 24, 2019 17:27
-
-
Save jduartedj/1fd612ffe76aa0a11df3570ef5e6d642 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=true&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
contract Mensagens { | |
//lista de nomes de pessoas | |
address[] public pessoas; | |
mapping(address => string) public nomes; | |
struct Mensagem { | |
string mensagem; | |
address recetor; | |
address emissor; | |
} | |
// lista de Mensagems recebidas em address | |
mapping(address => Mensagem[]) public recebidas; | |
// lista de mensagens enviadas por address | |
mapping(address => Mensagem[]) enviadas; | |
function enviaMensagem (string memory _texto, address recetor) public{ | |
Mensagem memory mess = Mensagem({ | |
mensagem: _texto, | |
recetor: recetor, | |
emissor: msg.sender | |
}); | |
recebidas[recetor].push( mess ); | |
enviadas[msg.sender].push( mess); | |
} | |
function registaPessoa (string memory _nome) public { | |
require(bytes(nomes[msg.sender]).length <= 0); | |
require(bytes(_nome).length > 0); | |
pessoas.push(msg.sender); | |
nomes[msg.sender] = _nome; | |
} | |
function listaPessoas () public returns(string memory){ | |
string memory ret = "Pessoas"; | |
for(uint8 i=0; i<pessoas.length; i++ ){ | |
ret = string(abi.encodePacked(ret , ";" , nomes[pessoas[i]])); | |
} | |
return ret; | |
} | |
function listaMensagens () public returns(string memory){ | |
string memory ret = ";;Recebidas"; | |
for(uint8 i=0; i<recebidas[msg.sender].length; i++ ){ | |
ret = string(abi.encodePacked(ret , ";" , recebidas[msg.sender][i].mensagem, ";" , recebidas[msg.sender][i].recetor, ";" , recebidas[msg.sender][i].emissor)); | |
} | |
ret = string(abi.encodePacked(ret , ";;Enviadas")); | |
for(uint8 i=0; i<enviadas[msg.sender].length; i++ ){ | |
ret = string(abi.encodePacked(ret , ";" , enviadas[msg.sender][i].mensagem, ";", enviadas[msg.sender][i].recetor, ";" , enviadas[msg.sender][i].emissor)); | |
} | |
return ret; | |
} | |
constructor() public { | |
pessoas.push(address(0)); | |
nomes[address(0)] = "Sistema"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment