-
Notifications
You must be signed in to change notification settings - Fork 1
/
logic.ts
202 lines (182 loc) · 6.49 KB
/
logic.ts
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
192
193
194
195
196
197
198
199
200
201
202
import { RuneClient } from "rune-games-sdk";
import { GameActions, GameState, LimbEnum } from "./types/types";
import { generateCardStack } from "./util/generateCardStack";
import { getPlayerState } from "./util/getPlayerState";
import { getWinners } from "./util/getWinner";
declare global {
const Rune: RuneClient<GameState, GameActions>;
}
const playerColors: string[] = [
"bg-ronchi",
"bg-willpower-orange",
"bg-vivid-raspberry",
"bg-blue-purple",
];
const attractedCardPositions: string[] = [
"fixed left-0 top-0 h-[20vw] w-[16vw] ",
"fixed right-0 top-0 h-[20vw] w-[16vw] ",
"fixed left-0 bottom-[27%] h-[20vw] w-[16vw] ",
"fixed right-0 bottom-[27%] h-[20vw] w-[16vw] ",
];
const generatedCardStack = generateCardStack(10);
Rune.initLogic({
updatesPerSecond: 30,
minPlayers: 4,
maxPlayers: 4,
setup: (playerIds): GameState => {
return {
gameOver: false,
testActionTriggered: false,
count: 0,
currentPlayerIndex: 0,
remainingTime: 60,
progress: 0,
currentRound: 1,
cardStack: generatedCardStack,
attractActive: false,
activeCard: generatedCardStack[0],
activeCardIndex: 0,
roundOver: false,
winner: null,
timeInSeconds: 0,
players: playerIds.map((playerId, i) => ({
key: playerId,
playerId: playerId,
playerColor: playerColors[i],
attractedCardPosition: attractedCardPositions[i],
limbs: [1, 1, 1, 1],
controlsOrder: ["Left Arm", "Left Leg", "Right Leg", "Right Arm"],
scoreForRound: 0,
totalScore: 0,
correctStreak: 0,
autoLimb: false,
attract: false,
win: false,
})),
};
},
actions: {
/* FIRST ARGUMENT IS A PAYLOAD, USE "_", WHEN PAYLOAD ISNT REQUIRED AND YOU STILL WANT TO ACCESS THE SECOND ARGUMENT. AS A SECOND ARGUMENT, EACH ACTION GETS ACCESS TO AN OBJECT CONTAINING THE CURRENT GAME STATE, THE PLAYER ID OF THE PLAYER INITIATING THE ACTION, AND AN ARRAY OF ALL PLAYER IDS. */
// THIS ACTION SHUFFLES THE ORDER OF THE CONTROLS FOR ALL PLAYERS EXCEPT THE PLAYER WHO INITIATED THE ACTION
shuffleEnemyControls: (_, { game, playerId: initiatingPlayerId }) => {
game.players.forEach((player) => {
if (player.playerId !== initiatingPlayerId) {
const possibleLimbs = [
"Right Arm",
"Left Leg",
"Left Arm",
"Right Leg",
];
for (let i = 2; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[possibleLimbs[i], possibleLimbs[j]] = [
possibleLimbs[j],
possibleLimbs[i],
];
}
player.controlsOrder = possibleLimbs;
}
});
},
// THIS ACTION ATTRACTS THE CARD TO THE PLAYER WHO INITIATED THE ACTION. IT CAN OVERRIDE EXISTING ATTRACT EFFECTS ON OTHER PLAYERS
toggleAttract(_, { game, playerId: initiatingPlayerId }) {
game.players.forEach((player) => {
player.attract = false;
});
const initiatingPlayer = getPlayerState(game, initiatingPlayerId);
initiatingPlayer.attract = true;
game.attractActive = true;
},
// THIS ACTION SETS A SINGLE LIMB TO THE CORRECT POSITION FOR THE PLAYER WHO INITIATED THE ACTION AND SETS THE AUTO LIMB PROPERTY TO TRUE
toggleAutoLimb: (
{ activeCardIndex },
{ game, playerId: initiatingPlayerId },
) => {
const initiatingPlayer = getPlayerState(game, initiatingPlayerId);
initiatingPlayer.autoLimb = true;
initiatingPlayer.limbs[LimbEnum.LeftArm] =
game.cardStack[activeCardIndex].limbs[LimbEnum.LeftArm];
},
// THIS ACTION SUBTRACTS A PASSED IN COST FROM THE CORRECT STREAK OF THE PLAYER WHO INITIATED THE ACTION
resetStreak: (_, { game, playerId: initiatingPlayerId }) => {
const initiatingPlayer = getPlayerState(game, initiatingPlayerId);
initiatingPlayer.correctStreak = 0;
},
// THIS ACTION CHANGES THE POSITION OF THE LIMB FOR THE PLAYER WHO INITIATED THE ACTION
toggleLimb: ({ limb }, { game, playerId: initiatingPlayerId }) => {
const player = getPlayerState(game, initiatingPlayerId);
player.limbs[limb] = (player.limbs[limb] % 3) + 1;
},
},
events: {
playerJoined: () => {
// Handle player joined
},
playerLeft() {
// Handle player left
},
},
// THIS UPDATE FUNCTION RUNS EVERY 1 SECOND AND UPDATES THE REMAINING TIME PROPERTY OF THE GAME STATE. IF THE REMAINING TIME IS 0, THE GAMEOVER PROPERTY IS SET TO TRUE AND THE WINNER PROPERTY IS SET TO THE PLAYER WITH THE HIGHEST SCORE
update: ({ game }) => {
game.progress = Rune.gameTime();
game.remainingTime = 60000 - game.progress;
game.gameOver = game.remainingTime <= 0;
const newTimeInSeconds = Math.floor(game.progress / 1000);
game.roundOver =
newTimeInSeconds !== 0 &&
newTimeInSeconds % 6 === 0 &&
newTimeInSeconds !== game.timeInSeconds &&
game.progress > 0;
const roundCleanup = () => {
game.players.forEach((player) => {
player.scoreForRound = player.limbs.reduce(
(totalPointsForRound, limbPose, i) => {
const poseMatchesCard =
limbPose === game.cardStack[game.activeCardIndex]?.limbs[i];
return poseMatchesCard
? totalPointsForRound + 1
: totalPointsForRound;
},
0,
);
player.totalScore = player.totalScore + player.scoreForRound;
player.totalScore + player.scoreForRound === player.totalScore + 4
? player.correctStreak++
: (player.correctStreak = 0);
player.autoLimb = false;
player.attract = false;
game.attractActive = false;
player.controlsOrder = [
"Left Arm",
"Left Leg",
"Right Leg",
"Right Arm",
];
});
game.currentRound++;
game.activeCardIndex++;
game.activeCard = game.cardStack[game.activeCardIndex];
};
if (game.roundOver) {
console.log("🔴", game.progress);
roundCleanup();
}
if (game.progress === 59) {
roundCleanup();
}
if (game.gameOver) {
const winners = getWinners(game.players);
winners.forEach((winner) => {
winner.win = true;
});
Rune.gameOver({
players: game.players.reduce(
(acc, player) => ({ ...acc, [player.playerId]: player.totalScore }),
{},
),
delayPopUp: false,
});
}
game.timeInSeconds = newTimeInSeconds;
},
});