repl.it
@rshetty/

cash.io

Nodejs

A mass multiplayer "CA$H" game for the Repl.it 2019 Game Jam.

fork
loading
Files
  • index.js
  • public
  • big bank boi [redacted].png
  • coin [redacted].png
  • jamPost.md
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);

players = {}; // Player list
coins = [];
leaderboard = [];
users = [];
// Use correct file directory
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});
 
server.listen(8080, function () {
  console.log(`Listening on ${server.address().port}`);
});

Array.prototype.contains = function(value) {
	for(var i=0; i< this.length; i++){
		if(this[i] == value)
			return true;
    }
	return false;
}

function insert(element, array) {
  array.splice(locationOf(element, array) + 1, 0, element);
  return locationOf(element, array) + 1;
}

function locationOf(element, array, start, end) {
  start = start || 0;
  end = end || array.length;
  var pivot = parseInt(start + (end - start) / 2, 10);
  if (end-start <= 1 || array[pivot] === element) return pivot;
  if (array[pivot] < element) {
    return locationOf(element, array, pivot, end);
  } else {
    return locationOf(element, array, start, pivot);
  }
}


function updateLeaderboard(){
    
	leaderboard = [];
	users = [];
	Object.keys(players).forEach(function(id){
        leaderboard.push(players[id].coins);
		users.push(players[id].username);
	});

    io.emit("leaderboardUpdate", {leaderboard: leaderboard, users: users})
}




function clone(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}

	var positions = []
	for (var i = 0; i <10; i++) {
		
		positions.push([Math.floor((Math.random() * 2600) + 400), Math.floor((Math.random() * 1500) + 200), Math.floor((Math.random() * 5) + 5)]);

	}
		setInterval(function(){
			if (coins.length < 300){
				var bankIndex = Math.floor((Math.random() * positions.length));
				var bank = {x: positions[bankIndex][0], y: positions[bankIndex][1]};
				var coinPos = {x: Math.random() < 0.5 ? bank.x + Math.floor((Math.random() * -50)) - 50 : bank.x + Math.floor((Math.random() * 50)) + 50, y: Math.random() < 0.5 ? bank.y + Math.floor((Math.random() * -50)) - 50 : bank.y + Math.floor((Math.random() * 50)) + 50}
				coins.push(coinPos);
				io.emit("newCoin", {x: coinPos.x, y: coinPos.y, bank: bank});
			}
		}, Math.floor(Math.random() * 1000) + 500);



io.on('connection', function (socket) {
  console.log('a user connected');
	

	socket.emit("bankPos", positions);
	socket.on("initPlayer", function(pos){
// Define a new player in the player list
	players[socket.id] = {
		x: pos.x,
		y: pos.y,
		xVel: 0,
		yVel: 0,
		playerId: socket.id,
		coins: 30,
        username: pos.username
	};
        
		// Send object to other players
		socket.broadcast.emit('newPlayer', players[socket.id]);
		updateLeaderboard();
	});

	// Send object to local player
	socket.emit('currentPlayers', {players: players, coins: coins});



	// Update player positions on server
	socket.on('playerMovement', function (movementData) {
		try{
		players[socket.id].x = movementData.x;
		players[socket.id].y = movementData.y;
		players[socket.id].xVel = movementData.xVel;
		players[socket.id].yVel = movementData.yVel;
		if (movementData.coins - players[socket.id].coins <= 10){
		players[socket.id].coins = movementData.coins;}
		// Emit new position to all players
		socket.broadcast.emit('playerMoved', players[socket.id]);
		updateLeaderboard();
		} catch{}
		
	});



	socket.on("consumption", function(coin){
		for (var i=0; i < coins.length; i++){
			if (coins[i] && coins[i].x == coin.x && coins[i].y == coin.y){
				coins.splice(i, 1);
				var id = socket.id;
				players[id].coins++;
				updateLeaderboard();
				socket.broadcast.emit("delCoin", {x: coin.x, y: coin.y, player: players[id]});
			}
		}
	});

	socket.on("collision", function(p){
		var local = players[p.player.id];
		var other = players[p.otherPlayer];
		var localVel = p.player.velocity;
		var otherCoinLoss = localVel >= 290 ? (localVel - 280) / 2 : 0
		otherCoinLoss = Math.round(otherCoinLoss);
		try{
    other.coins -= otherCoinLoss;} catch{}
		updateLeaderboard();

		try{
			other.coins = other.coins - otherCoinLoss;
			if (other.coins <= 0){
                
				other.coins = 0;
					for (var i = 0; i < 20; i++){
						var coinPos = {x: Math.random() < 0.5 ? other.x + Math.floor((Math.random() * -50)) - 50 : other.x + Math.floor((Math.random() * 50)) + 50, y: Math.random() < 0.5 ? other.y + Math.floor((Math.random() * -50)) - 50 : other.y + Math.floor((Math.random() * 50)) + 50}
						coins.push(coinPos);
						io.emit("newCoin", {x: coinPos.x, y: coinPos.y, bank: {x: other.x, y: other.y}});
					}
				}
		} catch{}
		io.emit("coinLoss", {p1: other});
	});


	socket.on('add username', function(username){
		players[socket.id].username = username;
	});

	socket.on('new message', function(messageData){
		socket.broadcast.emit('new message', {message: messageData.message, username: messageData.username});
	});

	socket.on('disconnect', function () {
    console.log('user disconnected');

		// Remove this player from our players object
		
		// Emit a message to all players to remove this player
		tempPlayer = players[socket.id];
		io.emit('disconnect', tempPlayer);
		delete players[socket.id];
  });
});

node v10.16.0