initial commit

This commit is contained in:
Baipyrus 2022-05-15 16:25:19 +02:00
commit 856e211022
8 changed files with 4338 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
server/node_modules/*

10
client/index.html Normal file
View File

@ -0,0 +1,10 @@
<html>
<head>
</head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="js/game.js"></script>
<script type="text/javascript" src="js/player.js"></script>
<script type="text/javascript" src="js/general.js"></script>
</body>
</html>

68
client/js/game.js Normal file
View File

@ -0,0 +1,68 @@
function game() {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canv.width, canv.height);
/*
Draw all the players and food particles relative to own client
Scale players and food particles sizes relative to clients own size
All players are thus scaled so that the
client has it's own size never changing
*/
const apparentSize = canv.height / 4;
const scalor = apparentSize / self.data.r;
ctx.strokeStyle = "white";
ctx.beginPath();
ctx.rect(canv.width / 2 - self.data.x * scalor, canv.height / 2 - self.data.y * scalor, boundaryWidth * scalor, boundaryHeight * scalor);
ctx.closePath();
ctx.stroke();
ctx.strokeStyle = "grey";
for (let i = 4; i < boundaryWidth; i+=4) {
ctx.beginPath();
ctx.moveTo(canv.width / 2 - (self.data.x - i) * scalor, canv.height / 2 - self.data.y * scalor);
ctx.lineTo(canv.width / 2 - (self.data.x - i) * scalor, canv.height / 2 - (self.data.y - boundaryHeight) * scalor);
ctx.closePath();
ctx.stroke();
}
for (let i = 4; i < boundaryHeight; i+=4) {
ctx.beginPath();
ctx.moveTo(canv.width / 2 - self.data.x * scalor, canv.height / 2 - (self.data.y - i) * scalor);
ctx.lineTo(canv.width / 2 - (self.data.x - boundaryWidth) * scalor, canv.height / 2 - (self.data.y - i) * scalor);
ctx.closePath();
ctx.stroke();
}
ctx.fillStyle = "rgb(200, 200, 200)";
food.forEach(e => {
ctx.beginPath();
ctx.ellipse(
canv.width / 2 - (self.data.x - e.x) * scalor,
canv.height / 2 - (self.data.y - e.y) * scalor
, 0.1 * scalor, 0.1 * scalor, 0, 0, Math.PI*2
);
ctx.closePath();
ctx.fill();
});
ctx.fillStyle = "white";
ctx.beginPath();
ctx.ellipse(canv.width / 2, canv.height / 2, apparentSize, apparentSize, 0, 0, Math.PI*2);
ctx.closePath();
ctx.fill();
players.forEach(e => {
ctx.beginPath();
ctx.ellipse(
canv.width / 2 - (self.data.x - e.x) * scalor,
canv.height / 2 - (self.data.y - e.y) * scalor
, e.r * scalor, e.r * scalor, 0, 0, Math.PI*2
);
ctx.closePath();
ctx.fill();
});
self.data.update();
}

47
client/js/general.js Normal file
View File

