ratpoison

This commit is contained in:
Arjun S
2025-08-04 11:55:18 +00:00
parent 0e1eb369d5
commit 6136ba8d8d
4 changed files with 99 additions and 31 deletions

View File

@@ -305,9 +305,25 @@ export default class GameServer implements Party.Server {
const attackerUserState = this.gameState.users[authenticatedUserId];
if (!targetUserState || !attackerUserState || targetUserId === authenticatedUserId) {
console.log(`Rat Poison: Attempt to poison self or non-existent user (${targetUserId}). Aborting.`);
return; // Cannot poison self or non-existent user
}
// Check if the target user is currently online
if (!this.userConnections.has(targetUserId)) {
const attackerConn = this.userConnections.get(authenticatedUserId);
if (attackerConn) {
attackerConn.send(JSON.stringify({
type: 'rat-poison-feedback',
message: `${targetUserState.name} is currently offline and cannot be targeted.`
}));
}
console.log(`Rat Poison: Target user ${targetUserState.name} (${targetUserId}) is offline. Attacker: ${attackerUserState.name} (${authenticatedUserId}).`);
return;
}
const now = Date.now();
if (targetUserState.ratPoisonImmunityUntil && targetUserState.ratPoisonImmunityUntil > now) {
// Target is immune, notify attacker
@@ -318,6 +334,7 @@ export default class GameServer implements Party.Server {
message: `${targetUserState.name} is currently immune to rat poison!`
}));
}
console.log(`Rat Poison: Target user ${targetUserState.name} (${targetUserId}) is immune until ${new Date(targetUserState.ratPoisonImmunityUntil).toISOString()}. Attacker: ${attackerUserState.name} (${authenticatedUserId}).`);
return;
}
@@ -325,7 +342,7 @@ export default class GameServer implements Party.Server {
const newsUpgrade = ALL_UPGRADES.find(u => u.id === 'news');
const newsTitles = newsUpgrade?.newsTitles || [];
if (newsTitles.length === 0) {
console.warn('No news titles found for rat poison challenge.');
console.warn('Rat Poison: No news titles found for rat poison challenge.');
return;
}
@@ -337,6 +354,9 @@ export default class GameServer implements Party.Server {
challengeString,
expiresAt,
};
targetUserState.ratPoisonImmunityUntil = expiresAt; // User is immune for the duration of the challenge
console.log(`Rat Poison: Challenge issued to ${targetUserState.name} (${targetUserId}). String: "${challengeString}", Expires: ${new Date(expiresAt).toISOString()}.`);
// Notify target user to start challenge
const targetConn = this.userConnections.get(targetUserId);
@@ -352,6 +372,7 @@ export default class GameServer implements Party.Server {
setTimeout(() => {
if (targetUserState.ratPoisonChallenge && targetUserState.ratPoisonChallenge.expiresAt === expiresAt) {
// Challenge not solved in time
console.log(`Rat Poison: Challenge for ${targetUserState.name} (${targetUserId}) expired.`);
this.applyRatPoisonPenalty(targetUserId);
this.broadcast();
}
@@ -363,12 +384,14 @@ export default class GameServer implements Party.Server {
handleSolveRatPoison(data: SolveRatPoisonMessage, authenticatedUserId: string) {
const userState = this.gameState.users[authenticatedUserId];
if (!userState || !userState.ratPoisonChallenge) {
console.log(`Rat Poison: No active challenge for user ${authenticatedUserId}. Aborting solve attempt.`);
return; // No active challenge
}
const now = Date.now();
if (now > userState.ratPoisonChallenge.expiresAt) {
// Challenge expired
console.log(`Rat Poison: User ${authenticatedUserId} attempted to solve expired challenge.`);
this.applyRatPoisonPenalty(authenticatedUserId);
} else if (data.challengeString === userState.ratPoisonChallenge.challengeString) {
// Challenge solved successfully
@@ -381,8 +404,10 @@ export default class GameServer implements Party.Server {
message: 'Challenge solved! You are immune for 2 minutes.'
}));
}
console.log(`Rat Poison: User ${authenticatedUserId} successfully solved challenge. Immunity until ${new Date(userState.ratPoisonImmunityUntil).toISOString()}.`);
} else {
// Challenge failed (wrong string)
console.log(`Rat Poison: User ${authenticatedUserId} failed challenge (incorrect string).`);
this.applyRatPoisonPenalty(authenticatedUserId);
const userConn = this.userConnections.get(authenticatedUserId);
if (userConn) {
@@ -400,11 +425,15 @@ export default class GameServer implements Party.Server {
applyRatPoisonPenalty(userId: string) {
const userState = this.gameState.users[userId];
if (!userState) return;
if (!userState) {
console.log(`Rat Poison: Attempted to apply penalty to non-existent user ${userId}.`);
return;
}
const clicksLost = Math.floor(userState.clicks * 0.30);
userState.clicks -= clicksLost;
userState.ratPoisonImmunityUntil = Date.now() + (20 * 1000); // 20 seconds immunity
console.log(`Rat Poison: Penalty applied to ${userState.name} (${userId}). Lost ${clicksLost} clicks. Immunity until ${new Date(userState.ratPoisonImmunityUntil).toISOString()}.`);
const userConn = this.userConnections.get(userId);
if (userConn) {