예제 #1
0
func (this *CallHandler) handle(text []string) (result string, err error) {
	if len(text) < 2 {
		err = errors.New("i need more info\n" + this.getHelp())
		return
	}
	groupname := mainhandler.getGroupName()
	if groupname == "" {
		err = errors.New("group not found groupid:" + mainhandler.rec.Group_id)
		return
	}
	content, err := models.GetWarDatabyclanname(groupname)
	if err != nil {
		fmt.Println(err.Error())
		err = errors.New("server error")
		return
	}
	if !content.IsEnable {
		result = "no war being"
		return
	}
	num1, err := strconv.Atoi(text[1])
	if err != nil {
		err = errors.New("arg1 need a number \n" + this.getHelp())
		return
	}
	newcallnum := -1
	newbattle := content.Battles[num1]
	for num, call := range newbattle.Callers {
		if call.Callername == mainhandler.rec.Name {
			newcallnum = num
		}
	}

	if newcallnum == -1 {
		newcallp := &models.Caller{mainhandler.rec.Name, -1, time.Now()}
		newbattle.Callers = append(newbattle.Callers, *newcallp)
	} else {
		newbattle.Callers[newcallnum].Calledtime = time.Now()
	}

	err = models.UpdateWarData(content.Id, bson.M{"$set": bson.M{fmt.Sprintf("battles.%d", num1): newbattle}})
	if err != nil {
		fmt.Println(err.Error())
		err = errors.New("server error")
		return
	}
	result = fmt.Sprintf("#%d called by %s", num1, mainhandler.rec.Name)
	return
}
예제 #2
0
func (this *ScoutHandler) handle(text []string) (result string, err error) {
	if len(text) < 2 {
		err = errors.New("i need more info\n" + this.getHelp())
		return
	}
	groupname := mainhandler.getGroupName()
	if groupname == "" {
		err = errors.New("group not found groupid:" + mainhandler.rec.Group_id)
		return
	}
	content, err := models.GetWarDatabyclanname(groupname)
	if err != nil {
		fmt.Println(err.Error())
		err = errors.New("server error")
		return
	}
	if !content.IsEnable {
		result = "no war being"
		return
	}
	num1, err := strconv.Atoi(text[1])
	if err != nil {
		err = errors.New("arg1 need a number \n" + this.getHelp())
		return
	}
	if content.Battles[num1].Scoutstate == "needscout" || content.Battles[num1].Scoutstate == "scouted" {
		result = fmt.Sprintf("#%d already %s", num1, content.Battles[num1].Scoutstate)
		return
	}

	err = models.UpdateWarData(content.Id, bson.M{"$set": bson.M{fmt.Sprintf("battles.%d.scoutstate", num1): "needscout"}})
	if err != nil {
		fmt.Println(err.Error())
		err = errors.New("server error")
		return
	}
	result = fmt.Sprintf("%s mark #%d need to scout", mainhandler.rec.Name, num1)
	return
}
예제 #3
0
func (this *EditwarHandler) handle(text []string) (result string, err error) {
	groupname := mainhandler.getGroupName()
	if groupname == "" {
		err = errors.New("group not found groupid:" + mainhandler.rec.Group_id)
		return
	}
	if len(text) < 3 {
		err = errors.New("i need more info\n" + this.getHelp())
		return
	}
	num1, err := strconv.Atoi(text[1])
	if err != nil || num1 < 0 {
		err = errors.New("arg2 must be number\n" + this.getHelp())
		return
	}
	num2, err := strconv.Atoi(text[2])
	isTime := strings.HasSuffix(text[2], "am") || strings.HasSuffix(text[2], "pm")
	if (err != nil || num2 < 0) && !isTime {
		err = errors.New("arg3 must be number or time(endwith am/pm)\n" + this.getHelp())
		return
	}

	if err == nil {
		enemyname := strings.Join(text[3:len(text)], " ")
		battles := make([]models.Battle, num2)
		battlep := &models.Battle{}
		battlep.Init()
		for index := range battles {
			battles[index] = *battlep
		}
		err := models.UpdateWarData(num1, bson.M{"$set": bson.M{"teamb": enemyname, "battles": battles}})
		if err == nil {
			content, err := models.GetWarData(num1)
			if err != nil {
				fmt.Println(err.Error())
				err = errors.New("server error")
			} else if !content.IsEnable {
				result = "no war being"
			} else {
				result = fmt.Sprintf("War #%d Edited \n %s VS %s \n %d vs %d \n War starts %s", content.Id, content.TeamA, content.TeamB, len(content.Battles), len(content.Battles), content.Begintime.Format("3:04PM MST 1/2/2006"))
			}

		}
	} else if isTime {
		now := time.Now()
		var h int
		if strings.HasSuffix(text[2], "am") {
			fmt.Printf("trim time:%s\n", strings.Trim(text[2], "am"))
			h, err = strconv.Atoi(strings.Trim(text[2], "am"))
			if err != nil || h < 0 {
				err = errors.New("wrong time format")
			} else {
				mi := 0
				if h > 12 {
					mi = int(math.Mod(float64(h), 100))
					h = int(h / 100)
				}
				y, m, d := now.Date()
				if now.Hour()*60+now.Minute() > h*60+mi { //if befor now so will be tmw
					d++
				}
				newtime := time.Date(y, m, d, h, mi, 0, 0, time.Local)
				err := models.UpdateWarData(num1, bson.M{"$set": bson.M{"begintime": newtime}})
				if err == nil {
					content, err := models.GetWarData(num1)
					if err != nil {
						fmt.Println(err.Error())
						err = errors.New("server error")
					} else if !content.IsEnable {
						result = "no war being"
					} else {
						result = fmt.Sprintf("War #%d Edited \n %s VS %s \n %d vs %d \n War starts %s", content.Id, content.TeamA, content.TeamB, len(content.Battles), len(content.Battles), content.Begintime.Format("3:04PM MST 1/2/2006"))
					}
				}
			}
		} else {

			h, err = strconv.Atoi(strings.Trim(text[2], "pm"))
			fmt.Printf("trim time:%d\n", h)
			if err != nil || h < 0 {
				err = errors.New("wrong time format")
			} else {
				mi := 0
				if h > 12 {
					mi = int(math.Mod(float64(h), 100))
					h = int(h / 100)
				}
				h = h + 12
				y, m, d := now.Date()
				if now.Hour()*60+now.Minute() > h*60+mi { //if befor now so will be tmw
					d++
				}
				fmt.Printf("trim time mi:%d\n", mi)
				newtime := time.Date(y, m, d, h, mi, 0, 0, time.Local)
				err := models.UpdateWarData(num1, bson.M{"$set": bson.M{"begintime": newtime}})
				if err == nil {
					content, err := models.GetWarData(num1)
					if err != nil {
						fmt.Println(err.Error())
						err = errors.New("server error")
					} else if !content.IsEnable {
						result = "no war being"
					} else {
						result = fmt.Sprintf("War #%d Edited \n %s VS %s \n %d vs %d \n War starts %s", content.Id, content.TeamA, content.TeamB, len(content.Battles), len(content.Battles), content.Begintime.Format("3:04PM MST 1/2/2006"))
					}
				}
			}
		}

	} else {
		err = errors.New("wrong arg \n" + this.getHelp())
	}

	return

}
예제 #4
0
func (this *StarHandler) handle(text []string) (result string, err error) {
	if len(text) < 3 {
		err = errors.New("i need more info\n" + this.getHelp())
		return
	}
	num1, err := strconv.Atoi(text[1])
	if err != nil || num1 < 0 {
		err = errors.New("arg1 need a number \n" + this.getHelp())
		return
	}
	num2, err := strconv.Atoi(text[2])
	if err != nil || num2 < 0 || num2 > 3 {
		err = errors.New("arg2 need a number \n" + this.getHelp())
		return
	}
	groupname := mainhandler.getGroupName()
	if groupname == "" {
		err = errors.New("group not found groupid:" + mainhandler.rec.Group_id)
		return
	}
	content, err := models.GetWarDatabyclanname(groupname)
	if err != nil {
		fmt.Println(err.Error())
		err = errors.New("server error")
		return
	}
	if !content.IsEnable {
		result = "no war being"
		return
	}

	newcallnum := -1
	newbattle := content.Battles[num1]

	for num, call := range newbattle.Callers {
		if call.Callername == mainhandler.rec.Name {
			newcallnum = num
		}
	}

	newbattle.Scouted()
	if newcallnum == -1 {
		newcallp := &models.Caller{mainhandler.rec.Name, num2, time.Now()}
		newbattle.Callers = append(newbattle.Callers, *newcallp)
	} else {
		newbattle.Callers[newcallnum].Calledtime = time.Now()
		newbattle.Callers[newcallnum].Starstate = num2
	}

	err = models.UpdateWarData(content.Id, bson.M{"$set": bson.M{fmt.Sprintf("battles.%d", num1): newbattle}})

	if err != nil {
		fmt.Println(err.Error())
		err = errors.New("server error")
		return
	}
	switch num2 {
	case 0:
		result = fmt.Sprintf("%s win %d star at #%d\n%s", mainhandler.rec.Name, num2, num1, zerostar[rand.Intn(len(zerostar))])
	case 1:
		result = fmt.Sprintf("%s win %d star at #%d\n%s", mainhandler.rec.Name, num2, num1, onestar[rand.Intn(len(onestar))])
	case 2:
		result = fmt.Sprintf("%s win %d stars at #%d\n%s", mainhandler.rec.Name, num2, num1, twostar[rand.Intn(len(twostar))])
	case 3:
		result = fmt.Sprintf("%s win %d stars at #%d\n%s", mainhandler.rec.Name, num2, num1, threestar[rand.Intn(len(threestar))])
	}

	return
}