main
Raw Download raw file
  1package main
  2
  3import (
  4	"errors"
  5	"fmt"
  6	"math/rand/v2"
  7	"net/http"
  8	"net/http/cookiejar"
  9	"time"
 10)
 11
 12type islandClient struct {
 13	client    *http.Client
 14	password  string
 15	sessionId string
 16
 17	Server     int
 18	User       string
 19	Resources  *Resoruces
 20	Buildings  map[string]*Building
 21	Production *Resoruces
 22}
 23
 24const (
 25	_baseURL = "https://www.ik-seite.de"
 26)
 27
 28func NewIslandClient(server int, user, password string) (*islandClient, error) {
 29
 30	// Create a cookie jar to store cookies
 31	jar, _ := cookiejar.New(nil)
 32
 33	// Initialize a new HTTP client with the cookie jar
 34	client := &http.Client{
 35		Jar: jar,
 36	}
 37
 38	ic := &islandClient{
 39		Server:   server,
 40		User:     user,
 41		password: password,
 42		client:   client,
 43	}
 44	return ic, nil
 45}
 46
 47func main() {
 48	//ic, err := NewIslandClient(16, "bob", "9kq143")
 49	ic, err := NewIslandClient(16, "mod_a", "7xbej8")
 50	if err != nil {
 51		fmt.Println(err)
 52		return
 53	}
 54
 55	err = ic.Login()
 56	if err != nil {
 57		fmt.Println(err)
 58		return
 59	}
 60
 61	err = ic.Overview()
 62	if err != nil {
 63		fmt.Println(err)
 64		return
 65	}
 66
 67	err = ic.Fortress()
 68	if err != nil {
 69		fmt.Println(err)
 70		return
 71	}
 72
 73	err = ic.UpdateProduction()
 74	if err != nil {
 75		fmt.Println(err)
 76		return
 77	}
 78
 79	fmt.Println(ic.Format())
 80
 81	sleep := 0 * time.Second
 82	minSleep := 60 * time.Minute
 83	for {
 84		if sleep != 0 {
 85			fmt.Printf(" .. Sleeping: %s", sleep.String())
 86			time.Sleep(sleep)
 87		}
 88
 89		ic.Overview()
 90		ic.Fortress()
 91		fmt.Println()
 92		fmt.Printf(ic.Status())
 93
 94		active, remaining := ic.FortressUpgrading()
 95		if active {
 96			randRound := time.Duration(rand.IntN(30)) * time.Second
 97			halfTime := time.Duration(remaining / 2).Round(randRound)
 98			sleep = halfTime
 99			if halfTime < minSleep {
100				sleep = minSleep
101			}
102			continue
103		}
104
105		gold := ic.Buildings[_GoldPit]
106		stone := ic.Buildings[_StoneBasin]
107		lumber := ic.Buildings[_Sawmill]
108		if gold.Level < 5 {
109			err = ic.Upgrade(gold)
110			if err != nil {
111				var deficitError *ErrNotEnoughResources
112				if errors.As(err, &deficitError) {
113					fmt.Printf(" .. %s\n%s - deficit .. %s delay",
114						gold.Name,
115						deficitError.Deficit.String(),
116						deficitError.Delay.String(),
117					)
118					sleep = max(deficitError.Delay/2, minSleep)
119					continue
120				}
121				fmt.Println("ERROR:", err)
122			}
123			sleep = minSleep
124			continue
125		}
126		if stone.Level < 5 {
127			err = ic.Upgrade(stone)
128			if err != nil {
129				var deficitError *ErrNotEnoughResources
130				if errors.As(err, &deficitError) {
131					fmt.Printf(" .. %s\n%s - deficit .. %s delay",
132						stone.Name,
133						deficitError.Deficit.String(),
134						deficitError.Delay.String(),
135					)
136					sleep = max(deficitError.Delay/2, minSleep)
137					continue
138				}
139				fmt.Println("ERROR:", err)
140			}
141			sleep = minSleep
142			continue
143		}
144		if lumber.Level < 5 {
145			err = ic.Upgrade(lumber)
146			if err != nil {
147				var deficitError *ErrNotEnoughResources
148				if errors.As(err, &deficitError) {
149					fmt.Printf(" .. %s\n%s - deficit .. %s delay",
150						lumber.Name,
151						deficitError.Deficit.String(),
152						deficitError.Delay.String(),
153					)
154					sleep = max(deficitError.Delay/2, minSleep)
155					continue
156				}
157				fmt.Println("ERROR:", err)
158			}
159			sleep = minSleep
160			continue
161		}
162		//err = ic.Upgrade(ic.Buildings[_StoneBasin])
163		//err = ic.Upgrade(ic.Buildings[_Sawmill])
164
165	}
166
167	//err = ic.Upgrade(ic.Buildings[_Fortress])
168	//if err != nil {
169	//	fmt.Println(err)
170	//}
171
172	//err = ic.Upgrade(ic.Buildings[_StoneBasin])
173	//fmt.Println(err)
174
175}