@ -0,0 +1,47 @@
let canv = document.createElement("canvas");
canv.width = window.innerWidth;
canv.height = window.innerHeight;
canv.style.position = "absolute";
canv.style.left = "0px";
canv.style.top = "0px";
let ctx = canv.getContext("2d");
document.body.appendChild(canv);
document.addEventListener("mousemove", evt => {
var rect = canv.getBoundingClientRect();
mouse = {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
});
function constrain(x, y, z) {
return (x < y) ? y : ((x > z) ? z : x);
}
let food = Array(), players = Array(), self, mouse = {x: 0, y: 0};
let frames = 60, loop = setInterval(game, 1000/frames), boundaryWidth, boundaryHeight, startingSize, foodIncrease, sizeAdvantage;
let socket = io();
socket.on('connected', package => {
food = package.food;
players = package.pdata;
boundaryWidth = package.boundaryWidth;
boundaryHeight = package.boundaryHeight;
movingSpeed = package.movingSpeed;
foodIncrease = package.foodIncrease;
sizeAdvantage = package.sizeAdvantage;
self = {
data: new Player(
Math.floor(Math.random() * boundaryWidth-1)+1,
Math.floor(Math.random() * boundaryHeight-1)+1,
1
),
id: package.ownID
};
});
socket.on('agarioUpdatePlayers', package => {
food = package.food;
players = package.pdata;
});

62
client/js/player.js Normal file
View File

@ -0,0 +1,62 @@
class Player {
constructor(x = -1, y = -1, r = 1) {
this.x = (x != -1) ? x : Math.floor(Math.random() * boundaryWidth);
this.y = (y != -1) ? y : Math.floor(Math.random() * boundaryHeight);
this.r = r;
}
update() {
let data = {
id: self.id,
data: {
x: 0,
y: 0,
r: 1
},
foodEaten: Array(),
playersEaten: Array()
};
const move = {
x: mouse.x - canv.width / 2,
y: mouse.y - canv.height / 2
};
const mag = Math.sqrt(Math.pow(move.x, 2) + Math.pow(move.y, 2));
this.x = constrain(this.x + (move.x / mag) * movingSpeed, this.r, boundaryWidth - this.r);
this.y = constrain(this.y + (move.y / mag) * movingSpeed, this.r, boundaryHeight - this.r);
for (let i = 0; i < players.length; i++) {
let p = players[i];
if (Math.pow(p.x - this.x, 2) + Math.pow(p.y - this.y, 2) <= Math.pow(this.r, 2) && p.r < this.r * (1 - sizeAdvantage)) {
this.r += p.r;
data.playersEaten.push({ data: p, id: players[i].id, index: i });
} else if (Math.pow(p.x - this.x, 2) + Math.pow(p.y - this.y, 2) <= Math.pow(p.r, 2) && this.r < p.r * (1 - sizeAdvantage)) {
this.x = Math.floor(Math.random() * boundaryWidth);
this.y = Math.floor(Math.random() * boundaryHeight);
this.r = 1;
}
}
for (let i = food.length - 1; i >= 0; i--) {
let e = food[i], used = false;;
for (let j = 0; j < data.foodEaten.length; j++) {
let f = data.foodEaten[j];
if (f.coordinates.x == e.x && f.coordinates.y == e.y) {
used = true;
}
}
if (Math.pow(e.x - this.x, 2) + Math.pow(e.y - this.y, 2) <= Math.pow(this.r, 2) && !used) {
this.r += foodIncrease;
data.foodEaten.push({ coordinates: e, index: i });
food.splice(i, 1);
}
}
data.data.x = this.x;
data.data.y = this.y;
data.data.r = this.r;
socket.emit('agarioPlayerUpdate', data);
}
}

4019
server/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

22
server/package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.24.0",
"cheerio": "^1.0.0-rc.10",
"express": "^4.17.3",
"global-agent": "^3.0.0",
"nodemon": "^2.0.15",
"reddit-image-fetcher": "^2.0.10",
"reddit.images": "^1.0.7",
"socket.io": "^4.4.1"
}
}

109
server/server.js Normal file
View File

@ -0,0 +1,109 @@
const port = 3000;
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const app = express();
const clientPath = __dirname+'/../client';
console.log('Serving static from ' + clientPath);
app.use(express.static(clientPath));
const server = http.createServer(app);
const io = socketio(server);
server.on('error', err => {
console.error('Server error:', err);
});
server.listen(port, () => {
console.log('Server Started on Port '+port);
});
const boundaryWidth = 1600;
const boundaryHeight = 900;
const foodCount = 1000;
const movingSpeed = 0.1;
const foodIncrease = 0.1;
const sizeAdvantage = 0.1;
let agarioPlayers = Array(), agarioFood = Array();
for (let i = 0; i < foodCount; i++) {
agarioFood.push({
x: Math.floor(Math.random() * boundaryWidth-1)+1,
y: Math.floor(Math.random() * boundaryHeight-1)+1
});
}
io.on('connection', socket => {
let id = -1;
console.log("New Player has connected.");
while (true) {
let current = Math.floor(Math.random()*89+10);
let isUsed = false;
for (let i = 0; i < agarioPlayers.length; i++) {
if (agarioPlayers[i].uid == current) {
isUsed = true;
break;
}
}
if (!isUsed) {
id = current;
break;
}
}
if (id != -1) {
console.log('Handing ID', id);
let temp = Array();
agarioPlayers.forEach(p => {
if (p.data != null) {
temp.push(p.data);
}
});
socket.emit('connected', {pdata: temp, ownID: id, food: agarioFood, boundaryWidth, boundaryHeight, movingSpeed, foodIncrease, sizeAdvantage});
agarioPlayers.push({socket, id, data: null});
}
socket.on('disconnect', () => {
for (let i = 0; i < agarioPlayers.length; i++) {
if (agarioPlayers[i].socket == socket) {
console.log("Disconnecting AGARIO User. ID", agarioPlayers[i].id);
agarioPlayers.splice(i, 1);
break;
}
}
});
socket.on('agarioPlayerUpdate', data => {
for (let i = 0; i < agarioPlayers.length; i++) {
if (agarioPlayers[i].id == data.id) {
for (let j = 0; j < data.foodEaten.length; j++) {
for (let k = 0; k < agarioFood.length; k++) {
let f = data.foodEaten[j];
if (f.coordinates.x == agarioFood[k].x && f.coordinates.y == agarioFood[k].y) {
agarioFood.splice(k, 1);
agarioFood.push({
x: Math.floor(Math.random() * boundaryWidth-1)+1,
y: Math.floor(Math.random() * boundaryHeight-1)+1
});
break;
}
}
}
agarioPlayers[i].data = data.data;
let temp = Array();
agarioPlayers.forEach(p => {
if (p.data != null && p.id != agarioPlayers[i].id) {
temp.push(p.data);
}
});
agarioPlayers[i].socket.emit('agarioUpdatePlayers', { pdata: temp, food: agarioFood });
break;
}
}
});
});