we have liftoff baby
This commit is contained in:
parent
9d76a55e70
commit
62fc53c96b
82
dealer.go
82
dealer.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
@ -73,17 +74,53 @@ func (d *Dealer) RegisterHandlers() {
|
|||||||
log.Printf("received membership event for non active game: %v", evt)
|
log.Printf("received membership event for non active game: %v", evt)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if evt.Sender != match.P1 || evt.Sender != match.P2 || evt.Sender != d.Client.UserID {
|
if evt.Sender != match.P1 && evt.Sender != match.P2 && evt.Sender != d.Client.UserID {
|
||||||
log.Println("membership event for non player")
|
log.Println("membership event for non player")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gameRoom := d.Client.Store.LoadRoom(evt.RoomID)
|
members, err := d.Client.JoinedMembers(evt.RoomID)
|
||||||
match.JoinOrLeave(gameRoom, evt)
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
match.JoinOrLeave(members.Joined, evt.Content.AsMember())
|
||||||
})
|
})
|
||||||
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
||||||
playerRoom, ok := d.PlayerRooms[evt.Sender]
|
playerRoom, pok := d.PlayerRooms[evt.Sender]
|
||||||
if ok {
|
if pok {
|
||||||
log.Printf("received direct message in room %v", playerRoom)
|
log.Printf("received direct message in room %v", playerRoom)
|
||||||
|
var roominfo PlayerRoomInfo
|
||||||
|
_ = d.Client.StateEvent(playerRoom, event.Type{"snen.player", event.StateEventType}, "player_info", &roominfo)
|
||||||
|
if roominfo.CurrentGame != "" {
|
||||||
|
//parse as gamecommand
|
||||||
|
match := d.RoomsList[roominfo.CurrentGame]
|
||||||
|
if match.Game == nil {
|
||||||
|
d.Client.SendText(playerRoom, "can't accept game commands yet")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resp := match.ParseAction(evt.Content.AsMessage().Body)
|
||||||
|
if resp.Body != "" {
|
||||||
|
_, err := d.Client.SendText(playerRoom, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
d.Client.SendText(roominfo.CurrentGame, fmt.Sprintf("%v played %v", evt.Sender, evt.Content.AsMessage().Body))
|
||||||
|
|
||||||
|
if !resp.Private {
|
||||||
|
d.Client.SendText(roominfo.CurrentGame, fmt.Sprintf("Result: %v", resp.Body))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//parse as out of game command
|
||||||
|
log.Println("out of game command received")
|
||||||
|
//TODO make this better
|
||||||
|
msg := evt.Content.AsMessage().Body
|
||||||
|
msg_s := strings.Split(msg, " ")
|
||||||
|
if msg_s[0] == "direct" {
|
||||||
|
d.NewMatch(evt.Sender, id.NewUserID(msg_s[1], d.HomeserverDomain))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
match, ok := d.RoomsList[evt.RoomID]
|
match, ok := d.RoomsList[evt.RoomID]
|
||||||
@ -95,26 +132,7 @@ func (d *Dealer) RegisterHandlers() {
|
|||||||
log.Println("ignoring message event from non player")
|
log.Println("ignoring message event from non player")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//parse as game command
|
//parse as in game command i.e. chat
|
||||||
resp := match.ParseAction(evt.Content.AsMessage().Body)
|
|
||||||
if resp.Private {
|
|
||||||
//respond privately
|
|
||||||
dm, ok := d.PlayerRooms[evt.Sender]
|
|
||||||
if !ok {
|
|
||||||
log.Println("tried to send private message to player without room")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err := d.Client.SendText(dm, resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_, err := d.Client.SendText(evt.RoomID, resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +203,7 @@ func (d *Dealer) NewMatch(p1, p2 id.UserID) *Match {
|
|||||||
match := NewMatch()
|
match := NewMatch()
|
||||||
roomname := fmt.Sprintf("tome_match_%v", match.ID)
|
roomname := fmt.Sprintf("tome_match_%v", match.ID)
|
||||||
createRes, err := d.Client.CreateRoom(&mautrix.ReqCreateRoom{
|
createRes, err := d.Client.CreateRoom(&mautrix.ReqCreateRoom{
|
||||||
Preset: "public",
|
Preset: "public_chat",
|
||||||
RoomAliasName: roomname,
|
RoomAliasName: roomname,
|
||||||
Invite: []id.UserID{p1, p2},
|
Invite: []id.UserID{p1, p2},
|
||||||
})
|
})
|
||||||
@ -195,9 +213,21 @@ 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]
|
||||||
match.P2 = p2
|
match.P2 = 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
|
||||||
|
|
||||||
|
d.Client.SendStateEvent(match.P1Room, event.Type{"snen.player", event.StateEventType}, "player_info", PlayerRoomInfo{
|
||||||
|
CurrentGame: match.MatrixRoom,
|
||||||
|
CurrentDeck: []int{},
|
||||||
|
})
|
||||||
|
d.Client.SendStateEvent(match.P2Room, event.Type{"snen.player", event.StateEventType}, "player_info", PlayerRoomInfo{
|
||||||
|
CurrentGame: match.MatrixRoom,
|
||||||
|
CurrentDeck: []int{},
|
||||||
|
})
|
||||||
|
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
|
42
match.go
42
match.go
@ -7,7 +7,6 @@ import (
|
|||||||
"git.saintnet.tech/tomecraft/tome_game"
|
"git.saintnet.tech/tomecraft/tome_game"
|
||||||
"git.saintnet.tech/tomecraft/tome_lib"
|
"git.saintnet.tech/tomecraft/tome_lib"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"maunium.net/go/mautrix"
|
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
@ -16,7 +15,9 @@ type Match struct {
|
|||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
Game *tome_game.Game
|
Game *tome_game.Game
|
||||||
P1 id.UserID
|
P1 id.UserID
|
||||||
|
P1Room id.RoomID
|
||||||
P2 id.UserID
|
P2 id.UserID
|
||||||
|
P2Room id.RoomID
|
||||||
Dealer id.UserID
|
Dealer id.UserID
|
||||||
MatrixRoom id.RoomID
|
MatrixRoom id.RoomID
|
||||||
}
|
}
|
||||||
@ -31,19 +32,21 @@ func NewMatch() *Match {
|
|||||||
ID: uuid.New(),
|
ID: uuid.New(),
|
||||||
Game: nil,
|
Game: nil,
|
||||||
P1: "",
|
P1: "",
|
||||||
|
P1Room: "",
|
||||||
P2: "",
|
P2: "",
|
||||||
|
P2Room: "",
|
||||||
MatrixRoom: "",
|
MatrixRoom: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Match) ParseAction(msg string) MatchResp {
|
func (m *Match) ParseAction(msg string) MatchResp {
|
||||||
var cmd *tome_lib.Command
|
var cmd tome_lib.Command
|
||||||
err := json.Unmarshal([]byte(msg), cmd)
|
err := json.Unmarshal([]byte(msg), &cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error unmarshaling gamecommand %v", err)
|
log.Printf("error unmarshaling gamecommand %v", err)
|
||||||
return MatchResp{Body: "", Private: false}
|
return MatchResp{Body: "", Private: false}
|
||||||
}
|
}
|
||||||
res := m.Game.Parse(cmd)
|
res := m.Game.Parse(&cmd)
|
||||||
match_res := MatchResp{
|
match_res := MatchResp{
|
||||||
Body: res.String(),
|
Body: res.String(),
|
||||||
Private: (cmd.Type == tome_lib.ActCmd && cmd.String() == "d"),
|
Private: (cmd.Type == tome_lib.ActCmd && cmd.String() == "d"),
|
||||||
@ -52,21 +55,30 @@ func (m *Match) ParseAction(msg string) MatchResp {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Match) JoinOrLeave(gameRoom *mautrix.Room, evt *event.Event) {
|
func (m *Match) JoinOrLeave(members map[id.UserID]struct {
|
||||||
if gameRoom == nil {
|
DisplayName *string `json:"display_name"`
|
||||||
panic("no matrix room for game")
|
AvatarURL *string `json:"avatar_url"`
|
||||||
}
|
}, evt *event.MemberEventContent) {
|
||||||
if evt.Content.AsMember().Membership == event.MembershipJoin {
|
_, p1ok := members[m.P1]
|
||||||
if gameRoom.GetMembershipState(m.P1) == event.MembershipJoin && gameRoom.GetMembershipState(m.P2) == event.MembershipJoin {
|
_, 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")
|
log.Println("both players are in the room now")
|
||||||
m.Game = tome_game.NewGame([]int{}, []int{})
|
m.Game = tome_game.NewGame([]int{}, []int{})
|
||||||
}
|
}
|
||||||
} else if evt.Content.AsMember().Membership == event.MembershipLeave {
|
} else if evt.Membership == event.MembershipLeave {
|
||||||
if m.Game.Status != tome_lib.StatusDraw ||
|
if !p1ok || !p2ok {
|
||||||
m.Game.Status != tome_lib.StatusScourgeWin ||
|
if m.Game.Status != tome_lib.StatusDraw ||
|
||||||
m.Game.Status != tome_lib.StatusSentinalWin {
|
m.Game.Status != tome_lib.StatusScourgeWin ||
|
||||||
|
m.Game.Status != tome_lib.StatusSentinalWin {
|
||||||
|
|
||||||
m.Game.Status = tome_lib.StatusStop
|
m.Game.Status = tome_lib.StatusStop
|
||||||
|
log.Println("user left match; stopping")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Println("disregarding membership event we don't care about")
|
log.Println("disregarding membership event we don't care about")
|
||||||
|
8
player_room.go
Normal file
8
player_room.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
|
type PlayerRoomInfo struct {
|
||||||
|
CurrentGame id.RoomID `json:'current_game'`
|
||||||
|
CurrentDeck []int `json:'current_deck'`
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user