网络游戏初步接入

This commit is contained in:
2024-10-18 15:11:40 +08:00
parent 263a57f636
commit b27d2b80f7
12 changed files with 306 additions and 67 deletions
+114 -7
View File
@@ -18,6 +18,9 @@ import (
"image/png"
"slices"
"sync"
"work_cation/global"
"work_cation/models"
"work_cation/service"
)
func TenChinaGameView() {
@@ -35,6 +38,39 @@ func TenChinaGameView() {
game.StartShow()
}
func StartGameListen() {
go func() {
for {
select {
case msg := <-global.Send.Game1Chan:
switch msg.Router {
case "/play_game":
g := global.GetGameInfo(msg.GameUuid)
if g != nil {
game := g.Obj.(*TenGame)
game.Play(game.userIndex, msg.Pos)
}
case "/close_game":
g := global.GetGameInfo(msg.GameUuid)
if g != nil {
dialog.ShowInformation("提示", "对方已退出", g.Obj.(*TenGame).w)
}
case "/start_game":
g := global.GetGameInfo(msg.GameUuid)
if g != nil {
fmt.Println("start_game")
continue
}
game := NewTenGame(nil)
online := &models.Online{ID: msg.User.ID, Ip: msg.User.Ip, Port: msg.User.Port}
game.AddNet(msg.GameUuid, 1, online, msg.User)
game.StartShow()
}
}
}
}()
}
// 格子棋 Checkered-Chess
type TenGame struct {
lock sync.Mutex // 玩家操作锁
@@ -60,6 +96,13 @@ type TenGame struct {
Ctx context.Context
Cancel func()
isNet bool // 是否是网络模式
uuid string
myIndex int
user *models.Users //
userIndex int
online *models.Online
}
func NewTenGame(winCallback func(int)) *TenGame {
@@ -83,22 +126,56 @@ func NewTenGame(winCallback func(int)) *TenGame {
winSum: 5,
Ctx: ctx,
Cancel: cancel,
isNet: false,
}
return game
}
func (t *TenGame) AddNet(uuid string, myIndex int, online *models.Online, user *models.Users) error {
t.isNet = true
t.online = online
t.myIndex = myIndex
t.user = user
t.uuid = uuid
info := &global.RunGameInfo{
Uuid: t.uuid,
Type: global.GameType1,
Obj: t,
}
global.CreateGame(info)
// 发起人
if myIndex == 0 {
t.userIndex = 1
_, err := service.Client.StartGame(t.online, t.uuid, global.GameType1)
if err != nil {
t.isNet = false
return err
}
}
return nil
}
func (t *TenGame) StartShow() {
myApp := fyne.CurrentApp()
myWindow := myApp.NewWindow(fmt.Sprintf("Game %0.f*%0.f win:%d", t.itemX, t.itemY, t.winSum))
t.w = myWindow
myWindow := myApp.NewWindow("")
t.w = myWindow
t.setTitle()
wSize := fyne.NewSize(t.itemX*(t.itemSize.Width), t.itemY*(t.itemSize.Height))
myWindow.Resize(wSize)
myWindow.SetContent(container.New(layout.NewMaxLayout(),
canvas.NewImageFromResource(drawABackgroundImage(wSize, int(t.itemX), int(t.itemY))),
t.CanvasObject()))
myWindow.SetOnClosed(func() {
if t.isNet {
global.DeleteGame(t.uuid)
service.Client.CloseGame(t.online, t.uuid)
}
})
myWindow.CenterOnScreen()
myWindow.RequestFocus()
myWindow.Show()
}
@@ -115,7 +192,10 @@ func (t *TenGame) CanvasObject() fyne.CanvasObject {
t.Items = append(t.Items, image)
toggle := widget.NewButton("", func() {
_ = t.Play(0, itemIndex)
err := t.Play(t.myIndex, itemIndex)
if err != nil {
dialog.ShowError(err, t.w)
}
})
toggle.Resize(t.itemSize)
@@ -132,21 +212,48 @@ func (t *TenGame) CanvasObject() fyne.CanvasObject {
func (t *TenGame) Play(userIndex int, pos int) error {
t.lock.Lock()
defer t.lock.Unlock()
//if t.currentRoundPlayer != userIndex {
// return errors.New("你急个der")
//}
if t.isNet && t.currentRoundPlayer != userIndex {
return errors.New("你急个der")
}
if slices.Contains(t.allMaps, pos) {
return errors.New("已经下过了")
}
if t.isNet && t.myIndex == userIndex {
// todo 广播数据
_, err := service.Client.PlayGame(t.online, t.uuid, pos)
if err != nil {
return err
}
}
// 下棋
t.allMaps = append(t.allMaps, pos)
t.play(pos)
// 推进
t.advance()
return nil
}
// 推进
func (t *TenGame) advance() {
t.currentRoundPlayer++
if t.currentRoundPlayer+1 > len(t.playerID2Image) {
t.currentRoundPlayer = 0
}
return nil
t.setTitle()
}
func (t *TenGame) setTitle() {
title := fmt.Sprintf("Game %0.f*%0.f win:%d", t.itemX, t.itemY, t.winSum)
if t.isNet {
title += fmt.Sprintf(" | 我 vs %s", t.user.Name)
if t.currentRoundPlayer == t.myIndex {
title += " | 我的回合"
} else {
title += " | 对手回合"
}
}
t.w.SetTitle(title)
}
/*