コード例 #1
0
func (this *OpenedwarHandler) handle(text []string) (result string, err error) {
	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
	}

	result = fmt.Sprintf("War #%d  \n %s VS %s \n %d vs %d \n War starts %s\n",
		content.Id,
		content.TeamA,
		content.TeamB,
		len(content.Battles),
		len(content.Battles),
		content.Begintime.Format("3:04PM MST 1/2/2006"))
	keys := []int{}
	for num, _ := range content.Battles {
		keys = append(keys, num)
	}
	battlesresult := ""
	sort.Ints(keys)
	for _, num := range keys {
		hightstar := -1
		hightstars := "ZZZ"
		called := false
		lineresult := fmt.Sprintf("||%d.%s ", num+1, content.Battles[num].Scoutstate)
		for _, caller := range content.Battles[num].Callers {
			if caller.Starstate > -1 && caller.Starstate < 4 {
				if caller.Starstate > hightstar {
					hightstar = caller.Starstate
					hightstars = caller.GetStarstate()
				}
				lineresult = lineresult + fmt.Sprintf("|%s %s", caller.Callername, caller.GetStarstate())
			} else {
				if time.Now().After(caller.Calledtime.Add(6 * time.Hour)) {
					continue
				} else {
					called = true
				}
			}
		}
		if hightstar != 3 && called == false {
			battlesresult = battlesresult + hightstars + lineresult + " open\n"
		}
	}
	result = result + battlesresult

	return
}
コード例 #2
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
}
コード例 #3
0
// @Title Get
// @Description get WarDataModel by clanname
// @Param	clanname		path 	string	true		"The key for clan"
// @Success 200 {object} models.WarDataModel
// @Failure 404  not found
// @Failure 403 :clanname is empty
// @router /:clanname [get]
func (c *WarDataController) GetWar() {

	clanname := c.Ctx.Input.Params[":clanname"]
	fmt.Printf("clanname:%s\n", clanname)
	if clanname == "" {
		c.Ctx.Output.SetStatus(403)
		c.Data["json"] = string("is empty")
	} else {
		ob, err := models.GetWarDatabyclanname(clanname)
		if err != nil {
			c.Ctx.Output.SetStatus(404)
			c.Data["json"] = err.Error()
			fmt.Printf("err:%s\n", err.Error())
		} else {
			c.Data["json"] = ob
			fmt.Printf("ob:%v\n", ob)
		}
	}
	c.ServeJson()
}
コード例 #4
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
}
コード例 #5
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
}
コード例 #6
0
func (this *ShowwarHandler) handle(text []string) (result string, err error) {
	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
	}
	if len(text) == 1 {
		result = fmt.Sprintf("War #%d  \n %s VS %s \n %d vs %d \n War starts %s\n",
			content.Id,
			content.TeamA,
			content.TeamB,
			len(content.Battles),
			len(content.Battles),
			content.Begintime.Format("3:04PM MST 1/2/2006"))
		keys := []int{}
		for num, _ := range content.Battles {
			keys = append(keys, num)
		}
		battlesresult := ""
		sort.Ints(keys)
		for _, num := range keys {
			hightstar := -1
			hightstars := "ZZZ"
			lineresult := fmt.Sprintf("||%d.%s ", num+1, content.Battles[num].Scoutstate)
			for _, caller := range content.Battles[num].Callers {
				if caller.Starstate > -1 && caller.Starstate < 4 {
					if caller.Starstate > hightstar {
						hightstar = caller.Starstate
						hightstars = caller.GetStarstate()
					}
					lineresult = lineresult + fmt.Sprintf("|%s %s", caller.Callername, caller.GetStarstate())
				} else {
					if time.Now().After(caller.Calledtime.Add(6 * time.Hour)) {
						lineresult = lineresult + fmt.Sprintf("|%s expried", caller.Callername)
					} else {
						expried := caller.Calledtime.Add(6 * time.Hour).Sub(time.Now())
						lineresult = lineresult + fmt.Sprintf("|%s %dh%dm ", caller.Callername, int(expried.Hours()), int(math.Mod(expried.Minutes(), 60)))
					}
				}
			}
			battlesresult = battlesresult + hightstars + lineresult + "\n"
		}
		result = result + battlesresult
	} else {
		num, err := strconv.Atoi(text[1])
		if err != nil {
			err = errors.New("arg2 must be number\n" + this.getHelp())
		} else {
			if num > len(content.Battles) {
				err = errors.New(fmt.Sprintf("last one is %d\n", len(content.Battles)))
			} else {
				battle := content.Battles[num-1]
				result = fmt.Sprintf("%d.%s ", num, battle.Scoutstate)
				for _, caller := range battle.Callers {
					if time.Now().After(caller.Calledtime.Add(6 * time.Hour)) {
						result = result + fmt.Sprintf("%s expried")
					} else {
						expried := caller.Calledtime.Add(6 * time.Hour).Sub(time.Now())
						result = result + fmt.Sprintf("%s %sh%sm ", caller.Callername, expried.Hours(), expried.Minutes())
					}
				}
			}
		}
	}

	return
}