Skip to content

Commit

Permalink
new stream - new rework!
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 committed Mar 22, 2024
1 parent 8645db8 commit 74cc2cb
Show file tree
Hide file tree
Showing 23 changed files with 3,649 additions and 3,172 deletions.
55 changes: 39 additions & 16 deletions apps/api/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
donateItemsFromPlayerHands,
findOrCreatePlayer,
findTreeToChop,
setPlayerChopping,
setTreeInProgress,
reserveTree,
sellItemsFromPlayerHands,
setPlayerMovingToTarget,
} from "./db.repository.ts";

export async function serveBot() {
Expand Down Expand Up @@ -44,7 +45,7 @@ export async function serveBot() {
createBotCommand(
"рубить",
async (params, { userId, userName, reply }) => {
console.log(userId, userName, params);
console.log("рубить", userId, userName, params);

const player = await findOrCreatePlayer({
twitchId: userId,
Expand All @@ -65,24 +66,19 @@ export async function serveBot() {
return;
}

// Send player to chop
const x = tree.x;
const y = tree.y;

await setPlayerChopping({ id: player.id, x, y });

// Working time
await setTreeInProgress(tree.id);
await reserveTree(tree.id);

await createCommand({
playerId: player.id,
command: "!рубить",
target: tree.id,
// Send player to chop
await setPlayerMovingToTarget({
id: player.id,
targetId: tree.id,
x: tree.x,
y: tree.y,
});
},
),
createBotCommand("дар", async (params, { userId, userName, reply }) => {
console.log(userId, userName, params);
console.log("дар", userId, userName, params);

const player = await findOrCreatePlayer({
twitchId: userId,
Expand All @@ -105,6 +101,33 @@ export async function serveBot() {
`${userName}, ты подарил деревне ресурсы, которые у тебя были в руках!`,
);
}),
createBotCommand(
"продать",
async (params, { userId, userName, reply }) => {
console.log("продать", userId, userName, params);

const player = await findOrCreatePlayer({
twitchId: userId,
userName,
});
if (!player || player.handsItemAmount === 0) {
// No way
void reply(`${userName}, руки твои пусты.`);
return;
}

await sellItemsFromPlayerHands(player.id);

await createCommand({
playerId: player.id,
command: "!продать",
});

void reply(
`${userName}, ты продал ресурсы торговцу, которые у тебя были в руках!`,
);
},
),
],
});

Expand Down
165 changes: 152 additions & 13 deletions apps/api/src/db.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function createPlayer(dto: { twitchId: string; userName: string }) {
id: createId(),
twitchId: dto.twitchId,
userName: dto.userName,
x: 600,
x: -100,
y: 600,
colorIndex,
},
Expand Down Expand Up @@ -43,18 +43,17 @@ export async function findOrCreatePlayer({
}

export function findActivePlayers() {
// Active = 10 minutes?
const time = new Date();
const milliseconds = 10 * 60 * 1000;
const lastActionAt = new Date(time.getTime() - milliseconds);
// Active = 20 minutes?
//const time = new Date();
//const milliseconds = 20 * 60 * 1000;
//const lastActionAt = new Date(time.getTime() - milliseconds);

return db.player.findMany({
where: { lastActionAt: { gte: lastActionAt } },
});
return db.player.findMany();
}

export function findTopByReputationPlayers() {
return db.player.findMany({
where: { reputation: { gt: 0 } },
orderBy: { reputation: "desc" },
take: 10,
});
Expand Down Expand Up @@ -85,22 +84,36 @@ export function findCommands() {
});
}

export function findTree(id: string) {
return db.tree.findUnique({ where: { id } });
}

export function findTrees() {
return db.tree.findMany();
}

export function findTreeToChop() {
return db.tree.findFirst({
where: {
size: { gte: 80 },
size: { gte: 50 },
inProgress: false,
isReserved: false,
},
orderBy: {
progressFinishAt: "asc",
},
});
}

export function reserveTree(id: string) {
return db.tree.update({
where: { id },
data: {
isReserved: true,
},
});
}

export function updateTree(dto: { id: string; size: number }) {
return db.tree.update({
where: { id: dto.id },
Expand Down Expand Up @@ -170,8 +183,20 @@ export function setPlayerMadeAction(playerId: string) {
});
}

export async function setPlayerChopping(dto: {
export function clearPlayerTarget(playerId: string) {
return db.player.update({
where: { id: playerId },
data: {
targetX: null,
targetY: null,
targetId: null,
},
});
}

export async function setPlayerMovingToTarget(dto: {
id: string;
targetId: string;
x: number;
y: number;
}) {
Expand All @@ -180,9 +205,69 @@ export async function setPlayerChopping(dto: {
return db.player.update({
where: { id: dto.id },
data: {
x: dto.x,
y: dto.y,
isBusy: true,
targetX: dto.x,
targetY: dto.y,
targetId: dto.targetId,
isBusy: true, // running?
lastActionAt: new Date(),
},
});
}

export async function setPlayerIsOnTarget(playerId: string) {
const player = await db.player.findUnique({
where: { id: playerId },
});
if (!player || !player.targetId || !player.targetX || !player.targetY) {
return null;
}

// If target is outScreen
if (player.targetId === "0") {
await setPlayerCoordinates(player.id, player.targetX, player.targetY);
await clearPlayerTarget(player.id);
await setPlayerNotBusy(player.id);
return;
}

// Find target
const tree = await findTree(player.targetId);
if (!tree) {
return null;
}

// After - will chop
await setPlayerCoordinates(player.id, tree.x, tree.y);
await setPlayerChopping(player.id);

// Working time
await setTreeInProgress(tree.id);

await createCommand({
playerId: player.id,
command: "!рубить",
target: tree.id,
});
}

export function setPlayerCoordinates(id: string, x: number, y: number) {
return db.player.update({
where: { id },
data: {
x,
y,
},
});
}

export async function setPlayerChopping(id: string) {
await setPlayerMadeAction(id);
await clearPlayerTarget(id); // now player is on target

return db.player.update({
where: { id },
data: {
isBusy: true, // chopping?
lastActionAt: new Date(),
},
});
Expand Down Expand Up @@ -268,6 +353,38 @@ export async function donateItemsFromPlayerHands(playerId: string) {
});
}

export async function sellItemsFromPlayerHands(playerId: string) {
const player = await db.player.findUnique({
where: { id: playerId },
});

// Check hands?
if (!player) {
return null;
}

// + coins for every resource
await db.player.update({
where: { id: playerId },
data: {
coins: {
increment: player.handsItemAmount,
},
},
});

await setPlayerMadeAction(playerId);

// Clear hands
return db.player.update({
where: { id: playerId },
data: {
handsItemType: null,
handsItemAmount: 0,
},
});
}

function setPlayerNotBusy(playerId: string) {
return db.player.update({
where: { id: playerId },
Expand All @@ -277,6 +394,27 @@ function setPlayerNotBusy(playerId: string) {
});
}

export async function findInactivePlayers() {
// Active = 10 minutes?
const time = new Date();
const milliseconds = 10 * 60 * 1000;
const lastActionAt = new Date(time.getTime() - milliseconds);

const outScreenX = -100;

const players = await db.player.findMany({
where: { lastActionAt: { lt: lastActionAt }, x: { not: outScreenX } },
});
for (const player of players) {
await setPlayerMovingToTarget({
id: player.id,
targetId: "0",
x: -100,
y: 600,
});
}
}

export async function findCompletedTrees() {
const trees = await db.tree.findMany({
where: {
Expand Down Expand Up @@ -308,6 +446,7 @@ export async function findCompletedTrees() {
await db.tree.update({
where: { id: tree.id },
data: {
isReserved: false,
inProgress: false,
resource: 0,
size: 0,
Expand Down
12 changes: 12 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
findTreeToChop,
findTrees,
findVillage,
setPlayerIsOnTarget,
updatePlayer,
updateTree,
} from "./db.repository.ts";
import { servePlayer } from "./player.ts";
import { serveTree } from "./tree.ts";

const app = new Hono();
Expand Down Expand Up @@ -49,6 +51,15 @@ app.patch("players/:id", async (c) => {
ok: true,
});
});
app.post("players/:id/target", async (c) => {
const id = c.req.param("id");

await setPlayerIsOnTarget(id);

return c.json({
ok: true,
});
});
app.get("/players/top", async (c) => {
const players = await findTopByReputationPlayers();

Expand Down Expand Up @@ -86,6 +97,7 @@ const port = 4001;
console.log(`Server is running on port ${port}`);

void serveBot();
void servePlayer();
void serveTree();

serve({
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { findInactivePlayers } from "./db.repository.ts";

export async function servePlayer() {
// Move inactive players
setInterval(() => {
void findInactivePlayers();
}, 5000);
}
Binary file added apps/client/public/creatures/rabbit1_left_64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/client/public/creatures/rabbit1_right_64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/client/public/creatures/wolf1_left_64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/client/public/creatures/wolf1_right_64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed apps/client/public/wolf.png
Binary file not shown.
Loading

0 comments on commit 74cc2cb

Please sign in to comment.