-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMRSmartContract.sol
149 lines (136 loc) · 4.12 KB
/
GMRSmartContract.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
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract GMRSmartContract {
struct Booking {
uint256 startTime;
uint256 endTime;
address customer;
string machine;
bool isbooked;
}
Booking[] public bookings;
string[] public machines;
uint256 slotPrice = 50;
address public owner;
constructor() public {
owner = msg.sender;
machines.push("A");
machines.push("B");
machines.push("C");
machines.push("D");
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function book(
string memory _machine_name,
uint256 _start_time,
uint256 _end_time
) public payable {
require(msg.value >= slotPrice);
require(checkTime(_start_time, _end_time));
require(checkSlot(_machine_name, _start_time, _end_time));
require(checkMachine(_machine_name));
bookings.push(
Booking(_start_time, _end_time, msg.sender, _machine_name, true)
);
}
function checkSlot(
string memory _machine_name,
uint256 _start_time,
uint256 _end_time
) public view returns (bool) {
bool slot_free = true;
for (uint256 i = 0; i < bookings.length; i++) {
if (
(keccak256(bytes(bookings[i].machine)) ==
keccak256(bytes(_machine_name))) &&
(bookings[i].startTime <= _start_time) &&
(bookings[i].endTime > _start_time)
) {
slot_free = false;
} else if (
(keccak256(bytes(bookings[i].machine)) ==
keccak256(bytes(_machine_name))) &&
(bookings[i].startTime < _end_time) &&
(bookings[i].endTime > _end_time)
) {
slot_free = false;
}
}
return slot_free;
}
function checkMachine(string memory _machine_name)
public
view
returns (bool)
{
bool machine_exists = false;
for (uint256 i = 0; i < machines.length; i++) {
if (
keccak256(bytes(machines[i])) == keccak256(bytes(_machine_name))
) {
machine_exists = true;
break;
}
}
return machine_exists;
}
function checkAccess(string memory _machine_name, uint256 _time)
public
view
returns (bool)
{
bool access_exists = true;
for (uint256 i = 0; i < bookings.length; i++) {
if (
(keccak256(bytes(bookings[i].machine)) ==
keccak256(bytes(_machine_name))) &&
(bookings[i].startTime <= _time) &&
(bookings[i].endTime > _time)
) {
access_exists = false;
}
}
return access_exists;
}
function checkTime(uint256 _start_time, uint256 _end_time)
internal
pure
returns (bool)
{
bool valid_time = false;
if ((_start_time > 0) && (_start_time < 2400)) {
valid_time = true;
}
if ((_end_time > 0) && (_end_time < 2400)) {
valid_time = true;
}
if (_start_time - (_start_time % 100) < 60) {
valid_time = true;
}
if (_end_time - (_end_time % 100) < 60) {
valid_time = true;
}
if (_start_time % 100 == _end_time % 100) {
if (_end_time - _start_time == 30) {
valid_time = true;
}
} else if ((_end_time % 100) - (_start_time % 100) == 1) {
if (_end_time - _start_time - 40 == 30) {
valid_time = true;
}
}
return valid_time;
}
function addMachine(string memory _machine_name) public onlyOwner {
machines.push(_machine_name);
}
function setSlotPrice(uint256 _slot_price) public onlyOwner {
slotPrice = _slot_price;
}
function withdraw(address _recipient) public payable onlyOwner {
payable(_recipient).transfer(address(this).balance);
}
}