This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: fix small differences between game versions, updating MailBox ha…
…ndlers to new version
- Loading branch information
ggmolly
committed
Jul 14, 2024
1 parent
5b25fb0
commit 245f876
Showing
14 changed files
with
325 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
package answer | ||
|
||
import ( | ||
"github.com/ggmolly/belfast/connection" | ||
"github.com/ggmolly/belfast/orm" | ||
// import ( | ||
// "github.com/ggmolly/belfast/connection" | ||
// "github.com/ggmolly/belfast/orm" | ||
|
||
"github.com/ggmolly/belfast/protobuf" | ||
) | ||
// "github.com/ggmolly/belfast/protobuf" | ||
// ) | ||
|
||
func DeleteAllMails(buffer *[]byte, client *connection.Client) (int, int, error) { | ||
mailIds := make([]uint32, len(client.Commander.Mails)) | ||
for i, mail := range client.Commander.Mails { | ||
mailIds[i] = mail.ID | ||
} | ||
response := protobuf.SC_30007{ | ||
IdList: mailIds, | ||
} | ||
if err := client.Commander.CleanMailbox(); err != nil { | ||
return 0, 30007, err | ||
} | ||
client.Commander.Mails = make([]orm.Mail, 0) | ||
client.Commander.MailsMap = make(map[uint32]*orm.Mail) | ||
return client.SendMessage(30007, &response) | ||
} | ||
// func DeleteAllMails(buffer *[]byte, client *connection.Client) (int, int, error) { | ||
// mailIds := make([]uint32, len(client.Commander.Mails)) | ||
// for i, mail := range client.Commander.Mails { | ||
// mailIds[i] = mail.ID | ||
// } | ||
// response := protobuf.SC_30007{ | ||
// IdList: mailIds, | ||
// } | ||
// if err := client.Commander.CleanMailbox(); err != nil { | ||
// return 0, 30007, err | ||
// } | ||
// client.Commander.Mails = make([]orm.Mail, 0) | ||
// client.Commander.MailsMap = make(map[uint32]*orm.Mail) | ||
// return client.SendMessage(30007, &response) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package answer | ||
|
||
import ( | ||
"github.com/ggmolly/belfast/connection" | ||
"github.com/ggmolly/belfast/orm" | ||
"github.com/ggmolly/belfast/protobuf" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
func mailToSimpleMailInfo(mail *orm.Mail) *protobuf.MAIL_SIMPLE_INFO { | ||
attachments := make([]*protobuf.DROPINFO, len(mail.Attachments)) | ||
for i, attachment := range mail.Attachments { | ||
attachments[i] = &protobuf.DROPINFO{ | ||
Type: proto.Uint32(attachment.Type), | ||
Id: proto.Uint32(attachment.ItemID), | ||
Number: proto.Uint32(attachment.Quantity), | ||
} | ||
} | ||
return &protobuf.MAIL_SIMPLE_INFO{ | ||
Id: proto.Uint32(mail.ID), | ||
Date: proto.Uint32(uint32(mail.Date.Unix())), | ||
Title: proto.String(mail.Title), | ||
Content: proto.String(mail.Body), | ||
AttachmentList: attachments, | ||
} | ||
} | ||
|
||
// Returns archived mails | ||
func GetCollectionMailList(buffer *[]byte, client *connection.Client) (int, int, error) { | ||
var payload protobuf.CS_30004 | ||
if err := proto.Unmarshal(*buffer, &payload); err != nil { | ||
return 0, 30002, err | ||
} | ||
var response protobuf.SC_30005 | ||
commanderMailsCount := uint32(len(client.Commander.Mails)) | ||
if commanderMailsCount == 0 { | ||
return client.SendMessage(30003, &response) | ||
} | ||
|
||
// If end range is 0, it means we want to send all the mails | ||
if payload.GetIndexEnd() == 0 { | ||
payload.IndexEnd = proto.Uint32(commanderMailsCount + 1) | ||
} | ||
|
||
// Lua range starts at 1, so we will compensate for that | ||
payload.IndexBegin = proto.Uint32(payload.GetIndexBegin() - 1) | ||
|
||
for i := payload.GetIndexBegin(); i < commanderMailsCount && i < payload.GetIndexEnd(); i++ { | ||
if client.Commander.Mails[i].IsArchived { | ||
response.MailList = append(response.MailList, mailToSimpleMailInfo(&client.Commander.Mails[i])) | ||
} | ||
} | ||
return client.SendMessage(30005, &response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
package answer | ||
|
||
import ( | ||
"github.com/ggmolly/belfast/connection" | ||
// import ( | ||
// "github.com/ggmolly/belfast/connection" | ||
|
||
"github.com/ggmolly/belfast/protobuf" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
// "github.com/ggmolly/belfast/protobuf" | ||
// "google.golang.org/protobuf/proto" | ||
// ) | ||
|
||
func GiveMailAttachments(buffer *[]byte, client *connection.Client) (int, int, error) { | ||
var data protobuf.CS_30004 | ||
err := proto.Unmarshal(*buffer, &data) | ||
if err != nil { | ||
return 0, 30005, err | ||
} | ||
var attachments []*protobuf.ATTACHMENT | ||
for _, mailId := range data.GetId() { | ||
mail, ok := client.Commander.MailsMap[mailId] | ||
if !ok { | ||
return 0, 30005, nil | ||
} | ||
mailAttachments, err := mail.CollectAttachments(client.Commander) | ||
if err != nil { | ||
return 0, 30005, err | ||
} | ||
attachments = append(attachments, mailAttachments...) | ||
} | ||
response := protobuf.SC_30005{ | ||
AttachmentList: attachments, | ||
} | ||
return client.SendMessage(30005, &response) | ||
} | ||
// func GiveMailAttachments(buffer *[]byte, client *connection.Client) (int, int, error) { | ||
// var data protobuf.CS_30004 | ||
// err := proto.Unmarshal(*buffer, &data) | ||
// if err != nil { | ||
// return 0, 30005, err | ||
// } | ||
// var attachments []*protobuf.ATTACHMENT | ||
// for _, mailId := range data.GetId() { | ||
// mail, ok := client.Commander.MailsMap[mailId] | ||
// if !ok { | ||
// return 0, 30005, nil | ||
// } | ||
// mailAttachments, err := mail.CollectAttachments(client.Commander) | ||
// if err != nil { | ||
// return 0, 30005, err | ||
// } | ||
// attachments = append(attachments, mailAttachments...) | ||
// } | ||
// response := protobuf.SC_30005{ | ||
// AttachmentList: attachments, | ||
// } | ||
// return client.SendMessage(30005, &response) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package answer | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/ggmolly/belfast/connection" | ||
"github.com/ggmolly/belfast/consts" | ||
"github.com/ggmolly/belfast/logger" | ||
"github.com/ggmolly/belfast/orm" | ||
"github.com/ggmolly/belfast/protobuf" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
type MailDealCmdHandler func(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error | ||
|
||
func handleMailDealCmdRead(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error { | ||
return mail.SetRead(true) | ||
} | ||
|
||
func handleMailDealCmdImportant(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error { | ||
return mail.SetImportant(true) | ||
} | ||
|
||
func handleMailDealCmdUnimportant(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error { | ||
return mail.SetImportant(false) | ||
} | ||
|
||
func handleMailDealCmdDelete(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error { | ||
if err := orm.GormDB.Delete(&orm.Mail{}, "read = ?", true).Error; err != nil { | ||
return err | ||
} | ||
|
||
// Reload mails | ||
if err := orm.GormDB.Preload("Attachments").Find(&client.Commander.Mails).Error; err != nil { | ||
log.Println("!, found mails:", len(client.Commander.Mails)) | ||
return err | ||
} | ||
|
||
// load MailsMap | ||
mail.Commander.MailsMap = make(map[uint32]*orm.Mail) | ||
for i, mail := range client.Commander.Mails { | ||
mail.Commander.MailsMap[mail.ID] = &client.Commander.Mails[i] | ||
} | ||
return nil | ||
} | ||
|
||
func handleMailDealCmdAttachment(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error { | ||
panic("not implemented") | ||
} | ||
|
||
func handleMailDealCmdOverflow(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error { | ||
panic("not implemented") | ||
} | ||
|
||
func handleMailDealCmdMove(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error { | ||
return mail.SetArchived(true) | ||
} | ||
|
||
var cmdHandlers = map[uint32]MailDealCmdHandler{ | ||
consts.MAIL_DEAL_CMDS_READ: handleMailDealCmdRead, | ||
consts.MAIL_DEAL_CMDS_IMPORTANT: handleMailDealCmdImportant, | ||
consts.MAIL_DEAL_CMDS_UNIMPORTANT: handleMailDealCmdUnimportant, | ||
consts.MAIL_DEAL_CMDS_DELETE: handleMailDealCmdDelete, | ||
consts.MAIL_DEAL_CMDS_ATTACHMENT: handleMailDealCmdAttachment, | ||
consts.MAIL_DEAL_CMDS_OVERFLOW: handleMailDealCmdOverflow, | ||
consts.MAIL_DEAL_CMDS_MOVE: handleMailDealCmdMove, | ||
} | ||
|
||
func HandleMailDealCmd(buffer *[]byte, client *connection.Client) (int, int, error) { | ||
var payload protobuf.CS_30006 | ||
if err := proto.Unmarshal(*buffer, &payload); err != nil { | ||
return 0, 30006, err | ||
} | ||
|
||
logger.LogEvent("Server", "MailCmd", payload.String(), logger.LOG_LEVEL_DEBUG) | ||
|
||
response := protobuf.SC_30007{ | ||
Result: proto.Uint32(0), | ||
} | ||
_, ok := cmdHandlers[payload.GetCmd()] | ||
if !ok { | ||
return 0, 30006, fmt.Errorf("unknown mail deal cmd: %d", payload.GetCmd()) | ||
} | ||
var mailIndex uint32 | ||
if len(payload.GetMatchList()) > 0 { | ||
mailIndex = payload.GetMatchList()[0].GetType() - 1 | ||
} | ||
if mailIndex >= uint32(len(client.Commander.Mails)) { | ||
response.Result = proto.Uint32(1) | ||
return client.SendMessage(30007, &response) | ||
} | ||
fn := cmdHandlers[payload.GetCmd()] | ||
mail := client.Commander.Mails[mailIndex] | ||
if err := fn(client, &payload, &response, &mail); err != nil { | ||
return 0, 30006, err | ||
} | ||
|
||
var unreadCount uint32 | ||
for _, mail := range client.Commander.Mails { | ||
if !mail.Read { | ||
unreadCount++ | ||
} | ||
} | ||
response.UnreadNumber = proto.Uint32(unreadCount) | ||
|
||
// Copy mail ids | ||
var mailIds []uint32 | ||
for _, mail := range client.Commander.Mails { | ||
if !mail.IsArchived { | ||
mailIds = append(mailIds, mail.ID) | ||
} | ||
} | ||
response.MailIdList = mailIds | ||
log.Println(response.String()) | ||
return client.SendMessage(30007, &response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.