main
Raw Download raw file
  1package main
  2
  3import (
  4	"fmt"
  5	"math/rand/v2"
  6	"strings"
  7	"time"
  8)
  9
 10const (
 11	// building id's
 12	_Fortress    = "b1"
 13	_GoldPit     = "b2"
 14	_StoneBasin  = "b3"
 15	_Sawmill     = "b4"
 16	_ResearchLab = "b5"
 17	_Garrison    = "b6"
 18	_Shipyard    = "b7"
 19	_Warehouse   = "b8"
 20	_Barricade   = "b9"
 21	_Graveyard   = "b10"
 22
 23	// building names'
 24	_FortressName    = "Fortress"
 25	_GoldPitName     = "Gold Pit"
 26	_StoneBasinName  = "Stone Basin"
 27	_SawmillName     = "Sawmill"
 28	_ResearchLabName = "Research Lab"
 29	_GarrisonName    = "Garrison"
 30	_ShipyardName    = "Shipyard"
 31	_WarehouseName   = "Warehouse"
 32	_BarricadeName   = "Barricade"
 33	_GraveyardName   = "Graveyard"
 34)
 35
 36type Building struct {
 37	Name    string
 38	Id      string
 39	Level   int
 40	Status  string
 41	Upgrade *Resoruces
 42}
 43
 44func (ic *islandClient) Upgrade(b *Building) error {
 45
 46	if b.Upgrade != nil {
 47		var dg, ds, dl int
 48		var deficit bool = false
 49		if ic.Resources.Gold < b.Upgrade.Gold {
 50			dg = b.Upgrade.Gold - ic.Resources.Gold
 51			deficit = true
 52		}
 53		if ic.Resources.Stone < b.Upgrade.Stone {
 54			ds = b.Upgrade.Stone - ic.Resources.Stone
 55			deficit = true
 56		}
 57		if ic.Resources.Lumber < b.Upgrade.Lumber {
 58			dl = b.Upgrade.Lumber - ic.Resources.Lumber
 59			deficit = true
 60		}
 61		if deficit {
 62			delayGold := time.Duration(
 63				int64(dg)/int64(ic.Production.Gold),
 64			) * time.Hour
 65			delayStone := time.Duration(
 66				int64(ds)/int64(ic.Production.Stone),
 67			) * time.Hour
 68			delayLumber := time.Duration(
 69				int64(dl)/int64(ic.Production.Lumber),
 70			) * time.Hour
 71
 72			max := delayGold
 73			if delayStone > max {
 74				max = delayStone
 75			}
 76			if delayLumber > max {
 77				max = delayLumber
 78			}
 79
 80			return NewNotEnoughResources(dg, ds, dl, max)
 81		}
 82	}
 83
 84	fmt.Printf("\n\t\tStarting upgrade: %s\n", b.Format())
 85
 86	params := page(_Fortress)
 87	params[_action] = b.Id
 88	randId := rand.IntN(1000)
 89	params["b1_id"] = fmt.Sprintf("%04d", randId)
 90	bodyBytes, err := ic.overview(params)
 91	if err != nil {
 92		return fmt.Errorf("failed to upgrade building=%s: %w", b.Name, err)
 93	}
 94	resourcesMsg := "Gold, stones or wood not sufficient!"
 95	if strings.Contains(string(bodyBytes), resourcesMsg) {
 96		return fmt.Errorf(
 97			"failed to upgrade building=%s unexpected resource deficit",
 98			b.Name)
 99	}
100
101	return nil
102}
103
104func nameToBuilding(name string) *Building {
105	switch name {
106	case _FortressName:
107		return &Building{
108			Name: _FortressName,
109			Id:   _Fortress,
110		}
111	case _GoldPitName:
112		return &Building{
113			Name: _GoldPitName,
114			Id:   _GoldPit,
115		}
116	case _StoneBasinName:
117		return &Building{
118			Name: _StoneBasinName,
119			Id:   _StoneBasin,
120		}
121	case _SawmillName:
122		return &Building{
123			Name: _SawmillName,
124			Id:   _Sawmill,
125		}
126	case _ResearchLabName:
127		return &Building{
128			Name: _ResearchLabName,
129			Id:   _ResearchLab,
130		}
131	case _GarrisonName:
132		return &Building{
133			Name: _GarrisonName,
134			Id:   _Garrison,
135		}
136	case _ShipyardName:
137		return &Building{
138			Name: _ShipyardName,
139			Id:   _Shipyard,
140		}
141	case _WarehouseName:
142		return &Building{
143			Name: _WarehouseName,
144			Id:   _Warehouse,
145		}
146	case _BarricadeName:
147		return &Building{
148			Name: _BarricadeName,
149			Id:   _Barricade,
150		}
151	case _GraveyardName:
152		return &Building{
153			Name: _GraveyardName,
154			Id:   _Graveyard,
155		}
156	default:
157		return nil
158	}
159}