-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower.go
75 lines (61 loc) · 1.47 KB
/
tower.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
package main
import (
"log"
"time"
"encoding/json"
)
type Tower struct {
rooms map[string]*DraftHub
}
func newTower() *Tower {
return &Tower{
rooms: make(map[string]*DraftHub),
}
}
// type Room struct {
// RoomId string `json:"roomId"`
//
// Bidders []*Bidder `json:"bidders"`
//
// Players []*Player `json:"players"`
// }
func newRoom(t *Tower, bidders []*Bidder, players []*Player) string {
roomId := createUuid()
newDraftRoom := newDraft(bidders, players, roomId)
// start new hub
go newDraftRoom.run()
timer := time.NewTimer(time.Hour * 24)
go func() {
<- timer.C
newDraftRoom.isActive = false
newDraftRoom.draftState.Running = false
delete(t.rooms, roomId)
log.Printf("room %s no longer active", roomId)
}()
// TODO watch out or memory leaks with this. Do go routines shut down when the parent does?
t.rooms[roomId] = newDraftRoom
return roomId
}
type RoomInfo struct {
RoomId string `json:"roomId"`
IsRunning bool `json:"isRunning"`
ActiveConnections int `json:"activeConnections"`
}
// get all rooms
func (t *Tower) getRoomsJson() []byte {
result := make([]*RoomInfo, len(t.rooms))
count := 0
for _, h := range t.rooms {
result[count] = &RoomInfo{
RoomId: h.draftId,
IsRunning: h.draftState.Running,
ActiveConnections: len(h.clients),
}
count += 1
}
response_json, err := json.Marshal(result)
if err != nil {
log.Printf("error: %v", err)
}
return response_json
}