"refactor(vscode): 移除项目中的VSCode特定GOROOT配置"
This commit is contained in:
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"go.toolsEnvVars": {
|
|
||||||
"GOROOT": "E:\\golang\\go"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package cool
|
package cool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"blazing/common/data/entity"
|
||||||
|
"blazing/common/utils"
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
"reflect"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/os/glog"
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
@@ -10,8 +12,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Mainplayer sync.Map //玩家数据
|
Mainplayer = &utils.SyncMap[uint32, *entity.Player]{} //玩家数据
|
||||||
CmdCache sync.Map //命令缓存
|
CmdCache = &utils.SyncMap[uint32, reflect.Value]{} //命令缓存
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -29,3 +31,13 @@ func init() {
|
|||||||
glog.Debug(context.Background(), "初始化雪花算法", newId)
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package entity
|
package entity
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"blazing/cool"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -54,16 +54,7 @@ func (p *Player) SendPack(b []byte) error {
|
|||||||
return err
|
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 检查是否已登录
|
// IsLoggedIn 检查是否已登录
|
||||||
func (lw *Player) IsLoggedIn() bool {
|
func (lw *Player) IsLoggedIn() bool {
|
||||||
|
|||||||
@@ -1,64 +1,42 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"sync"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 定义一个泛型的 SyncMap,支持任意类型的键和值
|
// 定义一个泛型的 SyncMap,支持任意类型的键和值
|
||||||
type SyncMap[K comparable, V any] struct {
|
type SyncMap[K comparable, V any] struct {
|
||||||
m sync.Map
|
m sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置键值对
|
// 设置键值对
|
||||||
func (sm *SyncMap[K, V]) Store(key K, value V) {
|
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) {
|
func (sm *SyncMap[K, V]) Load(key K) (V, bool) {
|
||||||
val, ok := sm.m.Load(key)
|
val, ok := sm.m.Load(key)
|
||||||
if ok {
|
if ok {
|
||||||
return val.(V), true
|
return val.(V), true
|
||||||
}
|
}
|
||||||
var zeroValue V
|
var zeroValue V
|
||||||
return zeroValue, false
|
return zeroValue, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除键值对
|
// 删除键值对
|
||||||
func (sm *SyncMap[K, V]) Delete(key K) {
|
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) {
|
func (sm *SyncMap[K, V]) Range(f func(key K, value V) bool) {
|
||||||
sm.m.Range(func(key, value any) bool {
|
sm.m.Range(func(key, value any) bool {
|
||||||
return f(key.(K), value.(V))
|
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
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
@@ -40,6 +40,31 @@ func TestThree(t *testing.T) {
|
|||||||
}
|
}
|
||||||
fmt.Println(b.Bytes())
|
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) {
|
func TestInit(t *testing.T) {
|
||||||
table := termtables.CreateTable()
|
table := termtables.CreateTable()
|
||||||
|
|||||||
@@ -46,9 +46,9 @@ func (h *Controller) QuitSelf(a int) error {
|
|||||||
for {
|
for {
|
||||||
|
|
||||||
//entity.ConutPlayer()
|
//entity.ConutPlayer()
|
||||||
fmt.Println("当前在线人数", entity.ConutPlayer())
|
fmt.Println("当前在线人数", cool.ConutPlayer())
|
||||||
|
|
||||||
if entity.ConutPlayer() <= 0 {
|
if cool.ConutPlayer() <= 0 {
|
||||||
//执行退出逻辑
|
//执行退出逻辑
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -165,13 +165,13 @@ func getcmd(t reflect.Type) uint32 {
|
|||||||
// 遍历结构体方法并执行RECV_cmd
|
// 遍历结构体方法并执行RECV_cmd
|
||||||
func Recv(c gnet.Conn, data handler.TomeeHeader) {
|
func Recv(c gnet.Conn, data handler.TomeeHeader) {
|
||||||
|
|
||||||
tt, ok := cool.CmdCache.Load(data.CMD)
|
cmdlister, ok := cool.CmdCache.Load(data.CMD)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
||||||
glog.Error(context.Background(), data.CMD, "cmd未注册")
|
glog.Error(context.Background(), data.CMD, "cmd未注册")
|
||||||
return //TODO 待实现cmd未注册
|
return //TODO 待实现cmd未注册
|
||||||
}
|
}
|
||||||
cmdlister := tt.(reflect.Value)
|
|
||||||
// fmt.Println(cmdlister)
|
// fmt.Println(cmdlister)
|
||||||
|
|
||||||
params := []reflect.Value{}
|
params := []reflect.Value{}
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ func GetPlayer(c gnet.Conn, userid uint32) *entity.Player { //TODO 这里待优
|
|||||||
}
|
}
|
||||||
var player *entity.Player
|
var player *entity.Player
|
||||||
if player1, ok := cool.Mainplayer.Load((userid)); ok {
|
if player1, ok := cool.Mainplayer.Load((userid)); ok {
|
||||||
player = player1.(*entity.Player) //取成功,否则创建
|
|
||||||
clientdata.SetPlayer(player)
|
clientdata.SetPlayer(player1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return player
|
return player
|
||||||
@@ -30,14 +30,14 @@ func KickPlayer(userid uint32) { //踢出玩家
|
|||||||
//TODO 返回错误码
|
//TODO 返回错误码
|
||||||
//var player *entity.Player
|
//var player *entity.Player
|
||||||
if player1, ok := cool.Mainplayer.Load((userid)); ok {
|
if player1, ok := cool.Mainplayer.Load((userid)); ok {
|
||||||
player := player1.(*entity.Player) //取成功,否则创建
|
//取成功,否则创建
|
||||||
head := handler.NewTomeeHeader()
|
head := handler.NewTomeeHeader()
|
||||||
head.Result = uint32(errorcode.ErrorCodes.ErrAlreadyLoggedIn)
|
head.Result = uint32(errorcode.ErrorCodes.ErrAlreadyLoggedIn)
|
||||||
head.UserID = userid
|
head.UserID = userid
|
||||||
head.CMD = 1001
|
head.CMD = 1001
|
||||||
|
|
||||||
player.SendPack(head.Pack(nil))
|
player1.SendPack(head.Pack(nil))
|
||||||
player.MainConn.MainConn.Close()
|
player1.MainConn.MainConn.Close()
|
||||||
// clientdata.Player = player
|
// clientdata.Player = player
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user