Permalink
Browse files

added moving, but non-working clocks

  • Loading branch information...
tux21b committed Apr 6, 2012
1 parent 0338e7e commit b21107cb61322c98dfb3115196c0168e788c09c1
Showing with 86 additions and 26 deletions.
  1. +6 −1 chess.css
  2. +31 −13 chess.go
  3. +1 −1 chess.html
  4. +48 −11 chess.js
View
@@ -39,6 +39,11 @@ header h1 {
padding: .5em 0 .4em;
}
+header h1 a, header h1 a:hover {
+ color: #333;
+ text-decoration: none;
+}
+
header p.intro {
float: right;
width: 60%;
@@ -50,7 +55,7 @@ header p.intro {
}
footer {
- margin-top: .5em;
+ margin-top: .8em;
}
footer div.stats {
View
@@ -13,6 +13,7 @@ import (
"math/rand"
"net/http"
"sync/atomic"
+ "time"
)
type figur int
@@ -122,20 +123,22 @@ func (b *Board) freeWay(ax, ay, bx, by int) bool {
// General message struct which is used for parsing client requests and sending
// back responses.
type Message struct {
- Cmd string
- Turn int
- Ax, Ay int
- Bx, By int
- Board *Board
- Color int
- NumPlayers int32
- History string
+ Cmd string
+ Turn int
+ Ax, Ay int
+ Bx, By int
+ Board *Board
+ Color int
+ NumPlayers int32
+ History string
+ RemainingA, RemainingB time.Duration
}
type Player struct {
- Conn *websocket.Conn
- Exit chan bool
- Color int
+ Conn *websocket.Conn
+ Exit chan bool
+ Color int
+ Remaining time.Duration
}
// Check wethever the player is still connected by sending a ping command.
@@ -187,18 +190,23 @@ func play(a, b Player) {
}
a.Color = WHITE
b.Color = BLACK
+ a.Remaining = 5 * time.Minute
+ b.Remaining = 5 * time.Minute
err := websocket.JSON.Send(a.Conn, Message{Cmd: "start", Board: board,
- Color: a.Color, Turn: turn, NumPlayers: atomic.LoadInt32(&numPlayers)})
+ Color: a.Color, Turn: turn, NumPlayers: atomic.LoadInt32(&numPlayers),
+ RemainingA: a.Remaining, RemainingB: b.Remaining})
if err != nil {
return
}
err = websocket.JSON.Send(b.Conn, Message{Cmd: "start", Board: board,
- Color: b.Color, Turn: turn, NumPlayers: atomic.LoadInt32(&numPlayers)})
+ Color: b.Color, Turn: turn, NumPlayers: atomic.LoadInt32(&numPlayers),
+ RemainingA: a.Remaining, RemainingB: b.Remaining})
if err != nil {
return
}
+ start := time.Now()
for {
var msg Message
if err := websocket.JSON.Receive(a.Conn, &msg); err != nil {
@@ -213,6 +221,16 @@ func play(a, b Player) {
if turn&1 == 1 {
msg.History = fmt.Sprintf("%d. %s", (turn+1)/2, msg.History)
}
+ now := time.Now()
+ a.Remaining -= now.Sub(start)
+ if a.Remaining < 0 {
+ a.Remaining = 0
+ }
+ start = now
+ msg.RemainingA, msg.RemainingB = a.Remaining, b.Remaining
+ if a.Color == BLACK {
+ msg.RemainingA, msg.RemainingB = b.Remaining, a.Remaining
+ }
websocket.JSON.Send(a.Conn, msg)
websocket.JSON.Send(b.Conn, msg)
a, b = b, a
View
@@ -14,7 +14,7 @@
Play chess with Go, HTML5,<br />
WebSockets and random strangers!
</p>
- <h1>ChessBuddy</h1>
+ <h1><a href="/">ChessBuddy</a></h1>
</header>
<aside>
View
@@ -21,6 +21,9 @@ function ChessGame(canvas, clocks, websocket) {
this.board[i] = 0;
this.ws = websocket;
this.status = 0;
+ this.totalTime = 0;
+ this.remainingA = 0;
+ this.remainingB = 0;
var _this = this;
this.ws.onmessage = function(e) { _this.process(e); };
@@ -29,6 +32,16 @@ function ChessGame(canvas, clocks, websocket) {
_this.click(e)
});
this.render();
+
+ this.clocks_int = window.setInterval(function() {
+ _this.tick();
+ }, 1000);
+
+ window.onbeforeunload = function(e) {
+ if (_this.status == 0 && _this.color != 0) {
+ return "Leaving the page will cancel the current game.";
+ }
+ };
}
@@ -92,9 +105,10 @@ ChessGame.prototype.render = function() {
ctx.fillText(msg, 4.5*size, 4.5*size);
}
- /* draw clocks */
- this.renderClock(0, 0, 130, 0, 1);
- this.renderClock(150, 0, 130, 0.25, -1);
+ this.renderClock(0, 0, 130,
+ this.totalTime > 0 ? this.remainingA / this.totalTime : 0, 1);
+ this.renderClock(150, 0, 130,
+ this.totalTime > 0 ? this.remainingB / this.totalTime : 0, -1);
};
ChessGame.prototype.renderClock = function(x, y, size, t, color) {
@@ -111,6 +125,12 @@ ChessGame.prototype.renderClock = function(x, y, size, t, color) {
ctx.closePath();
ctx.fill();
+ ctx.fillStyle = "#888";
+ ctx.font = 'bold 12pt "Helvetica Neue", Helvetica, Arial, sans-serif';
+ ctx.textAlign = "center";
+ ctx.textBaseline = "middle";
+ ctx.fillText(color > 0 ? "white" : "black", x+0.5*size, y+0.7*size);
+
ctx.fillStyle = active ? "#ff0000" : "#000000";
ctx.beginPath();
ctx.arc(x+0.5*size, y+0.5*size, 0.05*size, 0, Math.PI*2, true);
@@ -121,16 +141,10 @@ ChessGame.prototype.renderClock = function(x, y, size, t, color) {
ctx.lineWidth = 8;
ctx.beginPath();
ctx.moveTo(x+0.5*size, y+0.5*size);
- ctx.lineTo(x+0.5*size+0.475*size*Math.sin(t*Math.PI),
- y+0.5*size-0.475*size*Math.cos(t*Math.PI));
+ ctx.lineTo(x+0.5*size+0.475*size*Math.sin(t*2*Math.PI),
+ y+0.5*size-0.475*size*Math.cos(t*2*Math.PI));
ctx.closePath();
ctx.stroke();
-
- ctx.fillStyle = "#888";
- ctx.font = 'bold 12pt "Helvetica Neue", Helvetica, Arial, sans-serif';
- ctx.textAlign = "center";
- ctx.textBaseline = "middle";
- ctx.fillText(color > 0 ? "white" : "black", x+0.5*size, y+0.7*size);
}
@@ -180,17 +194,40 @@ ChessGame.prototype.process = function(e) {
this.board[msg.By*8+msg.Bx] = this.board[msg.Ay*8+msg.Ax];
this.board[msg.Ay*8+msg.Ax] = 0;
this.turn = msg.Turn+1;
+ this.remainingA = msg.RemainingA;
+ this.remainingB = msg.RemainingB;
document.getElementById("history").innerHTML += msg.History + " ";
this.render();
}
else if (msg.Cmd == "start") {
this.board = msg.Board;
this.color = msg.Color;
this.turn = 1;
+ this.totalTime = msg.RemainingA;
+ this.remainingA = msg.RemainingA;
+ this.remainingB = msg.RemainingB;
this.render();
}
else if (msg.Cmd == "ping") {
ws.send(JSON.stringify({Cmd: "pong"}));
}
document.getElementById("numPlayers").innerHTML = msg.NumPlayers;
}
+
+ChessGame.prototype.tick = function() {
+ if (this.status == 0) {
+ if ((this.turn % 2) == 1) {
+ this.remainingA -= 1000000000;
+ if (this.remainingA < 0)
+ this.remainingA = 0;
+ } else {
+ this.remainingB -= 1000000000;
+ if (this.remainingB < 0)
+ this.remainingB = 0;
+ }
+ }
+ this.renderClock(0, 0, 130,
+ this.totalTime > 0 ? this.remainingA / this.totalTime : 0, 1);
+ this.renderClock(150, 0, 130,
+ this.totalTime > 0 ? this.remainingB / this.totalTime : 0, -1);
+}

0 comments on commit b21107c

Please sign in to comment.