Robin Thellend | 320af42 | 2015-03-09 17:18:15 -0700 | [diff] [blame] | 1 | // Package rps is an example of vanadium service for playing the game of |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 2 | // Rock-Paper-Scissors. (http://en.wikipedia.org/wiki/Rock-paper-scissors) |
| 3 | // |
| 4 | // There are three different roles in the game: |
| 5 | // |
| 6 | // 1. Judge: A judge enforces the rules of the game and decides who |
| 7 | // the winner is. At the end of the game, the judge reports the |
| 8 | // final score to all the score keepers. |
| 9 | // |
| 10 | // 2. Player: A player can ask a judge to start a new game, it can |
| 11 | // challenge another player, and it can play a game. |
| 12 | // |
| 13 | // 3. ScoreKeeper: A score keeper receives the final score for a game |
| 14 | // after it ended. |
| 15 | package rps |
| 16 | |
Todd Wang | 07f1346 | 2015-03-13 15:54:15 -0700 | [diff] [blame] | 17 | import ( |
| 18 | "time" |
| 19 | "v.io/v23/services/security/access" |
| 20 | ) |
Asim Shankar | 4794c93 | 2014-11-24 17:45:56 -0800 | [diff] [blame] | 21 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 22 | type RockPaperScissors interface { |
| 23 | Judge |
| 24 | Player |
| 25 | ScoreKeeper |
| 26 | } |
| 27 | |
| 28 | type Judge interface { |
| 29 | // CreateGame creates a new game with the given game options and returns a game |
| 30 | // identifier that can be used by the players to join the game. |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 31 | CreateGame(Opts GameOptions) (GameId | error) {access.Write} |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 32 | // Play lets a player join an existing game and play. |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 33 | Play(Id GameId) stream<PlayerAction,JudgeAction> (PlayResult | error) {access.Write} |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 36 | // A GameId is used to uniquely identify a game within one Judge. |
| 37 | type GameId struct { |
| 38 | Id string |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // GameOptions specifies the parameters of a game. |
| 42 | type GameOptions struct { |
| 43 | NumRounds int32 // The number of rounds that a player must win to win the game. |
| 44 | GameType GameTypeTag // The type of game to play: Classic or LizardSpock. |
| 45 | } |
| 46 | |
| 47 | type GameTypeTag byte |
| 48 | const ( |
| 49 | Classic = GameTypeTag(0) // Rock-Paper-Scissors |
| 50 | LizardSpock = GameTypeTag(1) // Rock-Paper-Scissors-Lizard-Spock |
| 51 | ) |
| 52 | |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 53 | type PlayerAction union { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 54 | Move string // The move that the player wants to make. |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 55 | Quit unused // Indicates that the player is quitting the game. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 58 | type unused struct{} |
| 59 | |
| 60 | type JudgeAction union { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 61 | PlayerNum int32 // The player's number. |
| 62 | OpponentName string // The name of the opponent. |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 63 | MoveOptions []string // A list of allowed moves that the player must choose from. |
| 64 | RoundResult Round // The result of the previous round. |
| 65 | Score ScoreCard // The result of the game. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Sergey Rogulenko | 2f64f98 | 2015-02-05 14:14:46 -0800 | [diff] [blame] | 68 | type PlayersMoves [2]string |
| 69 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 70 | // Round represents the state of a round. |
| 71 | type Round struct { |
Sergey Rogulenko | 2f64f98 | 2015-02-05 14:14:46 -0800 | [diff] [blame] | 72 | Moves PlayersMoves // Each player's move. |
| 73 | Comment string // A text comment from judge about the round. |
| 74 | Winner WinnerTag // Who won the round. |
Todd Wang | 07f1346 | 2015-03-13 15:54:15 -0700 | [diff] [blame] | 75 | StartTime time.Time // The time at which the round started. |
| 76 | EndTime time.Time // The time at which the round ended. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // WinnerTag is a type used to indicate whether a round or a game was a draw, |
| 80 | // was won by player 1 or was won by player 2. |
| 81 | type WinnerTag byte |
| 82 | const ( |
| 83 | Draw = WinnerTag(0) |
| 84 | Player1 = WinnerTag(1) |
| 85 | Player2 = WinnerTag(2) |
| 86 | ) |
| 87 | |
| 88 | // PlayResult is the value returned by the Play method. It indicates the outcome of the game. |
| 89 | type PlayResult struct { |
| 90 | YouWon bool // True if the player receiving the result won the game. |
| 91 | } |
| 92 | |
| 93 | // Player can receive challenges from other players. |
| 94 | type Player interface { |
| 95 | // Challenge is used by other players to challenge this player to a game. If |
| 96 | // the challenge is accepted, the method returns nil. |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 97 | Challenge(Address string, Id GameId, Opts GameOptions) error {access.Write} |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // ScoreKeeper receives the outcome of games from Judges. |
| 101 | type ScoreKeeper interface { |
Robin Thellend | 0b9011a | 2015-02-04 16:44:36 -0800 | [diff] [blame] | 102 | Record(Score ScoreCard) error {access.Write} |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | type ScoreCard struct { |
| 106 | Opts GameOptions // The game options. |
| 107 | Judge string // The name of the judge. |
| 108 | Players []string // The name of the players. |
| 109 | Rounds []Round // The outcome of each round. |
Todd Wang | 07f1346 | 2015-03-13 15:54:15 -0700 | [diff] [blame] | 110 | StartTime time.Time // The time at which the game started. |
| 111 | EndTime time.Time // The time at which the game ended. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 112 | Winner WinnerTag // Who won the game. |
| 113 | } |