Skip to content

Commit

Permalink
Implement gacha history record subsystem
Browse files Browse the repository at this point in the history
* Frontend is not very beautiful yet
* Didn't include too much `some anime game` data in the page to avoid being DMCA'd
  • Loading branch information
mingjun97 authored and Melledy committed May 1, 2022
1 parent 22a651b commit 8cf4ef5
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 6 deletions.
176 changes: 176 additions & 0 deletions data/gacha_records.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<html>
<head>
<script>
// Debug entry
// record = [
// {"time": 10000341, "item": 1001},
// {"time": 10000342, "item": 1002},
// {"time": 10000343, "item": 1003},
// ];
// maxPage = 5;

// in production environment
record = {{REPLACE_RECORD}};
maxPage = {{REPLACE_MAXPAGE}};

// TODO: implement this mapper by yourself
// I don't want to put real items' name here to avoid being DMCA'd
mappings = {
'en-us': {
200: "Standard",
301: "Event Avatar",
302: "Event Weapon",
1041 : ["M0n4", "yellow"],
1032 : ["B4nn477", "purple"],
1035 : ["77", "yellow"]
},
'zh-cn': {
// encoding issues here, maybe we should consider load mappings remotely
// will display as "锟斤铐锟斤铐锟斤铐", lmao
// 200: "常驻",
// 301: "角色UP-1",
// 302: "武器UP"
200: "Standard",
301: "Event Avatar",
302: "Event Weapon",
}
};
mappings['default'] = mappings['en-us'];
</script>
<!-- TODO: Refine the CSS -->
<style>
a {
text-decoration: none !important;
}
.content {
width: 400px;
margin: auto;
display: flex;
flex-direction: column;
align-items: center;
}

.content .navbar {
margin: auto;
width: fit-content;
padding-top: 5px;
padding-bottom: 30px;
}

.yellow {
color: yellow;
}

.blue {
color: rgb(75, 107, 251);
}

.purple {
color: rgb(242, 40, 242);
}
</style>
</head>
<body style="background: skyblue;">
<div class="content">
<h1>Gacha Records</h1>
<h2 id="gacha-type"></h2>
<br/>
<div style="width: fit-content">
<table border="1">
<tbody id="container">
<tr>
<th>Time</th>
<th>Item</th>
</tr>
</tbody>
</table>
</div>
<div class="navbar">
<a href="" id="prev"> &lt; </a>
<span id="curpage">1</span>
<a href="" id="next"> &gt; </a>
</div>
</div>

<script>
var lang = new window.URLSearchParams(window.location.search).get("lang");
function itemMapper(itemID) {
if (mappings[lang] != null && mappings[lang][itemID] != null) {
var entry = mappings[lang][itemID];
if (entry){
return "<span class='" + entry[1] + "'>" + entry[0] + "</span>";
}
} else {
if (mappings['default'][itemID] != null) {
var entry = mappings['default'][itemID];
if (entry){
return "<span class='" + entry[1] + "'>" + entry[0] + "</span>";
}
}
}
return "<span class='blue'>" + itemID + "</span>";
}
function dateFormatter(timeStamp) {
var date = new Date(timeStamp);
if (lang == "en-us" || lang == null) { // MM/DD/YYYY hh:mm:ss.SSS
return String(date.getMonth()+1).padStart(2, "0") +
"/"+String(date.getDate()).padStart(2, "0")+
"/"+date.getFullYear()+
" "+String(date.getHours()).padStart(2, "0")+
":"+String(date.getMinutes()).padStart(2, "0")+
":"+String(date.getSeconds()).padStart(2, "0")+
"."+date.getMilliseconds();
} else if (lang == "zh-cn") { // YYYY/MM/DD hh:mm:ss.SSS
return date.getFullYear()+
"/" + String(date.getMonth()+1).padStart(2, "0") +
"/"+String(date.getDate()).padStart(2, "0")+
" "+String(date.getHours()).padStart(2, "0")+
":"+String(date.getMinutes()).padStart(2, "0")+
":"+String(date.getSeconds()).padStart(2, "0")+
"."+date.getMilliseconds();
}
}
(function (){
var container = document.getElementById("container");
record.forEach(element => {
var e = document.createElement("tr");

e.innerHTML= "<td>" + dateFormatter(element.time) + "</td><td>" + itemMapper(element.item) + "</td>";
container.appendChild(e);
});
// setup pagenation buttons
var page = parseInt(new window.URLSearchParams(window.location.search).get("p"));
if (!page){
page = 0;
}
document.getElementById("curpage").innerText = page + 1;
var href = new URL(window.location);
href.searchParams.set("p", page - 1);
document.getElementById("prev").href = href.toString();
href.searchParams.set("p", page + 1);
document.getElementById("next").href = href.toString();

if (page <= 0) {
document.getElementById("prev").style.display = "none";
}
if (page >= maxPage - 1) {
document.getElementById("next").style.display = "none";
}

// setup gacha type info
var gachaType = new window.URLSearchParams(window.location.search).get("gachaType");
var gachaString;
if (mappings[lang] != null && mappings[lang][gachaType] != null) {
gachaString = mappings[lang][gachaType];
}else{
gachaString = mappings['default'][gachaType];
if (gachaString == null) {
gachaString = gachaType;
}
}
document.getElementById("gacha-type").innerText = gachaString;
})();
</script>

</body>
</html>
39 changes: 39 additions & 0 deletions src/main/java/emu/grasscutter/database/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import java.util.List;

import com.mongodb.client.result.DeleteResult;
import dev.morphia.query.FindOptions;
import dev.morphia.query.Sort;
import dev.morphia.query.experimental.filters.Filters;
import emu.grasscutter.GameConstants;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.friends.Friendship;
import emu.grasscutter.game.gacha.GachaRecord;
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.player.Player;

