-
Notifications
You must be signed in to change notification settings - Fork 3
/
auction.coffee
191 lines (171 loc) · 5.76 KB
/
auction.coffee
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
StateMachine = require('./sm').StateMachine
Collection = require('./collection').Collection
states =
start:
full_name: 'Waiting for Bidders'
active:
full_name: 'Auction happening now!'
complete:
full_name: 'Auction Finished'
payment_pending:
full_name: 'Waiting for Payment'
payment_collected:
full_name: 'Payment Completed'
finished:
full_name: 'End State'
events =
bidder_joined:
transitions:
start: 'start'
active: 'active'
complete: 'complete'
payment_pending: 'payment_pending'
payment_collected: 'payment_collected'
callback: (bidder) ->
# todo , regisiter a 'disconnect' handler?
@bidders.push bidder
@bidderemit bidder, 'startup'
@bidderemit bidder, "newbid", @current_bid
@broadcast "new bidder! #{bidder.name}"
@broademit "bidder_joined", bidders: (p.id for p in @bidders)
true
bidder_left:
transitions:
start: 'start'
active: 'active'
complete: 'complete'
payment_pending: 'payment_pending'
payment_collected: 'payment_collected'
callback: (bidder) ->
@bidders = (p for p in @bidders when p != bidder)
@broadcast "bidder left! #{bidder.name}"
@broademit "bidder_left", bidders: (p.id for p in @bidders)
true
start_auction:
transitions:
start: 'active'
callback: () ->
@broadcast "auction started "
@broademit "started"
stop_auction:
transitions:
active: 'complete'
callback: (admin) ->
@broadcast "auction stopped"
@broademit "stopped"
auction_over:
transitions:
active: 'complete'
callback: () ->
@broadcast "auction over. Sold!"
@broademit "over", name: @current_bid.name, value: @current_bid.value
if @current_bidder?
@biddercast @current_bidder, "You've won and it's now time to pay"
@bidderemit @current_bidder, "winner", value: @current_bid.value, auction_name: @name
restart_auction:
transitions:
start: 'active'
active: 'active'
complete: 'active'
payment_pending: 'active'
payment_collected: 'active'
active: 'active'
callback: (admin) ->
@broadcast "auction restarted"
@broademit "restarted"
@bids = []
@current_bidder = null
@current_bid = { value: 0, name: admin.name, image: admin.image }
@broademit "newbid", @current_bid
going_auction:
transitions:
active: 'active'
callback: (admin) ->
@going1()
bid:
transitions:
active: 'active'
callback: (bid, bidder) ->
if @going?
clearTimeout @going
@going = null
bid.name = bidder.name
bid.image = bidder.image
# auction logic
console.log "auction #{@item} got bid #{bid.value} from #{bidder.name}"
if !@current_bid?
console.log "first bid accepted #{bid.value} from #{bidder.name}"
@bids.push bid
@current_bid = bid
@current_bidder = bidder
@broadcast "first bid from #{bidder.name}"
@biddercast bidder, "bid accepted"
@broademit "newbid", bid
@bidderemit bidder, "bidstatus", accepted: true
if bid.value > @current_bid.value
console.log "bid accepted"
@bids.push bid
@current_bid = bid
@current_bidder = bidder
@broadcast "new bid from #{bidder.name}"
@biddercast bidder, "bid accepted"
@broademit "newbid", bid
@bidderemit bidder, "bidstatus", accepted: true
else
console.log "bid rejected"
@biddercast bidder, "sorry, you've been out bid already"
@bidderemit bidder, "bidstatus", accepted: false
@bidderemit bidder, "newbid", @current_bid
class Auction extends StateMachine
constructor: (item: @item, description: @description ) ->
super('start', states, events)
@bidders = []
@current_bid = null
@current_bidder = null
@bids = []
@name = "#{Math.floor(Math.random() * 1000000000000)}" #TODO make unique
@on 'moved_state', (state_name) =>
@broadcast "auction moved to #{state_name}"
@getState('finished').on 'enter', =>
p.sm.trigger 'auction_over' for p in @bidders
# todo: move this cleanup to sm special 'finished' state or event
Auction.collection.remove @id
Auction.finished_collection.remove @id
bidderemit: (b, event, args...) ->
console.log "auction #{@item} bidderemit: #{b.name} #{event}", args...
b.emit(event, args...)
biddercast: (b, message) ->
console.log "auction #{@item} biddercast: #{b.name} #{message}"
b.emit("broadcast", message: message)
b.send(message)
broademit: (event, args...) ->
console.log "auction #{@item} broademit: #{event}", args...
b.emit(event, args...) for b in @bidders
broadcast: (message) ->
console.log "auction #{@item} broadcast: #{message}"
b.emit("broadcast", message: message) for b in @bidders
b.send(message) for b in @bidders
going1: ->
@broadcast 'going once'
@broademit 'going', left: 3, message: 'Going once!'
a = @
@going = setTimeout ->
a.broadcast 'going twice'
a.broademit 'going', left: 2, message: 'Going twice!'
a.going = setTimeout ->
a.broadcast 'going three times'
a.broademit 'going', left: 1, message: 'Going three times...!'
a.going = setTimeout ->
a.trigger 'auction_over'
a.broadcast 'Sold'
a.broademit 'going', left: 0, message: 'Sold!'
, 3500
, 3000
, 2500
# create an auction
auction = new Auction({ item: 'Mars Bar', description: 'chocolate bar'})
# until we have admin, make it the global live auction
global.live_auction = auction
# until we have admin, start the auction
auction.trigger('start_auction')
(exports ? window).Auction = Auction