Skip to content
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

add clue giving logic #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions assets/javascript/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@ window.Game = React.createClass({
}

$.get('/game/' + this.props.gameID, (data) => {
if (this.state.game && data.created_at != this.state.game.created_at) {
this.setState({codemaster: false});
}
if (this.state.game) {
if (data.round != this.state.game.round) {
this.changedTurn();
}
if (data.created_at != this.state.game.created_at) {
this.setState({codemaster: false});
}
}
this.setState({game: data});
setTimeout(this.refresh, 3000);
});
},

changedTurn: function() {
this.setState({hasClue: false});
},

toggleRole: function(e, role) {
e.preventDefault();
this.setState({codemaster: role=='codemaster'});
Expand Down Expand Up @@ -77,7 +86,7 @@ window.Game = React.createClass({

endTurn: function() {
$.post('/end-turn', JSON.stringify({game_id: this.state.game.id}),
(g) => { this.setState({game: g}); });
(g) => { this.setState({game: g}); this.changedTurn(); });
},

nextGame: function(e) {
Expand All @@ -86,6 +95,19 @@ window.Game = React.createClass({
(g) => { this.setState({game: g, codemaster: false}) });
},

giveClue: function(e) {
let clueWord = $('#clue-word')[0].value;
let clueCount = parseInt($('#clue-count')[0].value);
// How does this not have an error callback :(
$.post('/clue', JSON.stringify({
game_id: this.state.game.id,
word: clueWord,
count: clueCount
}), (g) => {
this.setState({game: g, hasClue: true});
});
},

render: function() {
if (!this.state.game) {
return (<p className="loading">Loading&hellip;</p>);
Expand All @@ -110,6 +132,23 @@ window.Game = React.createClass({
otherTeam = 'red';
}

let clueDOM;
if (this.state.codemaster) {
clueDOM = (
<div className="clue-line">
<input type="text" id="clue-word" disabled={this.state.hasClue} placeholder="word" autoFocus />
<input type="number" id="clue-count" disabled={this.state.hasClue} placeholder="count" />
<button onClick={this.giveClue}>Give Clue</button>
</div>
)
} else {
clueDOM = (
<div className="clue-line">
{this.state.game.clue ? "Clue: " + this.state.game.clue.word + "(" + this.state.game.clue.count + ")" : "Waiting for clue..."}
</div>
);
}

return (
<div id="game-view" className={this.state.codemaster ? "codemaster" : "player"}>
<div id="share">
Expand All @@ -118,6 +157,7 @@ window.Game = React.createClass({
<div id="status-line" className={statusClass}>
<div id="status" className="status-text">{status}</div>
</div>
{clueDOM}
<div id="button-line">
<div id="remaining">
<span className={this.state.game.starting_team+"-remaining"}>{this.remaining(this.state.game.starting_team)}</span>
Expand Down
2 changes: 2 additions & 0 deletions assets/stylesheets/game.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
font-weight: bold;
}

.clue-line { text-align: center; margin-bottom: 1em; }

#remaining { float: left; }
#remaining .red-remaining { color: #D13030; }
#remaining .blue-remaining { color: #4183CC; }
Expand Down
15 changes: 15 additions & 0 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func (t Team) Repeat(n int) []Team {
return s
}

type Clue struct {
Word string `json:"word"`
Count int `json:"count"`
}

type Game struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
Expand All @@ -65,6 +70,7 @@ type Game struct {
Words []string `json:"words"`
Layout []Team `json:"layout"`
Revealed []bool `json:"revealed"`
Clue *Clue `json:"clue"`
}

func (g *Game) checkWinningCondition() {
Expand Down Expand Up @@ -98,6 +104,7 @@ func (g *Game) NextTurn() error {
return errors.New("game is already over")
}
g.Round++
g.Clue = nil
return nil
}

Expand All @@ -119,6 +126,7 @@ func (g *Game) Guess(idx int) error {
g.checkWinningCondition()
if g.Layout[idx] != g.CurrentTeam() {
g.Round = g.Round + 1
g.Clue = nil
}
return nil
}
Expand All @@ -130,6 +138,13 @@ func (g *Game) CurrentTeam() Team {
return g.StartingTeam.Other()
}

func (g *Game) AddClue(word string, count int) {
g.Clue = &Clue{
Word: word,
Count: count,
}
}

func newGame(id string, words []string) *Game {
game := &Game{
ID: id,
Expand Down
38 changes: 38 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"html/template"
"net/http"
"path"
"regexp"
"sync"
"time"

"github.com/jbowens/assets"
"github.com/jbowens/dictionary"
)

var validClueRegex *regexp.Regexp

type Server struct {
Server http.Server

Expand Down Expand Up @@ -120,6 +123,38 @@ func (s *Server) handleNextGame(rw http.ResponseWriter, req *http.Request) {
writeJSON(rw, g)
}

// POST /clue
func (s *Server) handleClue(rw http.ResponseWriter, req *http.Request) {
var request struct {
GameID string `json:"game_id"`
Word string `json:"word"`
Count int `json:"count"`
}

decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&request); err != nil {
http.Error(rw, "Error decoding", 400)
return
}

s.mu.Lock()
defer s.mu.Unlock()

g, ok := s.games[request.GameID]
if !ok {
http.Error(rw, "No such game", 404)
return
}

if ok := validClueRegex.MatchString(request.Word); !ok {
http.Error(rw, "not a valid clue", 400)
return
}

g.AddClue(request.Word, request.Count)
writeJSON(rw, g)
}

func (s *Server) cleanupOldGames() {
s.mu.Lock()
defer s.mu.Unlock()
Expand All @@ -138,6 +173,8 @@ func (s *Server) cleanupOldGames() {
}

func (s *Server) Start() error {
validClueRegex, _ = regexp.Compile(`^[A-Za-z]+$`)

d, err := dictionary.Load("assets/original.txt")
if err != nil {
return err
Expand All @@ -164,6 +201,7 @@ func (s *Server) Start() error {
s.mux.HandleFunc("/next-game", s.handleNextGame)
s.mux.HandleFunc("/end-turn", s.handleEndTurn)
s.mux.HandleFunc("/guess", s.handleGuess)
s.mux.HandleFunc("/clue", s.handleClue)
s.mux.HandleFunc("/game/", s.handleRetrieveGame)

s.mux.Handle("/js/lib/", http.StripPrefix("/js/lib/", s.jslib))
Expand Down