完善聊天功能 初步构建十子棋功能
This commit is contained in:
+61
-36
@@ -5,6 +5,7 @@ import (
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"time"
|
||||
"work_cation/cfg"
|
||||
@@ -23,6 +24,7 @@ type ChatUserInfo struct {
|
||||
messages []models.ChatMessage
|
||||
shows *fyne.Container
|
||||
scroll *container.Scroll
|
||||
w fyne.Window
|
||||
}
|
||||
|
||||
var chat = &ChatView{usersChat: make(map[string]*ChatUserInfo)}
|
||||
@@ -35,13 +37,16 @@ func ListenChat() {
|
||||
if userInfo == nil {
|
||||
userInfo = &ChatUserInfo{}
|
||||
chat.usersChat[message.User.ID] = userInfo
|
||||
userInfo.messages = repo.ChatMsg.GetUserMsgs(global.DB, message.User.ID, -1)
|
||||
}
|
||||
userInfo.messages = append(userInfo.messages, message.ChatMessage)
|
||||
_ = repo.ChatMsg.Create(global.DB, &message.ChatMessage)
|
||||
userInfo.Users = message.User.Users
|
||||
if userInfo.shows == nil {
|
||||
OpenChat(message.User.Users)
|
||||
} else {
|
||||
userInfo.shows.Add(itemMessage(message.ChatMessage))
|
||||
userInfo.w.RequestFocus()
|
||||
userInfo.scroll.ScrollToBottom()
|
||||
}
|
||||
}
|
||||
@@ -53,11 +58,12 @@ func OpenChat(user models.Users) {
|
||||
if userInfo == nil {
|
||||
userInfo = &ChatUserInfo{}
|
||||
chat.usersChat[user.ID] = userInfo
|
||||
userInfo.messages = repo.ChatMsg.GetUserMsgs(global.DB, user.ID, -1)
|
||||
}
|
||||
if userInfo.shows == nil {
|
||||
w := fyne.CurrentApp().NewWindow(user.Name)
|
||||
w := fyne.CurrentApp().NewWindow(fmt.Sprintf("%s %s", user.Name, user.Ip))
|
||||
w.CenterOnScreen()
|
||||
w.Resize(fyne.NewSize(500, 300))
|
||||
w.Resize(fyne.NewSize(500, 420))
|
||||
list := container.NewVBox()
|
||||
for _, item := range userInfo.messages {
|
||||
list.Add(itemMessage(item))
|
||||
@@ -67,59 +73,78 @@ func OpenChat(user models.Users) {
|
||||
scroll.ScrollToBottom()
|
||||
// 发送表单
|
||||
en := widget.NewEntry()
|
||||
formBase := &widget.Form{
|
||||
SubmitText: "发送",
|
||||
OnSubmit: func() {
|
||||
if en.Text == "" {
|
||||
return
|
||||
}
|
||||
online := service.Zeroconf.GetOnline(user.ID)
|
||||
if online == nil {
|
||||
online = &models.Online{ID: user.ID, Ip: user.Ip, Port: cfg.T.ServerAddr}
|
||||
}
|
||||
msg, err := service.Client.Chat(online, en.Text)
|
||||
if err != nil || msg != "ok" {
|
||||
dialog.ShowInformation("发送失败", err.Error(), w)
|
||||
return
|
||||
}
|
||||
chatItem := models.ChatMessage{
|
||||
User: *repo.User.GetUserInfo(global.DB),
|
||||
Time: time.Now(),
|
||||
Text: en.Text,
|
||||
}
|
||||
userInfo.messages = append(userInfo.messages, chatItem)
|
||||
list.Add(itemMessage(chatItem))
|
||||
en.SetText("")
|
||||
scroll.ScrollToBottom()
|
||||
}}
|
||||
submit := func() {
|
||||
if en.Text == "" {
|
||||
return
|
||||
}
|
||||
online := service.Zeroconf.GetOnline(user.ID)
|
||||
if online == nil {
|
||||
online = &models.Online{ID: user.ID, Ip: user.Ip, Port: cfg.T.ServerAddr}
|
||||
}
|
||||
msg, err := service.Client.Chat(online, en.Text)
|
||||
if err != nil || msg != "ok" {
|
||||
dialog.ShowInformation("发送失败", err.Error(), w)
|
||||
return
|
||||
}
|
||||
my := repo.User.GetUserInfo(global.DB)
|
||||
chatItem := models.NewTextChatMsg(my, en.Text)
|
||||
userInfo.messages = append(userInfo.messages, *chatItem)
|
||||
_ = repo.ChatMsg.Create(global.DB, chatItem)
|
||||
list.Add(itemMessage(*chatItem))
|
||||
en.SetText("")
|
||||
scroll.ScrollToBottom()
|
||||
}
|
||||
|
||||
// 分享
|
||||
toolBar := widget.NewToolbar(
|
||||
widget.NewToolbarAction(theme.ContentAddIcon(), func() {
|
||||
dialog.ShowInformation("未开发", "分享脚本功能 尽请期待", w)
|
||||
}),
|
||||
widget.NewToolbarAction(theme.CancelIcon(), func() { w.Close() }),
|
||||
)
|
||||
button := widget.NewButton("", submit)
|
||||
button.SetIcon(theme.ConfirmIcon())
|
||||
en.OnSubmitted = func(_ string) { submit() }
|
||||
w.SetContent(container.NewBorder(nil,
|
||||
container.NewBorder(nil, toolBar, nil, button, en),
|
||||
nil, nil, scroll))
|
||||
|
||||
formBase.AppendItem(&widget.FormItem{Text: "", Widget: en})
|
||||
w.SetContent(container.NewBorder(nil, formBase, nil, nil, scroll))
|
||||
userInfo.shows = list
|
||||
userInfo.w = w
|
||||
|
||||
w.SetOnClosed(func() {
|
||||
userInfo.shows = nil
|
||||
userInfo.scroll = nil
|
||||
userInfo.w = nil
|
||||
})
|
||||
w.Show()
|
||||
}
|
||||
userInfo.w.RequestFocus()
|
||||
userInfo.scroll.ScrollToBottom()
|
||||
}
|
||||
|
||||
func itemMessage(msg models.ChatMessage) fyne.CanvasObject {
|
||||
my := repo.User.GetUserInfo(global.DB)
|
||||
var card *widget.Card
|
||||
if msg.User.ID != my.ID {
|
||||
card = widget.NewCard("",
|
||||
fmt.Sprintf("%s %s", msg.Time.Format(time.DateTime),
|
||||
msg.User.Name), widget.NewLabel(msg.Text))
|
||||
if msg.UserID != my.ID {
|
||||
title := fmt.Sprintf("%s %s", msg.CreateTx.Format(time.DateTime), msg.UserName)
|
||||
card = widget.NewCard("", "",
|
||||
container.NewVBox(
|
||||
widget.NewLabel(title),
|
||||
newLabel(msg.Text, fyne.TextAlignLeading)))
|
||||
} else {
|
||||
|
||||
title := fmt.Sprintf("%s %s", msg.Time.Format(time.DateTime), "我")
|
||||
title := fmt.Sprintf("%s %s", msg.CreateTx.Format(time.DateTime), "我")
|
||||
card = widget.NewCard("", "",
|
||||
container.NewVBox(
|
||||
widget.NewLabelWithStyle(title, fyne.TextAlignTrailing, fyne.TextStyle{}),
|
||||
widget.NewLabelWithStyle(msg.Text, fyne.TextAlignTrailing, fyne.TextStyle{})))
|
||||
newLabel(msg.Text, fyne.TextAlignTrailing)))
|
||||
|
||||
}
|
||||
return card
|
||||
}
|
||||
|
||||
func newLabel(title string, ali fyne.TextAlign) *widget.Label {
|
||||
text := widget.NewLabelWithStyle(title, ali, fyne.TextStyle{})
|
||||
text.Wrapping = fyne.TextWrapBreak
|
||||
return text
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
@@ -72,6 +73,7 @@ func itemFollowUserView(w fyne.Window, data *models.Online, user *models.UserFol
|
||||
}
|
||||
if err == nil && newUser.Name != user.Name {
|
||||
// 更新用户信息
|
||||
fmt.Println("更新用户信息:", user, newUser)
|
||||
repo.UserFollow.UnFollow(global.DB, user)
|
||||
repo.UserFollow.Follow(global.DB, &models.UserFollows{Users: *newUser})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"slices"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func TenChinaGameView() {
|
||||
myApp := fyne.CurrentApp()
|
||||
myWindow := myApp.NewWindow("Cross Flag Game")
|
||||
|
||||
gridWrap := container.NewGridWrap(fyne.NewSize(100, 100))
|
||||
|
||||
for i := 0; i < 9; i++ {
|
||||
//var itemIndex = i
|
||||
var item = widget.NewButton("", func() {})
|
||||
if i > 6 {
|
||||
item.SetIcon(theme.ConfirmIcon())
|
||||
} else {
|
||||
item.SetIcon(theme.CancelIcon())
|
||||
}
|
||||
gridWrap.Add(container.NewBorder(nil, nil, nil, nil, item))
|
||||
}
|
||||
myWindow.SetContent(container.NewScroll(gridWrap))
|
||||
|
||||
myWindow.Resize(fyne.NewSize(320, 318))
|
||||
myWindow.CenterOnScreen()
|
||||
myWindow.Show()
|
||||
}
|
||||
|
||||
type TenGame struct {
|
||||
lock sync.Mutex
|
||||
playerMax int
|
||||
CurrentRoundPlayer int
|
||||
players [][]int
|
||||
items []*widget.Button
|
||||
winCallback func(int)
|
||||
}
|
||||
|
||||
func (t *TenGame) Play(userIndex int, pos int) error {
|
||||
t.lock.Lock()
|
||||
defer t.lock.Unlock()
|
||||
if t.CurrentRoundPlayer != userIndex {
|
||||
return errors.New("not your turn")
|
||||
}
|
||||
t.play(pos)
|
||||
|
||||
t.CurrentRoundPlayer++
|
||||
if t.CurrentRoundPlayer+1 > t.playerMax {
|
||||
t.CurrentRoundPlayer = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
1, 2, 3
|
||||
4, 5, 6
|
||||
7, 8, 9
|
||||
*/
|
||||
func (t *TenGame) play(pos int) {
|
||||
// TODO
|
||||
playerIntList := t.players[t.CurrentRoundPlayer]
|
||||
playerIntList = append(playerIntList, pos)
|
||||
// 刷新数据
|
||||
if t.CurrentRoundPlayer == 1 {
|
||||
t.items[pos].SetIcon(theme.ConfirmIcon())
|
||||
} else {
|
||||
t.items[pos].SetIcon(theme.CancelIcon())
|
||||
}
|
||||
|
||||
// check 胜利 判断
|
||||
slices.Sort(playerIntList)
|
||||
|
||||
}
|
||||
|
||||
func (t *TenGame) winCheck(playerIntList []int) bool {
|
||||
for _, i := range playerIntList {
|
||||
var iI = i
|
||||
if t.checkLine(playerIntList, iI, 1) ||
|
||||
t.checkLine(playerIntList, iI, 2) ||
|
||||
t.checkLine(playerIntList, iI, 4) {
|
||||
return true
|
||||
}
|
||||
// todo slices.Delete()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *TenGame) checkLine(playerIntList []int, iI, add int) bool {
|
||||
for {
|
||||
iI += add
|
||||
if iI > 9 {
|
||||
return true
|
||||
}
|
||||
if !slices.Contains(playerIntList, iI) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+2
-26
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/data/binding"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
@@ -114,35 +113,12 @@ func baseOtherCardView(w fyne.Window, baseCardCopy models.BaseCard, online *mode
|
||||
baseCardV := widget.NewCard(baseCardCopy.Title, baseCardCopy.Text, container.NewBorder(nil, widget.NewToolbar(
|
||||
widget.NewToolbarAction(theme.DownloadIcon(), func() {
|
||||
b := saveCard.UpdateTx.Unix() == baseCardCopy.UpdateTx.Unix()
|
||||
fmt.Println(saveCard.UpdateTx, baseCardCopy.UpdateTx)
|
||||
if b {
|
||||
dialog.ShowInformation("结果", "已下载本地", w)
|
||||
return
|
||||
}
|
||||
// 下载到本地 若没关注自动关注
|
||||
|
||||
progress := binding.NewFloat()
|
||||
progress.Set(0)
|
||||
progressBar := widget.NewProgressBarWithData(progress)
|
||||
progressBar.Max = 100
|
||||
smaillWin := dialog.NewCustom("下载中", "关闭", progressBar, w)
|
||||
smaillWin.Show()
|
||||
err := service.Client.Download(online, baseCardCopy.UUID, progress)
|
||||
if err != nil {
|
||||
smaillWin.SetDismissText("失败")
|
||||
return
|
||||
}
|
||||
// 修改数据
|
||||
progress.Set(99)
|
||||
saveCard.UpdateTx = baseCardCopy.UpdateTx
|
||||
err = repo.BaseCard.CreateOrSave(global.DB, &baseCardCopy)
|
||||
if err != nil {
|
||||
smaillWin.SetDismissText("失败")
|
||||
return
|
||||
}
|
||||
progress.Set(100)
|
||||
smaillWin.SetDismissText("完成 请去[我的]查看")
|
||||
|
||||
// 下载到本地 TODO 若没关注自动关注
|
||||
service.BaseCard.DownloadCard(w, baseCardCopy, online)
|
||||
}),
|
||||
), nil, nil, infoWid))
|
||||
return baseCardV
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
)
|
||||
|
||||
func mainUserViews(w fyne.Window) fyne.CanvasObject {
|
||||
//TenChinaGameView()
|
||||
var userCard = widget.NewCard("", "", nil)
|
||||
user1 := repo.User.GetUserInfo(global.DB)
|
||||
refresh := func(user *models.Users) {
|
||||
|
||||
Reference in New Issue
Block a user