File size: 5,322 Bytes
18befff
 
 
 
 
 
 
 
 
 
 
 
 
2ff2803
 
30906bb
 
18befff
 
 
2048d4b
dded7ce
89e39ae
d36175e
d6b67ad
bac6736
18befff
 
d6b67ad
 
 
2ff2803
 
30906bb
 
d6b67ad
 
 
 
 
 
 
bac6736
 
d6b67ad
18befff
bac6736
 
 
 
 
 
 
18befff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30906bb
 
 
 
18befff
2ff2803
 
 
 
 
 
18befff
 
 
 
 
 
 
 
 
 
 
 
 
2048d4b
 
 
 
dded7ce
 
 
 
89e39ae
 
 
 
 
d6b67ad
 
 
 
 
 
 
d36175e
bac6736
 
 
 
 
 
d6b67ad
18befff
 
 
 
 
 
 
 
 
30906bb
89e39ae
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
const fs = require('fs');
const path = require('path');
const storage = require('./storage');

/**
 * lineup_persistence.js
 * Handles persistent storage for lineup state across bot restarts.
 * Fixes the issue where lineups reset when the space restarts.
 */

const LINEUP_DATA_FILE = path.join(storage.PERSISTENT_DIR, 'lineup_persistence.json');

let guildLineups = new Map();
let guildSelectedFormationA = new Map();
let guildSelectedFormationB = new Map();
// Shared formation selection map for text-based lineup (!lineup)
let guildSelectedFormation = new Map();
let lineupOwners = new Map();
let activeLineupMessages = new Map();
let managerBoards = new Map();
let guildCustomFormations = new Map();
let guildCustomFormationNames = new Map();
let guildPingType = new Map();
let guildFriendlyPingType = new Map();
let guildScrimMode = new Map(); // Track if lineup is in scrim mode
let guildLineupsB = new Map(); // Team B lineup for scrim mode

function save() {
  try {
    const data = {
      guildLineups: {},
      guildSelectedFormationA: Object.fromEntries(guildSelectedFormationA),
      guildSelectedFormationB: Object.fromEntries(guildSelectedFormationB),
      // Persist the shared text-based formation map as well
      guildSelectedFormation: Object.fromEntries(guildSelectedFormation),
      lineupOwners: Object.fromEntries(lineupOwners),
      activeLineupMessages: Object.fromEntries(activeLineupMessages),
      managerBoards: Object.fromEntries(managerBoards),
      guildCustomFormations: Object.fromEntries(guildCustomFormations),
      guildCustomFormationNames: Object.fromEntries(guildCustomFormationNames),
      guildPingType: Object.fromEntries(guildPingType),
      guildFriendlyPingType: Object.fromEntries(guildFriendlyPingType),
        guildScrimMode: Object.fromEntries(guildScrimMode),
        guildLineupsB: {}
    };

  for (const [guildId, lineupMap] of guildLineups) {
    data.guildLineups[guildId] = Object.fromEntries(lineupMap);
  }

  for (const [guildId, lineupMap] of guildLineupsB) {
    data.guildLineupsB[guildId] = Object.fromEntries(lineupMap);
  }

        fs.writeFileSync(LINEUP_DATA_FILE, JSON.stringify(data, null, 2));
        // console.log('[LINEUP PERSISTENCE] State saved successfully.');
    } catch (e) {
        console.error('[LINEUP PERSISTENCE] Save failed:', e.message);
    }
}

function load() {
    if (!fs.existsSync(LINEUP_DATA_FILE)) {
        console.log('[LINEUP PERSISTENCE] No existing persistence file found.');
        return;
    }
    
    try {
        const raw = fs.readFileSync(LINEUP_DATA_FILE, 'utf8');
        const data = JSON.parse(raw);
        
        if (data.guildLineups) {
            for (const [guildId, lineupObj] of Object.entries(data.guildLineups)) {
                guildLineups.set(guildId, new Map(Object.entries(lineupObj)));
            }
        }
        // Load shared text-based formation state if available
        if (data.guildSelectedFormation) {
            for (const [k, v] of Object.entries(data.guildSelectedFormation)) guildSelectedFormation.set(k, v);
        }
        
        if (data.guildSelectedFormationA) {
            for (const [k, v] of Object.entries(data.guildSelectedFormationA)) guildSelectedFormationA.set(k, v);
        }
        
        if (data.guildSelectedFormationB) {
            for (const [k, v] of Object.entries(data.guildSelectedFormationB)) guildSelectedFormationB.set(k, v);
        }
        
        if (data.lineupOwners) {
            for (const [k, v] of Object.entries(data.lineupOwners)) lineupOwners.set(k, v);
        }
        
        if (data.activeLineupMessages) {
            for (const [k, v] of Object.entries(data.activeLineupMessages)) activeLineupMessages.set(k, v);
        }

        if (data.managerBoards) {
            for (const [k, v] of Object.entries(data.managerBoards)) managerBoards.set(k, v);
        }

        if (data.guildCustomFormations) {
            for (const [k, v] of Object.entries(data.guildCustomFormations)) guildCustomFormations.set(k, v);
        }

        if (data.guildCustomFormationNames) {
            for (const [k, v] of Object.entries(data.guildCustomFormationNames)) guildCustomFormationNames.set(k, v);
        }

        if (data.guildPingType) {
            for (const [k, v] of Object.entries(data.guildPingType)) guildPingType.set(k, v);
        }

if (data.guildFriendlyPingType) {
    for (const [k, v] of Object.entries(data.guildFriendlyPingType)) guildFriendlyPingType.set(k, v);
  }

  if (data.guildScrimMode) {
    for (const [k, v] of Object.entries(data.guildScrimMode)) guildScrimMode.set(k, v === "true" || v === true);
  }

  if (data.guildLineupsB) {
    for (const [guildId, lineupObj] of Object.entries(data.guildLineupsB)) {
      guildLineupsB.set(guildId, new Map(Object.entries(lineupObj)));
    }
  }

  console.log('[LINEUP PERSISTENCE] Data loaded successfully.');
    } catch (e) {
        console.error('[LINEUP PERSISTENCE] Load failed:', e.message);
    }
}

// Initial load
load();

module.exports = {
  guildLineups, guildLineupsB, guildSelectedFormationA, guildSelectedFormationB, guildSelectedFormation, lineupOwners, activeLineupMessages, managerBoards, guildCustomFormations, guildCustomFormationNames, guildPingType, guildFriendlyPingType, guildScrimMode, save
};