-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyWeb3Club.sol
137 lines (106 loc) · 4.3 KB
/
MyWeb3Club.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// SPDX-License-Identifier: MIT
pragma solidity >= 0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.4/contracts/utils/Counters.sol";
import "./MyWeb3ClubNFT.sol";
contract MyWeb3Club{
using Counters for Counters.Counter;
Counters.Counter public _clubIds;
address clubNFTAddress;
struct User{
string avatar;
string nickname;
uint[] joinClubs;
}
struct Club{
uint clubId;
uint category;
address nftContract;
string avatar;
string title;
string description;
address creator;
uint joinCount;
}
mapping(address=>User) public UsersMapping;
mapping(uint=>Club) public ClubsMapping;
mapping(uint=>uint[]) public ClubNFTMapping;
mapping(uint=>uint[]) public CategoriesMapping;
constructor(address _clubNFTAddress){
clubNFTAddress=_clubNFTAddress;
}
function createUserInfo(string memory _avatar,string memory _nickname)public {
uint[] memory joinClubs;
if(UsersMapping[msg.sender].joinClubs.length==0){
joinClubs=new uint[](0);
}else{
joinClubs=UsersMapping[msg.sender].joinClubs;
}
User memory newUser=User(_avatar,_nickname,joinClubs);
UsersMapping[msg.sender]=newUser;
}
function getUserInfo()public view returns(string memory,string memory){
return (UsersMapping[msg.sender].avatar,UsersMapping[msg.sender].nickname);
}
function createClub(uint _category,address _nftContract,string memory _avatar,string memory _title,string memory _description)public{
uint clubId=_clubIds.current();
Club memory club=Club(clubId,_category,_nftContract,_avatar,_title,_description,msg.sender,0);
ClubsMapping[clubId]=club;
CategoriesMapping[_category].push(clubId);
_clubIds.increment();
}
function createNFT(uint clubId,string memory _content,string memory _img)public{
MyWeb3ClubNFT clubNFT=MyWeb3ClubNFT(clubNFTAddress);
uint nftId=clubNFT.mintNFT(msg.sender,_content,_img);
ClubNFTMapping[clubId].push(nftId);
}
function joinClub(uint clubId)public{
UsersMapping[msg.sender].joinClubs.push(clubId);
ClubsMapping[clubId].joinCount=ClubsMapping[clubId].joinCount+1;
}
function quitClub(uint clubId)public{
uint[] memory joinClubIds=UsersMapping[msg.sender].joinClubs;
require(joinClubIds.length>0);
for(uint i=0;i<joinClubIds.length;i++){
if(clubId==joinClubIds[i]){
UsersMapping[msg.sender].joinClubs[i] = UsersMapping[msg.sender].joinClubs[joinClubIds.length - 1];
UsersMapping[msg.sender].joinClubs.pop();
break;
}
}
ClubsMapping[clubId].joinCount=ClubsMapping[clubId].joinCount-1;
}
function getClubNFT(uint _clubId,uint _pageNum,uint _pageSize)public view returns(uint[] memory){
uint[] memory clubNFTs=ClubNFTMapping[_clubId];
uint[] memory clubNFTIds=getDataByPage(clubNFTs,_pageNum,_pageSize);
return clubNFTIds;
}
function getCategoriesData(uint categoriesCount) public view returns(uint [] memory){
uint[] memory categoriesData=new uint[](categoriesCount);
for(uint i=0;i<categoriesCount;i++){
categoriesData[i]=CategoriesMapping[i].length;
}
return categoriesData;
}
function getClubListByCategory(uint category,uint _pageNum,uint _pageSize) public view returns(uint[] memory){
return getDataByPage(CategoriesMapping[category],_pageNum,_pageSize);
}
function getDataByPage(uint[] memory _array,uint _pageNum,uint _pageSize)private pure returns(uint[] memory){
uint arrayLength;
uint arrayIndex=0;
uint dataStart=(_array.length-1) - (_pageNum-1) * _pageSize;
uint dataEnd;
if(dataStart<_pageSize){
dataEnd=0;
arrayLength=dataStart+1;
}else{
dataEnd=dataStart-_pageSize+1;
arrayLength=_pageSize;
}
uint[] memory returnArray=new uint[](arrayLength);
for(int i=int(dataStart);i>=int(dataEnd);i--){
returnArray[arrayIndex]=_array[uint(i)];
arrayIndex++;
}
return returnArray;
}
}