Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
Cow milking
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfredericks committed Dec 17, 2019
1 parent 37a0b98 commit ffe6341
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
17 changes: 16 additions & 1 deletion game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ var sio;
*/
exports.initGame = function(io, socket) {
sio = io;

socket.on('newGame', newGame);
socket.on('joinGame', joinGame)
socket.on('joinGame', joinGame);
socket.on('milkCow', milkCow);
}

/**
Expand Down Expand Up @@ -63,4 +65,17 @@ function startGame(gameId) {
setTimeout(function() {
sio.in(gameId).emit('startGame')
}, 3000);
}

/**
* Milk cow
* @param {object} data
*/
function milkCow(data) {
var gameId = data.gameId;
var socketId = data.socketId;
/* get sender socket */
var socket = sio.of('/').connected[socketId]
/* notify clients (excluding sender) */
socket.to(gameId).emit('otherPlayerMilked')
}
5 changes: 5 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ <h2>Game ID: <span id="gameId"></span></h2>
3... 2... 1...
</div>

<div id="game" class="hidden">
<img id="cow" onclick="milkCow()" src="images/cow_thing.svg">
</div>

<script src="/socket.io/socket.io.js"></script>
<script src="javascripts/state.js"></script>
<script src="javascripts/game.js"></script>
</body>

Expand Down
27 changes: 26 additions & 1 deletion public/javascripts/game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var socket = io();

socket.on('connect', function() {
/* add socket id to state */
state.me = socket.id;
})

/**
* Create new game
*/
Expand All @@ -11,12 +16,16 @@ function newGame() {
* Join existing game
*/
function joinGame() {
/* get game id */
var gameId = prompt('Game ID')
.toLowerCase()
.trim()
/* emit join game */
socket.emit('joinGame', {
gameId,
})
/* add game id to state */
state.gameId = gameId
}

socket.on('newGameCreated', function(data) {
Expand All @@ -27,6 +36,8 @@ socket.on('newGameCreated', function(data) {
document.getElementById('waitingForPlayers').setAttribute('class', '');
/* show game id */
document.getElementById('gameId').innerText = gameId;
/* add game id to state */
state.gameId = gameId
});

socket.on('showCountdown', function() {
Expand All @@ -41,8 +52,22 @@ socket.on('showCountdown', function() {
socket.on('startGame', function() {
/* hide countdown */
document.getElementById('gameCountdown').setAttribute('class', 'hidden');
/* show game */
document.getElementById('game').setAttribute('class', '');
})

socket.on('error2', function(message) {
alert(message);
})
})

socket.on('otherPlayerMilked', function() {
alert('other player milked the cow');
})

function milkCow() {
/* notify server that cow has been milked */
socket.emit('milkCow', {
gameId: state.gameId,
socketId: state.me
})
}
15 changes: 15 additions & 0 deletions public/javascripts/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var state = {
gameId: null,
/* socket id of client */
me: null
}

/**
* Reset state
*/
function resetState() {
state = {
gameId: null,
me: null
}
}
5 changes: 5 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ a {

.hidden {
display: none;
}

#cow {
width: 500px;
height: auto;
}
2 changes: 1 addition & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
res.render('index', { title: 'Express' });
});

module.exports = router;

0 comments on commit ffe6341

Please sign in to comment.