Some changes to users

This commit is contained in:
qowevisa 2024-06-07 12:19:37 +03:00
parent 5996975b4f
commit 666bfbea4b
2 changed files with 14 additions and 14 deletions

View File

@ -96,8 +96,8 @@ func handleClient(conn net.Conn) {
} }
conn.Write(answ) conn.Write(answ)
} else { } else {
idBytes := make([]byte, 4) idBytes := make([]byte, 2)
binary.BigEndian.PutUint32(idBytes, uint32(id)) binary.BigEndian.PutUint16(idBytes, id)
answ, err := com.ServerSendClientHisID(idBytes) answ, err := com.ServerSendClientHisID(idBytes)
if err != nil { if err != nil {
log.Printf("ERROR: %v\n", err) log.Printf("ERROR: %v\n", err)

View File

@ -11,14 +11,14 @@ var (
) )
type UserCenter struct { type UserCenter struct {
UsersSTOI map[string]int32 UsersSTOI map[string]uint16
UsersITOS map[int32]string UsersITOS map[uint16]string
Mu sync.Mutex Mu sync.Mutex
} }
func (u *UserCenter) Init() { func (u *UserCenter) Init() {
u.UsersSTOI = make(map[string]int32) u.UsersSTOI = make(map[string]uint16)
u.UsersITOS = make(map[int32]string) u.UsersITOS = make(map[uint16]string)
} }
var ( var (
@ -26,19 +26,19 @@ var (
ERROR_DONT_HAVE = errors.New("Username was not found") ERROR_DONT_HAVE = errors.New("Username was not found")
) )
func (u *UserCenter) AddUser(name string) (int32, error) { func (u *UserCenter) AddUser(name string) (uint16, error) {
u.Mu.Lock() u.Mu.Lock()
defer u.Mu.Unlock() defer u.Mu.Unlock()
_, alreadyHave := u.UsersSTOI[name] _, alreadyHave := u.UsersSTOI[name]
if alreadyHave { if alreadyHave {
return -1, ERROR_ALREADY_HAVE return 0, ERROR_ALREADY_HAVE
} }
var ret int32 var ret uint16
for { for {
rInt := rand.Int31() rInt := rand.Int31()
_, taken := u.UsersITOS[rInt] _, taken := u.UsersITOS[uint16(rInt)]
if !taken { if !taken {
ret = rInt ret = uint16(rInt)
break break
} }
} }
@ -47,15 +47,15 @@ func (u *UserCenter) AddUser(name string) (int32, error) {
return ret, nil return ret, nil
} }
func (u *UserCenter) GetID(name string) (int32, error) { func (u *UserCenter) GetID(name string) (uint16, error) {
id, have := u.UsersSTOI[name] id, have := u.UsersSTOI[name]
if !have { if !have {
return -1, ERROR_DONT_HAVE return 0, ERROR_DONT_HAVE
} }
return id, nil return id, nil
} }
func (u *UserCenter) GetName(id int32) (string, error) { func (u *UserCenter) GetName(id uint16) (string, error) {
name, have := u.UsersITOS[id] name, have := u.UsersITOS[id]
if !have { if !have {
return "", ERROR_DONT_HAVE return "", ERROR_DONT_HAVE