253 lines
7.6 KiB
TypeScript
253 lines
7.6 KiB
TypeScript
import WorldStar from "./WorldStar";
|
|
import NpcMgr from "../core/NpcMgr";
|
|
import PlayerMgr from "./PlayerMgr";
|
|
import SKLogger from "../gear/SKLogger";
|
|
import * as schedule from "node-schedule";
|
|
import GTimer from "../common/GTimer";
|
|
|
|
export default class PirateMgr {
|
|
|
|
private static _shared: PirateMgr;
|
|
|
|
static get shared(): PirateMgr {
|
|
if (!this._shared) {
|
|
this._shared = new PirateMgr();
|
|
}
|
|
return this._shared;
|
|
}
|
|
|
|
count: number;
|
|
minutes: number;
|
|
kill_count: any;
|
|
vecPirate: any[];
|
|
goods_award: any;
|
|
verify_timer: any;
|
|
refresh_timer: any;
|
|
|
|
|
|
constructor() {
|
|
this.vecPirate = [];
|
|
this.init();
|
|
this.count = 20;
|
|
this.minutes = 30;
|
|
this.kill_count = 0;
|
|
this.verify_timer = 0;
|
|
this.refresh_timer = 0;
|
|
|
|
|
|
}
|
|
|
|
init() {
|
|
// 海盗奖励
|
|
this.goods_award = [50014, 10406, 10110, 10609, 10604, 50016, 50033];
|
|
//每天12:15、18:15
|
|
// schedule.scheduleJob("0 15 12,18 * * ? ", () => {
|
|
// this.vecPirate = [];
|
|
// this.kill_count = 0;
|
|
// this.verify_timer = 0;
|
|
// this.refresh_timer = 0;
|
|
// this.checkMinutesPirate();
|
|
// this.checkAndCreatePirate();
|
|
// })
|
|
}
|
|
|
|
IsPirate(nOnlyID: any): boolean {
|
|
for (let it in this.vecPirate) {
|
|
let pStar = this.vecPirate[it];
|
|
if (pStar.nOnlyID == nOnlyID)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
FindPirate(onlyID: number): any {
|
|
// 检查海盗
|
|
for (let key in this.vecPirate) {
|
|
let item = this.vecPirate[key];
|
|
if (item.nOnlyID == onlyID) {
|
|
return item;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
ApplyChallenge(nNpcOnlyID: any, roleId: any): number {
|
|
let bomb = this.FindPirate(nNpcOnlyID);
|
|
if (bomb == null) {
|
|
return 1;
|
|
}
|
|
if (bomb.vecApply.length > 0) {
|
|
return 2;
|
|
}
|
|
bomb.vecApply.push(roleId);
|
|
let self = this;
|
|
setTimeout(() => {
|
|
self.triggerPirateBattle(nNpcOnlyID, roleId);
|
|
}, 500);
|
|
return 0;
|
|
}
|
|
|
|
// 挑战失败
|
|
ChallengeFail(npc_onlyid: any) {
|
|
let pBomb: any = this.FindPirate(npc_onlyid);
|
|
if (null == pBomb) {
|
|
return;
|
|
}
|
|
pBomb.Reset();
|
|
}
|
|
|
|
|
|
// 检查创造怪物
|
|
checkAndCreatePirate() {
|
|
if (this.verify()) {
|
|
if (this.refresh_timer == 0) {
|
|
this.refresh_timer = setTimeout(() => {
|
|
this.refresh_timer = 0;
|
|
this.checkAndCreatePirate();
|
|
}, 3 * 60 * 1000);
|
|
}
|
|
// 海盗-东海渔村
|
|
for (let i = 0; i < this.count; i++) {
|
|
this.vecPirate.push(new WorldStar(83000, 1010, 1, 0));
|
|
}
|
|
for (let it in this.vecPirate) {
|
|
let pWBomb = this.vecPirate[it];
|
|
pWBomb.Reset();
|
|
if (pWBomb.nOnlyID > 0) {
|
|
continue;
|
|
}
|
|
let stPos = pWBomb.GetPos();
|
|
pWBomb.nOnlyID = NpcMgr.shared.CreateNpc(pWBomb.nNpc, stPos.map, stPos.x, stPos.y, {
|
|
nKind: 0,
|
|
nID: 0
|
|
}, 0);
|
|
}
|
|
SKLogger.info(`海盗数量:${this.vecPirate.length}`)
|
|
}
|
|
}
|
|
|
|
// 触发战斗
|
|
triggerPirateBattle(npcOnlyID: any, roleId: any) {
|
|
let pirate = this.FindPirate(npcOnlyID);
|
|
if (pirate == null) {
|
|
return;
|
|
}
|
|
if (pirate.vecApply.length <= 0) {
|
|
return;
|
|
}
|
|
let npc = NpcMgr.shared.findNpc(npcOnlyID);
|
|
if (npc == null) {
|
|
return;
|
|
}
|
|
let player = PlayerMgr.shared.getPlayerByRoleId(roleId);
|
|
if (player == null) {
|
|
return;
|
|
}
|
|
let battle = player.monsterBattle(npc.monster_group);
|
|
if (battle == null) {
|
|
return;
|
|
}
|
|
battle.source = npcOnlyID;
|
|
}
|
|
|
|
// 清理海盗
|
|
CheckPirateDead(nOnlyID: any, battle: any) {
|
|
for (let it in this.vecPirate) {
|
|
let pWBomb = this.vecPirate[it];
|
|
if (pWBomb.nOnlyID != nOnlyID)
|
|
continue;
|
|
pWBomb.nOnlyID = 0;
|
|
NpcMgr.shared.DeleteNpc(nOnlyID);
|
|
this.kill_count++;
|
|
this.PirateAward(battle);
|
|
this.AdvanceClose();
|
|
break;
|
|
}
|
|
}
|
|
|
|
PirateAward(battle: any) {
|
|
if (battle != null && battle.campA != undefined) {
|
|
for (let role of battle.campA.broles) {
|
|
// 获取当前队伍
|
|
if (role.living_type == 1) {
|
|
if (role.source != null && role.source != undefined) {
|
|
let player = PlayerMgr.shared.getPlayerByRoleId(role.source.roleid);
|
|
if (player != null) {
|
|
// 获取随机道具
|
|
let item = this.goods_award[Math.floor((Math.random() * this.goods_award.length))];
|
|
let exp = player.level * 6000 + player.relive * 50000;
|
|
let pExp = Math.floor(exp * 1.2);
|
|
player.addExp(exp);
|
|
// 随机发送几个道具
|
|
player.addItem(50004, Math.floor(Math.random() * 2) + 1, true, "海盗奖励")
|
|
player.addItem(item, Math.floor(Math.random() * 3) + 1, true, "海盗奖励")
|
|
player.curPet && (player.curPet.addExp(pExp));
|
|
continue;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
verify(): boolean {
|
|
if (this.vecPirate.length > (this.count * 4)) {
|
|
//关闭定时器
|
|
clearInterval(this.refresh_timer);
|
|
this.refresh_timer = 1;
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
checkMinutesPirate() {
|
|
if (this.verify_timer == 0) {
|
|
this.verify_timer = setTimeout(() => {
|
|
this.verify_timer = 0;
|
|
this.checkMinutesPirate();
|
|
}, 1 * 60 * 1000);
|
|
}
|
|
if (GTimer.getMinutes() >= this.minutes) {
|
|
this.CheckWorldPirateAll();
|
|
if (this.verify_timer == 0) {
|
|
let strRichText = `<color=#00ff00 >入侵的海盗已被击退!看来中州大陆真是能人辈出啊!</c > `;
|
|
PlayerMgr.shared.broadcast('s2c_screen_msg', {
|
|
strRichText: strRichText,
|
|
bInsertFront: 0
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// 怪物全部杀完提前关闭活动
|
|
AdvanceClose() {
|
|
if (this.kill_count >= (this.count * 4)) {
|
|
this.kill_count = 0;
|
|
this.CheckWorldPirateAll();
|
|
let strRichText = `<color=#00ff00 >入侵的海盗已被击退!看来中州大陆真是能人辈出啊!</c > `;
|
|
PlayerMgr.shared.broadcast('s2c_screen_msg', {
|
|
strRichText: strRichText,
|
|
bInsertFront: 0
|
|
});
|
|
}
|
|
}
|
|
|
|
// 清理所有海盗
|
|
CheckWorldPirateAll() {
|
|
//关闭定时器
|
|
clearInterval(this.verify_timer);
|
|
clearInterval(this.refresh_timer);
|
|
SKLogger.info(`清理所有海盗!`)
|
|
for (let it in this.vecPirate) {
|
|
let pWBomb = this.vecPirate[it];
|
|
NpcMgr.shared.DeleteNpc(pWBomb.nOnlyID);
|
|
pWBomb.nOnlyID = 0;
|
|
}
|
|
this.vecPirate = [];
|
|
}
|
|
|
|
}
|