"refactor(vscode): 移除项目中的VSCode特定GOROOT配置"

This commit is contained in:
1
2025-07-25 06:22:16 +00:00
parent 7a0d6d9556
commit 43dc9394c5
7 changed files with 68 additions and 67 deletions

View File

@@ -1,5 +0,0 @@
{
"go.toolsEnvVars": {
"GOROOT": "E:\\golang\\go"
}
}

View File

@@ -1,8 +1,10 @@
package cool
import (
"blazing/common/data/entity"
"blazing/common/utils"
"context"
"sync"
"reflect"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/util/gconv"
@@ -10,8 +12,8 @@ import (
)
var (
Mainplayer sync.Map //玩家数据
CmdCache sync.Map //命令缓存
Mainplayer = &utils.SyncMap[uint32, *entity.Player]{} //玩家数据
CmdCache = &utils.SyncMap[uint32, reflect.Value]{} //命令缓存
)
func init() {
@@ -29,3 +31,13 @@ func init() {
glog.Debug(context.Background(), "初始化雪花算法", newId)
}
func ConutPlayer() int {
count := 0
Mainplayer.Range(func(uint32, *entity.Player) bool {
count++
return true // 继续遍历
})
return count
//fmt.Println("元素数量:", count) // 输出: 3
}

View File

@@ -1,7 +1,7 @@
package entity
import (
"blazing/cool"
"context"
"fmt"
"sync"
@@ -54,16 +54,7 @@ func (p *Player) SendPack(b []byte) error {
return err
}
func ConutPlayer() int {
count := 0
cool.Mainplayer.Range(func(key, value interface{}) bool {
count++
return true // 继续遍历
})
return count
//fmt.Println("元素数量:", count) // 输出: 3
}
// IsLoggedIn 检查是否已登录
func (lw *Player) IsLoggedIn() bool {

View File

@@ -1,64 +1,42 @@
package utils
import (
"fmt"
"sync"
"sync"
)
// 定义一个泛型的 SyncMap支持任意类型的键和值
type SyncMap[K comparable, V any] struct {
m sync.Map
m sync.Map
}
// 设置键值对
func (sm *SyncMap[K, V]) Store(key K, value V) {
sm.m.Store(key, value)
sm.m.Store(key, value)
}
func (sm *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool) {
val, ok := sm.m.LoadOrStore(key, value)
return val.(V), ok
}
// 获取键对应的值
func (sm *SyncMap[K, V]) Load(key K) (V, bool) {
val, ok := sm.m.Load(key)
if ok {
return val.(V), true
}
var zeroValue V
return zeroValue, false
val, ok := sm.m.Load(key)
if ok {
return val.(V), true
}
var zeroValue V
return zeroValue, false
}
// 删除键值对
func (sm *SyncMap[K, V]) Delete(key K) {
sm.m.Delete(key)
sm.m.Delete(key)
}
// 遍历所有键值对
func (sm *SyncMap[K, V]) Range(f func(key K, value V) bool) {
sm.m.Range(func(key, value any) bool {
return f(key.(K), value.(V))
})
sm.m.Range(func(key, value any) bool {
return f(key.(K), value.(V))
})
}
func main() {
// 使用 SyncMap 存储字符串 -> int 类型的键值对
sm1 := &SyncMap[string, int]{}
sm1.Store("apple", 5)
sm1.Store("banana", 10)
// 加载并打印值
if value, ok := sm1.Load("apple"); ok {
fmt.Println("apple:", value)
}
if value, ok := sm1.Load("banana"); ok {
fmt.Println("banana:", value)
}
// 使用 SyncMap 存储 int -> string 类型的键值对
sm2 := &SyncMap[int, string]{}
sm2.Store(1, "one")
sm2.Store(2, "two")
// 遍历所有键值对
sm2.Range(func(key int, value string) bool {
fmt.Printf("%d: %s\n", key, value)
return true
})
}

View File

@@ -40,6 +40,31 @@ func TestThree(t *testing.T) {
}
fmt.Println(b.Bytes())
}
func TestMap(t *testing.T) {
// 使用 SyncMap 存储字符串 -> int 类型的键值对
sm1 := &SyncMap[string, int]{}
sm1.Store("apple", 5)
sm1.Store("banana", 10)
// 加载并打印值
if value, ok := sm1.Load("apple"); ok {
fmt.Println("apple:", value)
}
if value, ok := sm1.Load("banana"); ok {
fmt.Println("banana:", value)
}
// 使用 SyncMap 存储 int -> string 类型的键值对
sm2 := &SyncMap[int, string]{}
sm2.Store(1, "one")
sm2.Store(2, "two")
// 遍历所有键值对
sm2.Range(func(key int, value string) bool {
fmt.Printf("%d: %s\n", key, value)
return true
})
}
func TestInit(t *testing.T) {
table := termtables.CreateTable()

View File

@@ -46,9 +46,9 @@ func (h *Controller) QuitSelf(a int) error {
for {
//entity.ConutPlayer()
fmt.Println("当前在线人数", entity.ConutPlayer())
fmt.Println("当前在线人数", cool.ConutPlayer())
if entity.ConutPlayer() <= 0 {
if cool.ConutPlayer() <= 0 {
//执行退出逻辑
os.Exit(1)
}
@@ -165,13 +165,13 @@ func getcmd(t reflect.Type) uint32 {
// 遍历结构体方法并执行RECV_cmd
func Recv(c gnet.Conn, data handler.TomeeHeader) {
tt, ok := cool.CmdCache.Load(data.CMD)
cmdlister, ok := cool.CmdCache.Load(data.CMD)
if !ok {
glog.Error(context.Background(), data.CMD, "cmd未注册")
return //TODO 待实现cmd未注册
}
cmdlister := tt.(reflect.Value)
// fmt.Println(cmdlister)
params := []reflect.Value{}

View File

@@ -19,8 +19,8 @@ func GetPlayer(c gnet.Conn, userid uint32) *entity.Player { //TODO 这里待优
}
var player *entity.Player
if player1, ok := cool.Mainplayer.Load((userid)); ok {
player = player1.(*entity.Player) //取成功,否则创建
clientdata.SetPlayer(player)
clientdata.SetPlayer(player1)
}
return player
@@ -30,14 +30,14 @@ func KickPlayer(userid uint32) { //踢出玩家
//TODO 返回错误码
//var player *entity.Player
if player1, ok := cool.Mainplayer.Load((userid)); ok {
player := player1.(*entity.Player) //取成功,否则创建
//取成功,否则创建
head := handler.NewTomeeHeader()
head.Result = uint32(errorcode.ErrorCodes.ErrAlreadyLoggedIn)
head.UserID = userid
head.CMD = 1001
player.SendPack(head.Pack(nil))
player.MainConn.MainConn.Close()
player1.SendPack(head.Pack(nil))
player1.MainConn.MainConn.Close()
// clientdata.Player = player
}