网络游戏初步接入
This commit is contained in:
+5
-5
@@ -41,14 +41,14 @@ func TestP2(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
f := func(tx *gorm.DB) *gorm.DB {
|
f := func(tx *gorm.DB) *gorm.DB {
|
||||||
// Find SELECT `id`,`value` FROM `test_models` WHERE id = 1 AND `test_models`.`deleted_at` IS NULL ORDER BY UserID desc
|
// Find SELECT `id`,`value` FROM `test_models` WHERE id = 1 AND `test_models`.`deleted_at` IS NULL ORDER BY User desc
|
||||||
// First SELECT `id`,`value` FROM `test_models` WHERE id = 1 AND `test_models`.`deleted_at` IS NULL ORDER BY UserID desc,`test_models`.`id` LIMIT 1
|
// First SELECT `id`,`value` FROM `test_models` WHERE id = 1 AND `test_models`.`deleted_at` IS NULL ORDER BY User desc,`test_models`.`id` LIMIT 1
|
||||||
// row value misused 滥用
|
// row value misused 滥用
|
||||||
return tx.
|
return tx.
|
||||||
Where("id in ?", id).
|
Where("id in ?", id).
|
||||||
//Or("id = ? ", id+1).
|
//Or("id = ? ", id+1).
|
||||||
Select("value").
|
Select("value").
|
||||||
Order("UserID desc").Find(&row)
|
Order("User desc").Find(&row)
|
||||||
}
|
}
|
||||||
|
|
||||||
testSql(db, f)
|
testSql(db, f)
|
||||||
@@ -65,7 +65,7 @@ func TestP3(t *testing.T) {
|
|||||||
|
|
||||||
f := func(tx *gorm.DB) *gorm.DB {
|
f := func(tx *gorm.DB) *gorm.DB {
|
||||||
// Update `test_models` SET `value`="[1 2]",`updated_at`="2024-10-05 13:21:47.253" WHERE id in (1,2) AND `test_models`.`deleted_at` IS NULL 返回个数:2 err: <nil>
|
// Update `test_models` SET `value`="[1 2]",`updated_at`="2024-10-05 13:21:47.253" WHERE id in (1,2) AND `test_models`.`deleted_at` IS NULL 返回个数:2 err: <nil>
|
||||||
// Save SELECT `id`,`value` FROM `test_models` WHERE id = 1 AND `test_models`.`deleted_at` IS NULL ORDER BY UserID desc,`test_models`.`id` LIMIT 1
|
// Save SELECT `id`,`value` FROM `test_models` WHERE id = 1 AND `test_models`.`deleted_at` IS NULL ORDER BY User desc,`test_models`.`id` LIMIT 1
|
||||||
// row value misused 滥用
|
// row value misused 滥用
|
||||||
return tx.Model(&TestModel{}).
|
return tx.Model(&TestModel{}).
|
||||||
Where("id in ?", 5).
|
Where("id in ?", 5).
|
||||||
@@ -75,7 +75,7 @@ func TestP3(t *testing.T) {
|
|||||||
//Update("value", []byte(fmt.Sprintf("%d", id)))
|
//Update("value", []byte(fmt.Sprintf("%d", id)))
|
||||||
//Or("id = ? ", id+1).
|
//Or("id = ? ", id+1).
|
||||||
//Select("value").
|
//Select("value").
|
||||||
//Order("UserID desc").Find(&row)
|
//Order("User desc").Find(&row)
|
||||||
}
|
}
|
||||||
|
|
||||||
testSql(db, f)
|
testSql(db, f)
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type RunGameInfo struct {
|
||||||
|
Uuid string
|
||||||
|
Type TyGameType
|
||||||
|
Obj any
|
||||||
|
}
|
||||||
|
|
||||||
|
var RunGames = make(map[string]*RunGameInfo)
|
||||||
|
var lock = sync.Mutex{}
|
||||||
|
|
||||||
|
func GetGameInfo(uuid string) *RunGameInfo {
|
||||||
|
return RunGames[uuid]
|
||||||
|
}
|
||||||
|
func CreateGame(game *RunGameInfo) {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
RunGames[game.Uuid] = game
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteGame(uuid string) {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
delete(RunGames, uuid)
|
||||||
|
}
|
||||||
|
|
||||||
|
type TyGameType = int
|
||||||
|
|
||||||
|
const (
|
||||||
|
GameType1 TyGameType = iota
|
||||||
|
)
|
||||||
@@ -36,6 +36,7 @@ func main() {
|
|||||||
func mainView(w fyne.Window) fyne.CanvasObject {
|
func mainView(w fyne.Window) fyne.CanvasObject {
|
||||||
service.Zeroconf.StartFindService()
|
service.Zeroconf.StartFindService()
|
||||||
views.ListenChat()
|
views.ListenChat()
|
||||||
|
views.StartGameListen()
|
||||||
// 启动服务
|
// 启动服务
|
||||||
if err := service.Server.StartListenServer(); err != nil {
|
if err := service.Server.StartListenServer(); err != nil {
|
||||||
return widget.NewLabel(err.Error())
|
return widget.NewLabel(err.Error())
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type GameMessage struct {
|
type GameMessage struct {
|
||||||
UserID string `json:"user_id"`
|
Router string `json:"router"`
|
||||||
Pos int `json:"pos"`
|
User *Users `json:"user_id"`
|
||||||
|
Pos int `json:"pos"`
|
||||||
|
GameType int
|
||||||
|
GameUuid string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ type Online struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *Online) Url(router string) string {
|
func (o *Online) Url(router string) string {
|
||||||
|
if o.Port == "" {
|
||||||
|
o.Port = cfg.T.ServerAddr
|
||||||
|
}
|
||||||
return fmt.Sprintf("http://%s%s%s", o.Ip, o.Port, router)
|
return fmt.Sprintf("http://%s%s%s", o.Ip, o.Port, router)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-8
@@ -1,12 +1,16 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type Users struct {
|
type Users struct {
|
||||||
ID string `gorm:"primarykey" json:"id"`
|
ID string `gorm:"primarykey" json:"id"`
|
||||||
Ip string `json:"ip"`
|
Ip string `json:"ip"`
|
||||||
Name string `json:"name"` // 昵称
|
UpdateTx time.Time `json:"update_tx"`
|
||||||
Avatar string `json:"avatar"` // 头像
|
Port string `json:"port"`
|
||||||
Cover string `json:"cover"` // 封面
|
Name string `json:"name"` // 昵称
|
||||||
Email string `json:"email"`
|
Avatar string `json:"avatar"` // 头像
|
||||||
Phone string `json:"phone"`
|
Cover string `json:"cover"` // 封面
|
||||||
Address string `json:"address"` // 工位
|
Email string `json:"email"`
|
||||||
|
Phone string `json:"phone"`
|
||||||
|
Address string `json:"address"` // 工位
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-1
@@ -3,6 +3,8 @@ package repo
|
|||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"time"
|
||||||
|
"work_cation/cfg"
|
||||||
"work_cation/models"
|
"work_cation/models"
|
||||||
"work_cation/pkg/utils"
|
"work_cation/pkg/utils"
|
||||||
)
|
)
|
||||||
@@ -38,6 +40,12 @@ func (u *userRepo) GetUserInfo(db *gorm.DB) *models.Users {
|
|||||||
}
|
}
|
||||||
db.Create(&users)
|
db.Create(&users)
|
||||||
}
|
}
|
||||||
|
if users.Port != cfg.T.ServerAddr {
|
||||||
|
users.Port = cfg.T.ServerAddr
|
||||||
|
users.UpdateTx = time.Now()
|
||||||
|
db.Model(&models.Users{}).Where("id = ?", users.ID).
|
||||||
|
Updates(users)
|
||||||
|
}
|
||||||
u.isNew = true
|
u.isNew = true
|
||||||
u.users = users
|
u.users = users
|
||||||
return &users
|
return &users
|
||||||
@@ -45,5 +53,6 @@ func (u *userRepo) GetUserInfo(db *gorm.DB) *models.Users {
|
|||||||
|
|
||||||
func (u *userRepo) Update(db *gorm.DB, newUser *models.Users) error {
|
func (u *userRepo) Update(db *gorm.DB, newUser *models.Users) error {
|
||||||
u.isNew = false
|
u.isNew = false
|
||||||
return db.Model(&models.Users{}).Where("UserID = ?", newUser.ID).Updates(newUser).Error
|
return db.Model(&models.Users{}).Where("id = ?", newUser.ID).
|
||||||
|
Updates(newUser).Error
|
||||||
}
|
}
|
||||||
|
|||||||
+56
-13
@@ -115,29 +115,45 @@ func (c *ClientService) Download(online *models.Online, uuid string, progress bi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClientService) Chat(online *models.Online, text string) (string, error) {
|
func (c *ClientService) Chat(online *models.Online, text string) (string, error) {
|
||||||
var user = repo.User.GetUserInfo(global.DB)
|
req := gin.H{
|
||||||
ctx, carnal := context.WithTimeout(context.TODO(), time.Millisecond*500)
|
"text": text,
|
||||||
defer carnal()
|
}
|
||||||
b, err := json.Marshal(gin.H{"text": text, "uuid": user.ID})
|
msg, err := PostSend("/chat", online, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
res, err := http.NewRequestWithContext(ctx, "POST", online.Url("/chat"), bytes.NewBuffer(b))
|
return msg["message"].(string), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientService) StartGame(online *models.Online, gameUuid string, gameType int) (string, error) {
|
||||||
|
req := gin.H{
|
||||||
|
"type": gameType,
|
||||||
|
"game_uuid": gameUuid,
|
||||||
|
}
|
||||||
|
msg, err := PostSend("/start_game", online, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
res.Header.Set("Content-Type", "application/json")
|
return msg["message"].(string), err
|
||||||
res.Header.Set("User-UserID", user.ID)
|
}
|
||||||
resp, err := http.DefaultClient.Do(res)
|
|
||||||
|
func (c *ClientService) PlayGame(online *models.Online, gameUuid string, pos int) (string, error) {
|
||||||
|
req := gin.H{
|
||||||
|
"pos": pos,
|
||||||
|
"game_uuid": gameUuid,
|
||||||
|
}
|
||||||
|
msg, err := PostSend("/play_game", online, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
return msg["message"].(string), err
|
||||||
data, err := io.ReadAll(resp.Body)
|
}
|
||||||
var msg = make(map[string]any)
|
|
||||||
if err = json.Unmarshal(data, &msg); err != nil {
|
func (c *ClientService) CloseGame(online *models.Online, gameUuid string) (string, error) {
|
||||||
return "", err
|
req := gin.H{
|
||||||
|
"game_uuid": gameUuid,
|
||||||
}
|
}
|
||||||
|
msg, err := PostSend("/close_game", online, req)
|
||||||
return msg["message"].(string), err
|
return msg["message"].(string), err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,3 +220,30 @@ func unzipFile(zipFile, destDir string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PostSend(router string, online *models.Online, req gin.H) (gin.H, error) {
|
||||||
|
var user = repo.User.GetUserInfo(global.DB)
|
||||||
|
ctx, carnal := context.WithTimeout(context.TODO(), time.Millisecond*500)
|
||||||
|
defer carnal()
|
||||||
|
b, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res, err := http.NewRequestWithContext(ctx, "POST", online.Url(router), bytes.NewBuffer(b))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res.Header.Set("Content-Type", "application/json")
|
||||||
|
res.Header.Set("User-User", user.ID)
|
||||||
|
resp, err := http.DefaultClient.Do(res)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
var msg = make(map[string]any)
|
||||||
|
if err = json.Unmarshal(data, &msg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return msg, err
|
||||||
|
}
|
||||||
|
|||||||
+53
-26
@@ -66,17 +66,14 @@ func (s *serverService) StartListenServer() error {
|
|||||||
router.GET("/download/card/:uuid", func(c *gin.Context) {
|
router.GET("/download/card/:uuid", func(c *gin.Context) {
|
||||||
uuid := c.Param("uuid")
|
uuid := c.Param("uuid")
|
||||||
card := repo.BaseCard.Find(global.DB, uuid)
|
card := repo.BaseCard.Find(global.DB, uuid)
|
||||||
fmt.Println("card:", card)
|
|
||||||
if card.UUID == "" {
|
if card.UUID == "" {
|
||||||
c.JSON(404, nil)
|
c.JSON(404, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cardPath := filepath.Join(cfg.T.CardDir, card.UUID)
|
cardPath := filepath.Join(cfg.T.CardDir, card.UUID)
|
||||||
|
|
||||||
// 设置响应头
|
// 设置响应头
|
||||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip", uuid))
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip", uuid))
|
||||||
c.Header("Content-Type", "application/zip")
|
c.Header("Content-Type", "application/zip")
|
||||||
|
|
||||||
err := zipFolder(cardPath, c.Writer)
|
err := zipFolder(cardPath, c.Writer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(500, err)
|
c.JSON(500, err)
|
||||||
@@ -85,15 +82,8 @@ func (s *serverService) StartListenServer() error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/chat", func(c *gin.Context) {
|
router.POST("/chat", func(c *gin.Context) {
|
||||||
uuid := c.GetHeader("User-UserID")
|
user, msg, is := publicPostCheck(c)
|
||||||
user := repo.UserFollow.GetUser(global.DB, uuid)
|
if !is {
|
||||||
if user.Ip != c.ClientIP() {
|
|
||||||
c.JSON(200, gin.H{"message": "对方未关注你"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var msg = make(map[string]interface{})
|
|
||||||
if err := c.ShouldBind(&msg); err != nil {
|
|
||||||
c.JSON(200, gin.H{"message": "输入异常"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
message := &models.Message{
|
message := &models.Message{
|
||||||
@@ -105,23 +95,45 @@ func (s *serverService) StartListenServer() error {
|
|||||||
c.JSON(200, gin.H{"message": "ok"})
|
c.JSON(200, gin.H{"message": "ok"})
|
||||||
})
|
})
|
||||||
|
|
||||||
// 游戏接口
|
// 开始游戏
|
||||||
router.POST("/game", func(c *gin.Context) {
|
router.POST("/start_game", func(c *gin.Context) {
|
||||||
uuid := c.GetHeader("User-UserID")
|
user, msg, is := publicPostCheck(c)
|
||||||
user := repo.UserFollow.GetUser(global.DB, uuid)
|
if !is {
|
||||||
if user.Ip != c.ClientIP() {
|
|
||||||
c.JSON(200, gin.H{"message": "对方未关注你"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var msg = make(map[string]interface{})
|
|
||||||
if err := c.ShouldBind(&msg); err != nil {
|
|
||||||
c.JSON(200, gin.H{"message": "输入异常"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
message := &models.GameMessage{
|
message := &models.GameMessage{
|
||||||
UserID: user.ID,
|
Router: "/start_game",
|
||||||
Pos: msg["text"].(int),
|
User: &user.Users,
|
||||||
|
GameType: int(msg["type"].(float64)),
|
||||||
|
GameUuid: msg["game_uuid"].(string),
|
||||||
|
}
|
||||||
|
global.Send.Game1Chan <- message
|
||||||
|
c.JSON(200, gin.H{"message": "ok"})
|
||||||
|
})
|
||||||
|
router.POST("/play_game", func(c *gin.Context) {
|
||||||
|
user, msg, is := publicPostCheck(c)
|
||||||
|
if !is {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
message := &models.GameMessage{
|
||||||
|
Router: "/play_game",
|
||||||
|
User: &user.Users,
|
||||||
|
Pos: int(msg["pos"].(float64)),
|
||||||
|
GameUuid: msg["game_uuid"].(string),
|
||||||
|
}
|
||||||
|
global.Send.Game1Chan <- message
|
||||||
|
c.JSON(200, gin.H{"message": "ok"})
|
||||||
|
})
|
||||||
|
|
||||||
|
router.POST("/close_game", func(c *gin.Context) {
|
||||||
|
user, msg, is := publicPostCheck(c)
|
||||||
|
if !is {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
message := &models.GameMessage{
|
||||||
|
Router: "/close_game",
|
||||||
|
User: &user.Users,
|
||||||
|
GameUuid: msg["game_uuid"].(string),
|
||||||
}
|
}
|
||||||
global.Send.Game1Chan <- message
|
global.Send.Game1Chan <- message
|
||||||
c.JSON(200, gin.H{"message": "ok"})
|
c.JSON(200, gin.H{"message": "ok"})
|
||||||
@@ -190,3 +202,18 @@ func zipFolder(folderPath string, zipFile io.Writer) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func publicPostCheck(c *gin.Context) (*models.UserFollows, map[string]interface{}, bool) {
|
||||||
|
uuid := c.GetHeader("User-User")
|
||||||
|
user := repo.UserFollow.GetUser(global.DB, uuid)
|
||||||
|
if user.Ip != c.ClientIP() {
|
||||||
|
c.JSON(200, gin.H{"message": "对方未关注你"})
|
||||||
|
return nil, nil, false
|
||||||
|
}
|
||||||
|
var msg = make(map[string]interface{})
|
||||||
|
if err := c.ShouldBind(&msg); err != nil {
|
||||||
|
c.JSON(200, gin.H{"message": "输入异常"})
|
||||||
|
return nil, nil, false
|
||||||
|
}
|
||||||
|
return user, msg, true
|
||||||
|
}
|
||||||
|
|||||||
+12
-2
@@ -8,9 +8,9 @@ import (
|
|||||||
"fyne.io/fyne/v2/theme"
|
"fyne.io/fyne/v2/theme"
|
||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/widget"
|
||||||
"time"
|
"time"
|
||||||
"work_cation/cfg"
|
|
||||||
"work_cation/global"
|
"work_cation/global"
|
||||||
"work_cation/models"
|
"work_cation/models"
|
||||||
|
"work_cation/pkg/utils"
|
||||||
"work_cation/repo"
|
"work_cation/repo"
|
||||||
"work_cation/service"
|
"work_cation/service"
|
||||||
)
|
)
|
||||||
@@ -79,7 +79,7 @@ func OpenChat(user models.Users) {
|
|||||||
}
|
}
|
||||||
online := service.Zeroconf.GetOnline(user.ID)
|
online := service.Zeroconf.GetOnline(user.ID)
|
||||||
if online == nil {
|
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)
|
msg, err := service.Client.Chat(online, en.Text)
|
||||||
if err != nil || msg != "ok" {
|
if err != nil || msg != "ok" {
|
||||||
@@ -101,6 +101,16 @@ func OpenChat(user models.Users) {
|
|||||||
dialog.ShowInformation("未开发", "分享脚本功能 尽请期待", w)
|
dialog.ShowInformation("未开发", "分享脚本功能 尽请期待", w)
|
||||||
}),
|
}),
|
||||||
widget.NewToolbarAction(theme.CancelIcon(), func() { w.Close() }),
|
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 := widget.NewButton("", submit)
|
||||||
button.SetIcon(theme.ConfirmIcon())
|
button.SetIcon(theme.ConfirmIcon())
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"fyne.io/fyne/v2/dialog"
|
"fyne.io/fyne/v2/dialog"
|
||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/widget"
|
||||||
"slices"
|
"slices"
|
||||||
"work_cation/cfg"
|
|
||||||
"work_cation/global"
|
"work_cation/global"
|
||||||
"work_cation/models"
|
"work_cation/models"
|
||||||
"work_cation/repo"
|
"work_cation/repo"
|
||||||
@@ -61,7 +60,7 @@ func itemFollowUserView(w fyne.Window, data *models.Online, user *models.UserFol
|
|||||||
go func() {
|
go func() {
|
||||||
defer func() { _ = recover() }()
|
defer func() { _ = recover() }()
|
||||||
if data == nil {
|
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)
|
newUser, err := service.Client.GetUser(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -71,7 +70,7 @@ func itemFollowUserView(w fyne.Window, data *models.Online, user *models.UserFol
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
onlineShow.SetText("在线")
|
onlineShow.SetText("在线")
|
||||||
}
|
}
|
||||||
if err == nil && newUser.Name != user.Name {
|
if err == nil && newUser.UpdateTx != user.UpdateTx {
|
||||||
// 更新用户信息
|
// 更新用户信息
|
||||||
fmt.Println("更新用户信息:", user, newUser)
|
fmt.Println("更新用户信息:", user, newUser)
|
||||||
repo.UserFollow.UnFollow(global.DB, user)
|
repo.UserFollow.UnFollow(global.DB, user)
|
||||||
|
|||||||
+114
-7
@@ -18,6 +18,9 @@ import (
|
|||||||
"image/png"
|
"image/png"
|
||||||
"slices"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
|
"work_cation/global"
|
||||||
|
"work_cation/models"
|
||||||
|
"work_cation/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TenChinaGameView() {
|
func TenChinaGameView() {
|
||||||
@@ -35,6 +38,39 @@ func TenChinaGameView() {
|
|||||||
game.StartShow()
|
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
|
// 格子棋 Checkered-Chess
|
||||||
type TenGame struct {
|
type TenGame struct {
|
||||||
lock sync.Mutex // 玩家操作锁
|
lock sync.Mutex // 玩家操作锁
|
||||||
@@ -60,6 +96,13 @@ type TenGame struct {
|
|||||||
|
|
||||||
Ctx context.Context
|
Ctx context.Context
|
||||||
Cancel func()
|
Cancel func()
|
||||||
|
|
||||||
|
isNet bool // 是否是网络模式
|
||||||
|
uuid string
|
||||||
|
myIndex int
|
||||||
|
user *models.Users //
|
||||||
|
userIndex int
|
||||||
|
online *models.Online
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTenGame(winCallback func(int)) *TenGame {
|
func NewTenGame(winCallback func(int)) *TenGame {
|
||||||
@@ -83,22 +126,56 @@ func NewTenGame(winCallback func(int)) *TenGame {
|
|||||||
winSum: 5,
|
winSum: 5,
|
||||||
Ctx: ctx,
|
Ctx: ctx,
|
||||||
Cancel: cancel,
|
Cancel: cancel,
|
||||||
|
isNet: false,
|
||||||
}
|
}
|
||||||
return game
|
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() {
|
func (t *TenGame) StartShow() {
|
||||||
myApp := fyne.CurrentApp()
|
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))
|
wSize := fyne.NewSize(t.itemX*(t.itemSize.Width), t.itemY*(t.itemSize.Height))
|
||||||
myWindow.Resize(wSize)
|
myWindow.Resize(wSize)
|
||||||
myWindow.SetContent(container.New(layout.NewMaxLayout(),
|
myWindow.SetContent(container.New(layout.NewMaxLayout(),
|
||||||
canvas.NewImageFromResource(drawABackgroundImage(wSize, int(t.itemX), int(t.itemY))),
|
canvas.NewImageFromResource(drawABackgroundImage(wSize, int(t.itemX), int(t.itemY))),
|
||||||
t.CanvasObject()))
|
t.CanvasObject()))
|
||||||
|
|
||||||
|
myWindow.SetOnClosed(func() {
|
||||||
|
if t.isNet {
|
||||||
|
global.DeleteGame(t.uuid)
|
||||||
|
service.Client.CloseGame(t.online, t.uuid)
|
||||||
|
}
|
||||||
|
})
|
||||||
myWindow.CenterOnScreen()
|
myWindow.CenterOnScreen()
|
||||||
|
myWindow.RequestFocus()
|
||||||
myWindow.Show()
|
myWindow.Show()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +192,10 @@ func (t *TenGame) CanvasObject() fyne.CanvasObject {
|
|||||||
t.Items = append(t.Items, image)
|
t.Items = append(t.Items, image)
|
||||||
|
|
||||||
toggle := widget.NewButton("", func() {
|
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)
|
toggle.Resize(t.itemSize)
|
||||||
|
|
||||||
@@ -132,21 +212,48 @@ func (t *TenGame) CanvasObject() fyne.CanvasObject {
|
|||||||
func (t *TenGame) Play(userIndex int, pos int) error {
|
func (t *TenGame) Play(userIndex int, pos int) error {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
//if t.currentRoundPlayer != userIndex {
|
if t.isNet && t.currentRoundPlayer != userIndex {
|
||||||
// return errors.New("你急个der")
|
return errors.New("你急个der")
|
||||||
//}
|
}
|
||||||
if slices.Contains(t.allMaps, pos) {
|
if slices.Contains(t.allMaps, pos) {
|
||||||
return errors.New("已经下过了")
|
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.allMaps = append(t.allMaps, pos)
|
||||||
t.play(pos)
|
t.play(pos)
|
||||||
// 推进
|
// 推进
|
||||||
|
t.advance()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 推进
|
||||||
|
func (t *TenGame) advance() {
|
||||||
t.currentRoundPlayer++
|
t.currentRoundPlayer++
|
||||||
if t.currentRoundPlayer+1 > len(t.playerID2Image) {
|
if t.currentRoundPlayer+1 > len(t.playerID2Image) {
|
||||||
t.currentRoundPlayer = 0
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user