-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Tech13-08/main
Enemy spawn code
- Loading branch information
Showing
3 changed files
with
151 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Nota30/Kiko/config" | ||
"github.com/bwmarrin/discordgo" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type ChannelActivity struct { | ||
MessageCount int | ||
LastUpdate time.Time | ||
LastAuthor string | ||
LastSpawn time.Time | ||
} | ||
|
||
// Mess with the variables and constants to adjust intervals of spawning and activity thresholds | ||
var ( | ||
channelActivityMap = make(map[string]*ChannelActivity) | ||
spawnCooldown = 1 * time.Minute | ||
) | ||
|
||
const ( | ||
activityThreshold = 3 | ||
activityTimeWindow = 2 * time.Minute | ||
) | ||
|
||
func ActivityHandler(session *discordgo.Session, message *discordgo.MessageCreate) { | ||
if message.Author.Bot || message.Author.ID == session.State.User.ID { | ||
return | ||
} | ||
|
||
channelID := message.ChannelID | ||
activity, exists := channelActivityMap[channelID] | ||
|
||
if !exists { | ||
fmt.Printf("Created channel activity map!!!\n") // Debug line | ||
activity = &ChannelActivity{ | ||
MessageCount: 1, | ||
LastUpdate: time.Now(), | ||
LastAuthor: message.Author.ID, | ||
LastSpawn: time.Now(), | ||
} | ||
channelActivityMap[channelID] = activity | ||
return | ||
} | ||
|
||
if time.Since(activity.LastUpdate) > activityTimeWindow { | ||
fmt.Printf("Time exceeded, reset activity\n") // Debug line | ||
activity.MessageCount = 1 | ||
activity.LastUpdate = time.Now() | ||
activity.LastAuthor = message.Author.ID | ||
return | ||
} | ||
|
||
// Comment this if statement if you want to test the system alone (otherwise it'll think you are spamming and won't work) | ||
//if session.State.User.ID != activity.LastAuthor { | ||
activity.MessageCount++ | ||
activity.LastUpdate = time.Now() | ||
activity.LastAuthor = message.Author.ID | ||
//} | ||
|
||
// Debug lines | ||
fmt.Printf("Message count: " + fmt.Sprint(activity.MessageCount) + "\n") | ||
fmt.Printf("Last Update: " + fmt.Sprint(activity.LastUpdate) + "\n") | ||
fmt.Printf("Last Author: " + fmt.Sprint(activity.LastAuthor) + "\n") | ||
fmt.Printf("Last Spawn: " + fmt.Sprint(activity.LastSpawn) + "\n") | ||
// Debug lines | ||
|
||
if activity.MessageCount >= activityThreshold { | ||
fmt.Printf("Activity threshold reached!" + "\n") // Debug line | ||
if time.Since(activity.LastSpawn) > spawnCooldown { | ||
fmt.Printf("Spawning enemy..." + "\n") // Debug line | ||
enemy := config.GetRandomEnemy() | ||
if enemy != nil { | ||
activity.LastSpawn = time.Now() | ||
enemyEmbed := config.GetEnemyEmbed(enemy) | ||
_, err := session.ChannelMessageSendEmbed(channelID, enemyEmbed) | ||
if err != nil { | ||
logrus.Error("Error with sending enemy embed") | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} |
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,59 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
const ( | ||
min = 1 | ||
max = 10 | ||
) | ||
|
||
type EnemyStruct struct { | ||
Name string | ||
Strength int | ||
Mana int | ||
Health int | ||
Defence int | ||
} | ||
|
||
var hollow = EnemyStruct{ | ||
Name: "Hollow", | ||
Strength: 100, | ||
Mana: 500, | ||
Health: 1500, | ||
Defence: 300, | ||
} | ||
|
||
var angel = EnemyStruct{ | ||
Name: "Angel", | ||
Strength: 300, | ||
Mana: 800, | ||
Health: 2000, | ||
Defence: 500, | ||
} | ||
|
||
var enemies = []EnemyStruct{hollow, angel} | ||
|
||
func GetRandomEnemy() *EnemyStruct { | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
spawnChance := (r.Uint32()%(max-min) + min) | ||
// 1 out of 10 chance for enemy to spawn | ||
if spawnChance == 5 { | ||
return &enemies[rand.Intn(len(enemies))] | ||
} | ||
return nil | ||
|
||
} | ||
|
||
func GetEnemyEmbed(enemy *EnemyStruct) *discordgo.MessageEmbed { | ||
return &discordgo.MessageEmbed{ | ||
Title: "Enemy Spawned!", | ||
Color: Color.Default, | ||
Description: "**" + enemy.Name + "**:\n\tStength:" + fmt.Sprint(enemy.Strength) + "\n\tHealth:" + fmt.Sprint(enemy.Health) + "\n\tMana:" + fmt.Sprint(enemy.Mana) + "\n\tDefence:" + fmt.Sprint(enemy.Defence), | ||
} | ||
} |