Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
+ render matrix
  • Loading branch information
grrrisu committed Apr 7, 2016
1 parent 8ba4ede commit 771b594
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
18 changes: 18 additions & 0 deletions examples/node/game_of_life/receiver.js
Expand Up @@ -10,11 +10,29 @@ module.exports = class Receiver {
}

listen(socket){
socket.on('game_of_life.build', (data) => {
let matrix = document.getElementById("matrix");
$(matrix).html('');
this.renderMatrix(matrix, JSON.parse(data.answer));
});

socket.on('game_of_life.*', (data) => {
console.log("data received", data);
let time = new Date().toLocaleString();
$('#messages').prepend('<tr class="message"><td>'+time+'</td><td>'+data.answer+'</td></tr>');
});
}

renderMatrix(matrix, fields){
fields.forEach((y) => {
let row = document.createElement("div");
$(row).addClass('clearfix');
matrix.appendChild(row);
y.forEach((x) => {
let alive_css = x.alive ? ' alive' : '';
$(row).append('<div class="cell'+alive_css+'"></div>');
});
});
}

}
8 changes: 3 additions & 5 deletions examples/node/public/game_of_life.html
Expand Up @@ -10,15 +10,13 @@
#matrix .cell {
width: 50px;
height: 50px;
background-color: #ddd;
float: left;
}

#matrix .cell .alive {
#matrix .cell.alive {
background-color: #333;
}

#matrix .cell .dead {
background-color: #ddd;
}
</style>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion examples/server/game_of_life/world.rb
Expand Up @@ -10,7 +10,7 @@ def create
srand
set_each_field_with_index do |x, y|
cell = Cell.new(self, x, y)
cell.alive = rand(3) == 2
cell.alive = rand(4) == 0
cell
end
end
Expand Down

0 comments on commit 771b594

Please sign in to comment.