matches are now technically playable

This commit is contained in:
stryan 2021-11-30 16:32:46 -05:00
parent ce7d2f72d0
commit 3f828a41c2
2 changed files with 81 additions and 34 deletions

View File

@ -34,7 +34,7 @@ func (d *Dealer) ConnectToMatrix(homeserver, uname, passwd string) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("Login successful") log.Println("Login successful")
d.Client = client d.Client = client
} }
@ -92,12 +92,16 @@ func (d *Dealer) RegisterHandlers() {
_ = d.Client.StateEvent(playerRoom, event.Type{"snen.player", event.StateEventType}, "player_info", &roominfo) _ = d.Client.StateEvent(playerRoom, event.Type{"snen.player", event.StateEventType}, "player_info", &roominfo)
if roominfo.CurrentGame != "" { if roominfo.CurrentGame != "" {
//parse as gamecommand //parse as gamecommand
match := d.RoomsList[roominfo.CurrentGame] match, ok := d.RoomsList[roominfo.CurrentGame]
if !ok {
log.Println("Dealer thinks a game is happening, but no game found")
return
}
if match.Game == nil { if match.Game == nil {
d.Client.SendText(playerRoom, "can't accept game commands yet") d.Client.SendText(playerRoom, "can't accept game commands yet")
return return
} }
resp := match.ParseAction(evt.Content.AsMessage().Body) resp := match.ParseAction(evt.Sender, evt.Content.AsMessage().Body)
if resp.Body != "" { if resp.Body != "" {
_, err := d.Client.SendText(playerRoom, resp.Body) _, err := d.Client.SendText(playerRoom, resp.Body)
if err != nil { if err != nil {
@ -145,7 +149,7 @@ func (d *Dealer) SetupRooms(domain string) {
RoomAliasName: "tomequeue", RoomAliasName: "tomequeue",
}) })
if createErr != nil { if createErr != nil {
//okay let's try joining the room first //okay let's try joining the room
res, err := d.Client.JoinRoom(room, "", nil) res, err := d.Client.JoinRoom(room, "", nil)
if err != nil { if err != nil {
log.Println("unable to create or join queue room") log.Println("unable to create or join queue room")
@ -154,8 +158,7 @@ func (d *Dealer) SetupRooms(domain string) {
} }
log.Printf("succesfully joined %v", res.RoomID) log.Printf("succesfully joined %v", res.RoomID)
d.QueueRoom = res.RoomID d.QueueRoom = res.RoomID
return } else {
}
resJoin, err := d.Client.JoinRoomByID(res.RoomID) resJoin, err := d.Client.JoinRoomByID(res.RoomID)
if err != nil { if err != nil {
log.Println("error joined recently created queue room") log.Println("error joined recently created queue room")
@ -163,7 +166,9 @@ func (d *Dealer) SetupRooms(domain string) {
} }
log.Printf("succesfully created and joined %v", resJoin.RoomID) log.Printf("succesfully created and joined %v", resJoin.RoomID)
d.QueueRoom = resJoin.RoomID d.QueueRoom = resJoin.RoomID
}
//load player rooms //load player rooms
log.Println("Loading known player rooms")
joinedResp, err := d.Client.JoinedRooms() joinedResp, err := d.Client.JoinedRooms()
if err != nil { if err != nil {
panic(err) panic(err)
@ -171,6 +176,7 @@ func (d *Dealer) SetupRooms(domain string) {
for _, r := range joinedResp.JoinedRooms { for _, r := range joinedResp.JoinedRooms {
tags, err := d.Client.GetTags(r) tags, err := d.Client.GetTags(r)
if err != nil { if err != nil {
log.Printf("can't get tags for room %v:%v", r, err)
continue continue
} }
_, ok := tags.Tags["tome_player_room"] _, ok := tags.Tags["tome_player_room"]
@ -186,16 +192,26 @@ func (d *Dealer) SetupRooms(domain string) {
keys = append(keys, k) keys = append(keys, k)
} }
if len(keys) != 2 { if len(keys) != 2 {
log.Printf("tagged player room %v has more then 2 members") log.Printf("tagged player room %v has more or less then 2 members:%v", r, len(keys))
if len(keys) == 1 && keys[0] == d.Client.UserID {
//clean up old player room
log.Printf("leaving player room %v that only has us in it", r)
d.Client.LeaveRoom(r)
}
continue continue
} }
for _, k := range keys { for _, k := range keys {
if k != d.Client.UserID { if k != d.Client.UserID {
d.PlayerRooms[k] = r d.PlayerRooms[k] = r
d.Client.SendStateEvent(r, event.Type{"snen.player", event.StateEventType}, "player_info", PlayerRoomInfo{
CurrentGame: "",
CurrentDeck: []int{},
})
} }
} }
} }
log.Println("initial room setup complete")
} }
func (d *Dealer) NewMatch(p1, p2 id.UserID) *Match { func (d *Dealer) NewMatch(p1, p2 id.UserID) *Match {
@ -213,8 +229,16 @@ func (d *Dealer) NewMatch(p1, p2 id.UserID) *Match {
} }
match.MatrixRoom = createRes.RoomID match.MatrixRoom = createRes.RoomID
match.P1 = p1 match.P1 = p1
match.P1Room = d.PlayerRooms[p1] _, found := d.PlayerRooms[p1]
if !found {
d.NewPlayerRoom(p1)
}
match.P2 = p2 match.P2 = p2
_, found = d.PlayerRooms[p2]
if !found {
d.NewPlayerRoom(p2)
}
match.P1Room = d.PlayerRooms[p1]
match.P2Room = d.PlayerRooms[p2] match.P2Room = d.PlayerRooms[p2]
match.Dealer = d.Client.UserID match.Dealer = d.Client.UserID
d.RoomsList[createRes.RoomID] = match d.RoomsList[createRes.RoomID] = match
@ -238,17 +262,7 @@ func (d *Dealer) QueueEvent(evt *event.Event) {
_, ok := d.PlayerRooms[evt.Sender] _, ok := d.PlayerRooms[evt.Sender]
if !ok { if !ok {
//open up DM with player //open up DM with player
createRes, err := d.Client.CreateRoom(&mautrix.ReqCreateRoom{ err := d.NewPlayerRoom(evt.Sender)
Preset: "private_chat",
Invite: []id.UserID{evt.Sender},
IsDirect: true,
})
if err != nil {
log.Println("error creating matrix room for player")
panic(err)
}
d.PlayerRooms[evt.Sender] = createRes.RoomID
err = d.Client.AddTag(createRes.RoomID, "tome_player_room", 0.0)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -258,3 +272,21 @@ func (d *Dealer) QueueEvent(evt *event.Event) {
} }
return return
} }
func (d *Dealer) NewPlayerRoom(user id.UserID) error {
createRes, err := d.Client.CreateRoom(&mautrix.ReqCreateRoom{
Preset: "private_chat",
Invite: []id.UserID{user},
IsDirect: true,
})
if err != nil {
log.Println("error creating matrix room for player")
return err
}
d.PlayerRooms[user] = createRes.RoomID
err = d.Client.AddTag(createRes.RoomID, "tome_player_room", 0.0)
if err != nil {
return err
}
return nil
}

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"encoding/json"
"log" "log"
"strings"
"git.saintnet.tech/tomecraft/tome_game" "git.saintnet.tech/tomecraft/tome_game"
"git.saintnet.tech/tomecraft/tome_lib" "git.saintnet.tech/tomecraft/tome_lib"
@ -27,6 +27,13 @@ type MatchResp struct {
Private bool Private bool
} }
func EmptyMatchResp() MatchResp {
return MatchResp{
Body: "",
Private: false,
}
}
func NewMatch() *Match { func NewMatch() *Match {
return &Match{ return &Match{
ID: uuid.New(), ID: uuid.New(),
@ -39,13 +46,20 @@ func NewMatch() *Match {
} }
} }
func (m *Match) ParseAction(msg string) MatchResp { func (m *Match) ParseAction(sender id.UserID, msg string) MatchResp {
var cmd tome_lib.Command var cmd tome_lib.Command
err := json.Unmarshal([]byte(msg), &cmd) if sender == m.P1 {
if err != nil { cmd.PlayerID = tome_lib.SentinalID
log.Printf("error unmarshaling gamecommand %v", err) } else if sender == m.P2 {
return MatchResp{Body: "", Private: false} cmd.PlayerID = tome_lib.ScourgeID
} }
msg_s := strings.Split(msg, " ")
ctype := tome_lib.CmdType(msg_s[0])
if ctype != tome_lib.ActCmd && msg_s[0] != tome_lib.StateCmd && ctype != tome_lib.DebugCmd && ctype != tome_lib.DebugCmd {
return EmptyMatchResp()
}
cmd.Type = ctype
cmd.Cmd = strings.Join(msg_s[1:], " ")
res := m.Game.Parse(&cmd) res := m.Game.Parse(&cmd)
match_res := MatchResp{ match_res := MatchResp{
Body: res.String(), Body: res.String(),
@ -72,12 +86,13 @@ func (m *Match) JoinOrLeave(members map[id.UserID]struct {
} }
} else if evt.Membership == event.MembershipLeave { } else if evt.Membership == event.MembershipLeave {
if !p1ok || !p2ok { if !p1ok || !p2ok {
if m.Game.Status != tome_lib.StatusDraw || log.Println("player has left match")
m.Game.Status != tome_lib.StatusScourgeWin || if m.Game.Status != tome_lib.StatusDraw &&
m.Game.Status != tome_lib.StatusScourgeWin &&
m.Game.Status != tome_lib.StatusSentinalWin { m.Game.Status != tome_lib.StatusSentinalWin {
m.Game.Status = tome_lib.StatusStop m.Game.Status = tome_lib.StatusStop
log.Println("user left match; stopping") log.Println("user left match before it ended; stopping")
} }
} }
} else { } else {