Start handling commands from players
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
1eef3f1201
commit
19c4b42bf4
3 changed files with 76 additions and 5 deletions
|
@ -1,15 +1,31 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
room "git.fromouter.space/mcg/cardgage/room/api"
|
room "git.fromouter.space/mcg/cardgage/room/api"
|
||||||
|
"git.fromouter.space/mcg/draft"
|
||||||
)
|
)
|
||||||
|
|
||||||
type draftBot struct {
|
type draftBot struct {
|
||||||
|
API BotInterface
|
||||||
Name string
|
Name string
|
||||||
|
Rooms map[string]roomInfo
|
||||||
Sessions map[string]session
|
Sessions map[string]session
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDraftBot(name string) *draftBot {
|
type roomInfo struct {
|
||||||
|
Name string
|
||||||
|
Owner string
|
||||||
|
}
|
||||||
|
|
||||||
|
// BotInterface is the interface needed by draftbot for working in cardgage
|
||||||
|
// This exists so that draftbot can be attached to a mocked API for testing
|
||||||
|
type BotInterface interface {
|
||||||
|
Send(room.BotMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDraftBot(botAPI BotInterface, name string) *draftBot {
|
||||||
return &draftBot{
|
return &draftBot{
|
||||||
Name: name,
|
Name: name,
|
||||||
Sessions: make(map[string]session),
|
Sessions: make(map[string]session),
|
||||||
|
@ -28,7 +44,7 @@ func (d *draftBot) onMessage(msg room.ServerMessage) {
|
||||||
}
|
}
|
||||||
// Only consider messages that speak directly to me
|
// Only consider messages that speak directly to me
|
||||||
if msg.Message.To == d.Name {
|
if msg.Message.To == d.Name {
|
||||||
d.handleMessage(*msg.Message)
|
d.handleMessage(msg.RoomID, *msg.Message)
|
||||||
}
|
}
|
||||||
case room.MsgEvent:
|
case room.MsgEvent:
|
||||||
if *logAll {
|
if *logAll {
|
||||||
|
@ -39,6 +55,57 @@ func (d *draftBot) onMessage(msg room.ServerMessage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *draftBot) handleMessage(msg room.Message) {
|
func (d *draftBot) sendMessage(roomid string, msg room.Message) {
|
||||||
|
d.API.Send(room.BotMessage{
|
||||||
|
RoomID: roomid,
|
||||||
|
Type: room.MsgMessage,
|
||||||
|
Message: &msg,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *draftBot) handleMessage(roomid string, msg room.Message) {
|
||||||
|
// Get session in room
|
||||||
|
session, ok := d.Sessions[roomid]
|
||||||
|
if !ok {
|
||||||
|
// Room does not have a currently running session, ignore unless it's the owner asking for specific stuff
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if player is in the draft
|
||||||
|
player, ok := session.Players[msg.From]
|
||||||
|
if !ok {
|
||||||
|
// Player not in draft, are they asking to join?
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
switch msg.Type {
|
||||||
|
// Player has picked a card
|
||||||
|
case "pick":
|
||||||
|
// Get picked card
|
||||||
|
picked := msg.Data.(string)
|
||||||
|
|
||||||
|
// Try to pick on player struct
|
||||||
|
err := player.Pick(draft.Card{ID: picked})
|
||||||
|
if err != nil {
|
||||||
|
if err == draft.ErrNotInPack {
|
||||||
|
d.sendMessage(roomid, room.Message{
|
||||||
|
To: msg.From,
|
||||||
|
Type: "invalid-pick",
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Technically not needed, as Pick can only throw ErrNotInPack right now
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.sendMessage(roomid, room.Message{
|
||||||
|
Channel: "draft",
|
||||||
|
Type: "card-picked",
|
||||||
|
Data: struct {
|
||||||
|
Player string
|
||||||
|
}{
|
||||||
|
msg.From,
|
||||||
|
},
|
||||||
|
Message: fmt.Sprintf("%s picked a card from his pack", msg.From),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -81,7 +81,7 @@ func runBot(name string, games, tags []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
draftbot := newDraftBot(name)
|
draftbot := newDraftBot(wsbot, name)
|
||||||
|
|
||||||
wsbot.Listen(draftbot.onMessage)
|
wsbot.Listen(draftbot.onMessage)
|
||||||
return errors.New("eof")
|
return errors.New("eof")
|
||||||
|
|
|
@ -25,6 +25,9 @@ type session struct {
|
||||||
Bots []*bot.Bot
|
Bots []*bot.Bot
|
||||||
Pod *draft.Pod
|
Pod *draft.Pod
|
||||||
|
|
||||||
|
// State management variables
|
||||||
|
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
|
||||||
|
@ -173,6 +176,7 @@ func (s *session) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start handling packs
|
// Start handling packs
|
||||||
|
s.started = true
|
||||||
go s.handlePicks()
|
go s.handlePicks()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue