网络游戏初步接入

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
+12 -2
View File
@@ -8,9 +8,9 @@ import (
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"time"
"work_cation/cfg"
"work_cation/global"
"work_cation/models"
"work_cation/pkg/utils"
"work_cation/repo"
"work_cation/service"
)
@@ -79,7 +79,7 @@ func OpenChat(user models.Users) {
}
online := service.Zeroconf.GetOnline(user.ID)
if online == nil {
online = &models.Online{ID: user.ID, Ip: user.Ip, Port: cfg.T.ServerAddr}
online = &models.Online{ID: user.ID, Ip: user.Ip, Port: user.Port}
}
msg, err := service.Client.Chat(online, en.Text)
if err != nil || msg != "ok" {
@@ -101,6 +101,16 @@ func OpenChat(user models.Users) {
dialog.ShowInformation("未开发", "分享脚本功能 尽请期待", w)
}),
widget.NewToolbarAction(theme.CancelIcon(), func() { w.Close() }),
widget.NewToolbarAction(theme.MailForwardIcon(), func() {
game := NewTenGame(nil)
online := &models.Online{ID: user.ID, Ip: user.Ip, Port: user.Port}
err := game.AddNet(utils.Uuid.CreateUUID(), 0, online, &user)
if err != nil {
dialog.ShowInformation("错误", err.Error(), w)
return
}
game.StartShow()
}),
)
button := widget.NewButton("", submit)
button.SetIcon(theme.ConfirmIcon())
+2 -3
View File
@@ -7,7 +7,6 @@ import (
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"slices"
"work_cation/cfg"
"work_cation/global"
"work_cation/models"
"work_cation/repo"
@@ -61,7 +60,7 @@ func itemFollowUserView(w fyne.Window, data *models.Online, user *models.UserFol
go func() {
defer func() { _ = recover() }()
if data == nil {
data = &models.Online{ID: user.ID, Ip: user.Ip, Port: cfg.T.ServerAddr}
data = &models.Online{ID: user.ID, Ip: user.Ip, Port: user.Port}
}
newUser, err := service.Client.GetUser(data)
if err != nil {
@@ -71,7 +70,7 @@ func itemFollowUserView(w fyne.Window, data *models.Online, user *models.UserFol
if err == nil {
onlineShow.SetText("在线")
}
if err == nil && newUser.Name != user.Name {
if err == nil && newUser.UpdateTx != user.UpdateTx {
// 更新用户信息
fmt.Println("更新用户信息:", user, newUser)
repo.UserFollow.UnFollow(global.DB, user)
+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)
}
/*