summaryrefslogtreecommitdiff
path: root/Year_3/Blockchain/RockPaperScissor.sol
blob: 528bc4c297ac4859e48564cfe8d917ff5fe06199 (plain)
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
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "TrustworthyRockPaperScissorsTournamentSpecs.sol";

contract TrustworthyRockPaperScissorsTournament is TrustworthyRockPaperScissorsTournamentSpecs {
    enum Move { ROCK, PAPER, SCISSOR }
    enum Status { IN_PROGRESS, END }
    struct PlayerInfo {
        address payable id;
        uint8 wins;
        Move[] games;
        uint fees;
    }

    mapping(Player => PlayerInfo) players;
    uint8 target;
    uint256 matchFee;
    Status status;
    Player winner;
    address payable owner;

    uint8 override public disputedMatches;
 
    constructor(
        address payable firstPlayer,
        address payable secondPlayer,
        uint8 targetWins,
        uint256 singleMatchFee
    ) {
        require(firstPlayer != secondPlayer, "Players must be two different addresses.");
        require(firstPlayer != address(0) && secondPlayer != address(0), "Players address are not valid.");

        players[Player.First].id = firstPlayer;
        players[Player.Second].id = secondPlayer;
        target = targetWins;
        matchFee = singleMatchFee;
        status = Status.IN_PROGRESS;
        disputedMatches = 0;
        owner = payable(msg.sender);
    }

    function moveRock() external override payable {
        move(Move.ROCK);
    }

    function movePaper() external override payable {
        move(Move.PAPER);
    }

    function moveScissor() external override payable {
        move(Move.SCISSOR);
    }

    modifier mustBeAPlayer() {
        require(msg.sender == players[Player.First].id || msg.sender == players[Player.Second].id, "You're not one of the players.");
        _;
    }

    function move(Move move_type) internal mustBeAPlayer {
        require(status != Status.END, "Game has already finish");
        require(msg.value >= matchFee, "You need to pass the minimum fee to play");

        Player p = (msg.sender == players[Player.First].id ? Player.First : Player.Second);
        players[p].games.push(move_type);
        players[p].fees += msg.value;

        checkGame();
    }

    function checkGame() internal {
        uint8 n = uint8(
                players[Player.First].games.length < players[Player.Second].games.length 
                ? players[Player.First].games.length
                : players[Player.Second].games.length
            );

        for (uint8 i = disputedMatches; i < n; ++i) {
            Move p1 = players[Player.First].games[i];
            Move p2 = players[Player.Second].games[i];

            // Fair play
            if (p1 == p2) continue;

            if (
                (p1 == Move.ROCK && p2 == Move.SCISSOR) ||
                (p1 == Move.SCISSOR && p2 == Move.PAPER) ||
                (p1 == Move.PAPER && p2 == Move.ROCK)
            ) {
                players[Player.First].wins++;
                emit MatchWonBy(Player.First, i);
            } else {
                players[Player.Second].wins++;
                emit MatchWonBy(Player.Second, i);
            }

            disputedMatches++;

            if (players[Player.First].wins == target) {
                emit TournamentWonBy(Player.First);
                winner = Player.First;
                endGame();
            } else if (players[Player.Second].wins == target) {
                emit TournamentWonBy(Player.Second);
                winner = Player.Second;
                endGame();
            }
        }
    }

    function endGame() internal {
        require(status != Status.END, "Game has already finish");

        assert(players[winner].wins == target);

        uint total = players[Player.First].fees + players[Player.Second].fees;
        assert(total == address(this).balance);
        players[winner].id.transfer(total);

        status = Status.END;
        selfdestruct(owner);
    }
}