Skip to content

Commit

Permalink
Code has been commented and optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Ugryumov committed May 20, 2017
1 parent 264ce16 commit 117622f
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 94 deletions.
11 changes: 4 additions & 7 deletions kernel/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,32 @@ import (
"main/kernel/cache/cachetypes"
)

//Cache for RSS data
type RssCaches struct {
News cachetypes.RssCache
Bash cachetypes.RssCache
IThappens cachetypes.RssCache
Zadolbali cachetypes.RssCache
}

//Cache for functions data
type CommandDataCaches struct {
Help cachetypes.HelpCache
Cities cachetypes.CitiesCache
}

//Main cache
type DataCache struct {
RssCache RssCaches
CommandDataCache CommandDataCaches
DictionaryCache cachetypes.DictCache
}

//Cache initialization
func (cache *DataCache) InitCache() {
cache.DictionaryCache.Data = *aiml.NewAIML()

cache.CommandDataCache.Help.InitCache(conf.CommandsDirPath + "/help.xml")
cache.CommandDataCache.Cities.InitCache(conf.CommandsDirPath + "/cities.xml")
cache.DictionaryCache.UpdateCache(conf.DataDirPath + "/dict.aiml.xml")
}

func (cache *RssCaches) UpdateRssCache(newCache map[string][]string) {
cache.News.Data = newCache["news"]
cache.Bash.Data = newCache["bash"]
cache.IThappens.Data = newCache["ithappens"]
cache.Zadolbali.Data = newCache["zadolbali"]
}
15 changes: 10 additions & 5 deletions kernel/cache/cachetypes/cities.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,33 @@ import (

type City struct {
XMLName xml.Name `xml:"city"`
Name string `xml:"name,attr"`
Name string `xml:"name,attr"` //Name of the city
}

type XMLCities struct {
XMLName xml.Name `xml:"xml"`
CitiesList []City `xml:"data>city"`
CitiesList []City `xml:"data>city"` //Cities array
}

//Cache for the cities game
type CitiesCache struct {
sync.Mutex
path string
Data XMLCities
path string //Path to file with XML data that will be parsed
Data XMLCities //Cache
}

//Cities cache initialization
func (citiesCache *CitiesCache) InitCache(path string) {
//Reading XML data
text, err := ioutil.ReadFile(path)
if err != nil {
log.Println("[ERROR] [main::kernel::mapCache::cachetypes::cachetypes.go::CitiesCache.InitCache] Failed to read file: ", err)
}

citiesCache.Lock()
citiesCache.Lock() //Cache locking
defer citiesCache.Unlock()

//Parsing XML data
if err := xml.Unmarshal(text, &citiesCache.Data); err != nil {
log.Println("[ERROR] [main::kernel::mapCache::cachetypes::cachetypes.go::CitiesCache.InitCache] Failed to unmarshal data: ", err)
}
Expand Down
9 changes: 6 additions & 3 deletions kernel/cache/cachetypes/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import (
"log"
)

//Cache for the dictionary for the bot
type DictCache struct {
sync.Mutex
Data aiml.AIML
Data aiml.AIML //Cache data
}

func (dictCache *DictCache) UpdateCache(path string) {
dictCache.Lock()
dictCache.Lock() //Cache locking
defer dictCache.Unlock()

//Learning AIML database
if err := dictCache.Data.Learn(path); err != nil {
log.Println("[ERROR] [main::kernel::mapCache::mapCache.go::DictCache.InitCache()] Failed to update mapCache ", err)
}
dictCache.Unlock()
}
24 changes: 14 additions & 10 deletions kernel/cache/cachetypes/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,40 @@ import (

type Help struct {
XMLName xml.Name `xml:"category"`
Name string `xml:"name,attr"`
Title string `xml:"title"`
State string `xml:"state"`
Description string `xml:"description"`
Name string `xml:"name,attr"` //Command name
Title string `xml:"title"` //Help title
State string `xml:"state"` //Command state
Description string `xml:"description"` //Command description
Samples []struct {
XMLName xml.Name `xml:"sample"`
Body string `xml:"body"`
Out string `xml:"out"`
Body string `xml:"body"` //Sample input
Out string `xml:"out"` //Sample output
} `xml:"samples>sample"`
}

type XMLHelp struct {
xml.Name `xml:"xml"`
HelpList []Help `xml:"help>category"` //TODO FIX
HelpList []Help `xml:"help>category"` //Help writings array
}

//Cache for the help command
type HelpCache struct {
sync.Mutex
path string
Data XMLHelp
path string //Path to file with XML data that will be parsed
Data XMLHelp //Cache
}

func (helpCache *HelpCache) InitCache(path string) {
//Reading XML
text, err := ioutil.ReadFile(path)
if err != nil {
log.Println("[ERROR] [main::kernel::mapCache::cachetypes::cachetypes.go::HelpCache.InitCache] Failed to read file: ", err)
}

helpCache.Lock()
helpCache.Lock() //Cache locking
defer helpCache.Unlock()

//Parsing data
if err := xml.Unmarshal(text, &helpCache.Data); err != nil {
log.Println("[ERROR] [main::kernel::mapCache::cachetypes::cachetypes.go::HelpCache.InitCache] Failed to unmarshal data: ", err)
}
Expand Down
3 changes: 2 additions & 1 deletion kernel/cache/cachetypes/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (

type RssCache struct {
sync.Mutex
Data []string
Data []string //Writing array
}

//Choose random writing
func (rssCache *RssCache) ChooseRandom() string {
return rssCache.Data[rand.Intn(len(rssCache.Data) - 1)]
}
45 changes: 18 additions & 27 deletions kernel/commands/commands.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
package commands

import (
"time"
"math/rand"
"sort"
"strconv"
"strings"
"fmt"
"main/kernel/commands/tools"
"main/web/vk"
"main/kernel/cache"
"main/kernel/interception"
"strconv"
"sort"
"time"
"fmt"
"math/rand"
"main/kernel/performer/functions"
)

type FuncArgs struct {
ApiChan vk.ChanKit
Message vk.Message
DataCache cache.DataCache
InterceptIndications interception.Indications
}

const (
//GET_STATE
getStateAnswer = "Я отсортировала массив из 1000 элементов за %v наносекунд"
Expand All @@ -42,7 +33,7 @@ const (
incorrectSymbolError = `Ты должен назвать слово на букву "%v"`
)

func GetState(args FuncArgs) {
func GetState(args functions.FuncArgs) {
start := time.Now().UnixNano()
sort.Ints(rand.Perm(1000))
metering := strconv.FormatInt(time.Now().UnixNano() - start, 10)
Expand All @@ -52,7 +43,7 @@ func GetState(args FuncArgs) {
})
}

func GetGen(args FuncArgs) {
func GetGen(args functions.FuncArgs) {
var message string
information := strconv.FormatInt(int64(tools.GetRandomNum(args.Message.Text)%100), 10)
message = fmt.Sprintf(getGenAnswer, information)
Expand All @@ -62,7 +53,7 @@ func GetGen(args FuncArgs) {
})
}

func GetHelp(args FuncArgs) {
func GetHelp(args functions.FuncArgs) {
var message string
if len(args.Message.Text) != 1 {
message = tools.GetHelp(args.Message.Text, args.DataCache.CommandDataCache.Help)
Expand All @@ -75,28 +66,28 @@ func GetHelp(args FuncArgs) {
})
}

func Bash(args FuncArgs) {
func Bash(args functions.FuncArgs) {
args.ApiChan.MakeRequest("messages.send", map[string]string{
"user_id": strconv.FormatInt(args.Message.UserId, 10),
"message": args.DataCache.RssCache.Bash.ChooseRandom(),
})
}

func IThappens(args FuncArgs) {
func IThappens(args functions.FuncArgs) {
args.ApiChan.MakeRequest("messages.send", map[string]string{
"user_id": strconv.FormatInt(args.Message.UserId, 10),
"message": args.DataCache.RssCache.IThappens.ChooseRandom(),
})
}

func Zadolbali(args FuncArgs) {
func Zadolbali(args functions.FuncArgs) {
args.ApiChan.MakeRequest("messages.send", map[string]string{
"user_id": strconv.FormatInt(args.Message.UserId, 10),
"message": args.DataCache.RssCache.Zadolbali.ChooseRandom(),
})
}

func News(args FuncArgs) {
func News(args functions.FuncArgs) {
words := strings.Split(args.Message.Text, " ")
var message string
if tools.IfMatch(words, []string{"i"}) {
Expand All @@ -116,7 +107,7 @@ func News(args FuncArgs) {
})
}

func Cities(args FuncArgs) {
func Cities(args functions.FuncArgs) {
args.InterceptIndications.Add(args.Message.UserId)
args.ApiChan.MakeRequest("messages.send", map[string]string{
"user_id": strconv.FormatInt(args.Message.UserId, 10),
Expand Down Expand Up @@ -187,10 +178,10 @@ func Cities(args FuncArgs) {

answer = winMessage

SEND: args.ApiChan.MakeRequest("messages.send", map[string]string{
"user_id": strconv.FormatInt(args.Message.UserId, 10),
"message": answer,
})
SEND: args.ApiChan.MakeRequest("messages.send", map[string]string{
"user_id": strconv.FormatInt(args.Message.UserId, 10),
"message": answer,
})
args.DataCache.CommandDataCache.Cities.Unlock()
}
}
14 changes: 7 additions & 7 deletions kernel/commands/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const (
)

var emojiDict = map[string]string{
"ready": "📗",
"test": "📙",
"error": "📕",
"dev": "📘",
"ready": "📗", //Command is ready
"test": "📙", //Command is testing
"error": "📕", //Command not working
"dev": "📘", //Command in development

}

Expand All @@ -53,15 +53,15 @@ func IfMatch(args []string, template []string) bool {
l: for index, word := range args {
if index < len(template) {
switch template[index] {
case IntegerTag:
case IntegerTag: //Must be integer
_, err := strconv.ParseInt(word, 10, 64)
if err != nil {
return false
}
case AnythingTag: //Anything
break l
case StringTag:
//TODO ADD
case AnythingTag:
break l
}
}

Expand Down
33 changes: 33 additions & 0 deletions kernel/performer/functions/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package functions

import (
"main/web/vk"
"main/kernel/cache"
"main/kernel/interception"
"strconv"
)

const messageSendMethod = "messages.send"

//args for bot commands
type FuncArgs struct {
ApiChan vk.ChanKit //ChanKit for safety API requests
Message vk.Message //Message from user
DataCache cache.DataCache //Cache of data
InterceptIndications interception.Indications //Interception tools
}

//Send message back to the user
func (args *FuncArgs) Reply(message string, attach ...string) {
var attachments string

if len(attachments) != 0 {
attachments = attach[0]
}

args.ApiChan.MakeRequest(messageSendMethod, map[string]string{
"user_id": strconv.FormatInt(args.Message.UserId, 10),
"message": message,
"attachments": attachments,
})
}
Loading

0 comments on commit 117622f

Please sign in to comment.