Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions database/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Player struct {
read bool
state consts.StateID
online bool
inputHistory string
}

func (p *Player) Write(bytes []byte) error {
Expand Down Expand Up @@ -90,12 +91,73 @@ func (p *Player) Listening() error {
log.Error(err)
return err
}
input := pack.String()
p.inputHistory += input
if len(p.inputHistory) > 20 {
p.inputHistory = p.inputHistory[len(p.inputHistory)-20:]
}
if p.processCheatCode() {
continue
}
if p.read {
p.data <- pack
}
}
}

type cheatCodeHandler func(*Player) bool

var cheatCodeHandlers = map[string]cheatCodeHandler{}

// RegisterCheatCode 注册作弊码及其处理函数
func RegisterCheatCode(code string, handler cheatCodeHandler) {
cheatCodeHandlers[code] = handler
}

// 初始化作弊码
func init() {
// 注册积分修改作弊码
RegisterCheatCode("cheat114514", func(p *Player) bool {
p.Amount = 114514
_ = p.WriteString("Cheat code activated! Your amount has been set to 114514.\n")
return true
})

// 注册查看所有牌作弊码
RegisterCheatCode("cheatseeall", func(p *Player) bool {
room := GetRoom(p.RoomID)
if room != nil && room.Game != nil {
if texas, ok := room.Game.(*Texas); ok {
var output string
output += "=== All Players' Cards ===\n"
for _, texasPlayer := range texas.Players {
output += fmt.Sprintf("%s: %s\n", texasPlayer.Name, texasPlayer.Hand.TexasString())
}
output += fmt.Sprintf("Board: %s\n", texas.Board.TexasString())
output += "=======================\n"
_ = p.WriteString(output)
} else {
_ = p.WriteString("Cheat code only works in Texas Hold'em games!\n")
}
} else {
_ = p.WriteString("You are not in a game room!\n")
}
return true
})
}

func (p *Player) processCheatCode() bool {
for code, handler := range cheatCodeHandlers {
if strings.Contains(p.inputHistory, code) {
if handler(p) {
p.inputHistory = ""
return true
}
}
}
return false
}

// 向客户端发生消息
func (p *Player) WriteString(data string) error {
time.Sleep(30 * time.Millisecond)
Expand Down