blob: 03ef07df32d814769bd1271e70f3855b3d980576 [file] [log] [blame]
Robin Thellend320af422015-03-09 17:18:15 -07001// Package rps is an example of vanadium service for playing the game of
Jiri Simsa37cdfc02014-10-09 18:00:43 -07002// 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.
15package rps
16
Todd Wang07f13462015-03-13 15:54:15 -070017import (
18 "time"
19 "v.io/v23/services/security/access"
20)
Asim Shankar4794c932014-11-24 17:45:56 -080021
Jiri Simsa37cdfc02014-10-09 18:00:43 -070022type RockPaperScissors interface {
23 Judge
24 Player
25 ScoreKeeper
26}
27
28type 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 Prosnitz2be28dc2015-03-11 13:33:22 -070031 CreateGame(Opts GameOptions) (GameId | error) {access.Write}
Jiri Simsa37cdfc02014-10-09 18:00:43 -070032 // Play lets a player join an existing game and play.
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -070033 Play(Id GameId) stream<PlayerAction,JudgeAction> (PlayResult | error) {access.Write}
Jiri Simsa37cdfc02014-10-09 18:00:43 -070034}
35
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -070036// A GameId is used to uniquely identify a game within one Judge.
37type GameId struct {
38 Id string
Jiri Simsa37cdfc02014-10-09 18:00:43 -070039}
40
41// GameOptions specifies the parameters of a game.
42type 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
47type GameTypeTag byte
48const (
49 Classic = GameTypeTag(0) // Rock-Paper-Scissors
50 LizardSpock = GameTypeTag(1) // Rock-Paper-Scissors-Lizard-Spock
51)
52
Robin Thellend636b3602015-02-11 14:57:44 -080053type PlayerAction union {
Jiri Simsa37cdfc02014-10-09 18:00:43 -070054 Move string // The move that the player wants to make.
Robin Thellend636b3602015-02-11 14:57:44 -080055 Quit unused // Indicates that the player is quitting the game.
Jiri Simsa37cdfc02014-10-09 18:00:43 -070056}
57
Robin Thellend636b3602015-02-11 14:57:44 -080058type unused struct{}
59
60type JudgeAction union {
Jiri Simsa37cdfc02014-10-09 18:00:43 -070061 PlayerNum int32 // The player's number.
62 OpponentName string // The name of the opponent.
Robin Thellend636b3602015-02-11 14:57:44 -080063 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 Simsa37cdfc02014-10-09 18:00:43 -070066}
67
Sergey Rogulenko2f64f982015-02-05 14:14:46 -080068type PlayersMoves [2]string
69
Jiri Simsa37cdfc02014-10-09 18:00:43 -070070// Round represents the state of a round.
71type Round struct {
Sergey Rogulenko2f64f982015-02-05 14:14:46 -080072 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 Wang07f13462015-03-13 15:54:15 -070075 StartTime time.Time // The time at which the round started.
76 EndTime time.Time // The time at which the round ended.
Jiri Simsa37cdfc02014-10-09 18:00:43 -070077}
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.
81type WinnerTag byte
82const (
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.
89type 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.
94type 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 Prosnitz2be28dc2015-03-11 13:33:22 -070097 Challenge(Address string, Id GameId, Opts GameOptions) error {access.Write}
Jiri Simsa37cdfc02014-10-09 18:00:43 -070098}
99
100// ScoreKeeper receives the outcome of games from Judges.
101type ScoreKeeper interface {
Robin Thellend0b9011a2015-02-04 16:44:36 -0800102 Record(Score ScoreCard) error {access.Write}
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700103}
104
105type 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 Wang07f13462015-03-13 15:54:15 -0700110 StartTime time.Time // The time at which the game started.
111 EndTime time.Time // The time at which the game ended.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700112 Winner WinnerTag // Who won the game.
113}