go_dreamfactory/modules/catchbugs/room.go
2023-11-08 21:41:51 +08:00

171 lines
4.2 KiB
Go

package catchbugs
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"time"
"google.golang.org/protobuf/proto"
)
type Room struct {
module *CatchBugs
data *pb.DBCatchBugsRoom
sessions []comm.IUserSession
starttime time.Time
round int32
handleplayer string
currindex int32
}
func (this *Room) GameStart() (err error) {
this.starttime = configure.Now()
if err = this.Broadcast("gameready", &pb.CatchbugsGameReadyPush{
ServicePath: fmt.Sprintf("%s/%s", this.module.service.GetType(), this.module.service.GetId()),
Room: this.data,
Countdown: 60,
}); err != nil {
this.module.Errorln(err)
}
return
}
func (this *Room) PlayerLoadEnd(uid string) (err error) {
if uid == this.data.Red.Info.Uid {
this.data.Red.Ready = true
} else {
this.data.Blue.Ready = true
}
if this.data.Rules.Headstart == 0 {
this.handleplayer = this.data.Red.Info.Uid
} else {
this.handleplayer = this.data.Blue.Info.Uid
}
if this.data.Red.Ready && this.data.Blue.Ready { //两个人都准备了
if err = this.Broadcast("roundstart", &pb.CatchbugsRoundStartPush{
Round: this.round,
Handleplayer: this.handleplayer,
}); err != nil {
this.module.Errorln(err)
}
}
return
}
//玩家操作
func (this *Room) PlayerHandle(uid string, handle *pb.CatchbugsHandleReq) (err error) {
if uid != this.handleplayer {
err = fmt.Errorf("It's not your operation!")
return
}
var (
issucc bool
)
if this.data.Red.Info.Uid == uid {
if this.data.Red.Lastopencard == 0 {
this.data.Red.Lastopencard = handle.Index
} else {
if this.data.Card[this.data.Red.Lastopencard].Cid == this.data.Card[handle.Index].Cid {
issucc = true
this.data.Card[this.data.Red.Lastopencard].Isopen = true
this.data.Card[handle.Index].Isopen = true
this.data.Red.Cards = append(this.data.Red.Cards, this.data.Card[this.data.Red.Lastopencard].Cid)
}
}
} else {
if this.data.Blue.Lastopencard == 0 {
this.data.Blue.Lastopencard = handle.Index
} else {
if this.data.Card[this.data.Blue.Lastopencard].Cid == this.data.Card[handle.Index].Cid {
issucc = true
this.data.Card[this.data.Red.Lastopencard].Isopen = true
this.data.Card[handle.Index].Isopen = true
this.data.Blue.Cards = append(this.data.Red.Cards, this.data.Card[this.data.Blue.Lastopencard].Cid)
}
}
}
if err = this.Broadcast("gameover", &pb.CatchbugsOpenCardPush{
Roomid: this.data.Rid,
Index: handle.Index,
Issucc: issucc,
}); err != nil {
this.module.Errorln(err)
}
return
}
//玩家操作
func (this *Room) PlayerHandleEnd(uid string, handle *pb.CatchbugsHandleEndReq) (err error) {
if uid != this.handleplayer {
err = fmt.Errorf("It's not your operation!")
return
}
this.round++
if this.data.Red.Info.Uid == uid {
this.handleplayer = this.data.Blue.Info.Uid
} else {
this.handleplayer = this.data.Red.Info.Uid
}
if err = this.Broadcast("roundstart", &pb.CatchbugsRoundStartPush{
Round: this.round,
Handleplayer: this.handleplayer,
}); err != nil {
this.module.Errorln(err)
}
return
}
func (this *Room) Broadcast(stype string, msg proto.Message) (err error) {
if err = this.module.SendMsgToSession(string(this.module.GetType()), stype, msg, this.sessions...); err != nil {
this.module.Errorln(err)
}
return
}
//技能效果1 随机2x2的区域 旋转
func Skill1Effect(cards []*pb.DBCatchBugsCard) (ramdonCard []*pb.DBCatchBugsCard) {
var (
cardsSlice []int32
ramdon int32 //随机点
x int32
y int32
offsetX, offsetY int32
)
for _, v := range cards {
if !v.Isopen {
cardsSlice = append(cardsSlice, v.Index)
}
}
ramdon = cardsSlice[comm.RandShuffle(len(cardsSlice))[0]]
x = ramdon % 4
y = ramdon / 4
offsetX = 1
if x == 3 {
offsetX = -1
}
offsetY = 1
if y == 6 {
offsetY = -1
}
ramdonCard = append(ramdonCard, cards[y*6+x])
ramdonCard = append(ramdonCard, cards[y*6+x+offsetX])
ramdonCard = append(ramdonCard, cards[(y+offsetY)*6+x+offsetX])
ramdonCard = append(ramdonCard, cards[(y+offsetY)*6+x])
index0 := ramdonCard[0].Index
for i, v := range ramdonCard {
if i < len(ramdonCard) {
v.Index = ramdonCard[i+1].Index
} else {
v.Index = index0
}
}
return
}