pet_home_server/src/server/game/mod/catnip/Catnip.go
2025-07-18 18:07:36 +08:00

207 lines
5.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package catnip
import (
"fmt"
"server/GoUtil"
catnipCfg "server/conf/catnip"
"server/game/mod/item"
)
type CatnipMod struct {
Id int
Game map[int]*CatnipGame
InviteList map[int][]*InviteInfo // 邀请列表key: 邀请者ID, value: 被邀请者ID
BeInvitedList map[int][]*InviteInfo // 被邀请列表key: 被邀请者ID, value: 邀请者ID
}
type InviteInfo struct {
InviteId int // 邀请者ID
Time int64 // 邀请时间
}
type CatnipGame struct {
Id int // 游戏ID
Partner int // 伙伴ID
Progress int // 进度
PartnerAdd int // 伙伴贡献
Reward []int // 已领取阶段奖励
Mul int // 倍数
Status int // 0: Not Started, 1: In Progress, 2: Completed
}
const (
GAME_STATUS_IDLE = 0 // 游戏未开始
GAME_STATUS_IN_PROGRESS = 1 // 游戏进行中
GAME_STATUS_COMPLETED = 2 // 游戏已完成
)
func (c *CatnipMod) InitData() {
// Initialize Catnip data here
if c.Game == nil {
c.Game = make(map[int]*CatnipGame)
}
if c.InviteList == nil {
c.InviteList = make(map[int][]*InviteInfo)
}
if c.BeInvitedList == nil {
c.BeInvitedList = make(map[int][]*InviteInfo)
}
}
func (c *CatnipMod) Login(Id int) int {
OldId := c.Id
if Id == 0 {
c.Id = 0
return OldId
}
if c.Id == Id {
return 0
}
c.Id = Id
c.Game = make(map[int]*CatnipGame)
GameNum := catnipCfg.GetGameNum(c.Id) // Assuming 1 is the default game ID
for i := 1; i <= GameNum; i++ {
c.Game[i] = &CatnipGame{
Id: i,
Partner: 0, // No partner initially
Progress: 0, // Initial progress
Status: GAME_STATUS_IDLE, // Not started
}
}
return c.Id
}
func (c *CatnipMod) ZeroUpdate(Id int) {
c.Login(Id)
}
func (c *CatnipMod) Invite(Id, Uid int) error {
GameInfo, ok := c.Game[Id]
if !ok {
return fmt.Errorf("game with ID %d does not exist", Id)
}
if GameInfo.Status != 0 {
return fmt.Errorf("game with ID %d is already in progress or completed", Id)
}
for _, invite := range c.InviteList[Id] {
if invite.InviteId == Uid {
return fmt.Errorf("user with ID %d is already invited to game ID %d", Uid, Id)
}
}
c.InviteList[Id] = append(c.InviteList[Id], &InviteInfo{
InviteId: Uid,
Time: GoUtil.Now(),
})
return nil
}
func (c *CatnipMod) BeInvited(Id, Uid int, Time int64) error {
GameInfo, ok := c.Game[Id]
if !ok {
return fmt.Errorf("game with ID %d does not exist", Id)
}
if GameInfo.Status != GAME_STATUS_IDLE {
return fmt.Errorf("game with ID %d is already in progress or completed", Id)
}
// Check if the user is already invited
for _, invite := range c.BeInvitedList[Uid] {
if invite.InviteId == Id {
return fmt.Errorf("user with ID %d has already been invited to game ID %d", Uid, Id)
}
}
c.BeInvitedList[Uid] = append(c.BeInvitedList[Uid], &InviteInfo{
InviteId: Id,
Time: Time,
})
return nil
}
func (c *CatnipMod) Agree(Id, Uid int) ([]int, error) {
GameInfo, ok := c.Game[Id]
if !ok {
return nil, fmt.Errorf("game with ID %d does not exist", Id)
}
if GameInfo.Status != GAME_STATUS_IDLE {
return nil, fmt.Errorf("game with ID %d is already in progress or completed", Id)
}
// Check if the user is in the invite list
inviteList, exists := c.InviteList[Id]
if !exists {
return nil, fmt.Errorf("no invites found for game ID %d", Id)
}
userExists := false
InviteUser := []int{}
for _, invite := range inviteList {
if invite.InviteId == Uid {
userExists = true
continue
}
InviteUser = append(InviteUser, invite.InviteId)
}
if !userExists {
return nil, fmt.Errorf("user with ID %d is not invited to game ID %d", Uid, Id)
}
c.InviteList[Id] = make([]*InviteInfo, 0) // Clear the invite list after agreeing
GameInfo.Partner = Uid // Set the partner for the game
GameInfo.Status = GAME_STATUS_COMPLETED // Set the game status to in progress
return InviteUser, nil
}
func (c *CatnipMod) DelInvited(Id, Uid int) error {
GameInfo, ok := c.Game[Id]
if !ok {
return fmt.Errorf("game with ID %d does not exist", Id)
}
if GameInfo.Status != GAME_STATUS_IDLE {
return fmt.Errorf("game with ID %d is already in progress or completed", Id)
}
for k, invite := range c.BeInvitedList[Id] {
if invite.InviteId == Uid {
// Remove the invite from the list
c.BeInvitedList[Id] = append(c.InviteList[Id][:k], c.InviteList[Id][k+1:]...)
return nil
}
}
return fmt.Errorf("user with ID %d is not invited to game ID %d", Uid, Id)
}
func (c *CatnipMod) Multiply(Id, Mul int) error {
GameInfo, ok := c.Game[Id]
if !ok {
return fmt.Errorf("game with ID %d does not exist", Id)
}
GameInfo.Mul = Mul
return nil
}
func (c *CatnipMod) Refuse(Id, Uid int) error {
GameInfo, ok := c.Game[Id]
if !ok {
return fmt.Errorf("game with ID %d does not exist", Id)
}
if GameInfo.Status != GAME_STATUS_IDLE {
return fmt.Errorf("game with ID %d is already in progress or completed", Id)
}
for k, invite := range c.InviteList[Id] {
if invite.InviteId == Uid {
// Remove the invite from the list
c.InviteList[Id] = append(c.InviteList[Id][:k], c.InviteList[Id][k+1:]...)
return nil
}
}
return nil
}
func (c *CatnipMod) Play(Id int) error {
return nil
}
func (c *CatnipMod) Reward(Id int) ([]*item.Item, error) {
return nil, nil
}
func (c *CatnipMod) GrandReward() ([]*item.Item, error) {
return nil, nil
}