Skip to content

Commit

Permalink
refactor: 重构签到脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
iDerekLi committed Jan 13, 2023
1 parent 85737c6 commit 2f9247e
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 159 deletions.
241 changes: 163 additions & 78 deletions workflows/checkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,31 @@ const utils = require("./utils/utils");
const pushMessage = require("./utils/pushMessage");
const env = require("./utils/env");

class CheckIn {
username = "";
cookie = "";
todayStatus = 0; // 未签到
incrPoint = 0;
sumPoint = 0; // 当前矿石数
contCount = 0; // 连续签到天数
sumCount = 0; // 累计签到天数
dipStatus = 0;
dipValue = 0; // 沾喜气
luckyValue = 0;
lottery = []; // 奖池
pointCost = 0; // 一次抽奖消耗
freeCount = 0; // 免费抽奖次数
drawLotteryHistory = {};
lotteryCount = 0;
luckyValueProbability = 0;
bugStatus = 0;
collectBugCount = 0;
userOwnBug = 0;
class Task {
constructor(juejin) {
this.juejin = juejin;
}

calledSdkSetting = false;
calledTrackGrowthEvent = false;
calledTrackOnloadEvent = false;
taskName = "";

pageSnapshot = null;
async run() {}

constructor(cookie) {
this.cookie = cookie;
toString() {
return `[${this.taskName}]`;
}
}

async run() {
const juejin = new JuejinHelper();
try {
await juejin.login(this.cookie);
} catch (e) {
console.error(e);
throw new Error("登录失败, 请尝试更新Cookies!");
}
class GrowthTask extends Task {
taskName = "成长任务";

this.username = juejin.getUser().user_name;
todayStatus = 0; // 未签到
incrPoint = 0;
sumPoint = 0; // 当前矿石数
contCount = 0; // 连续签到天数
sumCount = 0; // 累计签到天数

const growth = juejin.growth();
async run() {
const growth = this.juejin.growth();

const todayStatus = await growth.getTodayStatus();
if (!todayStatus) {
Expand All @@ -61,6 +43,18 @@ class CheckIn {
const counts = await growth.getCounts();
this.contCount = counts.cont_count;
this.sumCount = counts.sum_count;
}
}

class DipLuckyTask extends Task {
taskName = "沾喜气";

dipStatus = 0;
dipValue = 0;
luckyValue = 0;

async run() {
const growth = this.juejin.growth();

const luckyusersResult = await growth.getLotteriesLuckyUsers();
if (luckyusersResult.count > 0) {
Expand All @@ -76,6 +70,47 @@ class CheckIn {

const luckyResult = await growth.getMyLucky();
this.luckyValue = luckyResult.total_value;
}
}

class BugfixTask extends Task {
taskName = "Bugfix";

bugStatus = 0;
collectBugCount = 0;
userOwnBug = 0;

async run() {
const bugfix = this.juejin.bugfix();

const competition = await bugfix.getCompetition();
const bugfixInfo = await bugfix.getUser(competition);
this.userOwnBug = bugfixInfo.user_own_bug;

try {
const notCollectBugList = await bugfix.getNotCollectBugList();
await bugfix.collectBugBatch(notCollectBugList);
this.bugStatus = 1;
this.collectBugCount = notCollectBugList.length;
this.userOwnBug += this.collectBugCount;
} catch (e) {
this.bugStatus = 2;
}
}
}

class LotteriesTask extends Task {
taskName = "抽奖";

lottery = []; // 奖池
pointCost = 0; // 一次抽奖消耗
freeCount = 0; // 免费抽奖次数
drawLotteryHistory = {};
lotteryCount = 0;
luckyValueProbability = 0;

async run(growthTask, dipLuckyTask) {
const growth = this.juejin.growth();

const lotteryConfig = await growth.getLotteryConfig();
this.lottery = lotteryConfig.lottery;
Expand All @@ -87,13 +122,13 @@ class CheckIn {
while (freeCount > 0) {
const result = await growth.drawLottery();
this.drawLotteryHistory[result.lottery_id] = (this.drawLotteryHistory[result.lottery_id] || 0) + 1;
this.luckyValue = result.total_lucky_value;
dipLuckyTask.luckyValue = result.total_lucky_value;
freeCount--;
this.lotteryCount++;
await utils.wait(utils.randomRangeNumber(300, 1000));
}

this.sumPoint = await growth.getCurrentPoint();
growthTask.sumPoint = await growth.getCurrentPoint();

const getProbabilityOfWinning = sumPoint => {
const pointCost = this.pointCost;
Expand All @@ -103,38 +138,33 @@ class CheckIn {
for (let i = 0, length = Math.floor(totalDrawsNumber * 0.65); i < length; i++) {
supplyPoint += Math.ceil(Math.random() * 100);
}
const luckyValue = ((sumPoint + supplyPoint) / pointCost) * luckyValueCost + this.luckyValue;
const luckyValue = ((sumPoint + supplyPoint) / pointCost) * luckyValueCost + dipLuckyTask.luckyValue;
return luckyValue / 6000;
};

this.luckyValueProbability = getProbabilityOfWinning(this.sumPoint);
this.luckyValueProbability = getProbabilityOfWinning(growthTask.sumPoint);
}
}

// 收集bug
const bugfix = juejin.bugfix();
class SdkTask extends Task {
taskName = "埋点";

const competition = await bugfix.getCompetition();
const bugfixInfo = await bugfix.getUser(competition);
this.userOwnBug = bugfixInfo.user_own_bug;
calledSdkSetting = false;
calledTrackGrowthEvent = false;
calledTrackOnloadEvent = false;

try {
const notCollectBugList = await bugfix.getNotCollectBugList();
await bugfix.collectBugBatch(notCollectBugList);
this.bugStatus = 1;
this.collectBugCount = notCollectBugList.length;
this.userOwnBug += this.collectBugCount;
} catch (e) {
this.bugStatus = 2;
}
async run() {
console.log("------事件埋点追踪-------");

// 调用埋点
const sdk = juejin.sdk();
const sdk = this.juejin.sdk();

try {
await sdk.slardarSDKSetting();
this.calledSdkSetting = true;
} catch {
this.calledSdkSetting = false;
}
console.log(`SDK状态: ${this.calledSdkSetting ? "加载成功" : "加载失败"}`);

try {
const result = await sdk.mockTrackGrowthEvent();
Expand All @@ -146,6 +176,7 @@ class CheckIn {
} catch {
this.calledTrackGrowthEvent = false;
}
console.log(`成长API事件埋点: ${this.calledTrackGrowthEvent ? "调用成功" : "调用失败"}`);

try {
const result = await sdk.mockTrackOnloadEvent();
Expand All @@ -157,16 +188,19 @@ class CheckIn {
} catch {
this.calledTrackOnloadEvent = false;
}

console.log("------事件埋点追踪-------");
console.log(`SDK状态: ${this.calledSdkSetting ? "加载成功" : "加载失败"}`);
console.log(`成长API事件埋点: ${this.calledTrackGrowthEvent ? "调用成功" : "调用失败"}`);
console.log(`OnLoad事件埋点: ${this.calledTrackOnloadEvent ? "调用成功" : "调用失败"}`);

console.log("-------------------------");
}
}

console.log("------模拟访问-------");
class MockVisitTask extends Task {
taskName = "模拟访问";

async run() {
console.log("--------模拟访问---------");
try {
const browser = juejin.browser();
const browser = this.juejin.browser();
await browser.open();
try {
await browser.visitPage("/");
Expand All @@ -187,14 +221,53 @@ class CheckIn {
console.log("浏览器API异常");
}
console.log("-------------------------");
}
}

class CheckIn {
cookie = "";
username = "";

constructor(cookie) {
this.cookie = cookie;
}

async run() {
const juejin = new JuejinHelper();
try {
await juejin.login(this.cookie);
} catch (e) {
console.error(e.message);
throw new Error("登录失败, 请尝试更新Cookies!");
}

this.username = juejin.getUser().user_name;

this.growthTask = new GrowthTask(juejin);
this.dipLuckyTask = new DipLuckyTask(juejin);
this.lotteriesTask = new LotteriesTask(juejin);
this.bugfixTask = new BugfixTask(juejin);
this.sdkTask = new SdkTask(juejin);
this.mockVisitTask = new MockVisitTask(juejin);

await this.mockVisitTask.run();
await this.sdkTask.run();
console.log(`运行 ${this.growthTask.taskName}`);
await this.growthTask.run();
console.log(`运行 ${this.dipLuckyTask.taskName}`);
await this.dipLuckyTask.run();
console.log(`运行 ${this.lotteriesTask.taskName}`);
await this.lotteriesTask.run(this.growthTask, this.dipLuckyTask);
console.log(`运行 ${this.bugfixTask.taskName}`);
await this.bugfixTask.run();
await juejin.logout();
console.log("-------------------------");
}

toString() {
const drawLotteryHistory = Object.entries(this.drawLotteryHistory)
const drawLotteryHistory = Object.entries(this.lotteriesTask.drawLotteryHistory)
.map(([lottery_id, count]) => {
const lotteryItem = this.lottery.find(item => item.lottery_id === lottery_id);
const lotteryItem = this.lotteriesTask.lottery.find(item => item.lottery_id === lottery_id);
if (lotteryItem) {
return `${lotteryItem.lottery_name}: ${count}`;
}
Expand All @@ -204,24 +277,36 @@ class CheckIn {

return `
掘友: ${this.username}
${this.todayStatus === 1 ? `签到成功 +${this.incrPoint} 矿石` : this.todayStatus === 2 ? "今日已完成签到" : "签到失败"}
${this.dipStatus === 1 ? `沾喜气 +${this.dipValue} 幸运值` : this.dipStatus === 2 ? "今日已经沾过喜气" : "沾喜气失败"}
${
this.bugStatus === 1
? this.collectBugCount > 0
? `收集Bug +${this.collectBugCount}`
{
0: "签到失败",
1: `签到成功 +${this.growthTask.incrPoint} 矿石`,
2: "今日已完成签到"
}[this.growthTask.todayStatus]
}
${
{
0: "沾喜气失败",
1: `沾喜气 +${this.dipLuckyTask.dipValue} 幸运值`,
2: "今日已经沾过喜气"
}[this.dipLuckyTask.dipStatus]
}
${
this.bugfixTask.bugStatus === 1
? this.bugfixTask.collectBugCount > 0
? `收集Bug +${this.bugfixTask.collectBugCount}`
: "没有可收集Bug"
: "收集Bug失败"
}
连续签到天数 ${this.contCount}
累计签到天数 ${this.sumCount}
当前矿石数 ${this.sumPoint}
当前未消除Bug数量 ${this.userOwnBug}
当前幸运值 ${this.luckyValue}/6000
预测All In矿石累计幸运值比率 ${(this.luckyValueProbability * 100).toFixed(2) + "%"}
抽奖总次数 ${this.lotteryCount}
免费抽奖次数 ${this.freeCount}
${this.lotteryCount > 0 ? "==============\n" + drawLotteryHistory + "\n==============" : ""}
连续签到天数 ${this.growthTask.contCount}
累计签到天数 ${this.growthTask.sumCount}
当前矿石数 ${this.growthTask.sumPoint}
当前未消除Bug数量 ${this.bugfixTask.userOwnBug}
当前幸运值 ${this.dipLuckyTask.luckyValue}/6000
预测All In矿石累计幸运值比率 ${(this.lotteriesTask.luckyValueProbability * 100).toFixed(2) + "%"}
抽奖总次数 ${this.lotteriesTask.lotteryCount}
免费抽奖次数 ${this.lotteriesTask.freeCount}
${this.lotteriesTask.lotteryCount > 0 ? "==============\n" + drawLotteryHistory + "\n==============" : ""}
`.trim();
}
}
Expand Down
Loading

0 comments on commit 2f9247e

Please sign in to comment.