Mail.java 3.46 KB
Newer Older
Melledy's avatar
Melledy committed
1
package emu.grasscutter.game.mail;
2

3
import dev.morphia.annotations.Entity;
4
5
6
7
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Indexed;
import dev.morphia.annotations.Transient;
import emu.grasscutter.database.DatabaseHelper;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.player.Player;
9

10
11
12
13
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

14
import org.bson.types.ObjectId;
15

16
17
18
19
@Entity(value = "mail", useDiscriminator = false)
public class Mail {
	@Id private ObjectId id;
	@Indexed private int ownerUid;
20
21
22
23
24
25
26
27
    public MailContent mailContent;
    public List<MailItem> itemList;
    public long sendTime;
    public long expireTime;
    public int importance;
    public boolean isRead;
    public boolean isAttachmentGot;
    public int stateValue;
28
    @Transient private boolean shouldDelete;
29
30

    public Mail() {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
31
        this(new MailContent(), new ArrayList<MailItem>(), (int) Instant.now().getEpochSecond() + 604800); // TODO: add expire time to send mail command
32
33
34
    }

    public Mail(MailContent mailContent, List<MailItem> itemList, long expireTime) {
35
        this(mailContent, itemList, expireTime, 0);
36
37
38
39
40
41
42
43
44
    }

    public Mail(MailContent mailContent, List<MailItem> itemList, long expireTime, int importance) {
        this(mailContent, itemList, expireTime, importance, 1);
    }

    public Mail(MailContent mailContent, List<MailItem> itemList, long expireTime, int importance, int state) {
        this.mailContent = mailContent;
        this.itemList = itemList;
45
        this.sendTime = (int) Instant.now().getEpochSecond();
46
        this.expireTime = expireTime;
Benjamin Elsdon's avatar
Benjamin Elsdon committed
47
        this.importance = importance; // Starred mail, 0 = No star, 1 = Star.
48
49
        this.isRead = false;
        this.isAttachmentGot = false;
Benjamin Elsdon's avatar
Benjamin Elsdon committed
50
        this.stateValue = state; // Different mailboxes, 1 = Default, 3 = Gift-box.
51
52
    }

53
54
55
56
57
58
59
60
61
62
63
64
65
    public ObjectId getId() {
		return id;
	}

	public int getOwnerUid() {
		return ownerUid;
	}

	public void setOwnerUid(int ownerUid) {
		this.ownerUid = ownerUid;
	}

	@Entity
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    public static class MailContent {
        public String title;
        public String content;
        public String sender;

        public MailContent() {
            this.title = "";
            this.content = "loading...";
            this.sender = "loading";
        }

        public MailContent(String title, String content) {
            this(title, content, "Server");
        }

81
        public MailContent(String title, String content, Player sender) {
82
83
84
85
86
87
88
89
90
91
            this(title, content, sender.getNickname());
        }

        public MailContent(String title, String content, String sender) {
            this.title = title;
            this.content = content;
            this.sender = sender;
        }
    }

92
    @Entity
93
94
95
    public static class MailItem {
        public int itemId;
        public int itemCount;
96
        public int itemLevel;
97
98
99
100

        public MailItem() {
            this.itemId = 11101;
            this.itemCount = 1;
101
            this.itemLevel = 1;
102
103
104
105
106
107
        }

        public MailItem(int itemId) {
            this(itemId, 1);
        }

108
109
110
        public MailItem(int itemId, int itemCount) { this(itemId, itemCount, 1); }

        public MailItem(int itemId, int itemCount, int itemLevel) {
111
112
            this.itemId = itemId;
            this.itemCount = itemCount;
113
            this.itemLevel = itemLevel;
114
115
        }
    }
116
117
118
119
120
121
122
123

	public void save() {
		if (this.expireTime * 1000 < System.currentTimeMillis()) {
			DatabaseHelper.deleteMail(this);
		} else {
			DatabaseHelper.saveMail(this);
		}
	}
124
}