Advertisement
Guest User

Untitled

a guest
May 19th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 8.47 KB | None | 0 0
  1.  package Bot
  2.  
  3. import java.text.SimpleDateFormat
  4. import java.util.Date
  5.  
  6. import scala.collection.immutable
  7. import scala.collection.immutable.HashSet
  8.  
  9.  
  10. trait Repository {
  11.   var polls: Map[Int, Poll] = immutable.Map[Int, Poll]()
  12.  
  13.   def putInRep(id: Int, poll: Poll) {
  14.     polls = polls + (id -> poll)
  15.   }
  16.  
  17.   def getRep: Map[Int, Poll] = polls
  18.  
  19.   def removeFromRep(id: Int) {
  20.     polls = polls - id
  21.   }
  22.  
  23.   def cleanRep() {
  24.     polls = polls.empty
  25.   }
  26.  
  27.   def getPollById(id: Int): Poll = {
  28.     polls(id)
  29.   }
  30.  
  31.   def getPollByIdOption(id: Int): Option[Poll] = {
  32.     polls.get(id)
  33.   }
  34. }
  35.  
  36. case class Context(context:Option[Int])
  37.  
  38. object CommandImpl extends Repository {
  39.  
  40.  
  41.   val formatDate = new SimpleDateFormat("hh:mm:ss yy:MM:dd")
  42.  
  43.   val maxId = Stream.from(0).iterator
  44.  
  45.   val userID = Stream.from(0).iterator //TODO временно
  46.  
  47.   var context:Option[Int] = None
  48.  
  49.   def getMaxID: Int = {
  50.     maxId.next()
  51.   }
  52.  
  53.   def startTime(time: Option[String]): Option[Date] = {
  54.     if (time.isDefined) {
  55.       return Option(formatDate.parse(time.getOrElse(formatDate.format(new Date))))
  56.     }
  57.  
  58.     None
  59.   }
  60.  
  61.   def getTimeFromFormat(string: String): Date = {
  62.     formatDate.parse(string)
  63.   }
  64.  
  65.   def stopTime(time: Option[String]): Option[Date] = {
  66.     if (time.isDefined) {
  67.       return Option(formatDate.parse(time.getOrElse(formatDate.format(new Date))))
  68.     }
  69.     None
  70.   }
  71.  
  72.   def checkRoot(poll: Poll, id:Long):Boolean = {
  73.     poll.admin == id
  74.   }
  75.  
  76.   def createPoll(name: String, anonymityVar: Option[String], continuousOrAfterstopVar: Option[String],
  77.                  startTimeVar: Option[String], stopTimeVar: Option[String], user: User = User(0,"")): Int = {
  78.     val anonymity = anonymityVar.getOrElse("yes") == "yes"
  79.  
  80.     val continuousOrAfterstop = continuousOrAfterstopVar.getOrElse("afterstop") == "continuous"
  81.  
  82.     val startTime1 = startTime(startTimeVar)
  83.  
  84.     val stopTime1 = stopTime(stopTimeVar)
  85.  
  86.     val id = getMaxID
  87.  
  88.     putInRep(id, Poll(name,id, user.id, anonymity, continuousOrAfterstop, startTime1, stopTime1))
  89.  
  90.     id
  91.   }
  92.  
  93.   def createPollView(id: Int, name: String): String = {
  94.     s"😇 Poll *$name* was created, here is your poll id:\n👉 `$id` 👈\n" +
  95.       s"🦄 Type */begin ($id)* to continue"
  96.   }
  97.  
  98.   def listPolls(): String = {
  99.     if (getRep.isEmpty)
  100.       return s"😐 No polls created yet."
  101.     s"👉 Current polls:\n${getRep.aggregate("")((s, p) => s"$s *${p._1})* ${p._2.name}\n", _ + _)}"
  102.   }
  103.  
  104.   def deletePoll(id: Int, userIDE: User): String = {
  105.     getRep.get(id).map { poll =>
  106.       if (!checkRoot(poll, userIDE.id))
  107.         return s"😟 Sorry, you don't have enough permissions for doing this"
  108.       removeFromRep(id)
  109.       s"Poll deleted successfully 😈"
  110.     }.getOrElse(s"Can't find such poll 👻. Maybe it doesn't exist?")
  111.   }
  112.  
  113.   def startPoll(id: Int, date: Date, userIDE:User): String = {
  114.     getPollByIdOption(id).map { poll =>
  115.       if (!checkRoot(poll, userIDE.id))
  116.         return s"😟 Sorry, you don't have enough permissions for doing this"
  117.       if (PollCommand.active(poll, date) || poll.start_time.isDefined) {
  118.         return s"👌 Poll is running"
  119.       }
  120.  
  121.       if (poll.start_time.isEmpty) {
  122.         putInRep(id, PollCommand.start(poll, date))
  123.         return s"🤘 Poll has started"
  124.       }
  125.       return s"Can't start poll *$id* for some reason 😕"
  126.  
  127.     }.getOrElse(s"Can't find such poll 👻. Maybe it doesn't exist?")
  128.   }
  129.  
  130.  
  131.   def stopPoll(id: Int, date: Date, userIDE:User): String = {
  132.     getPollByIdOption(id).map { poll =>
  133.       if (!checkRoot(poll, userIDE.id))
  134.         return s"😟 Sorry, you don't have enough permissions for doing this"
  135.       if (!PollCommand.active(poll, date)) {
  136.         return s"Poll isn't active 😤. Be patient."
  137.       }
  138.       if (poll.end_time.isEmpty) {
  139.         putInRep(id, PollCommand.stop(poll, date))
  140.         return s"Poll is stopped ⛔"
  141.       }
  142.       else {
  143.         return s"Don't worry, poll will stop automatically 😉"
  144.       }
  145.       putInRep(id, PollCommand.stop(poll, date))
  146.       return s"Poll was stopped ⛔"
  147.  
  148.     }.getOrElse(s"Can't find such poll 👻. Maybe it doesn't exist?")
  149.  
  150.   }
  151.  
  152.  
  153.   def pollResult(id: Int): String = {
  154.     getRep.get(id).map { poll =>
  155.       PollCommand.getResult(getPollById(id), new Date)
  156.     }.getOrElse(s"Can't find such poll 👻. Maybe it doesn't exist?")
  157.   }
  158.  
  159.   def begin(id: Int): String = {
  160.     if (!getRep.contains(id))
  161.       return s"Can't find such poll 👻. Maybe it doesn't exist?"
  162.     context = Option(id)
  163.     s"🤓 Okay, now you can:" +
  164.       s"\n/add\\_question _(<question>)_ _(open|choice|multi)_," +
  165.       s"\n/delete\\_question _(<question number>)_," +
  166.       s"\n/answer _(<question number>)_ _(<answer>)_ or" +
  167.       s"\n/view all questions" +
  168.       s"\nAnd don't forget to /end 😉"
  169.   }
  170.  
  171.   def end(): String = {
  172.     context.map { id =>
  173.       context = None
  174.       s"🤓 You can try typing */result ($id)*"
  175.     }.getOrElse(s"Ah, you probably forgot to /begin 😌")
  176.   }
  177.  
  178.   def view(): String = {
  179.     getPollByIdOption(context.getOrElse(return s"Ah, you probably forgot to /begin 😌")).map { poll =>
  180.       PollCommand.getView(poll)
  181.  
  182.     }.getOrElse(s"Can't find such poll 👻. Maybe it doesn't exist?")
  183.   }
  184.  
  185.   def addQuestion(name: String, typeOfQuestion: String, list: List[String], userIDE:User): String = {
  186.     getPollByIdOption(context.getOrElse(return s"Ah, you probably forgot to /begin 😌")).map { poll =>
  187.       if (!checkRoot(poll, userIDE.id))
  188.         return s"😟 Sorry, you don't have enough permissions for doing this"
  189.       val question = Question(name,typeOfQuestion, HashSet[User](),list.map(e => Variant(e, Nil)))
  190.       putInRep(context.get, PollCommand.addQuestion(poll, question))
  191.       s"👌 Question _'$name'_ was added *(${poll.questions.size})*"
  192.  
  193.     }.getOrElse(s"Can't find such poll 👻. Maybe it doesn't exist?")
  194.   }
  195.  
  196.   def deleteQuestion(id:Int, userIDE:User): String = {
  197.     getPollByIdOption(context.getOrElse(return s"Ah, you probably forgot to /begin 😌")).map { poll =>
  198.       if (!checkRoot(poll, userIDE.id))
  199.         return s"😟 Sorry, you don't have enough permissions for doing this"
  200.       putInRep(context.get, PollCommand.deleteQuestionById(poll,id))
  201.       s"🤞 Question was deleted"
  202.  
  203.     }.getOrElse(s"Can't find such poll 👻. Maybe it doesn't exist?")
  204.   }
  205.  
  206.   def addAnswerOpen(id:Int, answer:String, user: User): String = {
  207.     context.map(cont => {
  208.       val poll = getPollByIdOption(cont).get
  209.       if (poll.questions(id).voitedUsers.contains(user))
  210.         return s"Hey, you can't vote twice! 🇷🇺"
  211.       if (poll.questions(id).typeOfQuestion != "open")
  212.         return s"😤 Nah, this is a *${poll.questions(id).typeOfQuestion}* question"
  213.       val b = QuestionHandler.addAnswer(poll.questions(id),poll.anonymity, 0, Answer(answer,Option(user)))
  214.       val a = PollCommand.updateQuestion(poll, id, b)
  215.       putInRep(cont, a)
  216.       "✔ Thank you for voting"
  217.  
  218.     }).getOrElse(s"Ah, you probably forgot to /begin 😌")
  219.   }
  220.  
  221.   def addAnswerChoice(id:Int, list: List[Int], user: User): String = {
  222.     context.map(cont => {
  223.       for(i <- list) yield {
  224.         val poll = getPollByIdOption(cont).get
  225.         if (poll.questions(id).voitedUsers.contains(user))
  226.           return s"Hey, you can't vote twice! 🇷🇺"
  227.         if (poll.questions(id).typeOfQuestion == "choice" && list.size>1)
  228.           return s"😤 Nah, this is a *${poll.questions(id).typeOfQuestion}* question." +
  229.             s"You can take only 1️⃣ option"
  230.         val b = QuestionHandler.addAnswer(poll.questions(id),poll.anonymity,  i, Answer("",Option(user)))
  231.         val a = PollCommand.updateQuestion(poll, id, b)
  232.         putInRep(cont, a)
  233.       }
  234.       "✔ Thank you for voting"
  235.     }).getOrElse(s"Ah, you probably forgot to /begin 😌")
  236.   }
  237.  
  238.   def printHelp(): String  = {
  239.       return s"👾 *Available commands:*" +
  240.       s"\n/create\\_poll - create new poll" +
  241.       s"\n/list - list current polls" +
  242.       s"\n/delete\\_poll - delete poll" +
  243.       s"\n/start\\_poll - start poll" +
  244.       s"\n/stop\\_poll - stop poll" +
  245.       s"\n/result - view poll results" +
  246.       s"\n/begin - start working with poll" +
  247.       s"\n\n👾 After */begin*, these commands will be available: 👇" +
  248.       s"\n/view - view all poll questions" +
  249.       s"\n/add\\_question - add question to the poll" +
  250.       s"\n/delete\\_question - delete question" +
  251.       s"\n/answer - answer to the question" +
  252.       s"\n/end - leave current poll"
  253.   }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement