91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
|
|
"git.saintnet.tech/tomecraft/tome_game"
|
|
"git.saintnet.tech/tomecraft/tome_lib"
|
|
"github.com/google/uuid"
|
|
"maunium.net/go/mautrix/event"
|
|
"maunium.net/go/mautrix/id"
|
|
)
|
|
|
|
type Match struct {
|
|
ID uuid.UUID
|
|
Game *tome_game.Game
|
|
P1 id.UserID
|
|
P1Room id.RoomID
|
|
P2 id.UserID
|
|
P2Room id.RoomID
|
|
Dealer id.UserID
|
|
MatrixRoom id.RoomID
|
|
}
|
|
|
|
type MatchResp struct {
|
|
Body string
|
|
Private bool
|
|
}
|
|
|
|
func NewMatch() *Match {
|
|
return &Match{
|
|
ID: uuid.New(),
|
|
Game: nil,
|
|
P1: "",
|
|
P1Room: "",
|
|
P2: "",
|
|
P2Room: "",
|
|
MatrixRoom: "",
|
|
}
|
|
}
|
|
|
|
func (m *Match) ParseAction(msg string) MatchResp {
|
|
var cmd tome_lib.Command
|
|
err := json.Unmarshal([]byte(msg), &cmd)
|
|
if err != nil {
|
|
log.Printf("error unmarshaling gamecommand %v", err)
|
|
return MatchResp{Body: "", Private: false}
|
|
}
|
|
res := m.Game.Parse(&cmd)
|
|
match_res := MatchResp{
|
|
Body: res.String(),
|
|
Private: (cmd.Type == tome_lib.ActCmd && cmd.String() == "d"),
|
|
}
|
|
return match_res
|
|
|
|
}
|
|
|
|
func (m *Match) JoinOrLeave(members map[id.UserID]struct {
|
|
DisplayName *string `json:"display_name"`
|
|
AvatarURL *string `json:"avatar_url"`
|
|
}, evt *event.MemberEventContent) {
|
|
_, p1ok := members[m.P1]
|
|
_, p2ok := members[m.P2]
|
|
if evt.Membership == event.MembershipJoin {
|
|
if m.Game != nil {
|
|
//players are already here, we don't care about joins
|
|
return
|
|
}
|
|
if p1ok && p2ok {
|
|
log.Println("both players are in the room now")
|
|
m.Game = tome_game.NewGame([]int{}, []int{})
|
|
}
|
|
} else if evt.Membership == event.MembershipLeave {
|
|
if !p1ok || !p2ok {
|
|
if m.Game.Status != tome_lib.StatusDraw ||
|
|
m.Game.Status != tome_lib.StatusScourgeWin ||
|
|
m.Game.Status != tome_lib.StatusSentinalWin {
|
|
|
|
m.Game.Status = tome_lib.StatusStop
|
|
log.Println("user left match; stopping")
|
|
}
|
|
}
|
|
} else {
|
|
log.Println("disregarding membership event we don't care about")
|
|
}
|
|
|
|
}
|
|
func (m *Match) IsPlayer(person id.UserID) bool {
|
|
return (person == m.P1 || person == m.P2 || person == m.Dealer)
|
|
}
|