From f8db7c14c810f4119fdc88a0e1afcbd8bd6bd52c Mon Sep 17 00:00:00 2001 From: efim Date: Thu, 16 Nov 2023 05:45:57 +0000 Subject: [PATCH] refactor: store all known as map for access --- rooms/room.go | 18 ++++++++++-------- routes/login_page.go | 5 +++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/rooms/room.go b/rooms/room.go index af9f0d6..11d1d87 100644 --- a/rooms/room.go +++ b/rooms/room.go @@ -13,15 +13,15 @@ type Room struct { CurrentSpeaker PersonId // i guess let's set to zero value when it's noone from the room // all people that were visiting room before, spectating now or being a participant // used to check person password against the name - AllKnownPeople []Person + AllKnownPeople map[PersonId]Person // person ids of people who are currently participating in the discussion at the table // TODO for now peson in seated at join and removed at logout in routes - Paricipants []PersonId + Paricipants []PersonId ParticipantHands map[PersonId]HandGesture Marks map[HandGesture]PersonId } -func (r *Room)InitMaps() { +func (r *Room) InitMaps() { if r.ParticipantHands == nil { r.ParticipantHands = make(map[PersonId]HandGesture) } @@ -35,7 +35,7 @@ func (r *Room)InitMaps() { // if you are speaking - change nothing // if nobody is speaking, set this person as a first speaker func (r *Room) RaiseHand(p PersonId, gesture HandGesture) Room { - if (r.CurrentSpeaker == p) { + if r.CurrentSpeaker == p { // if person already speaking, should first end speaking return *r } @@ -120,7 +120,7 @@ gestureIteration: func (r *Room) PersonToStandUpFromTable(p PersonId) { r.ReleaseHand(p) if slices.Contains(r.Paricipants, p) { - updated := slices.DeleteFunc(r.Paricipants, func(sittingPerson PersonId) bool { + updated := slices.DeleteFunc(r.Paricipants, func(sittingPerson PersonId) bool { return sittingPerson == p }) r.Paricipants = updated @@ -167,10 +167,12 @@ func (r *Room) Equal(other *Room) bool { if r.Name != other.Name || r.PasswordHash != other.PasswordHash || r.CurrentSpeaker != other.CurrentSpeaker { return false } - if !slices.Equal(r.AdminIds, other.AdminIds) || !slices.Equal(r.Paricipants, other.Paricipants) || !slices.Equal(r.AllKnownPeople, other.AllKnownPeople) { + if !slices.Equal(r.AdminIds, other.AdminIds) || !slices.Equal(r.Paricipants, other.Paricipants) { return false } - if !maps.Equal(r.ParticipantHands, other.ParticipantHands) || !maps.Equal(r.Marks, other.Marks) { + if !maps.Equal(r.ParticipantHands, other.ParticipantHands) || + !maps.Equal(r.Marks, other.Marks) || + !maps.Equal(r.AllKnownPeople, other.AllKnownPeople) { return false } return true @@ -188,7 +190,7 @@ const ( ) func GestureFromInt(num int) (HandGesture, bool) { - if (num >= int(ChangeTopic) && num <= int(Meta)) { + if num >= int(ChangeTopic) && num <= int(Meta) { return HandGesture(num), true } return HandGesture(0), false diff --git a/routes/login_page.go b/routes/login_page.go index 494aff0..a8a6b5e 100644 --- a/routes/login_page.go +++ b/routes/login_page.go @@ -135,7 +135,8 @@ func createRoomHandler(templateFs *embed.FS, PasswordHash: r.PostFormValue("roomPassword"), // TODO hash the password, not to store AdminIds: []rooms.PersonId{person.Id}, Paricipants: []rooms.PersonId{person.Id}, - AllKnownPeople: []rooms.Person{person}, + AllKnownPeople: map[rooms.PersonId]rooms.Person{ + person.Id: person}, } err = roomsM.Save(newRoom) if err != nil { @@ -247,7 +248,7 @@ func joinRoomHandler(templateFs *embed.FS, err := roomsM.Update(r.Context(), room.Name, func(fromRoom rooms.Room) (toRoom rooms.Room) { log.Printf("/login/join about to modify room %+v", fromRoom) toRoom = fromRoom - toRoom.AllKnownPeople = append(toRoom.AllKnownPeople, person) + toRoom.AllKnownPeople[person.Id] = person log.Printf("/login/join will save %+v", toRoom) return toRoom })