PlayerActivityData.java 4.11 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
import emu.grasscutter.net.proto.ActivityWatcherInfoOuterClass;
import emu.grasscutter.server.packet.send.PacketActivityUpdateWatcherNotify;
16
import emu.grasscutter.utils.Utils;
Akka's avatar
Akka committed
17
18
19
20
21
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.FieldDefaults;

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

@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
37
38
39
    /**
     * the detail data of each type of activity (Json format)
     */
Akka's avatar
Akka committed
40
41
    String detail;
    @Transient Player player;
Akka's avatar
Akka committed
42
    @Transient ActivityHandler activityHandler;
Akka's avatar
Akka committed
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
70
    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
71
    public void setDetail(Object detail){
72
        this.detail = Utils.jsonEncode(detail);
Akka's avatar
Akka committed
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
99
    }

    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
100
101
102
103
104
105
106
107
108
109
    @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
110
111
112
113
        public ActivityWatcherData getMetadata(){
            return GameData.getActivityWatcherDataMap().get(watcherId);
        }

Akka's avatar
Akka committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        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();
        }
    }
}