RuneHive-Game
Loading...
Searching...
No Matches
GlobalRecords.java
Go to the documentation of this file.
1/*
2package com.runehive.content.activity.record;
3
4import com.google.common.reflect.TypeToken;
5import com.google.gson.Gson;
6import com.google.gson.GsonBuilder;
7import com.google.gson.JsonParser;
8import com.runehive.content.activity.ActivityType;
9import com.runehive.game.world.entity.mob.player.Player;
10import com.runehive.net.packet.out.SendScrollbar;
11import com.runehive.net.packet.out.SendString;
12import com.runehive.util.GsonUtils;
13import com.runehive.util.Utility;
14
15import java.io.FileReader;
16import java.io.FileWriter;
17import java.lang.reflect.Type;
18import java.nio.file.Path;
19import java.nio.file.Paths;
20import java.util.*;
21
22*/
23/**
24 * Created by Daniel on 2017-11-22.
25 *//*
26
27public class GlobalRecords {
28
29 private static Set<GameRecord> GLOBAL_RECORDS = new HashSet<>();
30
31 public static void display(Player player, ActivityType activity) {
32 List<GameRecord> list = getActivities(activity);
33 player.gameRecord.showActivities(activity);
34
35 for (int index = 0, count = 0; index < 25; index++) {
36 if (index >= list.size())
37 continue;
38 GameRecord record = list.get(index);
39 count++;
40 String prefix = index == 1 ? "<clan=6>" : (count == 6 ? "<clan=5>" : (count == 11 ? "<clan=4>" : (count / 5) + 1 + ")"));
41 player.send(new SendString(prefix, 32401 + count));
42 count++;
43 player.send(new SendString(record.name, 32401 + count));
44 count++;
45 player.send(new SendString(Utility.getTime(record.time), 32401 + count));
46 count++;
47 player.send(new SendString(record.date, 32401 + count));
48 count++;
49 }
50
51 player.send(new SendScrollbar(32400, 696));
52 player.interfaceManager.open(32300);
53 }
54
55 public static void add(Player player, GameRecord tracker) {
56 GameRecord record = getTracker(player.getName(), tracker.activityType);
57 if (record == null) {
58 GLOBAL_RECORDS.add(tracker);
59 return;
60 }
61 if (tracker.time > record.time) {
62 GLOBAL_RECORDS.remove(record);
63 GLOBAL_RECORDS.add(tracker);
64 }
65 }
66
67 private static GameRecord getTracker(String name, ActivityType activity) {
68 Set<GameRecord> set = getGlobalRecords(name);
69
70 if (set == null || set.isEmpty()) {
71 return null;
72 }
73
74 for (GameRecord t : set) {
75 if (t.activityType == activity)
76 return t;
77 }
78 return null;
79 }
80
81 static GameRecord getTopRecord(ActivityType activity) {
82 List<GameRecord> records = getActivities(activity);
83 return records.isEmpty() ? null : records.get(0);
84 }
85
86 private static Set<GameRecord> getGlobalRecords(String name) {
87 Set<GameRecord> set = new HashSet<>();
88 if (GLOBAL_RECORDS == null || GLOBAL_RECORDS.isEmpty()) {
89 return set;
90 }
91
92 for (GameRecord tracker : GLOBAL_RECORDS) {
93 if (!tracker.name.equalsIgnoreCase(name))
94 continue;
95 set.add(tracker);
96 }
97 return set;
98 }
99
100 private static List<GameRecord> getActivities(ActivityType activity) {
101 List<GameRecord> set = new ArrayList<>();
102 for (GameRecord tracker : GLOBAL_RECORDS) {
103 if (tracker.activityType != activity)
104 continue;
105 set.add(tracker);
106 }
107 set.sort(Comparator.comparingLong(record -> record.time));
108 return set;
109 }
110
111 public static void load() {
112 Type type = new TypeToken<Set<GameRecord>>() {
113 }.getType();
114 Path path = Paths.get("data", "/content/game/game_records.json");
115 try (FileReader reader = new FileReader(path.toFile())) {
116 JsonParser parser = new JsonParser();
117 GLOBAL_RECORDS = new GsonBuilder().create().fromJson(parser.parse(reader), type);
118 } catch (Exception e) {
119 e.printStackTrace();
120 }
121 }
122
123 public static void save() {
124 new Thread(() -> {
125 try (FileWriter fw = new FileWriter("./data/content/game/game_records.json")) {
126 fw.write(GsonUtils.JSON_PRETTY_NO_NULLS.toJson(GLOBAL_RECORDS));
127 } catch (final Exception e) {
128 e.printStackTrace();
129 }
130 }).start();
131 }
132}
133*/