138 lines
4.5 KiB
TypeScript
138 lines
4.5 KiB
TypeScript
// 军团活动蛮夷入侵城门血量等数据存储
|
|
|
|
import { GUILD_ACTIVITY_STATUS } from "../../consts";
|
|
import { CityActivityData } from "../../domain/battleField/guildActivity";
|
|
import { getCityActivityGateHp } from "./guildActivityService";
|
|
|
|
// 军团诸侯混战等数据
|
|
export class CityActivityObject {
|
|
data: CityActivityData = new CityActivityData();
|
|
|
|
|
|
public getKey(serverId: number, cityId: number) {
|
|
return `${serverId}_${cityId}`;
|
|
}
|
|
|
|
public decodeKey(key: string) {
|
|
let arr = key.split('_');
|
|
return {
|
|
serverId: parseInt(arr[0]),
|
|
cityId: parseInt(arr[1])
|
|
}
|
|
}
|
|
|
|
public getObj( serverId: number, cityId: number, guildCode: string) {
|
|
let key = this.getKey(serverId, cityId);
|
|
return {
|
|
gateHp: this.data.gateHp.get(key),
|
|
maxHp: this.data.maxHp.get(key),
|
|
members: this.data.members.get(guildCode),
|
|
city: this.data.cities.get(key),
|
|
guild: this.data.guilds.get(guildCode)
|
|
}
|
|
}
|
|
|
|
public startActivity() {
|
|
this.data.startTime = Date.now();
|
|
this.data.guildActivityStatus = GUILD_ACTIVITY_STATUS.START;
|
|
}
|
|
|
|
public endActivity() {
|
|
this.data.guildActivityStatus = GUILD_ACTIVITY_STATUS.END;
|
|
}
|
|
|
|
public getStatus() {
|
|
return this.data.guildActivityStatus;
|
|
}
|
|
|
|
public getTimeGap() {
|
|
return Math.floor((Date.now() - this.data.startTime)/1000);
|
|
}
|
|
|
|
public lockCity(serverId: number, cityId: number) {
|
|
if(this.hasSent(serverId, cityId)) return false;
|
|
this.unlockCity(serverId, cityId);
|
|
return true;
|
|
}
|
|
|
|
private unlockCity(serverId: number, cityId: number) {
|
|
let key = this.getKey(serverId, cityId);
|
|
// let guildCodes = this.cities.get(key)||[];
|
|
// for(let guildCode of guildCodes) {
|
|
// this.members.delete(guildCode);
|
|
// this.guilds.delete(guildCode);
|
|
// }
|
|
// this.cities.delete(key);
|
|
this.data.sentCity.push(key);
|
|
}
|
|
|
|
public hasSent(serverId: number, cityId: number) {
|
|
let key = this.getKey(serverId, cityId);
|
|
return this.data.sentCity.indexOf(key) != -1;
|
|
}
|
|
|
|
public getAllCities() {
|
|
let allCities = new Array<{ serverId: number, cityId: number, guildCodes: string[]}>();
|
|
let serverlists = new Array<number>();
|
|
for(let [key, guildCodes] of this.data.cities) {
|
|
let { serverId, cityId } = this.decodeKey(key);
|
|
if(!this.hasSent(serverId, cityId)) {
|
|
allCities.push({ serverId, cityId, guildCodes });
|
|
if(!serverlists.includes(serverId)) serverlists.push(serverId);
|
|
}
|
|
}
|
|
return {cities: allCities, serverlists};
|
|
}
|
|
|
|
public getGuildsInCity(serverId: number, cityId: number) {
|
|
let key = this.getKey(serverId, cityId);
|
|
return this.data.cities.get(key)||[];
|
|
}
|
|
|
|
public getMembersOfGuild(guildCode: string) {
|
|
return this.data.members.get(guildCode)||[];
|
|
}
|
|
|
|
public async getGateHpAndInc(serverId: number, cityId: number, inc: number = 0) {
|
|
let key = this.getKey(serverId, cityId);
|
|
let gateHp = this.data.gateHp.get(key);
|
|
if(!this.data.gateHp.has(key)) {
|
|
gateHp = await getCityActivityGateHp(serverId, cityId);
|
|
this.data.gateHp.set(key, gateHp);
|
|
this.data.maxHp.set(key, gateHp);
|
|
}
|
|
if(inc != 0) {
|
|
gateHp += inc;
|
|
this.data.gateHp.set(key, gateHp);
|
|
}
|
|
if(gateHp <= 0) gateHp = 0;
|
|
return { gateHp, maxHp: this.data.maxHp.get(key)||0 }
|
|
}
|
|
|
|
public pushMembers(guildCode: string, roleId: string, job: number, code: string) {
|
|
|
|
if(this.data.members.has(guildCode)) {
|
|
let members = this.data.members.get(guildCode);
|
|
if(members.findIndex(cur => cur.roleId == roleId) == -1) {
|
|
members.push({ roleId, job, code });
|
|
}
|
|
} else {
|
|
let arr = new Array<string>();
|
|
arr.push(roleId);
|
|
this.data.members.set(guildCode, [{ roleId, job, code }]);
|
|
}
|
|
}
|
|
|
|
public pushGuild(guildCode: string, serverId: number, cityId: number) {
|
|
let key = this.getKey(serverId, cityId);
|
|
if(!this.data.cities.has(key)) {
|
|
this.data.cities.set(key, [guildCode]);
|
|
} else {
|
|
let arr = this.data.cities.get(key);
|
|
if(!arr.includes(guildCode)) {
|
|
arr.push(guildCode);
|
|
}
|
|
}
|
|
this.data.guilds.set(guildCode, cityId);
|
|
}
|
|
} |