79 lines
1.9 KiB
Go
79 lines
1.9 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"
|
|
"maunium.net/go/mautrix/event"
|
|
"maunium.net/go/mautrix/id"
|
|
)
|
|
|
|
type Match struct {
|
|
ID uuid.UUID
|
|
Game *tome_game.Game
|
|
P1 id.UserID
|
|
P2 id.UserID
|
|
Dealer id.UserID
|
|
MatrixRoom id.RoomID
|
|
}
|
|
|
|
type MatchResp struct {
|
|
Body string
|
|
Private bool
|
|
}
|
|
|
|
func NewMatch() *Match {
|
|
return &Match{
|
|
ID: uuid.New(),
|
|
Game: nil,
|
|
P1: "",
|
|
P2: "",
|
|
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(gameRoom *mautrix.Room, evt *event.Event) {
|
|
if gameRoom == nil {
|
|
panic("no matrix room for game")
|
|
}
|
|
if evt.Content.AsMember().Membership == event.MembershipJoin {
|
|
if gameRoom.GetMembershipState(m.P1) == event.MembershipJoin && gameRoom.GetMembershipState(m.P2) == event.MembershipJoin {
|
|
log.Println("both players are in the room now")
|
|
m.Game = tome_game.NewGame([]int{}, []int{})
|
|
}
|
|
} else if evt.Content.AsMember().Membership == event.MembershipLeave {
|
|
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
|
|
}
|
|
} 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)
|
|
}
|