-
Notifications
You must be signed in to change notification settings - Fork 0
/
gold-league.go
119 lines (95 loc) · 2.49 KB
/
gold-league.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
package main
import (
"log"
"net/http"
"encoding/json"
"io/ioutil"
"bytes"
)
// var GOLD_LEAGUE_APP_URI = "https://goldleagueffball.appspot.com"
var GOLD_LEAGUE_APP_URI = "http://localhost:5000"
type Owner struct {
Name string `json:"name"`
CapRoom int `json:"cap_room"`
YearsRemaining int `json:"years_remaining"`
SpotsAvailable int `json:"spots_available"`
}
// cast owner type to bidder
func ownerToBidder(owner *Owner) *Bidder {
return newBidder(owner.Name, owner.CapRoom, owner.SpotsAvailable)
}
func fetchOwners() []*Owner {
log.Println("FETCHING OWNERS")
// fetch owners
resp, err := http.Get(GOLD_LEAGUE_APP_URI + "/teams?shuffle=true")
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
var owners []*Owner
json.Unmarshal(body, &owners)
if len(owners) != 12 {
log.Println("Did not fetch 12 owners. Only %d", len(owners))
log.Fatal()
}
return owners
}
func fetchFreeAgents() []*Player {
log.Println("FETCHING FREE AGENTS")
// fetch owners
resp, err := http.Get(GOLD_LEAGUE_APP_URI + "/players/free-agents")
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
var players []*Player
json.Unmarshal(body, &players)
if len(players) < 1 {
log.Println("Did not fetch players")
log.Fatal()
}
return players
}
func recordBid(player *Player, bidder *Bidder, h *DraftHub) bool {
log.Println("RECORDING BID")
values := map[string]interface{}{"name": player.Name, "amount": player.bid.amount, "owner": bidder.Name}
jsonValue, _ := json.Marshal(values)
resp, err := http.Post(GOLD_LEAGUE_APP_URI + "/draft/" + h.draftId + "/player/result", "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
log.Println(err)
}
if resp.StatusCode == 201 {
return true
} else {
return false
}
}
type RollbackResponse struct {
Success bool `json:"success"`
PlayerName string `json:"player"`
}
func rollbackNomination(h *DraftHub) string {
resp, err := http.Get(GOLD_LEAGUE_APP_URI + "/draft/" + h.draftId + "/rollback-nomination")
if err != nil {
log.Println(err)
}
if resp.StatusCode != 200 {
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
var response *RollbackResponse
json.Unmarshal(body, &response)
return response.PlayerName
}