Java tutorial
/* * Copyright 2014. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.ramidore.logic.system; import javafx.scene.control.TableView; import lombok.Getter; import lombok.Setter; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.ramidore.bean.PbLogBean; import org.ramidore.bean.PbStatTable; import org.ramidore.core.PacketData; import org.ramidore.util.RamidoreUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * ?. * * @author atmark */ public class PointBattleLogic extends AbstractSystemMessageLogic { /** * Logger. */ private static final Logger LOG = LoggerFactory.getLogger(PointBattleLogic.class); /** * ?????. */ private static final String UNIT_PATTERN = "10005C1200000B00....0000(......)00"; /** * ?????. */ private static final String PATTERN = "^(?:.{2})*(" + UNIT_PATTERN + ")(?:.{2})*$"; /** * ??. */ private static Pattern unitPattern = Pattern.compile(UNIT_PATTERN); /** * ??. */ private static Pattern pattern = Pattern.compile(PATTERN); /** * JavaFX??????LinkedQueue?. */ @Getter private List<ConcurrentLinkedQueue<PbLogBean>> chartDataQList = new ArrayList<>(); /** * . */ @Setter private TableView<PbStatTable> statTable; /** * ?. * <p> * ??? */ private String id; /** * ?. */ private int sequentialNo = 1; /** * ????. */ private int currentStageNo = 0; /** * ?. */ private int stageSequentialNo = 1; /** * . */ private boolean isEnd = false; /** * ??. */ private PbLogBean currentData = null; /** * ???. */ private Map<Integer, Integer> pointMap = new HashMap<>(); /** * ??. */ private PbStatTable currentStat = null; /** * ??. */ private DuplicateChecker dupChecker; /** * . */ public PointBattleLogic() { currentStat = new PbStatTable(); for (int i = 0; i < 6; i++) { chartDataQList.add(new ConcurrentLinkedQueue<>()); } pointMap.put(0, 0); dupChecker = new DuplicateChecker(); } @Override public boolean execute(PacketData data) { Matcher matcher = pattern.matcher(data.getStrData()); if (!isEnd && currentStageNo < 6 && matcher.matches()) { if (currentStageNo == 0) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); this.id = sdf.format(new Date()); LOG.info(id); currentStat.setId(id); addStageNo(); } Matcher unitMatcher = unitPattern.matcher(data.getStrData()); while (unitMatcher.find()) { ConcurrentLinkedQueue<PbLogBean> dataQ = chartDataQList.get(currentStageNo - 1); ConcurrentLinkedQueue<PbLogBean> allDataQ = chartDataQList.get(5); int point = RamidoreUtil.intValueFromDescHexString(unitMatcher.group(1)); if (!dupChecker.check(point)) { continue; } pointMap.put(currentStageNo, point); currentData = new PbLogBean(id, sequentialNo, currentStageNo, stageSequentialNo, point, pointMap.get(currentStageNo - 1)); dataQ.add(currentData); allDataQ.add(currentData); // statRealTimeData(); LOG.info(sequentialNo + "\t" + currentStageNo + "\t" + stageSequentialNo + "\t" + point); sequentialNo++; stageSequentialNo++; } return true; } else if (currentStageNo == 6) { isEnd = true; } return false; } /** * ??. */ public void addStageNo() { currentStageNo++; stageSequentialNo = 1; } /** * ??. */ public void loadPastData() { Collection<File> fileList = FileUtils.listFiles(new File("./"), new String[] { "log" }, false); for (File file : fileList) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); List<String> list = IOUtils.readLines(br); if (list.size() < 2) { continue; } String id = list.get(0); if (id.equals(this.id)) { // ?? continue; } List<PbLogBean> dataList = new ArrayList<>(); for (int i = 1; i < list.size(); i++) { PbLogBean bean = loadLine(id, list.get(i)); dataList.add(bean); } statData(id, dataList); } catch (FileNotFoundException e) { LOG.error(ExceptionUtils.getStackTrace(e)); } catch (IOException e) { LOG.error(ExceptionUtils.getStackTrace(e)); } finally { IOUtils.closeQuietly(br); } } } /** * ?1???. * * @param id ? * @param line 1? * @return PointBatteleChartBean */ private PbLogBean loadLine(String id, String line) { String[] element = StringUtils.split(line, '\t'); if (element.length != 4) { return null; } return new PbLogBean(id, Integer.valueOf(element[0]), Integer.valueOf(element[1]), Integer.valueOf(element[2]), Integer.valueOf(element[3])); } /** * 1??????. * * @param id ? * @param dataList ? */ private void statData(String id, List<PbLogBean> dataList) { // key : ? value : Map<Integer, Integer> pointMap = new HashMap<>(); // key : ? value : ???????? Map<Integer, Integer> mobCountMap = new HashMap<>(); for (int i = 0; i < 7; i++) { // 0?? pointMap.put(i, 0); mobCountMap.put(i, 0); } for (PbLogBean data : dataList) { pointMap.put(data.getStageNo(), data.getPoint()); mobCountMap.put(data.getStageNo(), data.getStageSequentialNo()); } for (PbLogBean data : dataList) { data.setPointOffset(pointMap.get(data.getStageNo() - 1)); ConcurrentLinkedQueue<PbLogBean> dataQ = chartDataQList.get(data.getStageNo() - 1); ConcurrentLinkedQueue<PbLogBean> allDataQ = chartDataQList.get(chartDataQList.size() - 1); dataQ.add(data); allDataQ.add(data); } PbStatTable row = new PbStatTable(); row.setId(id); row.setPoint1(pointMap.get(1)); row.setMobCount1(mobCountMap.get(1)); row.setStage1(); row.setPoint2(pointMap.get(2) - pointMap.get(1)); row.setMobCount2((mobCountMap.get(2))); row.setStage2(); row.setPoint3(pointMap.get(3) - pointMap.get(2)); row.setMobCount3((mobCountMap.get(3))); row.setStage3(); row.setPoint4(pointMap.get(4) - pointMap.get(3)); row.setMobCount4(mobCountMap.get(4)); row.setStage4(); row.setPoint5(pointMap.get(5) - pointMap.get(4)); row.setMobCount5(mobCountMap.get(5)); row.setStage5(); row.setPoint2Total(row.getPoint1() + row.getPoint2()); row.setPoint3Total(row.getPoint2Total() + row.getPoint3()); row.setPoint4Total(row.getPoint3Total() + row.getPoint4()); row.setPointTotal(pointMap.get(5)); statTable.getItems().add(row); } /** * ???. */ private void statRealTimeData() { if (!statTable.getItems().contains(currentStat)) { statTable.getItems().add(0, currentStat); } PbStatTable stat = null; for (PbStatTable t : statTable.getItems()) { if (this.id.equals(t.getId())) { stat = t; break; } } if (stat == null) { return; } int p = currentData.getPoint(); switch (currentStageNo) { case 1: stat.setMobCount1(currentData.getStageSequentialNo()); stat.setPoint1(p); stat.setStage1(); break; case 2: stat.setMobCount2(currentData.getStageSequentialNo()); // ???? stat.setPoint2(p - stat.getPoint1()); stat.setStage2(); stat.setPoint2Total(p); break; case 3: stat.setMobCount3(currentData.getStageSequentialNo()); stat.setPoint3(p - (stat.getPoint1() + stat.getPoint2())); stat.setStage3(); stat.setPoint3Total(p); break; case 4: stat.setMobCount4(currentData.getStageSequentialNo()); stat.setPoint4(p - (stat.getPoint1() + stat.getPoint2() + stat.getPoint3())); stat.setStage4(); stat.setPoint4Total(p); break; case 5: stat.setMobCount5(currentData.getStageSequentialNo()); stat.setPoint5(p - (stat.getPoint1() + stat.getPoint2() + stat.getPoint3() + stat.getPoint4())); stat.setStage5(); break; default: stat.setPointTotal(p); break; } stat.setPointTotal(p); } /** * ????. * * @author atmark */ private class DuplicateChecker { private Set<Integer> set; public DuplicateChecker() { set = new HashSet<>(); } public boolean check(int point) { if (set.contains(point)) { return false; } set.add(point); return true; } } }