// SocketRouter is used to setup main martini function func SocketRouter(m *martini.ClassicMartini) { room = newDraftRoom() Init() m.Get("/draft", PlayerRequired, func(r render.Render, user site.User) { r.HTML(200, "draft", user.FacebookId) }) m.Get("/admin/start", func() { draft.Paused = false allowTicks = false Handle(Message{Type: "event", Text: "The round has started, LET THE BIDDING BEGIN"}) }) m.Get("/admin/reset", func() { draft.GetCurrentPlayer().HighestBid = 0 draft.GetCurrentPlayer().Team = "" Handle(Message{Type: "event", Text: "Admin reset current round, starting when they press the button.."}) allowTicks = false draft.Paused = true }) m.Get("/admin/skip", func() { Handle(Message{Type: "event", Text: "Admin skipped current player, waiting on him to start.."}) draft.Next() Handle(Message{Type: "update"}) }) m.Get("/player/refresh", func() { Handle(Message{Type: "refresh"}) }) m.Get("/admin/previous", func() { Handle(Message{Type: "event", Text: "Admin undid previous round, waiting on him to start.."}) draft.Previous() Handle(Message{Type: "update"}) }) // This is the sockets connection for the room, it is a json mapping to sockets. m.Get("/draft/:clientname", sockets.JSON(Message{}), func(params martini.Params, receiver <-chan *Message, sender chan<- *Message, done <-chan bool, disconnect chan<- int, err <-chan error) (int, string) { client := &Client{params["clientname"], receiver, sender, done, err, disconnect} room.appendClient(client) Handle(Message{Type: "login"}) // A single select can be used to do all the messaging for { select { case <-client.err: // Its gone jim case msg := <-client.in: // Most of code will be handled here log.Println(*msg) Handle(*msg) case <-client.done: room.removeClient(client) return 200, "OK" } } }) }
func handle_more_bid(msg Message, room *DraftRoom) { amt, err := strconv.Atoi(msg.Text) //log.Println(msg, err) if err == nil { amount := draft.GetCurrentPlayer().HighestBid + amt Handle(Message{Type: "bid", From: msg.From, Text: strconv.Itoa(amount)}) } }
// handle_login will give the player their stats, captains, current player, and upcoming players. func handle_update(msg Message, room *DraftRoom) { Handle(Message{Type: "captains"}) Handle(Message{Type: "upcoming"}) Handle(Message{Type: "current-player"}) Handle(Message{Type: "current-header"}) Handle(Message{Type: "event", Text: "Currently waiting to bid on.." + draft.GetCurrentPlayer().Ign}) for _, client := range room.clients { Handle(Message{Type: "bidder", From: client.ID}) } }
func handle_bid(msg Message, room *DraftRoom) { amt, err := strconv.Atoi(msg.Text) //log.Println(msg) if err == nil { bidSuccess := draft.Bid(msg.From, amt) captain := draft.GetAuctioner(msg.From) if bidSuccess { formattedStr := fmt.Sprintf("<h5>%s bid <span class='text-success'>%d</span> on <span class='text-success'>%s</span></h5>", captain.TeamName, amt, draft.GetCurrentPlayer().Ign) go Handle(Message{Type: "event", Text: formattedStr}) currentCountdown = startingCountdownTime allowTicks = true } } }
func handle_current_player(msg Message, room *DraftRoom) { var format string = ` <div class="row"> <div class="col-md-3">%s</div> <div class="col-md-4"><div class="row">Preferred Role: Score</div>%s</div> <div id="current_tier" class="text-muted col-md-4"> <div class="row">LKS: %s</div> <div class="row">Proficiency: %s</div> </div> </div> </div> ` player := draft.GetCurrentPlayer() roles := "" for _, val := range player.Proficiencies { roles += fmt.Sprintf(`<div class="row"><span class='text-info'>%s: </span><span>%s</span></div>`, val.Position, strconv.Itoa(val.Score)) } res := fmt.Sprintf(format, player.Ign, roles, strconv.Itoa(player.Score), strconv.FormatFloat(player.ProficiencyTotal, 'f', 2, 64)) room.broadcast(&Message{Type: "current-header", Text: player.Ign}) room.broadcast(&Message{Type: "current-player", Text: res}) }
func handle_winner(msg Message, room *DraftRoom) { Handle(Message{Type: "event", Text: draft.GetCurrentPlayer().Team + " bought " + draft.GetCurrentPlayer().Ign + " for " + strconv.Itoa(draft.GetCurrentPlayer().HighestBid)}) draft.Win() Handle(Message{Type: "update"}) draft.Paused = true }