138 lines
4.0 KiB
Go
138 lines
4.0 KiB
Go
package action
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
"mongo.games.com/goserver/core/basic"
|
|
"mongo.games.com/goserver/core/logger"
|
|
"mongo.games.com/goserver/core/netlib"
|
|
"mongo.games.com/goserver/core/task"
|
|
"mongo.games.com/goserver/core/timer"
|
|
|
|
"mongo.games.com/game/machine/machinedoll"
|
|
"mongo.games.com/game/protocol/machine"
|
|
)
|
|
|
|
type DoneFunc func(c net.Conn)
|
|
|
|
func Process(conn *machinedoll.Conn, sec time.Duration, f1, f2 []DoneFunc, isSync bool) {
|
|
var ch chan struct{}
|
|
if isSync {
|
|
ch = make(chan struct{}, 1)
|
|
}
|
|
task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} {
|
|
for _, v := range f1 {
|
|
v(conn)
|
|
}
|
|
if len(f2) > 0 {
|
|
timer.AfterTimer(func(h timer.TimerHandle, ud interface{}) bool {
|
|
Process(conn, 0, f2, nil, isSync)
|
|
if isSync {
|
|
ch <- struct{}{}
|
|
}
|
|
return true
|
|
}, nil, sec)
|
|
} else {
|
|
if isSync {
|
|
ch <- struct{}{}
|
|
}
|
|
}
|
|
return nil
|
|
}), nil).StartByFixExecutor(fmt.Sprintf("Machine%v", conn.Addr))
|
|
if isSync {
|
|
<-ch
|
|
}
|
|
}
|
|
|
|
// 移动
|
|
func SMDollMachinePerateHandler(session *netlib.Session, packetId int, data interface{}) error {
|
|
logger.Logger.Tracef("SMDollMachinePerateHandler %v", data)
|
|
msg, ok := data.(*machine.SMDollMachineoPerate)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
conn, ok := machinedoll.MachineMgr.ConnMap[int(msg.GetId())]
|
|
if !ok || conn == nil {
|
|
return nil
|
|
}
|
|
|
|
switch msg.Perate {
|
|
case 1:
|
|
//向前移动
|
|
Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Backward}, []DoneFunc{machinedoll.BackwardStop}, false)
|
|
case 2:
|
|
//向后移动
|
|
Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Forward}, []DoneFunc{machinedoll.ForwardStop}, false)
|
|
case 3:
|
|
//向左移动
|
|
Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Left}, []DoneFunc{machinedoll.LeftStop}, false)
|
|
case 4:
|
|
//向右移动
|
|
Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Right}, []DoneFunc{machinedoll.RightStop}, false)
|
|
case 5:
|
|
//投币
|
|
Process(conn, 0*time.Millisecond, []DoneFunc{machinedoll.Coin, machinedoll.Coin}, []DoneFunc{}, false)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 下抓
|
|
func SMDollMachineGrabHandler(session *netlib.Session, packetId int, data interface{}) error {
|
|
logger.Logger.Tracef("SMDollMachineGrabHandler %v", data)
|
|
msg, ok := data.(*machine.SMDollMachineGrab)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
conn, ok := machinedoll.MachineMgr.ConnMap[int(msg.GetId())]
|
|
if !ok || conn == nil {
|
|
return nil
|
|
}
|
|
|
|
send := func(net.Conn) {
|
|
session.Send(int(machine.DollMachinePacketID_PACKET_SMDollMachineGrab), &machine.MSDollMachineGrab{
|
|
Snid: msg.Snid,
|
|
Id: msg.GetId(),
|
|
Result: 1,
|
|
})
|
|
}
|
|
|
|
switch msg.GetTypeId() {
|
|
case 1:
|
|
//弱抓
|
|
Process(conn, 0, []DoneFunc{machinedoll.WeakGrab}, []DoneFunc{send}, false)
|
|
case 2:
|
|
//强力抓
|
|
Process(conn, 0, []DoneFunc{machinedoll.Grab}, []DoneFunc{send}, false)
|
|
case 3:
|
|
//必中抓
|
|
Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.SetPower}, []DoneFunc{machinedoll.Grab, send}, false)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 与游戏服务器连接成功,向游戏服务器推送所有娃娃机连接
|
|
func SMGameLinkSucceedHandler(session *netlib.Session, packetId int, data interface{}) error {
|
|
logger.Logger.Trace("与游戏服务器连接成功")
|
|
//开始向游戏服务器发送娃娃机连接信息
|
|
msg := &machine.MSDollMachineList{}
|
|
for i, _ := range machinedoll.MachineMgr.ConnMap {
|
|
info := &machine.DollMachine{}
|
|
info.Id = int32(i)
|
|
info.VideoAddr = "www.baidu.com"
|
|
msg.Data = append(msg.Data, info)
|
|
}
|
|
session.Send(int(machine.DollMachinePacketID_PACKET_MSDollMachineList), msg)
|
|
logger.Logger.Tracef("向游戏服务器发送娃娃机连接信息:%v", msg)
|
|
return nil
|
|
}
|
|
func init() {
|
|
netlib.Register(int(machine.DollMachinePacketID_PACKET_SMDollMachinePerate), &machine.SMDollMachineoPerate{}, SMDollMachinePerateHandler)
|
|
netlib.Register(int(machine.DollMachinePacketID_PACKET_SMDollMachineGrab), &machine.SMDollMachineGrab{}, SMDollMachineGrabHandler)
|
|
//链接成功 返回消息
|
|
netlib.Register(int(machine.DollMachinePacketID_PACKET_SMGameLinkSucceed), &machine.SMGameLinkSucceed{}, SMGameLinkSucceedHandler)
|
|
}
|