43 Commits

Author SHA1 Message Date
trianta a754319374 Merge pull request 'core: reorganize some pieces for the future' (#10) from reorganize into main
Reviewed-on: Trianta/trianta.dev#10
2024-11-16 19:12:30 -06:00
Trianta 4aeed537d7 core: update package to v0.2.2 2024-11-16 19:01:52 -06:00
Trianta f725b4e922 core: propagate changes throughout files 2024-11-16 02:45:38 -06:00
Trianta 8252f38bb7 core: move home page css to root and rename 2024-11-16 02:38:15 -06:00
Trianta e4d9afdd68 pong: remove unused css file 2024-11-16 02:32:58 -06:00
Trianta 90c868d9e9 docker: add healthcheck 2024-11-09 04:37:30 -06:00
Trianta 452c35c1b0 snake: slightly increase speed 2024-11-09 03:18:42 -06:00
Trianta 9dea6014fe snake: remove dealt with comments 2024-11-09 02:57:12 -06:00
Trianta e37d737d3a snake: decrease speed again 2024-11-09 02:50:24 -06:00
Trianta 65c7b930e3 meta: update package.json 2024-11-09 02:38:08 -06:00
Trianta 082176d75e snake: revert speed back 2024-11-09 02:32:14 -06:00
Trianta 18673c95c4 snake: decrease speed slightly more 2024-11-09 02:25:16 -06:00
Trianta cb52bd1561 snake: slightly decrease game speed 2024-11-09 02:09:06 -06:00
trianta 0c196e690f Merge pull request 'snake: add some misc tweaks to game and page' (#8) from snake into main
Reviewed-on: Trianta/trianta.dev#8
2024-11-09 01:42:25 -06:00
Trianta 532d8a480c snake: decrease timeout to make it faster 2024-11-09 01:39:46 -06:00
Trianta 92cfd954c1 snake: display information about game in stats 2024-11-09 01:39:16 -06:00
trianta 3bbc9701c5 Merge pull request 'core: add Snake using Typescript' (#7) from snake into main
Reviewed-on: Trianta/trianta.dev#7
2024-11-09 01:26:09 -06:00
Trianta 3699ed465b snake: not adding last piece was correct, bool check was not 2024-11-09 01:23:11 -06:00
Trianta b6fe678367 snake: add snake, but ignore when counting length left 2024-11-09 01:20:05 -06:00
Trianta f59e7cf747 snake: don't add the snake itself to path 2024-11-09 01:10:24 -06:00
Trianta 33d5c491c2 snake: move extraneous logging to debug 2024-11-05 21:23:34 -06:00
Trianta de80157f2e snake: revert last commit and minor cleanups 2024-11-05 21:21:16 -06:00
Trianta 86ef363d3b snake: attempt to move path refresh to post-regen 2024-11-05 21:11:46 -06:00
Trianta 727e13a1c9 snake: minor adjustment to trimming logic 2024-11-05 20:59:27 -06:00
Trianta acc1e2b042 snake: add enums for bit states 2024-11-05 20:08:55 -06:00
Trianta 2916b4e127 snake: clear path on reset 2024-11-05 18:26:53 -06:00
Trianta 61f59208a0 snake: rename global variables 2024-11-05 18:17:44 -06:00
Trianta 7f23f4a40c snake: add stashed changes 2024-11-05 17:29:07 -06:00
Trianta 457c7a22fa jenkins: remove old container instead of just stopping if exists 2024-09-17 20:12:37 -05:00
Trianta 6667a2f79b jenkins: remove old container if exists 2024-09-17 20:11:37 -05:00
Trianta de5f4c5eaf docker: fix directories when in container 2024-09-17 20:03:26 -05:00
Trianta 2967663a82 docker: correctly set up image 2024-09-17 19:49:49 -05:00
Trianta 477ce1461e docker: build before deploy 2024-09-17 19:20:32 -05:00
Trianta 3641cfd836 docker: deploy website to nginx dir 2024-09-17 19:19:15 -05:00
Trianta bda79f51a6 jenkins: add name to container on deploy 2024-09-17 19:14:33 -05:00
Trianta 98149cae07 docker: use nginx internal port 2024-09-17 19:09:54 -05:00
Trianta e0c09898ae jenkins: detach container on deploy 2024-09-17 19:03:13 -05:00
Trianta 0e34226b43 docker: use base image with apt 2024-09-17 18:43:39 -05:00
Trianta 4f6fbc4196 docker: use npm for Typescript stuff 2024-09-17 18:40:58 -05:00
trianta b8198afd25 dockerfile: use nginx to host static site 2024-09-17 14:51:59 -05:00
trianta 08645727a8 docker: remove start script 2024-09-17 02:53:37 -05:00
trianta c317e7ab08 npm: add build script 2024-09-17 02:51:23 -05:00
trianta 4f41b247ba jenkins: fix deploy 2024-09-17 02:46:51 -05:00
8 changed files with 124 additions and 136 deletions
+13 -3
View File
@@ -1,4 +1,6 @@
FROM node:14-alpine
FROM nginx:latest
RUN apt-get update && apt-get install -y nodejs npm netcat-openbsd
WORKDIR /app
@@ -10,6 +12,14 @@ COPY . .
RUN npm run build
EXPOSE 3466
WORKDIR /usr/share/nginx/html
COPY index.html .
COPY default.css .
CMD ["npm", "start"]
RUN mkdir -p /usr/share/nginx/html/public
RUN cp /app/build/* ./public
HEALTHCHECK CMD nc -z 127.0.0.1 80
EXPOSE 80
Vendored
+4 -1
View File
@@ -14,7 +14,10 @@ pipeline {
}
}
stage('Deploy') {
sh 'docker run -p 3466:3466 test.trianta.dev'
steps {
sh 'docker stop test && docker rm test || exit 0'
sh 'docker run -d -p 3466:80 --name test test.trianta.dev:latest'
}
}
}
}
View File
+5 -4
View File
@@ -3,7 +3,7 @@
<head>
<meta charset="utf-8"/>
<title>Trianta</title>
<link rel="stylesheet" href="src/home.css" />
<link rel="stylesheet" href="default.css" />
</head>
<body>
<div id="navigation">
@@ -15,7 +15,7 @@
<div class="card">
<h2 class="cardTop">Pong</h2>
<canvas width="750" height="585" id="pong"></canvas>
<script src="build/pong.js"></script>
<script src="public/pong.js"></script>
<div id="gameover" hidden="true">
<h1>Game Over</h1>
</div>
@@ -29,8 +29,9 @@
<div class="card">
<h2 class="cardTop">Snake</h2>
<canvas width="625" height="375" id="snake"></canvas>
<script src="build/snake.js"></script>
<p class="cardStats">Bottom Text<span></span></p>
<script src="public/snake.js"></script>
<p class="cardStats">Snake Head Location: <span id="snakeHead"></span></p>
<p class="cardStats">Food Location: <span id="snakeFood"></span></p>
<a class="navItem" href="https://lab.trianta.dev/Trianta/trianta.dev/src/branch/main/src/snake.ts">View Code</a>
</div>
</div>
+5 -5
View File
@@ -1,21 +1,21 @@
{
"name": "trianta.dev",
"version": "0.1.0",
"version": "0.2.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "trianta.dev",
"version": "0.1.0",
"version": "0.2.2",
"license": "MIT",
"dependencies": {
"typescript": "^5.5.4"
}
},
"node_modules/typescript": {
"version": "5.5.4",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz",
"integrity": "sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==",
"version": "5.6.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz",
"integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==",
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
+2 -1
View File
@@ -1,9 +1,10 @@
{
"name": "trianta.dev",
"version": "0.1.0",
"version": "0.2.2",
"description": "My personal website",
"main": "index.html",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
-23
View File
@@ -1,23 +0,0 @@
html, body {
height: 100%;
margin: 0;
}
body {
background: black;
display: flex;
align-items: center;
justify-content: center;
}
.button {
background: blue;
color: white;
padding: 15px 20px;
font-size: 32px;
font-family: Arial, Helvetica, sans-serif;
text-transform: uppercase;
cursor: pointer;
position: relative;
}
+95 -99
View File
@@ -1,9 +1,8 @@
/*
bits:
0 = snake
1 = food
2 = checked (for searching algorithms)
*/
enum BoardState {
SNAKE = 0,
FOOD = 1,
CHECKED = 2 // For search algorithms
}
// Bit functions for ease
function isBitSet(value: number, bit: number): boolean {
@@ -56,25 +55,22 @@ class SnakeCore {
height: number;
board: number[][];
body: Point[];
food: Point;
gameover: boolean;
foodAte: boolean;
constructor() {
// TODO: Add CSS stuff for page
this.canvas = document.getElementById('snake') as HTMLCanvasElement;
this.context = this.canvas.getContext('2d') as CanvasRenderingContext2D;
this.grid = 25; // size of grid squares
this.timeout = 100; // speed in ms
this.timeout = 32; // speed in ms
this.width = 25;
this.height = 15;
this.board = [];
for (let i = 0; i < this.height; i++) {
for (let i = 0; i < this.height; i++)
this.board.push(new Array(this.width));
for (let j = 0; j < this.width; j++) {
this.board[i][j] = 0;
}
}
this.body = [];
this.food = new Point();
this.gameover = false;
this.foodAte = true;
this.body.push(new Point(12, 8));
@@ -86,30 +82,38 @@ class SnakeCore {
}
reset() {
snake.gameover = false;
snake.foodAte = true;
console.debug("[TRACE] Reset was triggered");
this.gameover = false;
this.foodAte = true;
for (let i = 0; i < this.height; i++) {
for (let j = 0; j < this.width; j++) {
this.board[i][j] = 0;
}
}
snake.context.fillStyle = "black";
for (let i = 0; i < snake.height; i++) {
for (let j = 0; j < snake.width; j++) {
snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid);
}
}
while (this.body.length > 0)
this.body.pop();
while (g_snakebot.path.length > 0)
g_snakebot.path.pop();
this.startRandom();
this.foodRegen();
this.draw();
}
startRandom() {
this.body.push(new Point(Math.floor(Math.random() * this.width), Math.floor(Math.random() * this.height)));
}
foodRegen() {
if (!this.foodAte)
return;
console.debug("[TRACE] Food reset was triggered");
this.foodAte = false;
while (true) {
let tmp = new Point(Math.floor(Math.random() * this.width), Math.floor(Math.random() * this.height));
if (isBitSet(this.board[tmp.y][tmp.x], 0))
if (isBitSet(this.board[tmp.y][tmp.x], BoardState.SNAKE))
continue;
this.board[tmp.y][tmp.x] = bitSet(this.board[tmp.y][tmp.x], 1);
this.board[tmp.y][tmp.x] = bitSet(this.board[tmp.y][tmp.x], BoardState.FOOD);
this.food.copy(tmp);
return;
}
}
@@ -117,49 +121,54 @@ class SnakeCore {
// Simulate game logic
simulate() {
// Move snake
// TODO: Fix teleporting snake
let next: Point = new Point;
next.copy(bot.nextMove());
if (next.x < 0 || next.x > snake.width || next.y < 0 || next.y > snake.height) {
snake.gameover = true;
next.copy(g_snakebot.nextMove());
if (next.x < 0 || next.x > g_snake.width || next.y < 0 || next.y > g_snake.height) {
g_snake.gameover = true;
return;
}
if (isBitSet(snake.board[next.y][next.x], 0) && (snake.body.length > 1)) {
snake.gameover = true; // Game should end (Snake touching snake)
if (isBitSet(g_snake.board[next.y][next.x], BoardState.SNAKE) && (g_snake.body.length > 1)) {
g_snake.gameover = true; // Game should end (Snake touching snake)
return;
}
snake.board[next.y][next.x] = bitSet(snake.board[next.y][next.x], 0);
snake.body.push(next);
if (!isBitSet(snake.board[next.y][next.x], 1)) {
let old: Point = snake.body.shift() as Point;
snake.board[old.y][old.x] = bitClear(snake.board[old.y][old.x], 0);
g_snake.board[next.y][next.x] = bitSet(g_snake.board[next.y][next.x], BoardState.SNAKE);
g_snake.body.push(next);
if (!isBitSet(g_snake.board[next.y][next.x], BoardState.FOOD)) {
let old: Point = g_snake.body.shift() as Point;
g_snake.board[old.y][old.x] = bitClear(g_snake.board[old.y][old.x], BoardState.SNAKE);
} else {
snake.board[next.y][next.x] = bitClear(snake.board[next.y][next.x], 1);
snake.foodAte = true;
while (bot.path.length > 0)
bot.path.pop();
g_snake.board[next.y][next.x] = bitClear(g_snake.board[next.y][next.x], BoardState.FOOD);
g_snake.foodAte = true;
while (g_snakebot.path.length > 0)
g_snakebot.path.pop();
}
}
// Draw game to canvas
draw() {
// Clear the screen
snake.context.clearRect(0, 0, snake.canvas.width, snake.canvas.height);
g_snake.context.clearRect(0, 0, g_snake.canvas.width, g_snake.canvas.height);
// Draw game
for (let i = 0; i < snake.height; i++) {
for (let j = 0; j < snake.width; j++) {
if (isBitSet(snake.board[i][j], 0))
snake.context.fillStyle = "green";
else if (isBitSet(snake.board[i][j], 1))
snake.context.fillStyle = "red";
for (let i = 0; i < g_snake.height; i++) {
for (let j = 0; j < g_snake.width; j++) {
if (isBitSet(g_snake.board[i][j], BoardState.SNAKE))
g_snake.context.fillStyle = "green";
else if (isBitSet(g_snake.board[i][j], BoardState.FOOD))
g_snake.context.fillStyle = "red";
else
snake.context.fillStyle = "black";
snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid);
g_snake.context.fillStyle = "black";
g_snake.context.fillRect(j * g_snake.grid, i * g_snake.grid, g_snake.grid, g_snake.grid);
}
}
}
// Update text on page to match game
updatePageText() {
document.getElementById("snakeHead").innerHTML = "x: " + this.body[this.body.length - 1].x + " y: " + this.body[this.body.length - 1].y;
document.getElementById("snakeFood").innerHTML = "x: " + this.food.x + " y: " + this.food.y ;
}
}
class Bot {
@@ -172,10 +181,10 @@ class Bot {
}
bfs() {
var search: Point[] = [snake.head];
var search: Point[] = [g_snake.head];
while (search.length !== 0) {
let current: Point = search.shift() as Point;
if (isBitSet(snake.board[current.y][current.x], 2))
if (isBitSet(g_snake.board[current.y][current.x], BoardState.CHECKED))
continue;
this.pathUntrimmed.push(current);
let locals: Point[] = new Array(4);
@@ -188,104 +197,91 @@ class Bot {
locals[2].y -= 1;
locals[3].x -= 1;
for (const local of locals) {
if (local.x < 0 || local.x > snake.width - 1 || local.y < 0 || local.y > snake.height - 1) {
if (local.x < 0 || local.x > g_snake.width - 1 || local.y < 0 || local.y > g_snake.height - 1)
continue;
}
let value = snake.board[local.y][local.x];
if (isBitSet(value, 1)) {
let value = g_snake.board[local.y][local.x];
if (isBitSet(value, BoardState.FOOD)) {
this.pathUntrimmed.push(local);
return;
}
if (isBitSet(value, 2))
continue;
if (isBitSet(value, 0))
if (isBitSet(value, BoardState.CHECKED) || isBitSet(value, BoardState.SNAKE))
continue;
search.push(local);
}
snake.board[current.y][current.x] = bitSet(snake.board[current.y][current.x], 2);
g_snake.board[current.y][current.x] = bitSet(g_snake.board[current.y][current.x], BoardState.CHECKED);
}
}
// TODO: Fix trim function
trim() {
let reachedSnake = false;
this.path.push(this.pathUntrimmed.pop() as Point); // Push food location
while (this.pathUntrimmed.length !== 0) {
if (!reachedSnake) {
let location: Point = new Point;
location.copy(this.pathUntrimmed[this.pathUntrimmed.length - 1]);
if (isBitSet(snake.board[location.y][location.x], 0))
reachedSnake = true;
var delta = new Point;
delta = location.subtract(this.path[this.path.length - 1]);
if ((Math.abs(delta.x) + Math.abs(delta.y)) === 1)
this.path.push(location);
let location: Point = this.pathUntrimmed.pop();
if (reachedSnake)
continue;
if (isBitSet(g_snake.board[location.y][location.x], BoardState.SNAKE)) {
reachedSnake = true;
continue;
}
this.pathUntrimmed.pop();
var delta = new Point;
delta = location.subtract(this.path[this.path.length - 1]);
if ((Math.abs(delta.x) + Math.abs(delta.y)) === 1)
this.path.push(location);
}
}
unvisit() {
for (let i = 0; i < snake.height; i++) {
for (let j = 0; j < snake.width; j++) {
snake.board[i][j] = bitClear(snake.board[i][j], 2);
for (let i = 0; i < g_snake.height; i++) {
for (let j = 0; j < g_snake.width; j++) {
g_snake.board[i][j] = bitClear(g_snake.board[i][j], BoardState.CHECKED);
}
}
}
/*
values:
0 = left
1 = up
2 = right
4 = down
*/
nextMove() {
// Get new path to food
if (this.path.length === 0)
this.pathRefresh();
var next: Point = new Point;
next.copy(this.path.pop());
var delta = new Point;
delta = next.subtract(snake.head);
if (delta.x > 1) {
delta = next.subtract(g_snake.head);
if (delta.x > 1)
console.log("[ERR] delta.x > 1");
} else if (delta.x < -1) {
else if (delta.x < -1)
console.log("[ERR] delta.x < 1");
}
if (delta.y > 1) {
if (delta.y > 1)
console.log("[ERR] delta.y > 1");
} else if (delta.y < -1) {
else if (delta.y < -1)
console.log("[ERR] delta.y < 1");
}
return next;
}
autoplay() {
if (this.path.length > 0)
return;
pathRefresh() {
this.bfs();
this.trim();
this.unvisit();
}
}
const snake: SnakeCore = new SnakeCore(); // Singleton for snake game
const bot: Bot = new Bot(); // Singleton for bot playing
const g_snake: SnakeCore = new SnakeCore();
const g_snakebot: Bot = new Bot();
// game loop
function snakeloop() {
// Reset of needed
if (snake.gameover)
snake.reset();
// Input
bot.autoplay();
if (g_snake.gameover)
g_snake.reset();
snake.simulate();
// Simulate movement of snake
g_snake.simulate();
// Regenerate food if needed
snake.foodRegen();
g_snake.foodRegen();
snake.draw();
g_snake.draw();
g_snake.updatePageText();
}
// start the game
setInterval(snakeloop, snake.timeout);
setInterval(snakeloop, g_snake.timeout);