forked from synap5e/stove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.go
255 lines (227 loc) · 6.4 KB
/
draft.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package pegasus
import (
"github.com/HearthSim/hs-proto-go/pegasus/shared"
"github.com/HearthSim/hs-proto-go/pegasus/util"
"github.com/golang/protobuf/proto"
"log"
"time"
)
func (s *Draft) Init(sess *Session) {
sess.RegisterPacket(util.DraftBegin_ID, OnDraftBegin)
sess.RegisterPacket(util.DraftGetPicksAndContents_ID, OnDraftGetPicksAndContents)
sess.RegisterPacket(util.DraftRetire_ID, OnDraftRetire)
sess.RegisterPacket(util.DraftMakePick_ID, OnDraftMakePick)
sess.RegisterPacket(util.DraftAckRewards_ID, OnDraftAckRewards)
}
func MakeHeroChoices() (choices []DraftChoice) {
favoriteHeroes := []FavoriteHero{}
db.Limit(3).Find(&favoriteHeroes)
for i := 1; i <= 3; i++ {
choices = append(choices, DraftChoice{
CardID: favoriteHeroes[i-1].CardID,
ChoiceIndex: i,
Slot: 0,
})
}
return choices
}
func MakeCardChoices(slot int32) (choices []DraftChoice) {
cards := []DbfCard{}
// just use first 3 classic set cards as a placeholder
db.Limit(3).Where("is_collectible = ? and note_mini_guid GLOB ?", 1, "CS[12]_[0-9][0-9][0-9]").Find(&cards)
for i := 1; i <= 3; i++ {
choices = append(choices, DraftChoice{
CardID: cards[i-1].ID,
ChoiceIndex: i,
Slot: slot,
})
}
return choices
}
func ChoicesToCardDefs(choices []DraftChoice) (defs []*shared.CardDef) {
for _, choice := range choices {
defs = append(defs, &shared.CardDef{
Asset: proto.Int32(choice.CardID),
Premium: proto.Int32(int32(0)),
})
}
return defs
}
func OnDraftBegin(s *Session, body []byte) *Packet {
deck := Deck{
AccountID: s.Account.ID,
DeckType: int(shared.DeckType_DRAFT_DECK),
Name: "Arena Deck",
CardBackID: 0, //TODO
LastModified: time.Now().UTC(),
}
db.Create(&deck)
draft := Draft{
AccountID: s.Account.ID,
DeckID: deck.ID,
CurrentSlot: 0,
Choices: MakeHeroChoices(),
Ended: false,
}
db.Create(&draft)
choiceList := ChoicesToCardDefs(draft.Choices)
res := util.DraftBeginning{
DeckId: proto.Int64(deck.ID),
ChoiceList: choiceList,
}
return EncodePacket(util.DraftBeginning_ID, &res)
}
func OnDraftGetPicksAndContents(s *Session, body []byte) *Packet {
req := util.DraftGetPicksAndContents{}
err := proto.Unmarshal(body, &req)
if err != nil {
panic(err)
}
draft := Draft{}
if db.Where("not ended and account_id = ?", s.Account.ID).First(&draft).RecordNotFound() {
code := util.DraftError_DE_NOT_IN_DRAFT
res := util.DraftError{
ErrorCode: &code,
}
return EncodePacket(util.DraftError_ID, &res)
}
choices := []DraftChoice{}
db.Where("draft_id = ?", draft.ID).Find(&choices)
choiceList := ChoicesToCardDefs(choices)
deck := Deck{}
db.Where("id = ?", draft.DeckID).Preload("Cards").First(&deck)
heroDef := shared.CardDef{
Asset: proto.Int32(deck.HeroID),
Premium: proto.Int32(0),
}
cards := []*shared.DeckCardData{}
for i, card := range deck.Cards {
cards = append(cards, &shared.DeckCardData{
Def: MakeCardDef(card.CardID, 0),
Handle: proto.Int32(int32(i)),
Qty: proto.Int32(0),
Prev: proto.Int32(int32(i) - 1),
})
}
res := util.DraftChoicesAndContents{
DeckId: proto.Int64(draft.DeckID),
Slot: proto.Int32(draft.CurrentSlot),
Wins: proto.Int32(draft.Wins),
Losses: proto.Int32(draft.Losses),
Cards: cards,
ChoiceList: choiceList,
HeroDef: &heroDef,
}
return EncodePacket(util.DraftChoicesAndContents_ID, &res)
}
func OnDraftMakePick(s *Session, body []byte) *Packet {
req := util.DraftMakePick{}
err := proto.Unmarshal(body, &req)
if err != nil {
panic(err)
}
draft := Draft{}
if db.Where("not ended and account_id = ?", s.Account.ID).First(&draft).RecordNotFound() {
log.Panicf("received OnDraftMakePick for account with no active draft")
}
if req.GetSlot() != draft.CurrentSlot {
log.Panicf("received OnDraftMakePick for the wrong slot")
}
if req.GetDeckId() != draft.DeckID {
log.Panicf("received OnDraftMakePick for the wrong deck")
}
pick := DraftChoice{}
db.Where("draft_id = ? and choice_index = ?", draft.ID, req.GetIndex()).First(&pick)
db.Where("draft_id = ?", draft.ID).Delete(&DraftChoice{})
if draft.CurrentSlot == 0 {
deck := Deck{}
db.Where("id = ?", draft.DeckID).First(&deck)
deck.HeroID = pick.CardID
deck.HeroPremium = 0
deck.LastModified = time.Now().UTC()
db.Save(&deck)
} else {
card := DeckCard{
DeckID: draft.DeckID,
CardID: pick.CardID,
Premium: 0,
Num: 1,
}
db.Save(&card)
}
if draft.CurrentSlot < 30 {
draft.Choices = MakeCardChoices(draft.CurrentSlot)
}
draft.CurrentSlot += 1
db.Save(&draft)
choices := []*shared.CardDef{}
for _, choice := range draft.Choices {
choices = append(choices, &shared.CardDef{
Asset: proto.Int32(choice.CardID),
Premium: proto.Int32(int32(0)),
})
}
res := util.DraftChosen{
Chosen: MakeCardDef(pick.CardID, 0),
NextChoiceList: choices,
}
return EncodePacket(util.DraftChosen_ID, &res)
}
func MakeChest() (chest shared.RewardChest) {
// TODO take arguments to determine the contents
// There are up to 5 bags each which can hold a booster, card, dust or gold
chest.Bag1 = &shared.RewardBag{
RewardBooster: &shared.ProfileNoticeRewardBooster{
BoosterType: proto.Int32(1),
BoosterCount: proto.Int32(1),
},
}
chest.Bag2 = &shared.RewardBag{
RewardCard: &shared.ProfileNoticeRewardCard{
Card: MakeCardDef(2078, 1),
Quantity: proto.Int32(1),
},
}
chest.Bag3 = &shared.RewardBag{
RewardDust: &shared.ProfileNoticeRewardDust{
Amount: proto.Int32(69),
},
}
chest.Bag4 = &shared.RewardBag{
RewardGold: &shared.ProfileNoticeRewardGold{
Amount: proto.Int32(42),
},
}
return chest
}
func OnDraftRetire(s *Session, body []byte) *Packet {
req := util.DraftRetire{}
err := proto.Unmarshal(body, &req)
if err != nil {
panic(err)
}
draft := Draft{}
if db.Where("not ended and account_id = ?", s.Account.ID).First(&draft).RecordNotFound() {
log.Panicf("received OnDraftRetire for account with no active draft")
}
draft.Ended = true
draft.EndedAt = time.Now().UTC()
db.Save(&draft)
chest := MakeChest()
res := util.DraftRetired{
DeckId: req.DeckId,
Chest: &chest,
}
return EncodePacket(util.DraftRetired_ID, &res)
}
func OnDraftAckRewards(s *Session, body []byte) *Packet {
req := util.DraftAckRewards{}
err := proto.Unmarshal(body, &req)
if err != nil {
panic(err)
}
res := util.DraftRewardsAcked{
DeckId: req.DeckId,
}
return EncodePacket(util.DraftRewardsAcked_ID, &res)
}