go_dreamfactory/modules/caninerabbit/room.go
2023-11-22 17:32:04 +08:00

173 lines
4.4 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 string
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 { //两个人都准备了
if this.data.Rules.Headstart == 0 {
if this.data.Red.Ctype == 0 { //红方先手
this.currside = this.data.Red.Info.Uid
} else {
this.currside = this.data.Blue.Info.Uid
}
} else {
if this.data.Red.Ctype == 0 { //红方先手
this.currside = this.data.Blue.Info.Uid
} else {
this.currside = this.data.Red.Info.Uid
}
}
if err = this.Broadcast("gamestart", &pb.CanineRabbitGameStartPush{
Roomid: this.data.Rid,
Currplayer: this.currside,
}); err != nil {
this.module.Errorln(err)
}
}
return
}
//玩家操作
func (this *Room) PlayerHandle(uid string, handle *pb.CanineRabbitHandleReq) (err error) {
if this.currside == uid {
if this.currside == this.data.Red.Info.Uid {
this.currside = this.data.Blue.Info.Uid
} else {
this.currside = this.data.Red.Info.Uid
}
} else {
err = fmt.Errorf("It's not you who shoot!")
return
}
for _, v := range this.data.Chess {
if v.Id == handle.Chess.Id {
v.X = handle.Chess.X
v.Y = handle.Chess.Y
}
}
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) {
var (
winuid string = uid
)
if handle.Iswin {
if this.currside != this.data.Red.Info.Uid {
if this.data.Red.Ctype == 0 {
this.data.Red.Rabbitintegral += 1
} else {
this.data.Red.Houndintegral += 1
}
this.module.model.Change(this.data.Red.Info.Uid, map[string]interface{}{
"rabbitintegral": this.data.Red.Rabbitintegral,
"houndintegral": this.data.Red.Houndintegral,
})
} else {
if this.data.Blue.Ctype == 0 {
this.data.Blue.Rabbitintegral += 1
} else {
this.data.Blue.Houndintegral += 1
}
this.module.model.Change(this.data.Blue.Info.Uid, map[string]interface{}{
"rabbitintegral": this.data.Blue.Rabbitintegral,
"houndintegral": this.data.Blue.Houndintegral,
})
}
if err = this.Broadcast("gameover", &pb.CanineRabbitGameOverPush{
Winuid: winuid,
Admitdefeat: false,
Red: this.data.Red,
Blue: this.data.Blue,
}); err != nil {
this.module.Errorln(err)
}
} else {
if uid != this.data.Red.Info.Uid {
winuid = this.data.Red.Info.Uid
if this.data.Red.Ctype == 0 {
this.data.Red.Rabbitintegral += 1
} else {
this.data.Red.Houndintegral += 1
}
this.module.model.Change(this.data.Red.Info.Uid, map[string]interface{}{
"rabbitintegral": this.data.Red.Rabbitintegral,
"houndintegral": this.data.Red.Houndintegral,
})
} else {
winuid = this.data.Blue.Info.Uid
if this.data.Blue.Ctype == 0 {
this.data.Blue.Rabbitintegral += 1
} else {
this.data.Blue.Houndintegral += 1
}
this.module.model.Change(this.data.Blue.Info.Uid, map[string]interface{}{
"rabbitintegral": this.data.Blue.Rabbitintegral,
"houndintegral": this.data.Blue.Houndintegral,
})
}
if err = this.Broadcast("gameover", &pb.CanineRabbitGameOverPush{
Winuid: winuid,
Admitdefeat: true,
Red: this.data.Red,
Blue: this.data.Blue,
}); err != nil {
this.module.Errorln(err)
}
}
this.module.rooms.removeRoom(this.data.Rid)
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
}