Expand Down Expand Up @@ -78,6 +81,11 @@ public static Account getAccountByToken(String token) {
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
}

public static Account getAccountBySessionKey(String sessionKey) {
if(sessionKey == null) return null;
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("sessionKey", sessionKey)).first();
}

public static Account getAccountById(String uid) {
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
}
Expand Down Expand Up @@ -181,5 +189,36 @@ public static Friendship getReverseFriendship(Friendship friendship) {
)).first();
}

public static List<GachaRecord> getGachaRecords(int ownerId, int page, int gachaType){
return getGachaRecords(ownerId, page, gachaType, 10);
}

public static List<GachaRecord> getGachaRecords(int ownerId, int page, int gachaType, int pageSize){
return DatabaseManager.getDatastore().find(GachaRecord.class).filter(
Filters.eq("ownerId", ownerId),
Filters.eq("gachaType", gachaType)
).iterator(new FindOptions()
.sort(Sort.descending("transactionDate"))
.skip(pageSize * page)
.limit(pageSize)
).toList();
}

public static long getGachaRecordsMaxPage(int ownerId, int page, int gachaType){
return getGachaRecordsMaxPage(ownerId, page, gachaType, 10);
}

public static long getGachaRecordsMaxPage(int ownerId, int page, int gachaType, int pageSize){
long count = DatabaseManager.getDatastore().find(GachaRecord.class).filter(
Filters.eq("ownerId", ownerId),
Filters.eq("gachaType", gachaType)
).count();
return count / 10 + (count % 10 > 0 ? 1 : 0 );
}

public static void saveGachaRecord(GachaRecord gachaRecord){
DatabaseManager.getDatastore().save(gachaRecord);
}

public static char AWJVN = 'e';
}
3 changes: 2 additions & 1 deletion src/main/java/emu/grasscutter/database/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import emu.grasscutter.game.Account;
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.friends.Friendship;
import emu.grasscutter.game.gacha.GachaRecord;
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.player.Player;

Expand All @@ -28,7 +29,7 @@ public final class DatabaseManager {
private static Datastore dispatchDatastore;

private static final Class<?>[] mappedClasses = new Class<?>[] {
DatabaseCounter.class, Account.class, Player.class, Avatar.class, GameItem.class, Friendship.class
DatabaseCounter.class, Account.class, Player.class, Avatar.class, GameItem.class, Friendship.class, GachaRecord.class
};

public static Datastore getDatastore() {
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/emu/grasscutter/game/gacha/GachaBanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,21 @@ public int getEventChance() {
return eventChance;
}

@Deprecated
public GachaInfo toProto() {
String record = "http://" + (Grasscutter.getConfig().getDispatchOptions().PublicIp.isEmpty() ? Grasscutter.getConfig().getDispatchOptions().Ip : Grasscutter.getConfig().getDispatchOptions().PublicIp) + "/gacha";

return toProto("");
}
public GachaInfo toProto(String sessionKey) {
String record = "https://"
+ (Grasscutter.getConfig().getDispatchOptions().PublicIp.isEmpty() ?
Grasscutter.getConfig().getDispatchOptions().Ip :
Grasscutter.getConfig().getDispatchOptions().PublicIp)
+ ":"
+ Integer.toString(Grasscutter.getConfig().getDispatchOptions().PublicPort == 0 ?
Grasscutter.getConfig().getDispatchOptions().Port :
Grasscutter.getConfig().getDispatchOptions().PublicPort)
+ "/gacha?s=" + sessionKey + "&gachaType=" + gachaType;
// Grasscutter.getLogger().info("record = " + record);
GachaInfo.Builder info = GachaInfo.newBuilder()
.setGachaType(this.getGachaType())
.setScheduleId(this.getScheduleId())
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/emu/grasscutter/game/gacha/GachaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.gacha.GachaBanner.BannerType;
import emu.grasscutter.game.inventory.GameItem;
Expand Down Expand Up @@ -196,6 +197,10 @@ public synchronized void doPulls(Player player, int gachaType, int times) {
if (itemData == null) {
continue;
}

// Write gacha record
GachaRecord gachaRecord = new GachaRecord(itemId, player.getUid(), gachaType);
DatabaseHelper.saveGachaRecord(gachaRecord);

// Create gacha item
GachaItem.Builder gachaItem = GachaItem.newBuilder();
Expand Down Expand Up @@ -321,6 +326,7 @@ public synchronized void watchBannerJson(GameServerTickEvent tickEvent) {
}
}

@Deprecated
private synchronized GetGachaInfoRsp createProto() {
GetGachaInfoRsp.Builder proto = GetGachaInfoRsp.newBuilder().setGachaRandom(12345);

Expand All @@ -330,12 +336,26 @@ private synchronized GetGachaInfoRsp createProto() {

return proto.build();
}

private synchronized GetGachaInfoRsp createProto(String sessionKey) {
GetGachaInfoRsp.Builder proto = GetGachaInfoRsp.newBuilder().setGachaRandom(12345);

for (GachaBanner banner : getGachaBanners().values()) {
proto.addGachaInfoList(banner.toProto(sessionKey));
}

return proto.build();
}

@Deprecated
public GetGachaInfoRsp toProto() {
if (this.cachedProto == null) {
this.cachedProto = createProto();
}

return this.cachedProto;
}

public GetGachaInfoRsp toProto(String sessionKey) {
return createProto(sessionKey);
}
}
Loading

0 comments on commit 8cf4ef5

Please sign in to comment.