Okay, I understand. Here's an updated CinemaBooking.sol optimized for Remix IDE, building upon your Reserved contract as a foundation, and extending it to fully implement the cinema booking features. I'll provide the complete file content. This is designed to be easily copied and pasted into a single file in Remix.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract CinemaBooking is AccessControl, ReentrancyGuard {
// Roles Definition
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
// Structs
struct Movie {
uint256 id;
string title;
uint256 price; // Price in Wei
}
struct Showtime {
uint256 movieId;
uint256 time; // Unix timestamp
uint256 seatsAvailable;
uint256 totalSeats;
uint256 reserved; // Bitmask for seats reserved in THIS showtime
mapping(uint8 => address) seats; //Seat Number -> Address
}
struct Ticket {
uint256 showtimeId;
address buyer;
uint8 seatNumber; // Which seat the user booked
uint256 purchaseTime;
}
// State Variables
uint256 public movieCount;
uint256 public showtimeCount;
uint256 public ticketCount;
mapping(uint256 => Movie) public movies;
mapping(uint256 => Showtime) public showtimes;
mapping(uint256 => Ticket) public tickets;
mapping(address => uint256[]) public userTickets; // User's booked tickets
// Events
event MovieAdded(uint256 indexed movieId, string title, uint256 price);
event ShowtimeAdded(uint256 indexed showtimeId, uint256 movieId, uint256 time, uint256 totalSeats);
event TicketPurchased(uint256 indexed ticketId, uint256 showtimeId, address buyer, uint8 seatNumber);
event FundsWithdrawn(address indexed owner, uint256 amount);
event SeatReserved(uint256 showtimeId, address indexed reservedBy, uint8 seatNumber); //Added Seat Reserved Event
// Constructor
constructor() {
// Grant owner all roles
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_grantRole(ADMIN_ROLE, msg.sender);
}
// Modifiers
modifier onlyOwner() {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "Only owner can call this");
_;
}
modifier onlyAdmin() {
require(hasRole(ADMIN_ROLE, msg.sender) || hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "Only admin/owner can call this");
_;
}
// Movie Management (Owner/Admin)
function addMovie(string memory _title, uint256 _price) external onlyAdmin {
movieCount++;
movies[movieCount] = Movie(movieCount, _title, _price);
emit MovieAdded(movieCount, _title, _price);
}
function updateMoviePrice(uint256 _movieId, uint256 _newPrice) external onlyAdmin {
require(_movieId > 0 && _movieId <= movieCount, "Invalid movie ID");
movies[_movieId].price = _newPrice;
}
// Showtime Management (Owner/Admin)
function addShowtime(uint256 _movieId, uint256 _time, uint256 _totalSeats) external onlyAdmin {
require(_movieId > 0 && _movieId <= movieCount, "Invalid movie ID");
require(_time > block.timestamp, "Showtime must be in the future");
require(_totalSeats > 0 && _totalSeats <= 256 , "Seats must be greater than 0 and less than 256");
showtimeCount++;
showtimes[showtimeCount] = Showtime(_movieId, _time, _totalSeats, _totalSeats, 0);
emit ShowtimeAdded(showtimeCount, _movieId, _time, _totalSeats);
}
// Ticket Booking (Users)
function buyTicket(uint256 _showtimeId, uint8 _seatNumber) external payable nonReentrant {
require(_showtimeId > 0 && _showtimeId <= showtimeCount, "Invalid showtime ID");
Showtime storage showtime = showtimes[_showtimeId];
Movie storage movie = movies[showtime.movieId];
require(_seatNumber < showtime.totalSeats, "Seat number not available!"); //Valid Seat Number
require((showtime.reserved & (1 << _seatNumber)) == 0, "Seat already reserved!"); //Valid Seat Number already reserved or not
require(msg.value == movie.price, "Incorrect Ether sent");
require(showtime.time > block.timestamp, "Showtime has passed");
require(showtime.seatsAvailable > 0, "No seats available");
// Issue ticket
ticketCount++;
tickets[ticketCount] = Ticket(_showtimeId, msg.sender, _seatNumber, block.timestamp);
userTickets[msg.sender].push(ticketCount);
// Update seats
showtime.seatsAvailable--;
showtime.reserved |= (1 << _seatNumber); // Mark seat as reserved
showtime.seats[_seatNumber] = msg.sender; // Assign seat to buyer
emit TicketPurchased(ticketCount, _showtimeId, msg.sender, _seatNumber);
emit SeatReserved(_showtimeId, msg.sender, _seatNumber);
}
// Admin/Owner Views
function getTotalTicketsSold() external view onlyAdmin returns (uint256) {
return ticketCount;
}
function getRevenue() external view onlyAdmin returns (uint256) {
return address(this).balance;
}
function getUserTickets(address _user) external view onlyAdmin returns (uint256[] memory) {
return userTickets[_user];
}
function getMovieDetails(uint256 _movieId) external view onlyAdmin returns (string memory, uint256) {
require(_movieId > 0 && _movieId <= movieCount, "Invalid movie ID");
Movie storage movie = movies[_movieId];
return (movie.title, movie.price);
}
function getReservedSeatsByShowtime(uint256 _showtimeId) external view onlyAdmin returns (uint8[] memory) {
require(_showtimeId > 0 && _showtimeId <= showtimeCount, "Invalid showtime ID");
Showtime storage showtime = showtimes[_showtimeId];
uint8 count = 0;
// Count reserved seats first
for (uint8 i = 0; i < showtime.totalSeats; i++) {
if ((showtime.reserved & (1 << i)) != 0) { // Check if seat i is reserved using bitmask
count++;
}
}
// Populate the result array
uint8[] memory result = new uint8[](count);
uint8 index = 0;
for (uint8 i = 0; i < showtime.totalSeats; i++) {
if ((showtime.reserved & (1 << i)) != 0) { // Check if seat i is reserved using bitmask
result[index] = i;
index++;
}
}
return result;
}
// Owner Functions
function withdrawFunds() external onlyOwner {
uint256 balance = address(this).balance;
require(balance > 0, "No funds to withdraw");
payable(msg.sender).transfer(balance);
emit FundsWithdrawn(msg.sender, balance);
}
function addAdmin(address _newAdmin) external onlyOwner {
_grantRole(ADMIN_ROLE, _newAdmin);
}
function removeAdmin(address _admin) external onlyOwner {
revokeRole(ADMIN_ROLE, _admin);
}
// Fallback function to receive Ether
receive() external payable {}
}