forked from caiton1/OSS-Doorway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch.js
More file actions
209 lines (183 loc) · 5.41 KB
/
Copy pathscratch.js
File metadata and controls
209 lines (183 loc) · 5.41 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
import fs from "fs";
import { taskMapping } from "./taskMapping.js";
import { getQuestConfig } from "./src/config/questConfigGenerator.js";
import { getCompleteResponseObject } from "./src/config/responseGenerator.js";
// Constants configuration object
const CONFIG = {
paths: {
svg: "./src/templates/template.svg",
defaultReadme: "./src/templates/main.md",
progressReadme: "./src/templates/progress.md"
},
badgeDescriptions: {
Q0: "Configurator ⚙️",
Q1: "Explorer 🚀",
Q2: "Builder 🏗️",
Q3: "Contributor 🥇"
}
};
// Load configurations once
const questResponse = getCompleteResponseObject();
const quests = getQuestConfig();
const ossRepo = process.env.OSS_REPO;
const mapRepoLink = quests.map_repo_link;
// Utility functions
const utils = {
async updateGithubFile(context, { owner, repo, path, content, message }) {
try {
const { data: { sha } } = await context.octokit.repos.getContent({
owner,
repo,
path
});
await context.octokit.repos.createOrUpdateFileContents({
owner,
repo,
path,
message,
content: Buffer.from(content).toString('base64'),
committer: {
name: "QuestBuddy",
email: "naugitbot@gmail.com"
},
author: {
name: "QuestBuddy",
email: "naugitbot@gmail.com"
},
sha
});
} catch (error) {
console.error(`Error updating file ${path}:`, error);
}
},
async createGithubIssue(context, { owner, repo, title, body, labels = [] }) {
try {
return await context.octokit.issues.create({
owner,
repo,
title,
body,
labels
});
} catch (error) {
console.error("Error creating issue:", error);
return null;
}
}
};
// Quest Management Class
class QuestManager {
constructor(userData, context, db) {
this.userData = userData;
this.context = context;
this.db = db;
}
async acceptQuest(quest) {
if (!(quest in quests) || (this.userData.accepted && Object.keys(this.userData.accepted).length)) {
return false;
}
try {
this.userData.accepted = this.userData.accepted || {};
this.userData.accepted[quest] = {};
// Initialize tasks
for (const task in quests[quest]) {
if (task !== "metadata") {
this.userData.accepted[quest][task] = {
completed: false,
attempts: 0,
hints: 0,
timeStart: 0,
timeEnd: 0.0,
issueNum: 0
};
}
}
// Set current progress
this.userData.current = {
quest: quest,
task: "T1"
};
this.userData.completion = 0;
if (quest === 'Q0') {
await this.createQuestEnvironment(quest, "T1");
}
return true;
} catch (error) {
console.error("Error accepting quest:", error);
return false;
}
}
async completeTask(quest, task) {
try {
const questData = this.userData.accepted[quest];
if (!questData || !questData[task]) return false;
const points = quests[quest][task].points;
const xp = quests[quest][task].xp;
// Update task completion data
questData[task].completed = true;
questData[task].timeEnd = Date.now();
questData[task].issueNum = this.context.issue().issue_number;
// Update user stats
this.userData.points += points;
this.userData.xp += xp;
// Update completion percentage
const tasks = Object.keys(quests[quest]).filter(t => t !== "metadata");
const taskIndex = tasks.indexOf(task);
this.userData.completion = Math.round((taskIndex + 1) / tasks.length * 100) / 100;
// Handle next task or quest completion
if (taskIndex < tasks.length - 1) {
this.userData.current.task = tasks[taskIndex + 1];
await this.createQuestEnvironment(quest, this.userData.current.task);
} else {
this.userData.current.task = null;
await this.completeQuest(quest);
}
await this.closeIssue();
await this.updateReadme();
return true;
} catch (error) {
console.error("Error completing task:", error);
return false;
}
}
// ... Additional methods would follow similar pattern
}
// SVG Generation Class
class SVGGenerator {
constructor(userData, context) {
this.userData = userData;
this.context = context;
}
async generate() {
try {
const stats = this.calculateStats();
const svgContent = this.generateSVGContent(stats);
const filename = `userCards/draft-${Date.now()}.svg`;
await utils.updateGithubFile(this.context, {
owner: this.context.repo().owner,
repo: this.context.repo().repo,
path: filename,
content: svgContent,
message: `Update ${filename}`
});
return filename;
} catch (error) {
console.error("Error generating SVG:", error);
return null;
}
}
calculateStats() {
// Move all the stat calculations here
// Return an object with all necessary stats
}
generateSVGContent(stats) {
// Move SVG template generation here
// Use the stats to generate SVG content
}
}
// Export a simplified interface
export const gameFunction = {
createQuestManager: (userData, context, db) => new QuestManager(userData, context, db),
createSVGGenerator: (userData, context) => new SVGGenerator(userData, context),
utils
};