You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

305 lines
8.8 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
box-sizing: border-box;
}
.board-container {
background: seagreen;
bottom: 0;
left: 300px;
position: absolute;
right: 0;
top: 0;
}
.controls-container {
background: coral;
bottom: 0;
left: 0;
position: absolute;
right: 300px;
top: 0;
}
#board {
border-color: #aaa;
border-style: solid;
border-width: 1px 0 0 1px;
margin: 0 auto;
position: relative;
}
.board-wall-x {
background: #222;
height: 8px;
margin-top: -4px;
position: absolute;
width: 40px;
}
.board-wall-y {
background: #222;
height: 40px;
margin-left: -4px;
position: absolute;
width: 8px;
}
.board-robot {
border-radius: 18px;
height: 36px;
margin: 2px;
position: absolute;
width: 36px;
}
.square{
background: #ddd;
border-style: solid;
border-color: #aaa;
border-width: 0 1px 1px 0;
float: left;
height: 40px;
width: 40px;
}
.moves {
background: salmon;
}
.move-count {
background: yellow;
border-radius: 8px;
float: left;
font-size: 12px;
height: 16px;
line-height: 16px;
margin: 4px;
text-align: center;
width: 16px;
}
</style>
</head>
<body>
<div class="controls-container">
<div>
<span>Room ID</span>
<input type="text" id='game-id'>
<button type='button'>&gt;</button>
</div>
<div class="rounds">
<button type='button' id='game-start'>Start New Round</button>
<div class="timer">0:42</div>
</div>
<div class="players">
<div class="player">Grendel</div>
<div class="player">Barbarossa</div>
<div class="player">Pi</div>
<div class="player">Wendy</div>
</div>
<div class="moves">
<div class="move-count">1</div>
<div class="move-count">2</div>
<div class="move-count">3</div>
<div class="move-count">4</div>
<div class="move-count">5</div>
<div class="move-count">6</div>
<div class="move-count">7</div>
<div class="move-count">8</div>
<div class="move-count">9</div>
<div class="move-count">10</div>
<br>
<div class="move-count">11</div>
<div class="move-count">12</div>
<div class="move-count">13</div>
<div class="move-count">14</div>
<div class="move-count">15</div>
<div class="move-count">16</div>
<div class="move-count">17</div>
<div class="move-count">18</div>
<div class="move-count">19</div>
<div class="move-count">20</div>
<br>
<div class="move-count">21</div>
<div class="move-count">22</div>
<div class="move-count">23</div>
<div class="move-count">24</div>
<div class="move-count">25</div>
<div class="move-count">26</div>
<div class="move-count">27</div>
<div class="move-count">28</div>
<div class="move-count">29</div>
<div class="move-count">30</div>
</div>
</div>
<div class="board-container">
<div id="board"></div>
</div>
<script>
// TODO dynamic sizing of squares based on available height
// TODO dynamic move population
// TODO move websocket server to /core
// TODO dynamic socket server resolution
// TODO namespace server to /ricochet
// TODO [soap, xmpp]
// TODO a message must have a head and a body
// TODO your favorite games
const Cookie = {
getCookie: function(name) {
var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return v ? decodeURI(v[2]) : null;
},
setCookie: function(name, value, days) {
var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + "=" + encodeURI(value) + ";path=/;expires=" + d.toGMTString();
},
deleteCookie: function(name) {
Util.setCookie(name, '', -1);
}
};
var connection = new WebSocket('ws://localhost:8080/ricochet', ['soap', 'xmpp']);
// connection.onopen = function (evt) {};
connection.onerror = console.error;
connection.onmessage = function (msg) {
const data = JSON.parse(msg.data)
if (!data.head || !data.body) {
console.warn("Unprocessable entity: ", msg)
return;
}
switch(data.head.type) {
case 'connect':
break;
case 'disconnect':
break;
case 'walls':
Board.placeWalls(data.body);
break;
case 'robots':
Board.placeRobots(data.body);
break;
default:
console.warn("Unhandled message: ", msg)
}
};
document.getElementById('game-start').addEventListener('click', () => {
connection.send('says hello!')
})
const Board = {
squaresPerSide: 20,
squareSize: 40,
placeSquares: () => {
const board = document.getElementById('board');
board.style.width = (Board.squaresPerSide * Board.squareSize + 1) + 'px';
board.style.height = (Board.squaresPerSide * Board.squareSize + 1) + 'px';
while (board.hasChildElements) {
board.removeChild(board.firstChild);
}
for (let i = 0; i < Board.squaresPerSide; i++) {
for (let j = 0; j < Board.squaresPerSide; j++) {
const square = document.createElement('div');
square.className = 'square';
board.appendChild(square);
}
}
},
placeRobots: (robots) => {
robots.forEach(robot => {
const [color, x, y] = robot;
console.warn(color)
const r = document.createElement('div');
r.className = 'board-robot';
r.style.background = color;
r.style.left = 40 * x + 'px';
r.style.top = 40 * y + 'px';
document.getElementById('board').appendChild(r);
})
},
placeWall: (id, className, x, y) => {
if (document.getElementById(id)) {
return;
}
const w = document.createElement('wall');
w.id = id;
w.style.top = x + 'px';
w.style.left = y + 'px';
w.className = className;
document.getElementById('board').appendChild(w);
},
placeWalls: (walls) => {
walls.forEach(wall => {
const [x, y, north, south, east, west] = wall;
const pxX = 40 * x;
const pxY = 40 * y;
if (north) {
Board.placeWall(`${x}-${y}-n`, "board-wall-y", pxX, pxY);
}
if (south) {
Board.placeWall(`${x}-${y}-s`, "board-wall-y", pxX, pxY + Board.squareSize);
}
if (east) {
Board.placeWall(`${x}-${y}-e`, "board-wall-x", pxX, pxY);
}
if (west) {
Board.placeWall(`${x}-${y}-e`, "board-wall-x", pxX + Board.squareSize, pxY);
}
})
},
removeWalls: () => {
const walls = document.querySelector('board-wall-x').concat(document.querySelectorAll('board-wall-y'))
}
}
window.addEventListener('load', Board.placeSquares)
</script>
</body>
</html>