diff options
-rw-r--r-- | rooms.go | 19 |
1 files changed, 10 insertions, 9 deletions
@@ -6,14 +6,15 @@ import ( type Room struct { Messages chan Message - Buddies map[string]Buddy + Buddies map[string]*Buddy } func NewRoom() (r *Room) { r = new(Room) r.Messages = make(chan Message) - r.Buddies = make(map[string]Buddy) + r.Buddies = make(map[string]*Buddy) go r.Broadcast() + return } func (r *Room) Leave(nick string) { @@ -21,13 +22,13 @@ func (r *Room) Leave(nick string) { return } - delete(r.Buddies[nick]) + delete(r.Buddies, nick) if len(r.Buddies) == 0 { close(r.Messages) } else { r.Messages <- Message{ Type: MsgLeave, - From: nick, + User: nick, } } } @@ -47,7 +48,7 @@ func (r *Room) ListBuddies() (buddies []string) { return } -var rooms = make(map[string]Room) +var rooms = make(map[string]*Room) func Join(room, nick string) (*Buddy, *Room, error) { r, ok := rooms[room] @@ -57,19 +58,19 @@ func Join(room, nick string) (*Buddy, *Room, error) { } if _, there := r.Buddies[nick]; there { - return nil, room, errors.New("Nickname is already in use") + return nil, nil, errors.New("Nickname is already in use") } if len(r.Buddies) >= *perroom { - return nil, room, errors.New("Room is full") + return nil, nil, errors.New("Room is full") } r.Messages <- Message{ Type: MsgJoin, - From: nick, + User: nick, } b := NewBuddy(nick, r) r.Buddies[nick] = b - return b, nil + return b, r, nil } |