Skip to content

Commit

Permalink
Tweak autosaves, add past save loading (issue #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Absotively committed Feb 19, 2017
1 parent f190fb0 commit 9349d59
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
40 changes: 39 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ func playerForm(w http.ResponseWriter, r *http.Request) {
player.Name = name
player.Corp = corp
player.Runner = runner
saveWrapper(fmt.Sprintf("Edited player %s (was %s)", name, oldName))
if name == oldName {
saveWrapper(fmt.Sprintf("Edited player %s", name))
} else {
saveWrapper(fmt.Sprintf("Edited player %s (was %s)", name, oldName))
}
} else {
e = errors.New("No such player")
}
Expand Down Expand Up @@ -256,6 +260,38 @@ func recordResult(w http.ResponseWriter, r *http.Request) {
}
}

func saves(w http.ResponseWriter, r *http.Request) {
data, e := scanSaveFile(filename)
if e != nil {
fmt.Println(e)
seeOther(w, "/")
} else {
applyTemplate(w, savesTemplate, data)
}
}

func loadOldSave(w http.ResponseWriter, r *http.Request) {
defer seeOther(w, "/")
if r.Method == "POST" {
numberString := r.FormValue("save-number")
var number int
if numberString != "" {
var e error
number, e = strconv.Atoi(numberString)
if e != nil {
return
}
}
reason := r.FormValue("save-reason")

err := loadSave(&tournament, filename, number)
if err != nil {
return
}
saveWrapper(fmt.Sprintf("Loaded old save (%s)", reason))
}
}

func saveWrapper(reason string) error {
var e error
e = tournament.save(filename, reason)
Expand Down Expand Up @@ -303,5 +339,7 @@ func main() {
http.HandleFunc("/recordResult", recordResult)
http.HandleFunc("/finishRound", finishRound)
http.HandleFunc("/nextRound", startRound)
http.HandleFunc("/saves", saves)
http.HandleFunc("/load", loadOldSave)
http.ListenAndServe("localhost:8080", nil)
}
21 changes: 21 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const menuTemplate = `<h1>Tournament menu</h1>
<li><a href="/rounds">All rounds</a></li>
<li><form action="/finishRound" method="POST"><input type="submit" value="Finish round"></form></li>
<li><form action="/nextRound" method="POST"><input type="submit" value="Start next round"></form></li>
<li><a href="/saves">History/undo</a></li>
</ul>
`

Expand All @@ -40,6 +41,26 @@ const playerListTemplate = `<h1>Players</h1>
<p><a href="/">Menu</a></p>
`

const savesTemplate = `<h1>Saved tournament states</h1>
{{if .}}<p>Note that newer states are at the bottom</p>
<table>
<tr><th>Last action</th><th></th></tr>
{{range .}}<tr>
<td>{{.Reason}}</td>
<td>
<form action="/load" method="POST">
<input type="hidden" name="save-number" value="{{.Number}}">
<input type="hidden" name="save-reason" value="{{.Reason}}">
<input type="submit" value="Load">
</form>
</td>
</tr>{{end}}
</table>
{{else}}
<p>No saves.</p>
{{end}}
`

const standingsTemplate = `{{$t := .}}<h1>Standings</h1>
{{if .Standings}}<table id="standings">
<tr><th>Player</th><th>Pts</th><th>SoS</th><th>XSoS</th></tr>
Expand Down
1 change: 1 addition & 0 deletions tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ func scanSaveFile(file string) ([]saveHeader, error) {
defer f.Close()
dec := json.NewDecoder(f)
for dec.More() {
h.Reason = ""
e = dec.Decode(&h)
if e != nil {
return headers, e
Expand Down

0 comments on commit 9349d59

Please sign in to comment.