go_dreamfactory/modules/caninerabbit/room.go
2023-10-27 14:58:23 +08:00

112 lines
2.6 KiB
Go

package caninerabbit
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"time"
"google.golang.org/protobuf/proto"
)
type Room struct {
module *CanineRabbit
data *pb.DBCanineRabbitRoom
sessions []comm.IUserSession
starttime time.Time
currside int32
currindex int32
}
func (this *Room) GameStart() (err error) {
this.starttime = configure.Now()
if err = this.Broadcast("gameready", &pb.CanineRabbitGameReadyPush{
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.Red.Ready && this.data.Blue.Ready { //两个人都准备了
this.currside = 1
if err = this.Broadcast("gamestart", &pb.DColorGameStartPush{
Roomid: this.data.Rid,
Side: 1,
}); err != nil {
this.module.Errorln(err)
}
}
return
}
//玩家操作
func (this *Room) PlayerHandle(uid string, handle *pb.CanineRabbitHandleReq) (err error) {
for _, v := range this.data.Chess {
if v.Id == handle.Chess.Id {
v.X = handle.Chess.X
v.Y = handle.Chess.Y
}
}
if this.currside == 1 {
this.currside = 2
} else {
this.currside = 1
}
if err = this.Broadcast("gamehandle", &pb.CanineRabbitGameHandlePush{
Roomid: this.data.Rid,
Uid: uid,
Chess: handle.Chess,
}); err != nil {
this.module.Errorln(err)
}
return
}
//玩家操作
func (this *Room) PlayerWin(uid string, handle *pb.CanineRabbitWinReq) (err error) {
if handle.Iswin {
if this.currside == 1 {
this.data.Red.Score = 1
this.module.model.Change(this.data.Red.Info.Uid, map[string]interface{}{
"rabbitintegral": this.data.Red.Rabbitintegral,
"houndintegral": this.data.Red.Houndintegral,
})
} else {
this.data.Blue.Score = 1
this.module.model.Change(this.data.Blue.Info.Uid, map[string]interface{}{
"rabbitintegral": this.data.Red.Rabbitintegral,
"houndintegral": this.data.Red.Houndintegral,
})
}
if err = this.Broadcast("gameover", &pb.CanineRabbitGameOverPush{
Winside: this.currside,
RedIntegral: this.data.Red.Score,
BlueIntegral: this.data.Blue.Score,
}); 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
}