Add session creation and start commands
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
dbc7e7e0ba
commit
5158ab539a
4 changed files with 83 additions and 12 deletions
|
@ -9,7 +9,7 @@ type DraftBot struct {
|
||||||
API BotInterface
|
API BotInterface
|
||||||
Name string
|
Name string
|
||||||
Rooms map[string]roomInfo
|
Rooms map[string]roomInfo
|
||||||
Sessions map[string]session
|
Sessions map[string]*session
|
||||||
}
|
}
|
||||||
|
|
||||||
type roomInfo struct {
|
type roomInfo struct {
|
||||||
|
@ -29,7 +29,7 @@ func NewDraftBot(botAPI BotInterface, name string) *DraftBot {
|
||||||
return &DraftBot{
|
return &DraftBot{
|
||||||
API: botAPI,
|
API: botAPI,
|
||||||
Name: name,
|
Name: name,
|
||||||
Sessions: make(map[string]session),
|
Sessions: make(map[string]*session),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,6 +85,8 @@ func (d *DraftBot) handleMessage(roomid string, msg room.Message) {
|
||||||
d.commands(commandMap{
|
d.commands(commandMap{
|
||||||
// Player wants to join the session
|
// Player wants to join the session
|
||||||
"join": d.cmdJoinSession,
|
"join": d.cmdJoinSession,
|
||||||
|
// Owner wants to start the session (but not partecipate)
|
||||||
|
"start": d.cmdfnOnlyOwner(d.cmdStartSession),
|
||||||
})(roomid, msg)
|
})(roomid, msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -93,7 +95,6 @@ func (d *DraftBot) handleMessage(roomid string, msg room.Message) {
|
||||||
d.commands(commandMap{
|
d.commands(commandMap{
|
||||||
// Player has picked a card
|
// Player has picked a card
|
||||||
"pick": d.cmdPickCard,
|
"pick": d.cmdPickCard,
|
||||||
|
|
||||||
// Owner wants to start the session
|
// Owner wants to start the session
|
||||||
"start": d.cmdfnOnlyOwner(d.cmdStartSession),
|
"start": d.cmdfnOnlyOwner(d.cmdStartSession),
|
||||||
})(roomid, msg)
|
})(roomid, msg)
|
||||||
|
@ -124,3 +125,9 @@ func (d *DraftBot) handleEvent(roomid string, evt room.Event) {
|
||||||
delete(d.Rooms, roomid)
|
delete(d.Rooms, roomid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DraftBot) handleSessionMessages(roomid string, s *session) {
|
||||||
|
for msg := range s.messages {
|
||||||
|
d.sendMessage(roomid, msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
|
|
||||||
room "git.fromouter.space/mcg/cardgage/room/api"
|
room "git.fromouter.space/mcg/cardgage/room/api"
|
||||||
"git.fromouter.space/mcg/draft"
|
"git.fromouter.space/mcg/draft"
|
||||||
)
|
)
|
||||||
|
@ -100,11 +102,70 @@ func (d *DraftBot) cmdPickCard(roomid string, msg room.Message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DraftBot) cmdCreateSession(roomid string, msg room.Message) {
|
func (d *DraftBot) cmdCreateSession(roomid string, msg room.Message) {
|
||||||
//TODO
|
// Get session options from data
|
||||||
|
type sessionOptions struct {
|
||||||
|
players int
|
||||||
|
options draftOptions
|
||||||
|
}
|
||||||
|
var opt sessionOptions
|
||||||
|
err := mapstructure.Decode(msg.Data, &opt)
|
||||||
|
if err != nil {
|
||||||
|
d.sendMessage(roomid, room.Message{
|
||||||
|
To: msg.From,
|
||||||
|
Type: "invalid-data",
|
||||||
|
Message: "Error parsing session options: " + err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sess, err := newSession(opt.players, opt.options)
|
||||||
|
if err != nil {
|
||||||
|
d.sendMessage(roomid, room.Message{
|
||||||
|
To: msg.From,
|
||||||
|
Type: "session-create-error",
|
||||||
|
Data: err.Error(),
|
||||||
|
Message: "Error creating session: " + err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// All ok, assign session
|
||||||
|
d.Sessions[roomid] = sess
|
||||||
|
|
||||||
|
// Start handling messages for the session
|
||||||
|
go d.handleSessionMessages(roomid, sess)
|
||||||
|
|
||||||
|
// Tell everyone about the new session
|
||||||
|
d.sendMessage(roomid, room.Message{
|
||||||
|
Channel: "draft",
|
||||||
|
Type: "session-open",
|
||||||
|
Data: opt,
|
||||||
|
Message: fmt.Sprintf("Created a new draft session for %d players, type: %s", opt.players, opt.options.Type),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DraftBot) cmdStartSession(roomid string, msg room.Message) {
|
func (d *DraftBot) cmdStartSession(roomid string, msg room.Message) {
|
||||||
//TODO
|
// Get session
|
||||||
|
session, _ := d.Sessions[roomid]
|
||||||
|
|
||||||
|
// Try starting the session
|
||||||
|
err := session.Start()
|
||||||
|
if err != nil {
|
||||||
|
d.sendMessage(roomid, room.Message{
|
||||||
|
To: msg.From,
|
||||||
|
Type: "session-start-error",
|
||||||
|
Data: err.Error(),
|
||||||
|
Message: "Could not start session: " + err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tell everyone about the new session
|
||||||
|
d.sendMessage(roomid, room.Message{
|
||||||
|
Channel: "draft",
|
||||||
|
Type: "session-start",
|
||||||
|
Message: "Session started, get drafting!",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DraftBot) cmdfnOnlyOwner(wrapped commandHandler) commandHandler {
|
func (d *DraftBot) cmdfnOnlyOwner(wrapped commandHandler) commandHandler {
|
||||||
|
@ -121,8 +182,8 @@ func (d *DraftBot) cmdfnOnlyOwner(wrapped commandHandler) commandHandler {
|
||||||
if msg.From != roomData.Owner {
|
if msg.From != roomData.Owner {
|
||||||
d.sendMessage(roomid, room.Message{
|
d.sendMessage(roomid, room.Message{
|
||||||
To: msg.From,
|
To: msg.From,
|
||||||
Type: "no-session",
|
Type: "must-be-owner",
|
||||||
Message: "Sorry, only the owner can speak to me when no session is active",
|
Message: "Sorry, only the room's owner can tell me to do that",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,4 +7,5 @@ require (
|
||||||
git.fromouter.space/mcg/cardgage v0.0.4
|
git.fromouter.space/mcg/cardgage v0.0.4
|
||||||
git.fromouter.space/mcg/draft v0.0.3
|
git.fromouter.space/mcg/draft v0.0.3
|
||||||
github.com/go-kit/kit v0.8.0
|
github.com/go-kit/kit v0.8.0
|
||||||
|
github.com/mitchellh/mapstructure v1.1.2
|
||||||
)
|
)
|
||||||
|
|
|
@ -29,7 +29,7 @@ type session struct {
|
||||||
started bool // Has the draft started already?
|
started bool // Has the draft started already?
|
||||||
|
|
||||||
// Channels for communication while the session is running
|
// Channels for communication while the session is running
|
||||||
messages chan<- room.Message
|
messages chan room.Message
|
||||||
exit chan bool
|
exit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,6 @@ type draftOptions struct {
|
||||||
|
|
||||||
// Shared
|
// Shared
|
||||||
PackCount int `json:"pack_count,omitempty"` // Set and Cube
|
PackCount int `json:"pack_count,omitempty"` // Set and Cube
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (do draftOptions) getProvider() (draft.PackProvider, error) {
|
func (do draftOptions) getProvider() (draft.PackProvider, error) {
|
||||||
|
@ -108,9 +107,10 @@ func newSession(playerCount int, opt draftOptions) (*session, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &session{
|
return &session{
|
||||||
Options: opt,
|
Options: opt,
|
||||||
Pod: draft.MakePod(playerCount, provider),
|
Pod: draft.MakePod(playerCount, provider),
|
||||||
exit: make(chan bool),
|
messages: make(chan room.Message),
|
||||||
|
exit: make(chan bool),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,6 +250,8 @@ func (s *session) handlePicks() {
|
||||||
break
|
break
|
||||||
case <-s.exit:
|
case <-s.exit:
|
||||||
// Room closed, exit early
|
// Room closed, exit early
|
||||||
|
close(s.messages)
|
||||||
|
close(s.exit)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue