Skip to content

Commit

Permalink
fix: make all appends in realm re-init slices (#147)
Browse files Browse the repository at this point in the history
* fix: make all appends in realm re-init slices

* fix syntax :P
  • Loading branch information
thehowl authored Sep 25, 2023
1 parent 8741280 commit ab8c600
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
7 changes: 6 additions & 1 deletion realm/discovery.gno
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ func getPlayer(addr std.Address) *Player {
p.CategoryInfo[cat] = CategoryInfo{
PlayerRating: pr,
}
playerRatings[cat] = append(playerRatings[cat], pr)
// https://github.com/gnolang/gno/pull/1170
prs := append([]*PlayerRating{}, playerRatings[cat]...)
playerRatings[cat] = append(prs, pr)
}
playerStore.Set(addr.String(), p)
return p
Expand Down Expand Up @@ -387,6 +389,9 @@ func parseFilters(filters string) (r listGamesFilters) {
if idx < 0 {
panic("invalid filter: " + part)
}
// TODO: this is likely to misbehave due to https://github.com/gnolang/gno/issues/1135
// This is unlikely to cause much of an issue here, as we can expect player/white/black/finished
// to only exist once, though it should be kept in mind if/when adding additional filters.
filt, pred := part[:idx+1], part[idx+1:]
switch filt {
case "player:":
Expand Down
17 changes: 13 additions & 4 deletions realm/lobby.gno
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ func LobbyJoin(seconds, increment int) {
lobbyPlayer2Game.Remove(caller.String())

now := time.Now()
lobby[tc] = append(lobby[tc], lobbyPlayer{joinedAt: now, seenAt: now, player: getPlayer(caller)})
// https://github.com/gnolang/gno/pull/1170
newLobby := append([]lobbyPlayer{}, lobby[tc]...)
lobby[tc] = append(newLobby, lobbyPlayer{joinedAt: now, seenAt: now, player: getPlayer(caller)})
refreshLobby(tc)
}

Expand Down Expand Up @@ -143,7 +145,9 @@ func refreshLobby(tc tcLobby) {
// is seemingly active in the lobby.
for i := 0; i < len(lobby[tc]); i++ {
if now.Sub(lobby[tc][i].seenAt) >= time.Second*30 {
lobby[tc] = append(lobby[tc][:i], lobby[tc][i+1:]...)
// https://github.com/gnolang/gno/pull/1170
newLobby := append([]lobbyPlayer{}, lobby[tc][:i]...)
lobby[tc] = append(newLobby, lobby[tc][i+1:]...)
i--
}
}
Expand Down Expand Up @@ -195,7 +199,9 @@ func lobbyMatch(tc tcLobby, p1, p2 int) {
if p1 > p2 {
p1, p2 = p2, p1
}
nl := append(lobby[tc][:p1], lobby[tc][p1+1:p2]...)
// https://github.com/gnolang/gno/pull/1170
nl := append([]lobbyPlayer{}, lobby[tc][:p1]...)
nl = append(nl, lobby[tc][p1+1:p2]...)
nl = append(nl, lobby[tc][p2+1:]...)
lobby[tc] = nl

Expand Down Expand Up @@ -247,7 +253,10 @@ func LobbyQuit() {
for tc, sublob := range lobby {
for i, pl := range sublob {
if pl.player.Address == caller {
lobby[tc] = append(sublob[:i], sublob[i+1:]...)
// https://github.com/gnolang/gno/pull/1170
newLobby := append([]lobbyPlayer{}, sublob[:i]...)
newLobby = append(newLobby, sublob[i+1:]...)
lobby[tc] = newLobby
lobbyPlayer2Game.Remove(caller.String())
return
}
Expand Down
4 changes: 3 additions & 1 deletion realm/time.gno
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func (tc *TimeControl) AddMove() (valid bool) {
} else {
tc.BlackTime = nd
}
tc.MoveTimestamps = append(tc.MoveTimestamps, time.Now())
// https://github.com/gnolang/gno/pull/1170
mts := append([]time.Time{}, tc.MoveTimestamps...)
tc.MoveTimestamps = append(mts, time.Now())
return true
}

Expand Down

0 comments on commit ab8c600

Please sign in to comment.