-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared-config.js
More file actions
535 lines (456 loc) · 18.5 KB
/
shared-config.js
File metadata and controls
535 lines (456 loc) · 18.5 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// Shared configuration constants for space colonization game
// This file should be imported by both client and server
// =============================================================================
// GAME BOARD CONFIGURATION
// =============================================================================
const BOARD_CONFIG = {
// Default board dimensions
DEFAULT_WIDTH: 3,
DEFAULT_HEIGHT: 3,
// Grid calculation constants
GRID_MULTIPLIER: 4, // gridColumns = maxHexagonsInRow * 4 + 1
GRID_OFFSET: 1, // Additional grid columns
GRID_ROW_MULTIPLIER: 4, // gridRows = height * 4 + 3
GRID_ROW_OFFSET: 3, // Additional grid rows
// Hex positioning
HORIZONTAL_SPACING: 4, // Distance between hex centers horizontally
VERTICAL_SPACING: 4, // Distance between hex centers vertically (was 3, now matches server)
};
// =============================================================================
// HEX NODE POSITIONS (clock positions around hexagon)
// =============================================================================
const HEX_NODE_OFFSETS = [
{ row: -3, col: 0, clock: 12 }, // 12 o'clock (top)
{ row: -1, col: +2, clock: 2 }, // 2 o'clock (top-right)
{ row: 1, col: +2, clock: 4 }, // 4 o'clock (bottom-right)
{ row: 3, col: 0, clock: 6 }, // 6 o'clock (bottom)
{ row: 1, col: -2, clock: 8 }, // 8 o'clock (bottom-left)
{ row: -1, col: -2, clock: 10 } // 10 o'clock (top-left)
];
const HEX_NODE_COUNT = HEX_NODE_OFFSETS.length; // 6 nodes per hex
// =============================================================================
// DEFAULT CONFIGURATION - Removed static defaults in favor of dynamic functions
// =============================================================================
// Default configurations are now generated dynamically using:
// - autoFillResourceConfiguration(totalTiles)
// - generateDynamicNumberConfiguration(totalTiles)
// - calculateTotalTiles(width, height)
// =============================================================================
// TRAVEL SYSTEM CONFIGURATION
// =============================================================================
const TRAVEL_CONFIG = {
BASE_COST_PER_HOP: 1, // Zap cost per node hop
LONG_DISTANCE_THRESHOLD: 2, // Hops before snowflake cost applies
LONG_DISTANCE_COST: 1, // Additional snowflake cost for long travel
};
// =============================================================================
// EXCHANGE SYSTEM CONFIGURATION
// =============================================================================
const EXCHANGE_CONFIG = {
TRADE_RATIO: 4, // Resources needed for 1 unit (4:1 ratio)
};
// =============================================================================
// STARTING RESOURCES CONFIGURATION
// =============================================================================
const STARTING_RESOURCES = {
zap: 3, // Starting energy
wrench: 3, // Starting tools
cpu: 3, // Starting technology
leaf: 3, // Starting organic
atom:3, // Starting atomic
snowflake: 3 // Starting cryogenic
};
// Default empty resources (used for initialization)
const DEFAULT_RESOURCES = {
wrench: 0,
zap: 0,
cpu: 0,
leaf: 0,
atom: 0,
snowflake: 0
};
// =============================================================================
// BUILDING COSTS CONFIGURATION
// =============================================================================
const BUILDING_COSTS = {
STATION: {
zap: 1, // Energy cost
wrench: 1, // Tool cost
cpu: 1, // Technology cost
leaf: 1 // Organic cost
},
TRIAD: {
wrench: 2, // Tool cost
atom: 2, // Atomic cost
cpu: 1, // Technology cost
leaf: 2 // Organic cost
},
WEAPON: {
atom: 1, // Atomic cost
cpu: 1, // Technology cost
snowflake: 1 // Cryogenic cost
},
SHIELD: {
atom: 1, // Atomic cost
cpu: 1, // Technology cost
snowflake: 1 // Cryogenic cost
}
};
// =============================================================================
// ATTACK COSTS CONFIGURATION
// =============================================================================
const ATTACK_COSTS = {
STATION: {
weapon: 1 // Weapons needed to attack a station
},
TRIAD: {
weapon: 3 // Weapons needed to attack a triad
}
};
// =============================================================================
// POINTS SYSTEM CONFIGURATION
// =============================================================================
const POINTS_CONFIG = {
STATION: 1, // Points for regular stations
TRIAD: 3, // Points for triads
VICTORY_THRESHOLD: 10 // Points needed to win the game
};
// =============================================================================
// UI RENDERING CONFIGURATION
// =============================================================================
const UI_CONFIG = {
// Grid cell dimensions
CELL_SIZE: 30, // px - size of each grid cell
GRID_GAP: 1, // px - gap between grid cells
BOARD_PADDING: 20, // px - padding around game board
// Node and ship styling
NODE_BORDER_RADIUS: '50%', // Circular nodes and ships
NODE_Z_INDEX: 10, // Layer for intersection nodes
SHIP_Z_INDEX: 15, // Layer for ships (above nodes)
// Histogram heights
HISTOGRAM_HEIGHT_HOST: 48, // px - host histogram height
HISTOGRAM_HEIGHT_PLAYER: 36, // px - player histogram height
};
// =============================================================================
// NETWORK CONFIGURATION
// =============================================================================
const NETWORK_CONFIG = {
DEFAULT_PORT: 3001,
DEFAULT_HOST: '0.0.0.0', // Listen on all interfaces
DEFAULT_WS_PORT: '3001',
};
// =============================================================================
// GAME TIMING CONFIGURATION
// =============================================================================
const TIMING_CONFIG = {
PLACEMENT_TRANSITION_DELAY: 3000, // ms - delay before starting placement phase
CLEANUP_INTERVAL: 30000, // ms - server cleanup interval
WEBSOCKET_TIMEOUT: 120000, // ms - default websocket timeout
WEBSOCKET_MAX_TIMEOUT: 600000, // ms - maximum websocket timeout
};
// =============================================================================
// TURN SEQUENCE CONFIGURATION
// =============================================================================
const TURN_CONFIG = {
// Turn phases
PHASES: {
ROLL: 'roll', // Player must roll dice
ACTIONS: 'actions', // Player can take actions
ENDED: 'ended' // Turn has ended
},
// Action limits per turn (-1 = unlimited, limited by resources)
ACTION_LIMITS: {
TRAVEL: 1, // Can travel once per turn
BUILD_STATION: -1, // Unlimited (resource-limited)
BUILD_TRADE: -1, // Unlimited (resource-limited)
MAKE_WEAPONS: -1, // Unlimited (resource-limited)
ADD_SHIELDS: -1, // Unlimited (resource-limited)
EXCHANGE: -1, // Unlimited (resource-limited)
ATTACK_PLAYER: -1 // Unlimited (resource-limited)
},
// Action types for tracking
ACTION_TYPES: {
TRAVEL: 'travel',
BUILD_STATION: 'build_station',
BUILD_TRADE: 'build_trade',
MAKE_WEAPONS: 'make_weapons',
ADD_SHIELDS: 'add_shields',
EXCHANGE: 'exchange',
ATTACK_PLAYER: 'attack_player'
}
};
// =============================================================================
// DICE PROBABILITY DATA
// =============================================================================
const DICE_PROBABILITIES = {
2: { ways: 1, probability: 1 / 36 },
3: { ways: 2, probability: 2 / 36 },
4: { ways: 3, probability: 3 / 36 },
5: { ways: 4, probability: 4 / 36 },
6: { ways: 5, probability: 5 / 36 },
7: { ways: 6, probability: 6 / 36 },
8: { ways: 5, probability: 5 / 36 },
9: { ways: 4, probability: 4 / 36 },
10: { ways: 3, probability: 3 / 36 },
11: { ways: 2, probability: 2 / 36 },
12: { ways: 1, probability: 1 / 36 }
};
// =============================================================================
// PLAYER COLORS
// =============================================================================
const PLAYER_COLORS = [
{ name: 'red', bg: 'bg-red-500', text: 'text-red-500', border: 'border-red-500' },
{ name: 'blue', bg: 'bg-blue-500', text: 'text-blue-500', border: 'border-blue-500' },
{ name: 'green', bg: 'bg-green-500', text: 'text-green-500', border: 'border-green-500' },
{ name: 'yellow', bg: 'bg-yellow-500', text: 'text-yellow-500', border: 'border-yellow-500' },
{ name: 'purple', bg: 'bg-purple-500', text: 'text-purple-500', border: 'border-purple-500' },
{ name: 'pink', bg: 'bg-pink-500', text: 'text-pink-500', border: 'border-pink-500' },
{ name: 'orange', bg: 'bg-orange-500', text: 'text-orange-500', border: 'border-orange-500' },
{ name: 'cyan', bg: 'bg-cyan-500', text: 'text-cyan-500', border: 'border-cyan-500' },
{ name: 'indigo', bg: 'bg-indigo-500', text: 'text-indigo-500', border: 'border-indigo-500' },
{ name: 'emerald', bg: 'bg-emerald-500', text: 'text-emerald-500', border: 'border-emerald-500' }
];
// =============================================================================
// RESOURCE TYPES
// =============================================================================
const RESOURCE_TYPES = [
{ key: 'wrench', name: 'Wrench', color: 'text-gray-700', bgColor: 'bg-gray-400', icon: 'Wrench' },
{ key: 'zap', name: 'Zap', color: 'text-yellow-700', bgColor: 'bg-yellow-400', icon: 'Zap' },
{ key: 'cpu', name: 'Cpu', color: 'text-blue-700', bgColor: 'bg-blue-400', icon: 'Cpu' },
{ key: 'leaf', name: 'Leaf', color: 'text-green-700', bgColor: 'bg-green-400', icon: 'Leaf' },
{ key: 'atom', name: 'Atom', color: 'text-red-700', bgColor: 'bg-red-400', icon: 'Atom' },
{ key: 'snowflake', name: 'Snowflake', color: 'text-cyan-700', bgColor: 'bg-cyan-400', icon: 'Snowflake' }
];
// =============================================================================
// VALIDATION HELPERS
// =============================================================================
const validateBoardConfig = (config) => {
const totalResources = Object.values(config.resourceConfig || {}).reduce((sum, count) => sum + count, 0);
const totalNumbers = Object.values(config.numberConfig || {}).reduce((sum, count) => sum + count, 0);
return {
isValid: totalResources === totalNumbers,
totalResources,
totalNumbers,
errors: totalResources !== totalNumbers ? ['Resource and number counts must match'] : []
};
};
const calculateTotalTiles = (width, height, layoutStyle = 'square') => {
if (layoutStyle === 'hex') {
// True hex grid calculation
// Height has to be odd. This validation will be done elsewhere.
let i = 1;
let tileCount = 0;
while (i <= Math.floor(height / 2)) {
tileCount = tileCount + (width - i) * 2;
i++;
}
tileCount += width;
return tileCount;
} else {
// Current "square" layout (actually rectangular honeycomb pattern)
// Calculate total tiles using hexagonal pattern: even rows = width, odd rows = width-1
const evenRows = Math.ceil(height / 2);
const oddRows = Math.floor(height / 2);
return (evenRows * width) + (oddRows * (width - 1));
}
};
// =============================================================================
// HEX LAYOUT VALIDATION FUNCTIONS
// =============================================================================
// Validate that height is odd for hex layout
const validateHexHeight = (height) => {
return height % 2 === 1;
};
// Calculate maximum height for given width to ensure no row has 0 tiles
const getMaxHeightForWidth = (width) => {
return (width * 2) - 1;
};
// Comprehensive hex layout validation
const validateHexDimensions = (width, height) => {
if (height % 2 === 0) {
return {
valid: false,
error: "Height must be odd for hex layout",
suggestedHeight: height % 2 === 0 ? height + 1 : height
};
}
const maxHeight = getMaxHeightForWidth(width);
if (height > maxHeight) {
return {
valid: false,
error: `Height too large. Maximum ${maxHeight} for width ${width}`,
suggestedHeight: maxHeight % 2 === 1 ? maxHeight : maxHeight - 1
};
}
return { valid: true };
};
// Get the minimum number of tiles in any row for given dimensions
const getMinTilesInRow = (width, height) => {
const centerRow = Math.floor(height / 2);
return width - centerRow;
};
// =============================================================================
// DYNAMIC CONFIGURATION GENERATORS
// =============================================================================
// Auto-fill resource configuration for any board size with even distribution
function autoFillResourceConfiguration(totalTiles) {
// Ensure totalTiles is a valid positive number
const validTotalTiles = Math.max(1, Math.floor(totalTiles) || 6);
// Start with even distribution across all resource types
const resourceTypes = ['wrench', 'zap', 'cpu', 'leaf', 'atom', 'snowflake'];
const baseAmount = Math.floor(validTotalTiles / resourceTypes.length);
const config = {
wrench: baseAmount,
zap: baseAmount,
cpu: baseAmount,
leaf: baseAmount,
atom: baseAmount,
snowflake: baseAmount
};
// Distribute remaining tiles to maintain exact total
let remaining = validTotalTiles - (baseAmount * resourceTypes.length);
let index = 0;
while (remaining > 0) {
const resourceKey = resourceTypes[index % resourceTypes.length];
config[resourceKey]++;
remaining--;
index++;
}
return config;
}
// Generate dynamic number configuration for any board size
function generateDynamicNumberConfiguration(totalTiles) {
// Ensure totalTiles is a valid positive number
const validTotalTiles = Math.max(1, Math.floor(totalTiles) || 6);
const config = {
2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0,
8: 0, 9: 0, 10: 0, 11: 0, 12: 0
};
// First, distribute evenly across all numbers
const baseAmountPerNumber = Math.floor(validTotalTiles / 11);
Object.keys(config).forEach(num => {
config[num] = baseAmountPerNumber;
});
// Calculate remaining tiles
let remainingTiles = validTotalTiles - (baseAmountPerNumber * 11);
// Priority order: 7,6,8,5,9,4,10,3,11,12,2 (7 has highest priority)
const priorityOrder = [7, 6, 8, 5, 9, 4, 10, 3, 11, 12, 2];
// Distribute remaining tiles according to priority
for (let i = 0; i < priorityOrder.length && remainingTiles > 0; i++) {
const number = priorityOrder[i];
config[number]++;
remainingTiles--;
}
return config;
}
// =============================================================================
// VALIDATION MESSAGES
// =============================================================================
const VALIDATION_MESSAGES = {
GAME_NOT_FOUND: 'Game not found',
NOT_YOUR_TURN: 'Not your turn or invalid player',
INVALID_PHASE: 'Action not allowed in current phase. Roll dice first.',
PLAYER_RESOURCES_NOT_FOUND: 'Player resources not found',
INSUFFICIENT_RESOURCES: 'Insufficient resources',
POSITION_OCCUPIED: 'Position is already occupied',
SHIP_NOT_FOUND: 'You have no ship at the starting position',
STATION_NOT_FOUND: 'Station not found or not owned by you'
};
// =============================================================================
// GAME STATE CONSTANTS
// =============================================================================
const GAME_STATES = {
LOBBY: 'lobby',
PLAYER_ORDER: 'player_order',
PLACEMENT: 'placement',
PLAYING: 'playing'
};
const GAME_PHASES = {
ROLL: 'roll',
ROLLED: 'rolled',
ACTIONS: 'actions'
};
const BOARD_LAYOUT_STYLES = {
SQUARE: 'square',
HEX: 'hex',
DEFAULT: 'hex'
};
// =============================================================================
// GAME FEATURES CONFIGURATION
// =============================================================================
const GAME_FEATURES = {
TRIAD_FREE_STATIONS: false, // Enable free stations around triads
TRIAD_MAX_FREE_STATIONS: -1, // Max free stations per triad (-1 = unlimited)
TRIAD_FREE_STATION_RANGE: 1 // Distance for free station placement (1 = adjacent)
};
// =============================================================================
// WEBSOCKET MESSAGE TYPES
// =============================================================================
const MESSAGE_TYPES = {
// Player management
JOIN: 'join',
MESSAGE: 'message',
CHANGE_COLOR: 'changeColor',
// Game management
UPDATE_GAME_BOARD: 'updateGameBoard',
START_GAME: 'startGame',
SHUFFLE_BOARD: 'shuffleBoard',
// Game state
ROLL_DICE: 'rollDice',
END_TURN: 'endTurn',
ROLL_FOR_PLAYER_ORDER: 'rollForPlayerOrder',
PLACE_SHIP: 'placeShip',
// Actions
TRAVEL_SHIP: 'travelShip',
BUILD_STATION: 'buildStation',
BUILD_TRIAD: 'buildTriad',
MAKE_WEAPONS: 'makeWeapons',
EXCHANGE: 'exchange',
ATTACK_PLAYER: 'attackPlayer',
// Response types
PLAYERS: 'players',
GAME_BOARD: 'gameBoard',
GAME_STATE: 'gameState',
PLAYER_RESOURCES: 'playerResources',
PLAYER_WEAPONS: 'playerWeapons',
PLAYER_POINTS: 'playerPoints',
SHIP_PLACEMENTS: 'shipPlacements',
STATION_PLACEMENTS: 'stationPlacements',
GAME_EVENT: 'gameEvent'
};
// =============================================================================
// MODULE EXPORTS
// =============================================================================
module.exports = {
BOARD_CONFIG,
HEX_NODE_OFFSETS,
HEX_NODE_COUNT,
TRAVEL_CONFIG,
EXCHANGE_CONFIG,
STARTING_RESOURCES,
DEFAULT_RESOURCES,
BUILDING_COSTS,
ATTACK_COSTS,
POINTS_CONFIG,
UI_CONFIG,
NETWORK_CONFIG,
TIMING_CONFIG,
TURN_CONFIG,
DICE_PROBABILITIES,
PLAYER_COLORS,
RESOURCE_TYPES,
VALIDATION_MESSAGES,
GAME_STATES,
GAME_PHASES,
BOARD_LAYOUT_STYLES,
GAME_FEATURES,
MESSAGE_TYPES,
validateBoardConfig,
calculateTotalTiles,
autoFillResourceConfiguration,
generateDynamicNumberConfiguration,
validateHexHeight,
getMaxHeightForWidth,
validateHexDimensions,
getMinTilesInRow
};