-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDAppInstance.ts
201 lines (169 loc) · 4.57 KB
/
IDAppInstance.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { IMessagingProvider } from '@daocasino/dc-messaging'
import { ETHInstance } from '@daocasino/dc-ethereum-utils'
import { GameInfo } from './GameInfo'
import Contract from 'web3/eth/contract'
import { IStatisticsServerConnectParams } from './IStatisticsServerConnectParams'
export type UserId = string
export interface DAppInstanceParams {
userId: UserId
num: number
rules: any
roomAddress: string
gameLogicFunction: () => IGameLogic
gameContractInstance: Contract
gameContractAddress: string
roomProvider: IMessagingProvider
onFinish: (userId: UserId) => void
gameInfo: GameInfo
Eth: ETHInstance,
statistics?: IStatisticsServerConnectParams
}
export interface DAppInstanceView {
deposit: number
playerBalance: number
bankrollerBalance: number
profit: number
playerAddress: string
}
export interface IDAppInstance {
on(event: string, func: (data: any) => void)
start(): Promise<void> | void
}
/*
* Client
*/
export interface OpenChannelParams {
channelId: string
playerAddress: string
bankrollerAddress: string
playerDepositWei: string
bankrollerDepositWei: string
openingBlock: string
n: string
e: string
}
export interface ConnectParams {
playerDeposit: number
}
export interface GetChannelDataParams extends ConnectParams {
playerAddress: string
channelId: string
}
export interface GameData {
// options for random numbers
// ex.: [[0,10],[50,100]] - get 2 random numbers,
// first from 0 to 10, and second from 50 to 100
randomRanges: number[][]
// Some other data from game-developer
custom?: any
}
export interface FullGameData {
seed: string
randomRanges: GameData['randomRanges']
custom?: GameData['custom']
}
export interface PlayParams {
// array of user bets on current round [2,1,4]
userBets: number[]
gameData: GameData
}
export interface PlayResult {
profit: number // user win in current round
data?: any // Some other data from game-developer
}
export interface IGameLogic {
// Basic game function
play: (
userBets: number[],
gameData: GameData,
randoms: number[]
) => PlayResult
}
// Channel state object
export interface State {
data: {
_id: string
_playerBalance: number
_bankrollerBalance: number
_totalBet: number
_session: number
}
hash: string // sha3 hash of SolidityTypeValue data
signs: {}
}
interface PeerBalance {
bankroller: number
player: number
}
export interface ChannelStateData {
deposits: PeerBalance
balance: PeerBalance
profit: PeerBalance
}
export interface IDAppPlayerInstance extends IDAppInstance {
getChannelStateData: () => ChannelStateData
// find bankroller in p2p network and "connect"
connect(connectData: ConnectParams): Promise<any>
// send open channel TX on game contract (oneStepGame.sol)
openChannel(
openChannelData: OpenChannelParams,
signature: string
): Promise<any>
/*
Call game logic function on dealer side and client side
verify randoms and channelState
*/
play(
params: PlayParams
): Promise<{ profit: number; randoms: number[]; data?: any }>
// Send close channel TX on game contract (oneStepGame.sol)
// ask dealer to sign data for close by consent and send TX
closeChannel(
closeParams: CloseChannelParams,
paramsSignature: string
): Promise<any>
disconnect()
}
export interface CloseChannelParams {
_id: string
_playerBalance: number
_bankrollerBalance: number
_totalBet: number
_session: number
}
/*
* Dealer / bankroller
*/
export interface IDAppDealerInstance extends IDAppInstance {
getOpenChannelData(
data: ConnectParams,
signature: string
): Promise<SignedResponse<OpenChannelParams>>
checkOpenChannel(): Promise<any | Error>
/*
Call game logic function on dealer side
*/
callPlay(
userBets: PlayParams['userBets'], // array of humanreadable format token value 1 = 1 * 10**18
// specified data for game
gameData: FullGameData,
session: number, // aka nonce, every call session++ on channelState
sign: string // ETHsign of sended data / previous args
): Promise<{
playResult: PlayResult
randoms: number[] // randoms arg applied to gamelogic function
rndSig: string // random params for verify on client side
state: State // bankroller signed channel state
}>
confirmState(state: State): boolean
consentCloseChannel(stateSignature: string): ConsentResult
checkCloseChannel(): Promise<any | Error>
}
export interface ConsentResult {
consentSignature: string
bankrollerAddress: string
}
export interface SignedResponse<TResponse> {
response: TResponse
signature: string
}