Skip to content

Commit

Permalink
add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
dwg65535 committed Apr 15, 2019
1 parent 06a01e3 commit cf4af69
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ type Client struct {
toServer chan []interface{} //robot发送给服务器
}

//重置状态
func (c *Client) reset() {
c.UserInfo.Role = 1
c.HandPokers = make([]int, 0, 21)
c.Ready = false
c.IsCalled = false
}

//发送房间内已有的牌桌信息
func (c *Client) sendRoomTables() {
res := make([][2]int, 0)
for _, table := range c.Room.Tables {
Expand Down Expand Up @@ -105,6 +107,7 @@ func (c *Client) sendMsg(msg []interface{}) {
}
}

//光比客户端
func (c *Client) close() {
if c.Table != nil {
for _, client := range c.Table.TableClients {
Expand Down Expand Up @@ -139,6 +142,7 @@ func (c *Client) close() {
}
}

//可能是因为版本问题,导致有些未处理的error
func (c *Client) readPump() {
defer func() {
//logs.Debug("readPump exit")
Expand Down
1 change: 1 addition & 0 deletions service/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"landlord/common"
)

//处理websocket请求
func wsRequest(data []interface{}, client *Client) {
defer func() {
if r := recover(); r != nil {
Expand Down
2 changes: 2 additions & 0 deletions service/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (c *Client) runRobot() {
}
}

//自动出牌
func (c *Client) autoShotPoker() {
//因为机器人休眠一秒后才出牌,有可能因用户退出而关闭chan
defer func() {
Expand All @@ -102,6 +103,7 @@ func (c *Client) autoShotPoker() {
c.toServer <- req
}

//自动叫分
func (c *Client) autoCallScore() {
defer func() {
err := recover()
Expand Down
1 change: 1 addition & 0 deletions service/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Room struct {
EntranceFee int
}

//新建牌桌
func (r *Room) newTable(client *Client) (table *Table) {
roomManager.Lock.Lock()
defer roomManager.Lock.Unlock()
Expand Down
24 changes: 21 additions & 3 deletions service/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (table *Table) allCalled() bool {
return true
}

//一局结束
func (table *Table) gameOver(client *Client) {
coin := table.Creator.Room.EntranceFee * table.GameManage.MaxCallScore * table.GameManage.Multiple
table.State = GameEnd
Expand All @@ -70,6 +71,7 @@ func (table *Table) gameOver(client *Client) {
logs.Debug("table[%d] game over", table.TableId)
}

//叫分阶段结束
func (table *Table) callEnd() {
table.State = GamePlaying
table.GameManage.FirstCallScore = table.GameManage.FirstCallScore.Next
Expand All @@ -90,6 +92,7 @@ func (table *Table) callEnd() {
}
}

//客户端加入牌桌
func (table *Table) joinTable(c *Client) {
table.Lock.Lock()
defer table.Lock.Unlock()
Expand Down Expand Up @@ -118,21 +121,20 @@ func (table *Table) joinTable(c *Client) {
table.State = GameCallScore
table.dealPoker()
} else if c.Room.AllowRobot {
time.Sleep(time.Microsecond * 10)
go table.addRobot(c.Room)
logs.Debug("robot join ok")
}
}

//加入机器人
func (table *Table) addRobot(room *Room) {
logs.Debug("robot [%v] join table", fmt.Sprintf("ROBOT-%d", len(table.TableClients)))
rand.Seed(time.Now().UnixNano())
if len(table.TableClients) < 3 {
client := &Client{
Room: room,
HandPokers: make([]int, 0, 21),
UserInfo: &UserInfo{
UserId: UserId(rand.Intn(10000)),
UserId: table.getRobotID() ,
Username: fmt.Sprintf("ROBOT-%d", len(table.TableClients)),
Coin: 10000,
},
Expand All @@ -145,6 +147,20 @@ func (table *Table) addRobot(room *Room) {
}
}

//生成随机robotID
func (table *Table)getRobotID() (robot UserId) {
time.Sleep(time.Microsecond * 10)
rand.Seed(time.Now().UnixNano())
robot = UserId(rand.Intn(10000))
table.Lock.RLock()
defer table.Lock.RUnlock()
if _,ok := table.TableClients[robot];ok {
return table.getRobotID()
}
return
}

//发牌
func (table *Table) dealPoker() {
logs.Debug("deal poker")
table.GameManage.Pokers = make([]int, 0)
Expand Down Expand Up @@ -197,6 +213,7 @@ func (table *Table) reset() {
}
}

//洗牌
func (table *Table) ShufflePokers() {
logs.Debug("ShufflePokers")
r := rand.New(rand.NewSource(time.Now().Unix()))
Expand All @@ -208,6 +225,7 @@ func (table *Table) ShufflePokers() {
}
}

//同步用户信息
func (table *Table) syncUser() () {
logs.Debug("sync user")
response := make([]interface{}, 0, 3)
Expand Down

0 comments on commit cf4af69

Please sign in to comment.