PlayerActivityData.java 4.1 KB
Newer Older
Akka's avatar
Akka committed
1
2
3
4
5
package emu.grasscutter.game.activity;

import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Transient;
Akka's avatar
Akka committed
6
7
8
9
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.excels.ActivityWatcherData;
Akka's avatar
Akka committed
10
import emu.grasscutter.database.DatabaseHelper;
Akka's avatar
Akka committed
11
import emu.grasscutter.game.inventory.GameItem;
Akka's avatar
Akka committed
12
import emu.grasscutter.game.player.Player;
Akka's avatar
Akka committed
13
import emu.grasscutter.game.props.ActionReason;
Akka's avatar
Akka committed
14
15
16
17
18
19
20
import emu.grasscutter.net.proto.ActivityWatcherInfoOuterClass;
import emu.grasscutter.server.packet.send.PacketActivityUpdateWatcherNotify;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.FieldDefaults;

Akka's avatar
Akka committed
21
import java.util.ArrayList;
Akka's avatar
Akka committed
22
23
import java.util.List;
import java.util.Map;
Akka's avatar
Akka committed
24
import java.util.Optional;
Akka's avatar
Akka committed
25
26
27
28
29
30
31
32
33
34
35

@Entity("activities")
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
@Builder(builderMethodName = "of")
public class PlayerActivityData {
    @Id
    String id;
    int uid;
    int activityId;
    Map<Integer, WatcherInfo> watcherInfoMap;
Akka's avatar
Akka committed
36
37
38
    /**
     * the detail data of each type of activity (Json format)
     */
Akka's avatar
Akka committed
39
40
    String detail;
    @Transient Player player;
Akka's avatar
Akka committed
41
    @Transient ActivityHandler activityHandler;
Akka's avatar
Akka committed
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
    public void save(){
        DatabaseHelper.savePlayerActivityData(this);
    }

    public static PlayerActivityData getByPlayer(Player player, int activityId){
        return DatabaseHelper.getPlayerActivityData(player.getUid(), activityId);
    }

    public synchronized void addWatcherProgress(int watcherId){
        var watcherInfo = watcherInfoMap.get(watcherId);
        if(watcherInfo == null){
            return;
        }

        if(watcherInfo.curProgress >= watcherInfo.totalProgress){
            return;
        }

        watcherInfo.curProgress++;
        getPlayer().sendPacket(new PacketActivityUpdateWatcherNotify(activityId, watcherInfo));
    }

    public List<ActivityWatcherInfoOuterClass.ActivityWatcherInfo> getAllWatcherInfoList() {
        return watcherInfoMap.values().stream()
            .map(WatcherInfo::toProto)
            .toList();
    }

Akka's avatar
Akka committed
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
    public void setDetail(Object detail){
        this.detail = Grasscutter.getGsonFactory().toJson(detail);
    }

    public void takeWatcherReward(int watcherId) {
        var watcher = watcherInfoMap.get(watcherId);
        if(watcher == null || watcher.isTakenReward()){
            return;
        }

        var reward = Optional.of(watcher)
            .map(WatcherInfo::getMetadata)
            .map(ActivityWatcherData::getRewardID)
            .map(id -> GameData.getRewardDataMap().get(id.intValue()));

        if(reward.isEmpty()){
            return;
        }

        List<GameItem> rewards = new ArrayList<>();
        for (ItemParamData param : reward.get().getRewardItemList()) {
            rewards.add(new GameItem(param.getId(), Math.max(param.getCount(), 1)));
        }

        player.getInventory().addItems(rewards, ActionReason.ActivityWatcher);
        watcher.setTakenReward(true);
        save();
    }

Akka's avatar
Akka committed
99
100
101
102
103
104
105
106
107
108
    @Entity
    @Data
    @FieldDefaults(level = AccessLevel.PRIVATE)
    @Builder(builderMethodName = "of")
    public static class WatcherInfo{
        int watcherId;
        int totalProgress;
        int curProgress;
        boolean isTakenReward;

Akka's avatar
Akka committed
109
110
111
112
        public ActivityWatcherData getMetadata(){
            return GameData.getActivityWatcherDataMap().get(watcherId);
        }

Akka's avatar
Akka committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        public static WatcherInfo init(ActivityWatcher watcher){
            return WatcherInfo.of()
                .watcherId(watcher.getWatcherId())
                .totalProgress(watcher.getActivityWatcherData().getProgress())
                .isTakenReward(false)
                .build();
        }

        public ActivityWatcherInfoOuterClass.ActivityWatcherInfo toProto(){
            return ActivityWatcherInfoOuterClass.ActivityWatcherInfo.newBuilder()
                .setWatcherId(watcherId)
                .setCurProgress(curProgress)
                .setTotalProgress(totalProgress)
                .setIsTakenReward(isTakenReward)
                .build();
        }
    }
}