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 {
panic(err)
}
fmt.Println("Login successful")
log.Println("Login successful")
d.Client = client
}
@ -92,12 +92,16 @@ func (d *Dealer) RegisterHandlers() {
_ = d.Client.StateEvent(playerRoom, event.Type{"snen.player", event.StateEventType}, "player_info", &roominfo)
if roominfo.CurrentGame != "" {
//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 {
d.Client.SendText(playerRoom, "can't accept game commands yet")
return
}
resp := match.ParseAction(evt.Content.AsMessage().Body)
resp := match.ParseAction(evt.Sender, evt.Content.AsMessage().Body)
if resp.Body != "" {
_, err := d.Client.SendText(playerRoom, resp.Body)
if err != nil {
@ -145,7 +149,7 @@ func (d *Dealer) SetupRooms(domain string) {
RoomAliasName: "tomequeue",
})
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)
if err != nil {
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)
d.QueueRoom = res.RoomID
return
}
} else {
resJoin, err := d.Client.JoinRoomByID(res.RoomID)
if err != nil {
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)
d.QueueRoom = resJoin.RoomID
}
//load player rooms
log.Println("Loading known player rooms")
joinedResp, err := d.Client.JoinedRooms()
if err != nil {
panic(err)
@ -171,6 +176,7 @@ func (d *Dealer) SetupRooms(domain string) {
for _, r := range joinedResp.JoinedRooms {
tags, err := d.Client.GetTags(r)
if err != nil {
log.Printf("can't get tags for room %v:%v", r, err)
continue
}
_, ok := tags.Tags["tome_player_room"]
@ -186,16 +192,26 @@ func (d *Dealer) SetupRooms(domain string) {
keys = append(keys, k)
}
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
}
for _, k := range keys {
if k != d.Client.UserID {
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 {
@ -213,8 +229,16 @@ func (d *Dealer) NewMatch(p1, p2 id.UserID) *Match {
}
match.MatrixRoom = createRes.RoomID
match.P1 = p1
match.P1Room = d.PlayerRooms[p1]
_, found := d.PlayerRooms[p1]
if !found {
d.NewPlayerRoom(p1)
}
match.P2 = p2
_, found = d.PlayerRooms[p2]
if !found {
d.NewPlayerRoom(p2)
}
match.P1Room = d.PlayerRooms[p1]
match.P2Room = d.PlayerRooms[p2]
match.Dealer = d.Client.UserID
d.RoomsList[createRes.RoomID] = match
@ -238,17 +262,7 @@ func (d *Dealer) QueueEvent(evt *event.Event) {
_, ok := d.PlayerRooms[evt.Sender]
if !ok {
//open up DM with player
createRes, err := d.Client.CreateRoom(&mautrix.ReqCreateRoom{
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)
err := d.NewPlayerRoom(evt.Sender)
if err != nil {
panic(err)
}
@ -258,3 +272,21 @@ func (d *Dealer) QueueEvent(evt *event.Event) {
}
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
import (
"encoding/json"
"log"
"strings"
"git.saintnet.tech/tomecraft/tome_game"
"git.saintnet.tech/tomecraft/tome_lib"
@ -27,6 +27,13 @@ type MatchResp struct {
Private bool
}
func EmptyMatchResp() MatchResp {
return MatchResp{
Body: "",
Private: false,
}
}
func NewMatch() *Match {
return &Match{
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
err := json.Unmarshal([]byte(msg), &cmd)
if err != nil {
log.Printf("error unmarshaling gamecommand %v", err)
return MatchResp{Body: "", Private: false}
if sender == m.P1 {
cmd.PlayerID = tome_lib.SentinalID
} else if sender == m.P2 {
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)
match_res := MatchResp{
Body: res.String(),
@ -72,12 +86,13 @@ func (m *Match) JoinOrLeave(members map[id.UserID]struct {
}
} else if evt.Membership == event.MembershipLeave {
if !p1ok || !p2ok {
if m.Game.Status != tome_lib.StatusDraw ||
m.Game.Status != tome_lib.StatusScourgeWin ||
log.Println("player has left match")
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")
log.Println("user left match before it ended; stopping")
}
}
} else {