Demo entry 6775498

jjj

   

Submitted by jjj on Nov 25, 2018 at 13:22
Language: JavaScript. Code size: 2.4 kB.

pragma solidity ^ 0.4.18;
contract ChatRoom {
  uint private peopleNum; // 聊天室人数;
  address[] private id_address; // id对应的地址;
  mapping(address => uint) id; // 地址对应的id
  mapping(uint => string) nickname; // id到昵称的对应

  address private owner; // 群主;
  mapping(address => bool) administer; // 是否是管理

  uint private chatNum; // 发言数目;
  string[] private chatRecord; // 发言记录;
  uint[] private speaker_inChat; // 每条发言对应的id;

  function ChatRoom() public {
    peopleNum = 1;
    id_address.push(msg.sender); // 填充0索引而已
    id_address.push(msg.sender);
    id[msg.sender] = peopleNum;

    owner = msg.sender;
    administer[msg.sender] = true;

    chatNum = 0;
  }
  function join(address joiner) public returns(bool) {
    if (id[msg.sender] == 0 || administer[msg.sender] == false)
    {
      return false;
    }
    else
    {
      peopleNum += 1;
      id_address.push(joiner);
      id[joiner] = peopleNum;

      return true;
    }
  }
  function setAdmin(address newAdmin) public returns(bool) {
    if (id[msg.sender] != 1 || id[newAdmin] == 0)
    {
      return false;
    }
    else
    {
      administer[newAdmin] = true;

      return true;
    }
  }
  function speak(string memory _chat) public returns(bool) {
    if (id[msg.sender] == 0)
    {
      return false;
    }
    else
    {
      chatRecord.push(_chat);
      speaker_inChat.push(id[msg.sender]);
      chatNum += 1;
      return true;
    }
  }
  function getSpeak(uint index) view public returns(address, string) {
    address name;
    string memory _speak;
    if (id[msg.sender] == 0)
    {
      return (name, _speak);
    }
    else if (index >= 0 && index < chatNum)
    {
      name = id_address[speaker_inChat[index]];
      _speak = chatRecord[index];
      return (name, _speak);
    }
    else
    {
      return (name, _speak);
    }
  }
//   function getAllSpeak() public returns(address[],string[]) {
//     address[] memory name = new address[](chatNum);
//     string[] memory speak = new string[](chatNum);
//     if (id[msg.sender] == 0)
//     {
//       return (name, speak);
//     }
//     else
//     {
//       for(uint i = 0; i < chatNum; i++) {
//           name[i] = id_address[speaker_inChat[i]];
//       }
//       return (name, chatRecord);
//     }
//   }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).