hjl
2024-07-08 0287a322e16580901ddb8e781df9da616f4751f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package com.ruoyi.study.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.study.domain.TGame;
import com.ruoyi.study.domain.TGameRecord;
import com.ruoyi.study.domain.TStudy;
import com.ruoyi.study.domain.TUserStudy;
import com.ruoyi.study.dto.CompleteGameDTO;
import com.ruoyi.study.dto.CompleteStudyDTO;
import com.ruoyi.study.mapper.TUserStudyMapper;
import com.ruoyi.study.service.ITGameRecordService;
import com.ruoyi.study.service.ITUserStudyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 自主游戏 服务实现类
 * </p>
 *
 * @author 无关风月
 * @since 2024-04-26
 */
@Service
public class TUserStudyServiceImpl extends ServiceImpl<TUserStudyMapper, TUserStudy> implements ITUserStudyService {
 
    @Resource
    private ITGameRecordService gameRecordService;
 
    /**
     * 所属day map
     */
    private static final Map<String, Integer> DAY_MAP = new HashMap<>(12);
 
    /**
     * 所属day map
     */
    private static final Map<Integer, Integer> GAME_DIFFICULTY_MAP = new HashMap<>(12);
 
    static {
        // 所属day
        DAY_MAP.put(Constants.ONE_STR, Constants.TWO);
        DAY_MAP.put(Constants.TWO_STR, Constants.THREE);
        DAY_MAP.put(Constants.THREE_STR, Constants.FOUR);
        DAY_MAP.put(Constants.FOUR_STR, Constants.FIVE);
        DAY_MAP.put(Constants.FIVE_STR, Constants.SIX);
        DAY_MAP.put(Constants.SIX_STR, Constants.ONE);
        // 游戏难度
        GAME_DIFFICULTY_MAP.put(Constants.ZERO, Constants.ONE);
        GAME_DIFFICULTY_MAP.put(Constants.ONE, Constants.TWO);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resettingStudyRecord(String time) {
        List<TUserStudy> list = lambdaQuery().eq(TUserStudy::getDisabled, 0).list();
        switch (time) {
            case Constants.DAY:
                list.forEach(data -> data.setTodayStudy(0));
                break;
            case Constants.WEEK:
                list.forEach(data -> data.setWeekStudy(0));
                break;
            case Constants.MONTH:
                list.forEach(data -> data.setMonthStudy(0));
                break;
            default:
        }
        // 自旋重试
        int number = 0;
        boolean update = this.updateBatchById(list);
        while (!update) {
            if (number >= 3) {
                break;
            }
            update = this.updateBatchById(list);
            number++;
        }
    }
 
    @Override
    public Boolean exchangeStudyRecord(TUserStudy userStudyRecord, Integer userId, CompleteStudyDTO completeStudy) {
        Integer studyTime = completeStudy.getStudyTime();
        // 学习记录
        Integer week = userStudyRecord.getWeek();
        Integer day = userStudyRecord.getDay();
        if (week.equals(completeStudy.getWeek()) && day.equals(completeStudy.getDay())) {
            Integer type = completeStudy.getType();
            if (Constants.ONE.equals(type)) {
                userStudyRecord.setListen(Constants.ONE_HUNDRED);
                userStudyRecord.setLook(Constants.BURDEN_ONE);
                userStudyRecord.setInduction(Constants.BURDEN_ONE);
                userStudyRecord.setAnswer(Constants.BURDEN_ONE);
                userStudyRecord.setPair(Constants.BURDEN_ONE);
            } else if (Constants.TWO.equals(type)) {
                userStudyRecord.setLook(Constants.ONE_HUNDRED);
                userStudyRecord.setInduction(Constants.BURDEN_ONE);
                userStudyRecord.setAnswer(Constants.BURDEN_ONE);
                userStudyRecord.setPair(Constants.BURDEN_ONE);
            } else if (Constants.THREE.equals(type)) {
                userStudyRecord.setInduction(Constants.ONE_HUNDRED);
                userStudyRecord.setAnswer(Constants.BURDEN_ONE);
                userStudyRecord.setPair(Constants.BURDEN_ONE);
            } else if (Constants.FOUR.equals(type)) {
                userStudyRecord.setAnswer(Constants.ONE_HUNDRED);
                userStudyRecord.setPair(Constants.BURDEN_ONE);
            } else if (Constants.FIVE.equals(type)) {
                Integer nextDay = DAY_MAP.get(String.valueOf(userStudyRecord.getDay()));
                // type为5并且day为5应该进入游戏日
                if (Constants.FIVE.equals(completeStudy.getDay())) {
                    userStudyRecord.setPair(Constants.ONE_HUNDRED);
                    // 游戏难度初始化 0(入门难度)
                    userStudyRecord.setGameDifficulty(Constants.ZERO);
                } else {
                    userStudyRecord.setListen(Constants.BURDEN_ONE);
                    userStudyRecord.setLook(Constants.BURDEN_ONE);
                    userStudyRecord.setInduction(Constants.BURDEN_ONE);
                    userStudyRecord.setAnswer(Constants.BURDEN_ONE);
                    userStudyRecord.setPair(Constants.BURDEN_ONE);
                }
                userStudyRecord.setDay(nextDay);
            }
        }
        // 更新学习时长
        userStudyRecord.setTotalStudy(userStudyRecord.getTotalStudy() + studyTime);
        userStudyRecord.setTodayStudy(userStudyRecord.getTodayStudy() + studyTime);
        userStudyRecord.setWeekStudy(userStudyRecord.getWeekStudy() + studyTime);
        userStudyRecord.setMonthStudy(userStudyRecord.getMonthStudy() + studyTime);
        // 更新学习进度及学习时长
        return this.updateById(userStudyRecord);
    }
 
    @Override
    public void checkRate(TGame game, Integer userid, CompleteGameDTO completeStudy, List<TStudy> studyList) {
        String answerRate = game.getAnswerRate();
        String rate = game.getRate().split(",")[Constants.TWO];
        // 校验是否完成超级听力
        List<TGameRecord> gameRecordList = gameRecordService.lambdaQuery().eq(TGameRecord::getGameId, game.getId())
                .eq(TGameRecord::getGameDifficulty, Constants.TWO)
                .ge(TGameRecord::getAccuracy, rate).list();
        // 下一周目逻辑
        int quarterItem = 1;
        Map<Integer, List<TStudy>> studyMap = new HashMap<>(8);
        for (TStudy study : studyList) {
            Integer quarter = study.getQuarter();
            List<TStudy> itemList = studyMap.get(quarter);
            if (null == itemList) {
                itemList = new ArrayList<>();
            }
            itemList.add(study);
            studyMap.put(quarter, itemList);
            // 记录学习季度
            if (study.getId().equals(game.getStudyId())) {
                quarterItem = quarter;
            }
        }
        // 获取当前季度所有周目
        List<TStudy> studyList1 = studyMap.get(quarterItem);
        // 正确率达到通关率
        if (completeStudy.getAccuracy() >= Integer.parseInt(answerRate)
                && !gameRecordList.isEmpty()) {
            // 获取用户超级记忆游戏记录
            TUserStudy userStudy = this.lambdaQuery().eq(TUserStudy::getUserId, userid).one();
            for (int i = 0; i < studyList1.size(); i++) {
                TStudy tStudy = studyList1.get(i);
                if (tStudy.getWeek().equals(userStudy.getWeek())) {
                    if (studyList1.size() == 1 || i + 1 == studyList1.size()) {
                        // 是否为当前季度最后一周目
                        List<TStudy> studyList2 = studyMap.get(quarterItem + 1);
                        // 下一季度数据为空
                        if (null != studyList2 && !studyList2.isEmpty()) {
                            TStudy tStudy1 = studyList2.get(Constants.ZERO);
                            userStudy.setWeek(tStudy1.getWeek());
                        }
                    } else {
                        TStudy tStudy1 = studyList1.get(i + 1);
                        userStudy.setWeek(tStudy1.getWeek());
                    }
                    break;
                }
            }
            userStudy.setDay(DAY_MAP.get(String.valueOf(userStudy.getDay())));
            userStudy.setLook(Constants.BURDEN_ONE);
            userStudy.setPair(Constants.BURDEN_ONE);
            userStudy.setInduction(Constants.BURDEN_ONE);
            userStudy.setListen(Constants.BURDEN_ONE);
            userStudy.setAnswer(Constants.BURDEN_ONE);
            userStudy.setGameDifficulty(Constants.ZERO);
            this.updateById(userStudy);
        }
    }
 
    @Override
    public void gameHearing(TGame game, Integer userid, CompleteGameDTO completeStudy, List<TStudy> studyList) {
        // 超级听力通关率
        String answerRate = game.getRate().split(",")[completeStudy.getDifficulty()];
        if (completeStudy.getAccuracy() >= Integer.parseInt(answerRate)) {
            // 获取用户超级记忆游戏记录
            TUserStudy userStudy = this.lambdaQuery().eq(TUserStudy::getUserId, userid).one();
            // 更新游戏难度学习记录
            if (!Constants.TWO.equals(userStudy.getGameDifficulty())) {
                // 获取用户游戏进度
                List<TGameRecord> list = gameRecordService.lambdaQuery().eq(TGameRecord::getUserId, userid)
                        .eq(TGameRecord::getGameId, game.getId())
                        .ge(TGameRecord::getAccuracy, answerRate)
                        .eq(TGameRecord::getGameDifficulty, completeStudy.getDifficulty()).list();
                if (!list.isEmpty()) {
                    Integer integer = GAME_DIFFICULTY_MAP.get(completeStudy.getDifficulty());
                    userStudy.setGameDifficulty(integer);
                }
            } else {
                // 游戏难度为2,进入下一周目学习
                List<TGameRecord> gameRecordList = gameRecordService.lambdaQuery().eq(TGameRecord::getGameId, game.getId())
                        .eq(TGameRecord::getGameName, Constants.MEMORY)
                        .ge(TGameRecord::getAccuracy, game.getAnswerRate()).list();
                // 判断是否完成超级记忆
                if (!gameRecordList.isEmpty()) {
                    int quarterItem = 1;
                    Map<Integer, List<TStudy>> studyMap = new HashMap<>(8);
                    for (TStudy study : studyList) {
                        Integer quarter = study.getQuarter();
                        List<TStudy> itemList = studyMap.get(quarter);
                        if (null == itemList) {
                            itemList = new ArrayList<>();
                        }
                        itemList.add(study);
                        studyMap.put(quarter, itemList);
                        // 记录学习季度
                        if (study.getId().equals(game.getStudyId())) {
                            quarterItem = quarter;
                        }
                    }
                    // 获取当前季度所有周目
                    List<TStudy> studyList1 = studyMap.get(quarterItem);
                    // 获取用户超级记忆游戏记录
                    for (int i = 0; i < studyList1.size(); i++) {
                        TStudy tStudy = studyList1.get(i);
                        if (tStudy.getWeek().equals(userStudy.getWeek())) {
                            if (studyList1.size() == 1 || i + 1 == studyList1.size()) {
                                // 是否为当前季度最后一周目
                                List<TStudy> studyList2 = studyMap.get(quarterItem + 1);
                                // 下一季度数据为空
                                if (null != studyList2 && !studyList2.isEmpty()) {
                                    TStudy tStudy1 = studyList2.get(Constants.ZERO);
                                    userStudy.setWeek(tStudy1.getWeek());
                                }
                            } else {
                                TStudy tStudy1 = studyList1.get(i + 1);
                                userStudy.setWeek(tStudy1.getWeek());
                            }
                        }
                    }
                    userStudy.setDay(DAY_MAP.get(String.valueOf(userStudy.getDay())));
                    userStudy.setLook(Constants.BURDEN_ONE);
                    userStudy.setPair(Constants.BURDEN_ONE);
                    userStudy.setInduction(Constants.BURDEN_ONE);
                    userStudy.setListen(Constants.BURDEN_ONE);
                    userStudy.setAnswer(Constants.BURDEN_ONE);
                    userStudy.setGameDifficulty(Constants.ZERO);
                }
            }
            this.updateById(userStudy);
        }
    }
 
}