-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardController.sol
99 lines (84 loc) · 3.61 KB
/
BoardController.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import './CardController.sol';
import './GameConstants.sol';
import './BoardField.sol';
contract BoardController is BoardField, GameConstants {
mapping(uint256 => Board) roomBoard;
struct Board {
mapping(uint256 => uint256) cardPositionToOwnerId; // 0 -> bank | 1 -> 1st player (0 index)
mapping(uint256 => uint256) cardPositionToLevel;
}
function getDowngradeCost(uint16[] calldata cards) internal view returns(uint256) {
uint256 totalCost = 0;
for (uint256 i = 0; i < cards.length; i++) {
Card storage card = monopolyField[cards[i]];
totalCost += card.price / 2;
}
return totalCost;
}
function getUpgradeCost(uint16[] calldata cards) internal view returns(uint256) {
return getDowngradeCost(cards) + (cards.length) * UPGRADE_DELTA;
}
function upgradeCards(uint256 roomId, uint16[] calldata cards) internal returns(bool) {
for (uint256 i = 0; i < cards.length; i++) {
Card storage card = monopolyField[cards[i]];
require(
card.cardType == CardType.Property,
"Card is not property, you cannot upgrade it"
);
Board storage board = roomBoard[roomId];
board.cardPositionToLevel[cards[i]] += 1;
}
return true;
}
function downgradeCards(uint256 roomId, uint16[] calldata cards) internal returns(bool) {
for (uint256 i = 0; i < cards.length; i++) {
Card storage card = monopolyField[cards[i]];
require(
card.cardType == CardType.Property,
"Card is not property, you cannot upgrade it"
);
Board storage board = roomBoard[roomId];
board.cardPositionToLevel[cards[i]] -= 1;
}
return true;
}
function isCardFree(uint256 roomId, uint256 position) public view returns(bool) {
Board storage board = roomBoard[roomId];
if (!isProperty(position)) {
return false;
}
return board.cardPositionToOwnerId[position] != 0;
}
function isCardOwned(uint256 roomId, uint256 position) public view returns(bool) {
Board storage board = roomBoard[roomId];
if (!isProperty(position)) {
return false;
}
return board.cardPositionToOwnerId[position] == 0;
}
function isCardType(uint256 position, CardType cardType) public view returns(bool) {
return monopolyField[position].cardType == cardType;
}
function isProperty(uint256 position) public view returns(bool) {
return monopolyField[position].cardType == CardType.Property;
}
function getCardPrice(uint256 position) public view returns(uint256) {
return monopolyField[position].price;
}
function getCardRent(uint256 roomId, uint256 position) public view returns(uint256) {
Board storage board = roomBoard[roomId];
uint256 cardLevel = board.cardPositionToLevel[position];
Card storage card = monopolyField[position];
return card.rentPrices[cardLevel];
}
function getCardOwnerId(uint256 roomId, uint256 position) public view returns(uint256) {
Board storage board = roomBoard[roomId];
return board.cardPositionToOwnerId[position];
}
function setCardOwnerId(uint256 roomId, uint256 position, uint256 ownerId) internal {
Board storage board = roomBoard[roomId];
board.cardPositionToOwnerId[position] = ownerId;
}
}