This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
116 lines (96 loc) · 3.43 KB
/
start.js
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
try {
const uno_deck = require('../../../../helpers/others/uno_deck.json');
const { unoCards } = require('../../../../helpers/others/uno_cards.json');
const { shuffle } = require('../../../../helpers/others/functions.js');
const kv = require('../../../../helpers/kv/functions.js');
const messages = require('../../../../helpers/channels/messages/functions.js');
// debugging
// console.log(context)
// get channel id from context (this is the channel where the game is being played)
let { gameChannel } = context.params.game
// get guild id
let { guild_id } = context.params.event
// get playerlist from context (array of player objects)
let playerlist = context.params.game.players
// create starting deck
let startingDeck = await shuffle(uno_deck);
// create draw pile
let drawPile = [];
// create player hands
let playerHands = [];
// deal 7 cards to each player
// let playerHands = playerlist.map(player => {
// let hand = [];
// for (let i = 0; i < 7; i++) {
// hand.push(startingDeck.pop());
// }
// return {
// id: player.id,
// hand: hand
// }
// })
for (let i = 0; i < playerlist.length; i++) {
playerHands[i] = startingDeck.slice(0, 7);
}
// create discard pile
let discardPile = [];
// add first card to discard pile
discardPile.push(startingDeck.pop());
// pick a random player from playerlist that starts the game
let startingPlayer = playerlist[Math.floor(Math.random() * playerlist.length)];
// create game object
let game = {
channel: gameChannel, // channel where the game is being played
playerlist: playerlist, // array of player objects
playerHands: playerHands, // object with player ids as keys and arrays of cards as values
drawPile: drawPile, // draw pile is empty at the start of the game
discardPile: discardPile, // discard pile has one card at the start of the game
currentPlayer: startingPlayer, // player object of the player that starts the game
direction: 1, // reverse
drawTwo: 0, // +2
skip: 0, // skip
wildColor: null, // color change because of wildcard
colorChangeBy: null, // player who changed the color
wildDrawFour: 0, // +4
playersWithUno: [], // array of player ids that have uno
unoCalled: [], // array of player ids that called uno
unoMissedCalled: false, // did someone notice that a player missed calling uno?
unoMissedCalledBy: null, // player who noticed that someone missed calling uno
};
// debugging
console.log(game);
console.log(game.playerHands);
// save game to kv
await kv.set(`gameDetails-UNO-${guild_id}-${gameChannel}`, game, 604800 /* a week */);
// TODO add buttons to starting message
// labels: enhancement
// assignees: larssieboy18
// lines: 86,105
// send starting message to game channel
let startingMessage = await messages.create(gameChannel, '', [
{
title: "UNO",
description: `**<@${startingPlayer.id}>** starts the game!`,
color: 0x00ff00,
fields: [
{
name: "Current Card",
value: `${discardPile[0]}`,
inline: true,
},
{
name: "Current Player",
value: `${startingPlayer.name}`,
inline: true,
},
],
}
], []);
// debugging
console.log(startingMessage)
// did an error occur?
return false
} catch (errorStartingGame) {
console.error(errorStartingGame)
return errorStartingGame
}