Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- //"fmt"
- "net/http"
- "strings"
- "gopkg.in/telegram-bot-api.v4"
- )
- var (
- BotToken = "XXX"
- WebhookURL = "https://525f2cb5.ngrok.io"
- )
- type Bag struct {
- items []string
- }
- type Player struct {
- bag *Bag
- roomID int
- }
- type Room struct {
- description string
- quest string
- items []string
- nearRooms map[string]int
- canGo string
- wellcomeMessage string
- isOpen bool
- }
- type Game struct {
- player Player
- rooms []*Room
- }
- type GameBot struct {
- bot *tgbotapi.BotAPI
- activeGames map[int]*Game
- }
- func NewGameBot() (*GameBot, error) {
- bot, err := tgbotapi.NewBotAPI(BotToken)
- if err != nil {
- return nil, err
- }
- _, err = bot.SetWebhook(tgbotapi.NewWebhook(WebhookURL))
- if err != nil {
- return nil, err
- }
- activeGames := make(map[int]*Game)
- return &GameBot{bot, activeGames}, nil
- }
- func contains(slice []string, word string) (int, bool) {
- for i, w := range slice {
- if w == word {
- return i, true
- }
- }
- return 0, false
- }
- func (t *GameBot) execUpdate(update tgbotapi.Update) {
- user := update.Message.From
- command := update.Message.Text
- switch {
- case strings.Contains(command, "/start"):
- t.Start(user, update)
- case strings.Contains(command, "/reset"):
- t.Reset(user, update)
- case strings.Contains(command, "осмотреться"):
- t.LookAround(user, update)
- case strings.Contains(command, "идти"):
- t.Go(command[9:], user, update)
- case strings.Contains(command, "одеть"):
- t.Equip(command[11:], user, update)
- case strings.Contains(command, "взять"):
- t.Take(command[11:], user, update)
- case strings.Contains(command, "применить"):
- t.Apply(command[19:], user, update)
- default:
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "неизвестная команда",
- ))
- }
- }
- func MakeNewGame() *Game {
- player := Player{nil, 0}
- rooms := []*Room{
- {
- "ты находишься на кухне, на столе чай",
- "надо собрать рюкзак и идти в универ",
- []string{},
- map[string]int{
- "коридор": 1,
- },
- "коридор",
- "кухня, ничего интересного",
- true,
- },
- {
- "ничего интересного",
- "",
- []string{},
- map[string]int{
- "кухня": 0,
- "комната": 2,
- "улица": 3,
- },
- "кухня, комната, улица",
- "ничего интересного",
- true,
- },
- {
- "на столе: ключи, конспекты, на стуле - рюкзак",
- "",
- []string{
- "ключи",
- "конспекты",
- "рюкзак",
- },
- map[string]int{
- "коридор": 1,
- },
- "коридор",
- "ты в своей комнате",
- true,
- },
- {
- "на улице уже вовсю готовятся к новому году",
- "",
- []string{},
- map[string]int{
- "домой": 1,
- },
- "домой",
- "на улице уже вовсю готовятся к новому году",
- false,
- },
- }
- return &Game{player, rooms}
- }
- func startGameBot(ctx context.Context) error {
- t, err := NewGameBot()
- if err != nil {
- return err
- }
- updates := t.bot.ListenForWebhook("/")
- go http.ListenAndServe(":8080", nil)
- for update := range updates {
- t.execUpdate(update)
- }
- return nil
- }
- func main() {
- err := startGameBot(context.Background())
- if err != nil {
- panic(err)
- }
- }
- func (t *GameBot) Start(user *tgbotapi.User, update tgbotapi.Update) {
- t.activeGames[user.ID] = MakeNewGame()
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "добро пожаловать в игру!",
- ))
- }
- func (t *GameBot) Reset(user *tgbotapi.User, update tgbotapi.Update) {
- delete(t.activeGames, user.ID)
- game := MakeNewGame()
- t.activeGames[user.ID] = game
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "состояние игры сброшено",
- ))
- }
- func (t *GameBot) LookAround(user *tgbotapi.User, update tgbotapi.Update) {
- game := t.activeGames[user.ID]
- room := game.rooms[game.player.roomID]
- answer := room.description
- if game.player.bag != nil {
- if strings.Contains(room.quest, "собрать рюкзак и ") {
- room.quest = strings.Replace(room.quest, "собрать рюкзак и ", "", -1)
- } else if strings.Contains(room.quest, "собрать рюкзак и ") {
- room.quest = strings.Replace(room.quest, "собрать рюкзак", "", -1)
- }
- }
- if room.quest != "" {
- answer += ", " + room.quest
- }
- answer += "."
- if len(room.nearRooms) > 0 {
- answer += " можно пройти - " + room.canGo
- answer += "."
- }
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- answer,
- ))
- }
- func (t *GameBot) Go(roomName string, user *tgbotapi.User, update tgbotapi.Update) {
- game := t.activeGames[user.ID]
- room := game.rooms[game.player.roomID]
- roomID, ok := room.nearRooms[roomName]
- if !ok {
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "нет пути в "+roomName,
- ))
- return
- }
- newRoom := game.rooms[roomID]
- if newRoom.isOpen {
- game.player.roomID = roomID
- answer := newRoom.wellcomeMessage + "."
- if len(newRoom.nearRooms) > 0 {
- answer += " можно пройти - " + newRoom.canGo
- answer += "."
- }
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- answer,
- ))
- } else {
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "дверь закрыта",
- ))
- }
- }
- func (t *GameBot) Equip(itemName string, user *tgbotapi.User, update tgbotapi.Update) {
- game := t.activeGames[user.ID]
- room := game.rooms[game.player.roomID]
- if itemName == "рюкзак" {
- i, ok := contains(room.items, itemName)
- if ok {
- game.player.bag = new(Bag)
- }
- room.items = append(room.items[:i], room.items[i+1:]...)
- room.description = strings.Replace(room.description, ", на стуле - рюкзак", "", -1)
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "вы одели: "+itemName,
- ))
- }
- }
- func (t *GameBot) Take(itemName string, user *tgbotapi.User, update tgbotapi.Update) {
- game := t.activeGames[user.ID]
- room := game.rooms[game.player.roomID]
- if game.player.bag != nil {
- i, ok := contains(room.items, itemName)
- if ok {
- game.player.bag.items = append(game.player.bag.items, itemName)
- } else {
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "нет такого",
- ))
- return
- }
- room.items = append(room.items[:i], room.items[i+1:]...)
- lastIndex := strings.LastIndex(room.description, itemName) + len(itemName)
- if lastIndex < len(room.description) && room.description[lastIndex:lastIndex+1] == "," {
- room.description = strings.Replace(room.description, " "+itemName+",", "", -1)
- } else {
- room.description = strings.Replace(room.description, " "+itemName, "", -1)
- }
- if len(room.items) == 0 {
- room.description = "пустая комната"
- }
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "предмет добавлен в инвентарь: "+itemName,
- ))
- } else {
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "некуда класть",
- ))
- }
- }
- func (t *GameBot) Apply(args string, user *tgbotapi.User, update tgbotapi.Update) {
- game := t.activeGames[user.ID]
- room := game.rooms[game.player.roomID]
- ar := strings.Split(args, " ")
- ok := false
- if game.player.bag != nil {
- _, ok = contains(game.player.bag.items, ar[0])
- }
- if !ok {
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "нет предмета в инвентаре - "+ar[0],
- ))
- return
- }
- if ar[1] != "дверь" {
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "не к чему применить",
- ))
- return
- }
- if args == "ключи дверь" {
- // Ищем комнату с дверью
- haveDoor := false
- var closedRoomID int
- for _, roomID := range room.nearRooms {
- if !game.rooms[roomID].isOpen {
- haveDoor = true
- closedRoomID = roomID
- break
- }
- }
- if haveDoor {
- game.rooms[closedRoomID].isOpen = true
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "дверь открыта",
- ))
- } else {
- t.bot.Send(tgbotapi.NewMessage(
- update.Message.Chat.ID,
- "здесь нет дверей",
- ))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement