@takaro/modules 0.0.13 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/main.js +2 -0
  2. package/dist/main.js.map +1 -1
  3. package/dist/modules/dailyRewards/commands/daily.d.ts +1 -0
  4. package/dist/modules/dailyRewards/commands/daily.js +106 -0
  5. package/dist/modules/dailyRewards/commands/daily.js.map +1 -0
  6. package/dist/modules/dailyRewards/commands/streak.d.ts +1 -0
  7. package/dist/modules/dailyRewards/commands/streak.js +34 -0
  8. package/dist/modules/dailyRewards/commands/streak.js.map +1 -0
  9. package/dist/modules/dailyRewards/commands/topstreak.d.ts +1 -0
  10. package/dist/modules/dailyRewards/commands/topstreak.js +44 -0
  11. package/dist/modules/dailyRewards/commands/topstreak.js.map +1 -0
  12. package/dist/modules/dailyRewards/functions/utils.d.ts +5 -0
  13. package/dist/modules/dailyRewards/functions/utils.js +32 -0
  14. package/dist/modules/dailyRewards/functions/utils.js.map +1 -0
  15. package/dist/modules/dailyRewards/hooks/dailyLoginCheck.d.ts +1 -0
  16. package/dist/modules/dailyRewards/hooks/dailyLoginCheck.js +19 -0
  17. package/dist/modules/dailyRewards/hooks/dailyLoginCheck.js.map +1 -0
  18. package/dist/modules/dailyRewards/index.d.ts +4 -0
  19. package/dist/modules/dailyRewards/index.js +117 -0
  20. package/dist/modules/dailyRewards/index.js.map +1 -0
  21. package/dist/modules/economyUtils/commands/shop.js +6 -6
  22. package/dist/modules/economyUtils/commands/shop.js.map +1 -1
  23. package/dist/modules/economyUtils/cronJobs/zombieKillReward.d.ts +1 -0
  24. package/dist/modules/economyUtils/cronJobs/zombieKillReward.js +66 -0
  25. package/dist/modules/economyUtils/cronJobs/zombieKillReward.js.map +1 -0
  26. package/dist/modules/economyUtils/index.js +21 -2
  27. package/dist/modules/economyUtils/index.js.map +1 -1
  28. package/dist/modules/geoBlock/hooks/IPDetected.js +4 -2
  29. package/dist/modules/geoBlock/hooks/IPDetected.js.map +1 -1
  30. package/dist/modules/gimme/commands/gimme.js +17 -6
  31. package/dist/modules/gimme/commands/gimme.js.map +1 -1
  32. package/dist/modules/gimme/index.js +23 -2
  33. package/dist/modules/gimme/index.js.map +1 -1
  34. package/dist/modules/playerOnboarding/commands/starterkit.js +16 -3
  35. package/dist/modules/playerOnboarding/commands/starterkit.js.map +1 -1
  36. package/dist/modules/playerOnboarding/index.js +24 -3
  37. package/dist/modules/playerOnboarding/index.js.map +1 -1
  38. package/dist/modules.json +88 -11
  39. package/package.json +1 -1
  40. package/scripts/buildBuiltinJson.ts +46 -2
  41. package/src/__tests__/economy/shop.integration.test.ts +23 -0
  42. package/src/__tests__/economy/zombieKillReward.integration.test.ts +296 -0
  43. package/src/__tests__/gimme.integration.test.ts +8 -2
  44. package/src/__tests__/onboarding.integration.test.ts +23 -20
  45. package/src/main.ts +2 -0
  46. package/src/modules/dailyRewards/commands/daily.js +118 -0
  47. package/src/modules/dailyRewards/commands/streak.js +42 -0
  48. package/src/modules/dailyRewards/commands/topstreak.js +54 -0
  49. package/src/modules/dailyRewards/functions/utils.js +36 -0
  50. package/src/modules/dailyRewards/hooks/dailyLoginCheck.js +24 -0
  51. package/src/modules/dailyRewards/index.ts +126 -0
  52. package/src/modules/economyUtils/commands/shop.js +6 -6
  53. package/src/modules/economyUtils/cronJobs/zombieKillReward.js +82 -0
  54. package/src/modules/economyUtils/index.ts +23 -2
  55. package/src/modules/geoBlock/hooks/IPDetected.js +4 -2
  56. package/src/modules/gimme/commands/gimme.js +16 -6
  57. package/src/modules/gimme/index.ts +23 -2
  58. package/src/modules/playerOnboarding/commands/starterkit.js +19 -5
  59. package/src/modules/playerOnboarding/index.ts +24 -3
@@ -0,0 +1,24 @@
1
+ import { data, takaro } from '@takaro/helpers';
2
+ import { getLastClaim } from './utils.js';
3
+
4
+ async function main() {
5
+ const { pog, gameServerId, module: mod } = data;
6
+ const prefix = (await takaro.settings.settingsControllerGetOne('commandPrefix', gameServerId)).data.data.value;
7
+
8
+ const lastClaim = await getLastClaim(gameServerId, pog.playerId, mod.moduleId);
9
+
10
+ // First time player
11
+ if (!lastClaim) {
12
+ await pog.pm(`Welcome! Use ${prefix}daily to claim your first daily reward and start your streak!`);
13
+ return;
14
+ }
15
+
16
+ const now = new Date();
17
+ const nextClaimTime = new Date(lastClaim.getTime() + 24 * 60 * 60 * 1000);
18
+
19
+ if (now >= nextClaimTime) {
20
+ await pog.pm(`Your daily reward is ready! Use ${prefix}daily to claim it!`);
21
+ }
22
+ }
23
+
24
+ await main();
@@ -0,0 +1,126 @@
1
+ /* eslint-disable quotes */
2
+ import { BuiltinModule, ICommand, IFunction, IHook, IPermission } from '../../BuiltinModule.js';
3
+
4
+ export class DailyRewards extends BuiltinModule<DailyRewards> {
5
+ constructor() {
6
+ super(
7
+ 'dailyRewards',
8
+ 'Provides daily login rewards with streak tracking',
9
+ JSON.stringify({
10
+ $schema: 'http://json-schema.org/draft-07/schema#',
11
+ type: 'object',
12
+ properties: {
13
+ baseReward: {
14
+ type: 'number',
15
+ title: 'Base Reward',
16
+ description: 'Base amount of currency given for daily rewards. This is multiplied by streak level.',
17
+ default: 100,
18
+ minimum: 1,
19
+ },
20
+ maxStreak: {
21
+ type: 'number',
22
+ title: 'Maximum Streak',
23
+ description: 'Maximum streak level a player can reach',
24
+ default: 365,
25
+ minimum: 1,
26
+ },
27
+ milestoneRewards: {
28
+ type: 'array',
29
+ title: 'Milestone Rewards',
30
+ description: 'Additional rewards for reaching certain streak milestones',
31
+ items: {
32
+ type: 'object',
33
+ properties: {
34
+ days: {
35
+ type: 'number',
36
+ description: 'Days needed to reach milestone',
37
+ minimum: 1,
38
+ },
39
+ reward: {
40
+ type: 'number',
41
+ description: 'Bonus reward amount',
42
+ },
43
+ message: {
44
+ type: 'string',
45
+ description: 'Message to show when milestone is reached',
46
+ },
47
+ },
48
+ },
49
+ default: [
50
+ { days: 7, reward: 1000, message: 'You did it! 7 days in a row!' },
51
+ { days: 30, reward: 5000, message: "A whole month! You're on fire!" },
52
+ { days: 90, reward: 20000, message: "90 days! You're unstoppable!" },
53
+ { days: 180, reward: 50000, message: "Half a year! You're a legend!" },
54
+ { days: 365, reward: 150000, message: "365 days! You're a true champion!" },
55
+ ],
56
+ },
57
+ },
58
+ required: ['baseReward', 'maxStreak', 'milestoneRewards'],
59
+ additionalProperties: false,
60
+ }),
61
+ );
62
+
63
+ this.functions = [
64
+ new IFunction({
65
+ name: 'utils',
66
+ function: this.loadFn('functions', 'utils'),
67
+ }),
68
+ ];
69
+
70
+ this.permissions = [
71
+ new IPermission({
72
+ permission: 'DAILY_CLAIM',
73
+ friendlyName: 'Claim Daily Rewards',
74
+ description: 'Allows the player to claim daily rewards',
75
+ canHaveCount: false,
76
+ }),
77
+ new IPermission({
78
+ permission: 'DAILY_REWARD_MULTIPLIER',
79
+ friendlyName: 'Multiplier',
80
+ description:
81
+ 'Control the multiplier per role. This is useful to give your donors a little extra. Count is an integer multiplier.',
82
+ canHaveCount: true,
83
+ }),
84
+ ];
85
+
86
+ this.commands = [
87
+ new ICommand({
88
+ function: this.loadFn('commands', 'daily'),
89
+ name: 'daily',
90
+ trigger: 'daily',
91
+ helpText: 'Claim your daily reward',
92
+ arguments: [],
93
+ }),
94
+ new ICommand({
95
+ function: this.loadFn('commands', 'streak'),
96
+ name: 'streak',
97
+ trigger: 'streak',
98
+ helpText: 'Check your current daily reward streak and next claim time',
99
+ arguments: [],
100
+ }),
101
+ new ICommand({
102
+ function: this.loadFn('commands', 'topstreak'),
103
+ name: 'topstreak',
104
+ trigger: 'topstreak',
105
+ helpText: 'Shows the players with highest daily reward streaks',
106
+ arguments: [
107
+ {
108
+ name: 'count',
109
+ type: 'number',
110
+ defaultValue: '5',
111
+ helpText: 'Number of players to show (max 25)',
112
+ position: 0,
113
+ },
114
+ ],
115
+ }),
116
+ ];
117
+
118
+ this.hooks = [
119
+ new IHook({
120
+ eventType: 'player-connected',
121
+ name: 'dailyLoginCheck',
122
+ function: this.loadFn('hooks', 'dailyLoginCheck'),
123
+ }),
124
+ ];
125
+ }
126
+ }
@@ -1,7 +1,7 @@
1
1
  import { takaro, data, TakaroUserError } from '@takaro/helpers';
2
2
 
3
3
  async function main() {
4
- const { arguments: args, player, gameServerId, user } = data;
4
+ const { arguments: args, player, gameServerId } = data;
5
5
  const { page, item, action } = args;
6
6
  const prefix = (await takaro.settings.settingsControllerGetOne('commandPrefix', gameServerId)).data.data.value;
7
7
 
@@ -35,12 +35,14 @@ async function main() {
35
35
  const currencyName = (await takaro.settings.settingsControllerGetOne('currencyName', data.gameServerId)).data.data;
36
36
 
37
37
  if (!item) {
38
- // List the shop items
38
+ // List the shop items with index
39
+ let index = 1;
39
40
  for (const listing of shopItems.data.data) {
40
41
  const items = listing.items.slice(0, 3).map((item) => {
41
42
  return `${item.amount}x ${item.item.name}`;
42
43
  });
43
- await player.pm(`- ${listing.name} - ${listing.price} ${currencyName.value}. ${items.join(', ')}`);
44
+ await player.pm(`${index} - ${listing.name} - ${listing.price} ${currencyName.value}. ${items.join(', ')}`);
45
+ index++;
44
46
  }
45
47
  return;
46
48
  }
@@ -69,12 +71,10 @@ async function main() {
69
71
  }
70
72
 
71
73
  if (action === 'buy') {
72
- if (!user) throw new TakaroUserError('You must link your account to Takaro to use this command.');
73
-
74
74
  const orderRes = await takaro.shopOrder.shopOrderControllerCreate({
75
75
  amount: 1,
76
76
  listingId: selectedItem.id,
77
- userId: user.id,
77
+ playerId: player.id,
78
78
  });
79
79
  await player.pm(`You have purchased ${selectedItem.name} for ${selectedItem.price} ${currencyName.value}.`);
80
80
  await takaro.shopOrder.shopOrderControllerClaim(orderRes.data.data.id);
@@ -0,0 +1,82 @@
1
+ import { data, takaro, checkPermission } from '@takaro/helpers';
2
+
3
+ const VARIABLE_KEY = 'lastZombieKillReward';
4
+
5
+ async function main() {
6
+ const { gameServerId, module: mod } = data;
7
+
8
+ const lastRunRes = (
9
+ await takaro.variable.variableControllerSearch({
10
+ filters: {
11
+ key: [VARIABLE_KEY],
12
+ gameServerId: [gameServerId],
13
+ moduleId: [mod.moduleId],
14
+ },
15
+ })
16
+ ).data.data;
17
+
18
+ // We last ran the rewards script at this time
19
+ // If this is the first time we run it, just get the last 5 minutes
20
+ const lastRun = lastRunRes.length ? new Date(JSON.parse(lastRunRes[0].value)) : new Date(Date.now() - 5 * 60 * 1000);
21
+
22
+ // Fetch all the kill events since the last time we gave out rewards
23
+ const killEvents = (
24
+ await takaro.event.eventControllerSearch({
25
+ filters: { eventName: ['entity-killed'], gameserverId: [gameServerId] },
26
+ greaterThan: { createdAt: lastRun.toISOString() },
27
+ limit: 1000,
28
+ })
29
+ ).data.data;
30
+
31
+ console.log(`Found ${killEvents.length} kill events since ${lastRun.toISOString()}`);
32
+
33
+ // Group the events by player
34
+ const playerKills = {};
35
+ for (const killEvent of killEvents) {
36
+ if (!playerKills[killEvent.playerId]) {
37
+ playerKills[killEvent.playerId] = [];
38
+ }
39
+
40
+ playerKills[killEvent.playerId].push(killEvent);
41
+ }
42
+
43
+ // Give each player their reward
44
+ // We use Promise.allSettled to run this concurrently
45
+ const results = await Promise.allSettled(
46
+ Object.entries(playerKills).map(async ([playerId, kills]) => {
47
+ const pog = (await takaro.playerOnGameserver.playerOnGameServerControllerGetOne(gameServerId, playerId)).data
48
+ .data;
49
+ const hasPermission = checkPermission(pog, 'ZOMBIE_KILL_REWARD_OVERRIDE');
50
+ const defaultReward = mod.userConfig.zombieKillReward;
51
+ const reward = hasPermission && hasPermission.count != null ? hasPermission.count : defaultReward;
52
+ const totalReward = reward * kills.length;
53
+ return takaro.playerOnGameserver.playerOnGameServerControllerAddCurrency(gameServerId, playerId, {
54
+ currency: totalReward,
55
+ });
56
+ }),
57
+ );
58
+
59
+ // Log any errors
60
+ for (const result of results) {
61
+ if (result.status === 'rejected') {
62
+ console.error(result.reason);
63
+ throw new Error(`Failed to give rewards: ${result.reason}`);
64
+ }
65
+ }
66
+
67
+ // Update the last run time
68
+ if (lastRunRes.length) {
69
+ await takaro.variable.variableControllerUpdate(lastRunRes[0].id, {
70
+ value: JSON.stringify(new Date()),
71
+ });
72
+ } else {
73
+ await takaro.variable.variableControllerCreate({
74
+ key: VARIABLE_KEY,
75
+ value: JSON.stringify(new Date()),
76
+ moduleId: mod.moduleId,
77
+ gameServerId,
78
+ });
79
+ }
80
+ }
81
+
82
+ await main();
@@ -1,4 +1,4 @@
1
- import { BuiltinModule, ICommand, IPermission } from '../../BuiltinModule.js';
1
+ import { BuiltinModule, ICommand, ICronJob, IPermission } from '../../BuiltinModule.js';
2
2
 
3
3
  export class EconomyUtils extends BuiltinModule<EconomyUtils> {
4
4
  constructor() {
@@ -16,6 +16,13 @@ export class EconomyUtils extends BuiltinModule<EconomyUtils> {
16
16
  'When a player transfers money, they must confirm the transfer when the amount is equal or above this value. Set to 0 to disable.',
17
17
  default: 0,
18
18
  },
19
+ zombieKillReward: {
20
+ title: 'Zombie kill reward',
21
+ type: 'number',
22
+ description:
23
+ 'The default amount of currency a player receives for killing a zombie. This can be overridden by roles.',
24
+ default: 1,
25
+ },
19
26
  },
20
27
  required: [],
21
28
  additionalProperties: false,
@@ -30,6 +37,20 @@ export class EconomyUtils extends BuiltinModule<EconomyUtils> {
30
37
  'Allows players to manage currency of other players. This includes granting and revoking currency.',
31
38
  canHaveCount: false,
32
39
  }),
40
+ new IPermission({
41
+ permission: 'ZOMBIE_KILL_REWARD_OVERRIDE',
42
+ friendlyName: 'Zombie kill reward override',
43
+ description: 'Allows a role to override the amount of currency a player receives for killing a entity.',
44
+ canHaveCount: true,
45
+ }),
46
+ ];
47
+
48
+ this.cronJobs = [
49
+ new ICronJob({
50
+ function: this.loadFn('cronJobs', 'zombieKillReward'),
51
+ name: 'zombieKillReward',
52
+ temporalValue: '*/5 * * * *',
53
+ }),
33
54
  ];
34
55
 
35
56
  this.commands = [
@@ -73,7 +94,7 @@ export class EconomyUtils extends BuiltinModule<EconomyUtils> {
73
94
  function: this.loadFn('commands', 'revokeCurrency'),
74
95
  name: 'revokeCurrency',
75
96
  trigger: 'revokecurrency',
76
- helpText: 'Grant money to a player. The money is not taken from your own balance but is new currency.',
97
+ helpText: 'Revokes money from a player. The money disappears.',
77
98
  arguments: [
78
99
  {
79
100
  name: 'receiver',
@@ -9,9 +9,11 @@ async function main() {
9
9
  if (ban) {
10
10
  const now = new Date();
11
11
  const expiresAt = new Date(now.getTime() + banDuration * 1000);
12
- await takaro.gameserver.gameServerControllerBanPlayer(gameServerId, player.id, {
12
+ await takaro.player.banControllerCreate({
13
+ gameServerId,
14
+ playerId: player.id,
15
+ until: expiresAt,
13
16
  reason: message,
14
- expiresAt,
15
17
  });
16
18
  } else {
17
19
  await takaro.gameserver.gameServerControllerKickPlayer(gameServerId, player.id, {
@@ -15,12 +15,22 @@ async function main() {
15
15
  const randomOption = items.concat(commands)[randomIndex];
16
16
 
17
17
  if (randomIndex < items.length) {
18
- await takaro.gameserver.gameServerControllerGiveItem(data.gameServerId, data.player.id, {
19
- name: randomOption,
20
- amount: 1,
21
- quality: 0,
22
- });
23
- await data.player.pm(`You received ${randomOption}!`);
18
+ if (typeof randomOption === 'string') {
19
+ await takaro.gameserver.gameServerControllerGiveItem(data.gameServerId, data.player.id, {
20
+ name: randomOption,
21
+ amount: 1,
22
+ quality: '0',
23
+ });
24
+ await data.player.pm(`You received ${randomOption}!`);
25
+ } else {
26
+ const item = (await takaro.item.itemControllerFindOne(randomOption.item)).data.data;
27
+ await takaro.gameserver.gameServerControllerGiveItem(data.gameServerId, data.player.id, {
28
+ name: item.code,
29
+ amount: randomOption.amount,
30
+ quality: randomOption.quality ?? '',
31
+ });
32
+ await data.player.pm(`You received ${randomOption.amount}x ${item.name}!`);
33
+ }
24
34
  } else {
25
35
  await takaro.gameserver.gameServerControllerExecuteCommand(data.gameServerId, { command: randomOption });
26
36
  }
@@ -16,7 +16,22 @@ export class Gimme extends BuiltinModule<Gimme> {
16
16
  description: 'List of items that a player can receive.',
17
17
  uniqueItems: true,
18
18
  items: {
19
- type: 'string',
19
+ type: 'object',
20
+ title: 'Item',
21
+ properties: {
22
+ item: {
23
+ type: 'string',
24
+ title: 'Item',
25
+ },
26
+ amount: {
27
+ type: 'number',
28
+ title: 'Amount',
29
+ },
30
+ quality: {
31
+ type: 'string',
32
+ title: 'Quality',
33
+ },
34
+ },
20
35
  },
21
36
  },
22
37
  commands: {
@@ -32,7 +47,13 @@ export class Gimme extends BuiltinModule<Gimme> {
32
47
  additionalProperties: false,
33
48
  }),
34
49
  JSON.stringify({
35
- items: { 'ui:widget': 'item' },
50
+ items: {
51
+ items: {
52
+ item: {
53
+ 'ui:widget': 'item',
54
+ },
55
+ },
56
+ },
36
57
  }),
37
58
  );
38
59
 
@@ -7,7 +7,7 @@ async function main() {
7
7
 
8
8
  if (!items || items.length === 0) {
9
9
  throw new TakaroUserError(
10
- 'No starter kit items configured. Please ask your server administrator to configure this.'
10
+ 'No starter kit items configured. Please ask your server administrator to configure this.',
11
11
  );
12
12
  }
13
13
 
@@ -25,13 +25,27 @@ async function main() {
25
25
 
26
26
  await data.player.pm('You are about to receive your starter kit...');
27
27
 
28
+ const itemRecords = (await takaro.item.itemControllerSearch({ filters: { id: items.map((_) => _.item) } })).data.data;
29
+ const fullItems = items.map((item) => {
30
+ const itemRecord = itemRecords.find((record) => record.id === item.item);
31
+ if (!itemRecord) {
32
+ throw new TakaroUserError(`Item with ID ${item.item} not found.`);
33
+ }
34
+ return {
35
+ code: itemRecord.code,
36
+ quality: item.quality,
37
+ amount: item.amount,
38
+ };
39
+ });
40
+
28
41
  await Promise.all(
29
- items.map(async (item) => {
42
+ fullItems.map(async (item) => {
30
43
  return takaro.gameserver.gameServerControllerGiveItem(data.gameServerId, data.player.id, {
31
- name: item,
32
- amount: 1,
44
+ name: item.code,
45
+ quality: item.quality ?? '',
46
+ amount: item.amount,
33
47
  });
34
- })
48
+ }),
35
49
  );
36
50
 
37
51
  await takaro.variable.variableControllerCreate({
@@ -20,13 +20,28 @@ export class PlayerOnboarding extends BuiltinModule<PlayerOnboarding> {
20
20
  },
21
21
  starterKitItems: {
22
22
  type: 'array',
23
- 'x-component': 'item',
24
23
  title: 'Starter kit items',
24
+ 'x-component': 'item',
25
25
  description:
26
26
  'List of items a player will receive when they execute the starterkit command for the first time.',
27
27
  uniqueItems: true,
28
28
  items: {
29
- type: 'string',
29
+ type: 'object',
30
+ title: 'Item',
31
+ properties: {
32
+ item: {
33
+ type: 'string',
34
+ title: 'Item',
35
+ },
36
+ amount: {
37
+ type: 'number',
38
+ title: 'Amount',
39
+ },
40
+ quality: {
41
+ type: 'string',
42
+ title: 'Quality',
43
+ },
44
+ },
30
45
  },
31
46
  },
32
47
  },
@@ -34,7 +49,13 @@ export class PlayerOnboarding extends BuiltinModule<PlayerOnboarding> {
34
49
  additionalProperties: false,
35
50
  }),
36
51
  JSON.stringify({
37
- starterKitItems: { 'ui:widget': 'item' },
52
+ starterKitItems: {
53
+ items: {
54
+ item: {
55
+ 'ui:widget': 'item',
56
+ },
57
+ },
58
+ },
38
59
  }),
39
60
  );
40
61