Commit 748e64a6 authored by Yazawazi's avatar Yazawazi Committed by GitHub
Browse files

Moon Card

parent addfb5eb
...@@ -67,6 +67,9 @@ dependencies { ...@@ -67,6 +67,9 @@ dependencies {
implementation group: 'org.greenrobot', name: 'eventbus-java', version: '3.3.1' implementation group: 'org.greenrobot', name: 'eventbus-java', version: '3.3.1'
implementation group: 'org.danilopianini', name: 'java-quadtree', version: '0.1.9' implementation group: 'org.danilopianini', name: 'java-quadtree', version: '0.1.9'
implementation group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.2'
implementation group: 'org.quartz-scheduler', name: 'quartz-jobs', version: '2.3.2'
protobuf files('proto/') protobuf files('proto/')
} }
......
...@@ -81,6 +81,11 @@ public class GenshinPlayer { ...@@ -81,6 +81,11 @@ public class GenshinPlayer {
private int mainCharacterId; private int mainCharacterId;
private boolean godmode; private boolean godmode;
private boolean moonCard;
private Date moonCardStartTime;
private int moonCardDuration;
private Set<Date> moonCardGetTimes;
@Transient private boolean paused; @Transient private boolean paused;
@Transient private int enterSceneToken; @Transient private int enterSceneToken;
@Transient private SceneLoadState sceneState; @Transient private SceneLoadState sceneState;
...@@ -124,6 +129,7 @@ public class GenshinPlayer { ...@@ -124,6 +129,7 @@ public class GenshinPlayer {
this.birthday = new PlayerBirthday(); this.birthday = new PlayerBirthday();
this.rewardedLevels = new HashSet<>(); this.rewardedLevels = new HashSet<>();
this.moonCardGetTimes = new HashSet<>();
} }
// On player creation // On player creation
...@@ -486,6 +492,95 @@ public class GenshinPlayer { ...@@ -486,6 +492,95 @@ public class GenshinPlayer {
this.regionId = regionId; this.regionId = regionId;
} }
public boolean inMoonCard() {
return moonCard;
}
public void setMoonCard(boolean moonCard) {
this.moonCard = moonCard;
}
public void addMoonCardDays(int days) {
this.moonCardDuration += days;
}
public int getMoonCardDuration() {
return moonCardDuration;
}
public void setMoonCardDuration(int moonCardDuration) {
this.moonCardDuration = moonCardDuration;
}
public Date getMoonCardStartTime() {
return moonCardStartTime;
}
public void setMoonCardStartTime(Date moonCardStartTime) {
this.moonCardStartTime = moonCardStartTime;
}
public Set<Date> getMoonCardGetTimes() {
return moonCardGetTimes;
}
public void setMoonCardGetTimes(Set<Date> moonCardGetTimes) {
this.moonCardGetTimes = moonCardGetTimes;
}
public int getMoonCardRemainDays() {
Calendar remainCalendar = Calendar.getInstance();
remainCalendar.setTime(moonCardStartTime);
remainCalendar.add(Calendar.DATE, moonCardDuration);
Date theLastDay = remainCalendar.getTime();
Date now = DateHelper.onlyYDM(new Date());
return (int) ((theLastDay.getTime() - now.getTime()) / (24 * 60 * 60 * 1000)); // By copilot
}
public void rechargeMoonCard() {
LinkedList<GenshinItem> items = new LinkedList<GenshinItem>();
for (int i = 0; i < 300; i++) {
items.add(new GenshinItem(203));
}
inventory.addItems(items);
if (!moonCard) {
moonCard = true;
Date now = new Date();
moonCardStartTime = DateHelper.onlyYDM(now);
moonCardDuration = 30;
} else {
moonCardDuration += 30;
}
if (!moonCardGetTimes.contains(moonCardStartTime)) {
moonCardGetTimes.add(moonCardStartTime);
}
}
public void getTodayMoonCard() {
if (!moonCard) {
return;
}
Date now = DateHelper.onlyYDM(new Date());
if (moonCardGetTimes.contains(now)) {
return;
}
Date stopTime = new Date();
Calendar stopCalendar = Calendar.getInstance();
stopCalendar.setTime(stopTime);
stopCalendar.add(Calendar.DATE, moonCardDuration);
stopTime = stopCalendar.getTime();
if (now.after(stopTime)) {
moonCard = false;
return;
}
moonCardGetTimes.add(now);
addMoonCardDays(1);
GenshinItem genshinItem = new GenshinItem(201, 90);
getInventory().addItem(genshinItem);
session.send(new PacketItemAddHintNotify(genshinItem, ActionReason.BlessingRedeemReward));
session.send(new PacketCardProductRewardNotify(getMoonCardRemainDays()));
}
public boolean inGodmode() { public boolean inGodmode() {
return godmode; return godmode;
} }
......
...@@ -922,6 +922,11 @@ public class InventoryManager { ...@@ -922,6 +922,11 @@ public class InventoryManager {
default: default:
break; break;
} }
if (useItem.getItemId() == 1202) {
player.rechargeMoonCard();
used = 1;
}
if (used > 0) { if (used > 0) {
player.getInventory().removeItem(useItem, used); player.getInventory().removeItem(useItem, used);
......
...@@ -25,6 +25,7 @@ import emu.grasscutter.server.event.ServerEvent; ...@@ -25,6 +25,7 @@ import emu.grasscutter.server.event.ServerEvent;
import emu.grasscutter.server.event.game.ServerTickEvent; import emu.grasscutter.server.event.game.ServerTickEvent;
import emu.grasscutter.server.event.internal.ServerStartEvent; import emu.grasscutter.server.event.internal.ServerStartEvent;
import emu.grasscutter.server.event.internal.ServerStopEvent; import emu.grasscutter.server.event.internal.ServerStopEvent;
import emu.grasscutter.task.TaskMap;
public final class GameServer extends MihoyoKcpServer { public final class GameServer extends MihoyoKcpServer {
private final InetSocketAddress address; private final InetSocketAddress address;
...@@ -40,6 +41,7 @@ public final class GameServer extends MihoyoKcpServer { ...@@ -40,6 +41,7 @@ public final class GameServer extends MihoyoKcpServer {
private final MultiplayerManager multiplayerManager; private final MultiplayerManager multiplayerManager;
private final DungeonManager dungeonManager; private final DungeonManager dungeonManager;
private final CommandMap commandMap; private final CommandMap commandMap;
private final TaskMap taskMap;
public GameServer(InetSocketAddress address) { public GameServer(InetSocketAddress address) {
super(address); super(address);
...@@ -57,6 +59,7 @@ public final class GameServer extends MihoyoKcpServer { ...@@ -57,6 +59,7 @@ public final class GameServer extends MihoyoKcpServer {
this.multiplayerManager = new MultiplayerManager(this); this.multiplayerManager = new MultiplayerManager(this);
this.dungeonManager = new DungeonManager(this); this.dungeonManager = new DungeonManager(this);
this.commandMap = new CommandMap(true); this.commandMap = new CommandMap(true);
this.taskMap = new TaskMap(true);
// Schedule game loop. // Schedule game loop.
Timer gameLoop = new Timer(); Timer gameLoop = new Timer();
...@@ -114,6 +117,10 @@ public final class GameServer extends MihoyoKcpServer { ...@@ -114,6 +117,10 @@ public final class GameServer extends MihoyoKcpServer {
public CommandMap getCommandMap() { public CommandMap getCommandMap() {
return this.commandMap; return this.commandMap;
} }
public TaskMap getTaskMap() {
return this.taskMap;
}
public void registerPlayer(GenshinPlayer player) { public void registerPlayer(GenshinPlayer player) {
getPlayers().put(player.getUid(), player); getPlayers().put(player.getUid(), player);
......
package emu.grasscutter.task;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Task {
String taskName() default "NO_NAME";
String taskCronExpression() default "0 0 0 0 0 ?";
String triggerName() default "NO_NAME";
}
package emu.grasscutter.task;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public interface TaskHandler extends Job {
default void execute(JobExecutionContext context) throws JobExecutionException {
}
}
package emu.grasscutter.task;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.spi.MutableTrigger;
import org.reflections.Reflections;
import java.util.*;
@SuppressWarnings({"UnusedReturnValue", "unused"})
public final class TaskMap {
private final Map<String, TaskHandler> tasks = new HashMap<>();
private final Map<String, Task> annotations = new HashMap<>();
private final SchedulerFactory schedulerFactory = new StdSchedulerFactory();
public TaskMap() {
this(false);
}
public TaskMap(boolean scan) {
if (scan) this.scan();
}
public static TaskMap getInstance() {
return Grasscutter.getGameServer().getTaskMap();
}
public TaskMap registerTask(String taskName, TaskHandler task) {
Task annotation = task.getClass().getAnnotation(Task.class);
this.annotations.put(taskName, annotation);
this.tasks.put(taskName, task);
// register task
try {
Scheduler scheduler = schedulerFactory.getScheduler();
JobDetail job = JobBuilder
.newJob(task.getClass())
.withIdentity(taskName)
.build();
Trigger convTrigger = TriggerBuilder.newTrigger()
.withIdentity(annotation.triggerName())
.withSchedule(CronScheduleBuilder.cronSchedule(annotation.taskCronExpression()))
.build();
scheduler.scheduleJob(job, convTrigger);
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
return this;
}
public List<TaskHandler> getHandlersAsList() {
return new LinkedList<>(this.tasks.values());
}
public HashMap<String, TaskHandler> getHandlers() {
return new LinkedHashMap<>(this.tasks);
}
public TaskHandler getHandler(String taskName) {
return this.tasks.get(taskName);
}
private void scan() {
Reflections reflector = Grasscutter.reflector;
Set<Class<?>> classes = reflector.getTypesAnnotatedWith(Task.class);
System.out.println("Found " + classes.size() + " tasks.");
classes.forEach(annotated -> {
try {
Task taskData = annotated.getAnnotation(Task.class);
Object object = annotated.newInstance();
if (object instanceof TaskHandler)
this.registerTask(taskData.taskName(), (TaskHandler) object);
else Grasscutter.getLogger().error("Class " + annotated.getName() + " is not a TaskHandler!");
} catch (Exception exception) {
Grasscutter.getLogger().error("Failed to register task handler for " + annotated.getSimpleName(), exception);
}
});
}
}
package emu.grasscutter.task.tasks;
import emu.grasscutter.database.DatabaseManager;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.task.Task;
import emu.grasscutter.task.TaskHandler;
import java.util.List;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
@Task(taskName = "MoonCard", taskCronExpression = "0 0 0 * * ?", triggerName = "MoonCardTrigger")
public final class MoonCard implements TaskHandler {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
List<GenshinPlayer> players = DatabaseManager.getDatastore().find(GenshinPlayer.class).stream().toList();
for (GenshinPlayer player : players) {
if (player.isOnline()) {
if (player.inMoonCard()) {
player.getTodayMoonCard();
}
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment