aboutsummaryrefslogtreecommitdiff
path: root/internal/game/sys_construction.go
blob: 06713cd7f2519ee017b804487e5e7fddd793abf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package game

import (
	"fmt"

	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) processSawmill(sess *net.Session, p *player.Player) {
	logTypes := map[string]struct {
		plankID string
		cost    int
		name    string
	}{
		"logs":          {"planks", 5, "regular planks"},
		"oak_logs":      {"oak_planks", 10, "oak planks"},
		"teak_logs":     {"teak_planks", 20, "teak planks"},
		"mahogany_logs": {"mahogany_planks", 40, "mahogany planks"},
	}

	totalCost := 0
	totalPlanks := 0
	type plankResult struct {
		plankID string
		qty     int
	}

	var results []plankResult

	for logID, info := range logTypes {
		qty := p.CountItem(logID)
		if qty == 0 {
			continue
		}
		cost := qty * info.cost
		totalCost += cost
		totalPlanks += qty
		results = append(results, plankResult{info.plankID, qty})
		p.RemoveItem(logID, qty)
	}

	if totalPlanks == 0 {
		sess.WriteLine("You don't have any logs to process.")
		return
	}

	if p.Credits < totalCost {
		sess.WriteLine(fmt.Sprintf("You need %d credits to process all your logs (you have %d).", totalCost, p.Credits))
		return
	}

	p.Credits -= totalCost

	processed := 0
	for _, r := range results {
		for i := 0; i < r.qty; i++ {
			freeSlot := p.FirstFreeSlot()
			if freeSlot == -1 {
				g.World.AddGroundItem(p.RoomID, r.plankID, r.qty-i)
				break
			}
			p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: r.plankID, Quantity: 1})
			processed++
		}
	}

	g.AccountStore.SaveCharacter(p)

	sess.WriteLine(fmt.Sprintf("The sawmill operator processes your logs into %d planks for %d credits.", processed, totalCost))
	if processed < totalPlanks {
		sess.WriteLine(fmt.Sprintf("Your inventory is full. The remaining %d planks fall to the ground.", totalPlanks-processed))
	}
}