-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix UI deadlocks + improve visual word colors #26
Open
cortze
wants to merge
1
commit into
main
Choose a base branch
from
feature/ui-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ package wordle | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/p2p-games/wordle/model" | ||
) | ||
|
@@ -21,7 +21,8 @@ type WordleUI struct { | |
|
||
CannonicalHeader *model.Header | ||
|
||
tm *TerminalManager | ||
uiInitialized bool | ||
tm *TerminalManager | ||
} | ||
|
||
func NewWordleUI(ctx context.Context, wordleServ *Service, peerId string) *WordleUI { | ||
|
@@ -33,6 +34,10 @@ func NewWordleUI(ctx context.Context, wordleServ *Service, peerId string) *Wordl | |
} | ||
|
||
wordleServ.SetLog(func(s string) { | ||
// wait untill UI is initialilzed | ||
for !ui.uiInitialized { | ||
time.Sleep(200 * time.Millisecond) | ||
} | ||
Comment on lines
+37
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing this, you can call SetLog when you know you can do it (UI is initialized) from the parent class. |
||
ui.AddDebugItem(s) | ||
}) | ||
|
||
|
@@ -48,43 +53,69 @@ func (w *WordleUI) Run() { | |
panic("non able to load any header from the datastore, not even genesis??!") | ||
} | ||
|
||
// generate a guess channel | ||
userGuessC := make(chan guess) | ||
|
||
// generate a new game | ||
w.CurrentGame = NewWordGame(w.ctx, w.PeerId, w.CannonicalHeader.PeerID, w.CannonicalHeader.Proposal, w.WordleServ) | ||
w.CurrentGame = NewWordGame(w.PeerId, w.CannonicalHeader.PeerID, w.CannonicalHeader.Proposal, userGuessC) | ||
|
||
// generate a terminal manager | ||
w.tm = NewTerminalManager(w.ctx, w.CurrentGame) | ||
err = w.tm.Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
// get the channel for incoming headers | ||
incomingHeaders, err := w.WordleServ.Guesses(w.ctx) | ||
if err != nil { | ||
panic("unable to retrieve the channel of headers from the user interface") | ||
} | ||
|
||
for { | ||
select { | ||
case recHeader := <-incomingHeaders: // incoming New Message from surrounding peers | ||
w.AddDebugItem(fmt.Sprintf("guess received from %s", recHeader.PeerID)) | ||
// verify weather the header is correct or not | ||
if model.Verify(recHeader.Guess, w.CannonicalHeader.Proposal) { | ||
w.CannonicalHeader = recHeader | ||
// generate a new one game | ||
w.CurrentGame = NewWordGame(w.ctx, w.PeerId, w.CannonicalHeader.PeerID, recHeader.Proposal, w.WordleServ) | ||
|
||
// refresh the terminal manager | ||
w.tm.Game = w.CurrentGame | ||
} else { | ||
// Actually, there isn't anything else to do | ||
continue | ||
go func() { | ||
for { | ||
select { | ||
case userGuess := <-userGuessC: | ||
w.tm.AddDebugMsg("about to send new guess") | ||
// notify the service of a new User Guess | ||
err = w.WordleServ.Guess(w.ctx, userGuess.Guess, userGuess.Proposal) | ||
if err != nil { | ||
w.tm.AddDebugMsg("error sending guess" + userGuess.Guess + " - " + err.Error()) | ||
} | ||
|
||
// check if the guess if the guess was right for debug-msg purpose | ||
bools, err := model.VerifyString(userGuess.Guess, w.CannonicalHeader.Proposal) | ||
if err != nil { | ||
w.tm.AddDebugMsg("error verifying the guess" + err.Error()) | ||
} | ||
|
||
if IsGuessSuccess(bools) { | ||
w.tm.AddDebugMsg("succ guess sent") | ||
} else { | ||
w.tm.AddDebugMsg("wrong guess sent") | ||
} | ||
|
||
case recHeader := <-incomingHeaders: // incoming New Message from surrounding peers | ||
//w.AddDebugItem(fmt.Sprintf("guess received from %s", recHeader.PeerID)) | ||
// verify weather the header is correct or not | ||
if model.Verify(recHeader.Guess, w.CannonicalHeader.Proposal) { | ||
w.CannonicalHeader = recHeader | ||
// generate a new one game | ||
|
||
w.CurrentGame = NewWordGame(w.PeerId, w.CannonicalHeader.PeerID, recHeader.Proposal, userGuessC) | ||
// refresh the terminal manager | ||
w.tm.Game = w.CurrentGame | ||
} | ||
case <-w.ctx.Done(): // context shutdow | ||
return | ||
} | ||
case <-w.ctx.Done(): // context shutdow | ||
return | ||
|
||
// render the new state of the UI | ||
w.tm.RefreshWordleState() | ||
} | ||
}() | ||
|
||
// generate a terminal manager | ||
w.tm = NewTerminalManager(w.ctx, w.CurrentGame) | ||
err = w.tm.Run(&w.uiInitialized) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
} | ||
|
||
func (w *WordleUI) AddDebugItem(s string) { | ||
w.tm.AddDebugItem(s) | ||
w.tm.AddDebugMsg(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, basing initialization on a timer is not a good idea in my opinion. You should have someplace in your code where you are sure everything is initialized.