Skip to main content
PulseScanner.io

Address

0x5f4ed105a176cb1a0ea25be4d3e09673a5ee52ff
Current Holdings
$0.00
TXs sent
not counted
First Active
2026-01-04
block 25,446,062
Last Active
254 days ago
block 25,446,071
Funded By
not identified

Net worth historyi

No net-worth snapshots recorded yet
exact matchPokerDeck_v2solc 0.8.31+commit.fd3a2265runtime exact · creation exact
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

/**
 * @title PokerDeck v2 - Session-Based Architecture
 * @notice Efficient poker game management with session-based structure
 * @dev Step 1: Session Management System
 */
contract PokerDeck_v2 {

    // ============================================
    // STEP 1: SESSION MANAGEMENT
    // ============================================

    /// @notice Session structure containing all game state
    struct Session {
        uint256 sessionId;           // Unique session identifier (derived from block number)
        uint256 blockNumber;         // Block number when session started
        uint256 startTime;           // Timestamp when session started
        uint256 endTime;             // Timestamp when session ended (0 if ongoing)
        address creator;             // Address that created the session
        uint8 playerCount;           // Number of players in this session
        bool isActive;               // Whether session is currently active
        bool isCompleted;            // Whether session has been completed
        bytes4 sessionHash;          // 4 bytes derived from block number for uniqueness
    }

    /// @notice Mapping of sessionId to Session data
    mapping(uint256 => Session) public sessions;

    /// @notice Array of all session IDs for enumeration
    uint256[] public allSessionIds;

    /// @notice Mapping to track which sessions are ongoing
    mapping(uint256 => bool) public ongoingSessions;

    /// @notice Mapping to track completed sessions
    mapping(uint256 => bool) public completedSessions;

    /// @notice Current active session ID (0 if none active)
    uint256 public currentSessionId;

    /// @notice Total number of sessions created
    uint256 public totalSessions;

    /// @notice Maximum players per session
    uint8 public constant MAX_PLAYERS = 9;

    // Events
    event SessionStarted(
        uint256 indexed sessionId,
        uint256 blockNumber,
        bytes4 sessionHash,
        address indexed creator
    );

    event SessionEnded(
        uint256 indexed sessionId,
        uint256 endTime,
        uint8 playerCount
    );

    // Modifiers
    modifier noActiveSession() {
        require(currentSessionId == 0 || !sessions[currentSessionId].isActive,
            "Active session exists");
        _;
    }

    modifier activeSessionExists() {
        require(currentSessionId != 0 && sessions[currentSessionId].isActive,
            "No active session");
        _;
    }

    modifier validSession(uint256 sessionId) {
        require(sessions[sessionId].blockNumber != 0, "Session does not exist");
        _;
    }

    // ============================================
    // SESSION MANAGEMENT FUNCTIONS
    // ============================================

    /**
     * @notice Start a new poker session
     * @dev Derives session ID from block number and creates new session
     * @return sessionId The newly created session ID
     */
    function startNewSession() external noActiveSession returns (uint256 sessionId) {
        // Get current block number
        uint256 blockNum = block.number;

        // Derive 4-byte hash from block number
        // Takes last 4 bytes of the keccak256 hash of block number
        bytes4 sessionHash = bytes4(keccak256(abi.encodePacked(blockNum, block.timestamp)));

        // Create session ID (use block number as base, ensuring uniqueness)
        sessionId = blockNum + totalSessions;

        // Create new session
        Session storage newSession = sessions[sessionId];
        newSession.sessionId = sessionId;
        newSession.blockNumber = blockNum;
        newSession.startTime = block.timestamp;
        newSession.endTime = 0;
        newSession.creator = msg.sender;
        newSession.playerCount = 0;
        newSession.isActive = true;
        newSession.isCompleted = false;
        newSession.sessionHash = sessionHash;

        // Update tracking
        allSessionIds.push(sessionId);
        ongoingSessions[sessionId] = true;
        currentSessionId = sessionId;
        totalSessions++;

        emit SessionStarted(sessionId, blockNum, sessionHash, msg.sender);

        return sessionId;
    }

    /**
     * @notice End the current active session
     * @dev Marks session as completed and inactive
     */
    function endSession() external activeSessionExists {
        Session storage session = sessions[currentSessionId];

        require(session.creator == msg.sender, "Only creator can end session");
        require(session.isActive, "Session already ended");

        // Mark session as ended
        session.isActive = false;
        session.isCompleted = true;
        session.endTime = block.timestamp;

        // Update tracking
        ongoingSessions[currentSessionId] = false;
        completedSessions[currentSessionId] = true;

        emit SessionEnded(currentSessionId, block.timestamp, session.playerCount);

        // Reset current session
        currentSessionId = 0;
    }

    // ============================================
    // SESSION QUERY FUNCTIONS
    // ============================================

    /**
     * @notice Get list of all ongoing sessions
     * @return sessionIds Array of ongoing session IDs
     */
    function listOngoingSessions() external view returns (uint256[] memory sessionIds) {
        // Count ongoing sessions
        uint256 count = 0;
        for (uint256 i = 0; i < allSessionIds.length; i++) {
            if (ongoingSessions[allSessionIds[i]]) {
                count++;
            }
        }

        // Create array and populate
        sessionIds = new uint256[](count);
        uint256 index = 0;
        for (uint256 i = 0; i < allSessionIds.length; i++) {
            if (ongoingSessions[allSessionIds[i]]) {
                sessionIds[index] = allSessionIds[i];
                index++;
            }
        }

        return sessionIds;
    }

    /**
     * @notice Get list of all completed sessions
     * @return sessionIds Array of completed session IDs
     */
    function listCompletedSessions() external view returns (uint256[] memory sessionIds) {
        // Count completed sessions
        uint256 count = 0;
        for (uint256 i = 0; i < allSessionIds.length; i++) {
            if (completedSessions[allSessionIds[i]]) {
                count++;
            }
        }

        // Create array and populate
        sessionIds = new uint256[](count);
        uint256 index = 0;
        for (uint256 i = 0; i < allSessionIds.length; i++) {
            if (completedSessions[allSessionIds[i]]) {
                sessionIds[index] = allSessionIds[i];
                index++;
            }
        }

        return sessionIds;
    }

    /**
     * @notice Get detailed information about a specific session
     * @param sessionId The session ID to query
     * @return session The session details
     */
    function getSessionDetails(uint256 sessionId)
        external
        view
        validSession(sessionId)
        returns (Session memory session)
    {
        return sessions[sessionId];
    }

    /**
     * @notice Get the current active session details
     * @return session The current active session (if any)
     */
    function getCurrentSession() external view returns (Session memory session) {
        require(currentSessionId != 0, "No active session");
        return sessions[currentSessionId];
    }

    /**
     * @notice Check if a session is active
     * @param sessionId The session ID to check
     * @return isActive True if session is active
     */
    function isSessionActive(uint256 sessionId)
        external
        view
        validSession(sessionId)
        returns (bool)
    {
        return sessions[sessionId].isActive;
    }

    /**
     * @notice Get total number of sessions
     * @return Total count of all sessions created
     */
    function getTotalSessions() external view returns (uint256) {
        return totalSessions;
    }

    /**
     * @notice Get all session IDs
     * @return Array of all session IDs
     */
    function getAllSessionIds() external view returns (uint256[] memory) {
        return allSessionIds;
    }

    /**
     * @notice Get session statistics
     * @return total Total sessions created
     * @return ongoing Number of ongoing sessions
     * @return completed Number of completed sessions
     */
    function getSessionStats() external view returns (
        uint256 total,
        uint256 ongoing,
        uint256 completed
    ) {
        total = totalSessions;

        // Count ongoing
        for (uint256 i = 0; i < allSessionIds.length; i++) {
            if (ongoingSessions[allSessionIds[i]]) {
                ongoing++;
            }
            if (completedSessions[allSessionIds[i]]) {
                completed++;
            }
        }

        return (total, ongoing, completed);
    }

    /**
     * @notice Get session hash (4 bytes derived from block number)
     * @param sessionId The session ID to query
     * @return The 4-byte session hash
     */
    function getSessionHash(uint256 sessionId)
        external
        view
        validSession(sessionId)
        returns (bytes4)
    {
        return sessions[sessionId].sessionHash;
    }

    /**
     * @notice Get session duration
     * @param sessionId The session ID to query
     * @return duration Time duration of session (0 if still ongoing)
     */
    function getSessionDuration(uint256 sessionId)
        external
        view
        validSession(sessionId)
        returns (uint256 duration)
    {
        Session memory session = sessions[sessionId];

        if (session.isActive) {
            // Ongoing session - calculate from start to now
            return block.timestamp - session.startTime;
        } else {
            // Completed session - calculate from start to end
            return session.endTime - session.startTime;
        }
    }
}