-
Notifications
You must be signed in to change notification settings - Fork 1
/
Auction.go
85 lines (70 loc) · 2.49 KB
/
Auction.go
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
package main
import (
"fmt"
"github.com/daviddengcn/go-colortext"
"time"
)
type Auction struct {
highestBid *Bid
itemUp Item_I
endTime time.Time
}
type Bid struct {
bidder *ClientConnection
durationFromEnd time.Duration
amount int
}
func newAuction(item Item_I) *Auction {
a := new(Auction)
a.endTime = time.Now().Add(time.Minute * 1) //This could also come in from user
a.highestBid = nil
a.itemUp = item
return a
}
func (a *Auction) determineWinner() *Bid {
return a.highestBid
}
func (a *Auction) timeTillOver() time.Duration {
return a.endTime.Sub(time.Now())
}
func (a *Auction) isOver() bool {
return time.Now().Sub(a.endTime).Seconds() > 0
}
func (a *Auction) awardItemToWinner(winner *Bid) {
winner.bidder.Write(newServerMessageS("You won the auction."))
winner.bidder.GiveItem(a.itemUp)
}
func (a *Auction) getAuctionInfo() ServerMessage {
msg := newFormattedStringCollection()
msg.addMessage2("\nAttention Players! There is an auction going on. The current status of the auction is the following:\n")
msg.addMessage2(fmt.Sprintf("\tItem: %-15s ", a.itemUp.GetName()))
if a.highestBid != nil {
msg.addMessage2(fmt.Sprintf("Current Bid: %d Bidder: %-15s", a.highestBid.amount, a.highestBid.bidder.GetCharactersName()))
} else {
msg.addMessage2("\tCurrent Bid: None")
}
msg.addMessage2("\tTime left: " + a.endTime.Sub(time.Now()).String() + "\n")
msg.addMessage2("If you would like to bid on this item then type 'bid [amount]' where amount is the amount of gold you want to bid.\n")
return newServerMessageFS(msg.fmtedStrings)
}
func (a *Auction) bidOnItem(amount int, bidder *ClientConnection, timeOfBid time.Time) []FormattedString {
estimatedTime := timeOfBid.Add(-1 * bidder.GetAverageRoundTripTime())
distance := a.endTime.Sub(estimatedTime)
if distance > 0 {
bid := new(Bid)
bid.bidder = bidder
bid.amount = amount
bid.durationFromEnd = distance
if a.highestBid == nil || a.highestBid.amount < amount {
a.highestBid = bid
return newFormattedStringSplice2(ct.Green, "Your bid was recorded for time: "+estimatedTime.String()+"\n")
} else if a.highestBid.amount == amount && distance > a.highestBid.durationFromEnd {
a.highestBid = bid
return newFormattedStringSplice2(ct.Green, "Your bid was recorded for time: "+estimatedTime.String()+"\n")
} else {
return newFormattedStringSplice2(ct.Red, "Your bid was too low.")
}
} else {
return newFormattedStringSplice2(ct.Red, "The auction ended before you could place your bid.")
}
}