Skip to content

Commit

Permalink
correctly loading the texts
Browse files Browse the repository at this point in the history
  • Loading branch information
shilangyu committed Nov 24, 2019
1 parent db7c561 commit 195a4eb
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ import (
"io/ioutil"
"math/rand"
"os"
"path"
"strings"
"time"

"github.com/shilangyu/typer-go/settings"
"github.com/shilangyu/typer-go/utils"
)

// ChooseText randomly chooses a text from the dataset
func ChooseText() (string, error) {
if _, err := os.Stat(settings.I.TextsPath); os.IsNotExist(err) {
return "", errors.New("Didnt find typer texts, make sure your path is correct")
}
rand.Seed(time.Now().UTC().UnixNano())

bytes, err := ioutil.ReadFile(path.Join(utils.Root(), "texts.txt"))
bytes, err := ioutil.ReadFile(settings.I.TextsPath)
if err != nil {
return "", nil
return "", errors.New("Couldnt load the typer texts, make sure the permission are correct")
}
content := string(bytes)
texts := strings.Split(content, "\n")
texts := strings.Split(string(bytes), "\n")
texts = texts[:len(texts)-1]

return texts[rand.Intn(len(texts))], nil
Expand Down

0 comments on commit 195a4eb

Please sign in to comment.