]> Skullheadx's Git Forge - monopoly-web.git/commitdiff
helper funcs for has monopoly and adjust money
authorSkullheadx <admonty1@protonmail.com>
Sun, 17 May 2026 03:25:48 +0000 (23:25 -0400)
committerSkullheadx <admonty1@protonmail.com>
Sun, 17 May 2026 03:25:48 +0000 (23:25 -0400)
game/color.go
game/game.go
game/go.go
game/movement.go

index cde26feefe28cebae921c5fa99cb2ce206443a1f..13547827e4194b905a3ac5816e4b6d66acbbb5e1 100644 (file)
@@ -1 +1,21 @@
 package game
+
+func processOwnedColors() {
+       for _, ocv := range OwnedColorVisitors {
+               visitorID := ocv.visitorID
+               ownerID := ocv.ownerID
+               colorID := ocv.colorID
+
+               prop := ColorProperties[colorID]
+               prices := ColorPropertyRents[colorID]
+
+               var rent int32 = prices[prop.Houses]
+
+               if prop.Houses == 0 && HasMonopoly(ownerID, prop.GroupID) {
+                       rent *= 2
+               }
+
+               AdjustPlayerMoney(visitorID, -rent)
+               AdjustPlayerMoney(ownerID, rent)
+       }
+}
index 0a95690a869f45ca637b8d99476a5cbe9e2be002..b137ba9d0a5bf3685a1631ec0726b3f987cff789 100644 (file)
@@ -1,33 +1,42 @@
 package game
 
-const MaxPlayers int32 = 8
-
-var Users [MaxPlayers]User
-
-//
-// const UtilityPrice int32 = 150
-// const UtilityRentMult = [2]int32{4, 10}
-// const UtilityMortgageValue int32 = 75
-//
-// var Utilities = [2]Utility{
-//     {Name: "Electric Company", Mortgaged: false},
-//     {Name: "Waterworks", Mortgaged: false},
-// }
-//
-// const RailroadPrice int32 = 200
-// const RailroadRent = [4]int32{25, 50, 100, 200}
-// const RailroadMortgageValue int32 = 100
-//
-// var RailRoads = [4]RailRoad{
-//     {Name: "Reading Railroad", Mortgaged: false},
-//     {Name: "Pennsylvania Railroad", Mortgaged: false},
-//     {Name: "B.&O. Railroad", Mortgaged: false},
-//     {Name: "Short Line", Mortgaged: false},
-// }
-//
-//
-// var Chest = [3]Chest{
-//     {Name: "Community Chest"},
-//     {Name: "Community Chest"},
-//     {Name: "Community Chest"},
-// }
+var Users []User
+var DebtEvents []int32
+
+func HasMonopoly(playerID int32, targetGroup ColorGroup) bool {
+       var ownedCount int32
+       for colorID, ownerID := range PropertyOwners {
+               if ownerID == playerID && ColorProperties[colorID].GroupID == targetGroup {
+                       ownedCount++
+               }
+       }
+
+       return ownedCount == ColorGroupSizes[targetGroup]
+}
+
+func IsInDebt(playerID int32) (bool, int32) {
+       for i, pID := range DebtEvents {
+               if pID == playerID {
+                       return true, int32(i)
+               }
+       }
+       return false, -1
+}
+
+func AdjustPlayerMoney(playerID int32, amount int32) {
+       Users[playerID].Money += amount
+
+       inDebt, i := IsInDebt(playerID)
+
+       if Users[playerID].Money < 0 {
+               if !inDebt {
+                       DebtEvents = append(DebtEvents, playerID)
+               }
+       } else { // Money >= 0
+               if inDebt { // remove player from DebtEvents table
+                       DebtEvents[i] = DebtEvents[len(DebtEvents)-1]
+                       DebtEvents = DebtEvents[:len(DebtEvents)-1]
+               }
+       }
+
+}
index cc1ae84a7691888b874d8f3fccd528298f8bcd46..d88505b68be27dda87cdcb958ecf2d12a344de1c 100644 (file)
@@ -1,7 +1,9 @@
 package game
 
+const GO_SALARY int32 = 200
+
 func ProcessGo() {
        for _, playerID := range GoVisitors {
-               Users[playerID].Money += 200
+               AdjustPlayerMoney(playerID, GO_SALARY)
        }
 }
index 28017be583a7e52379ddc57577e7df0bb513ae85..92356bdd73f52a30e484978be010fe201233f2a8 100644 (file)
@@ -50,36 +50,60 @@ type PropertyStatic struct {
        Price int32
 }
 
+type ColorGroup int32
+
+const (
+       GroupBrown ColorGroup = iota
+       GroupLightBlue
+       GroupPink
+       GroupOrange
+       GroupRed
+       GroupYellow
+       GroupGreen
+       GroupDarkBlue
+)
+
+var ColorGroupSizes = [...]int32{
+       GroupBrown:     2,
+       GroupLightBlue: 3,
+       GroupPink:      3,
+       GroupOrange:    3,
+       GroupRed:       3,
+       GroupYellow:    3,
+       GroupGreen:     3,
+       GroupDarkBlue:  2,
+}
+
 type ColorProperty struct {
        Name    string
        Price   int32
-       GroupID int32
+       GroupID ColorGroup
        Houses  int32
 }
 
 var ColorProperties = []ColorProperty{
-       {GroupID: 0, Houses: 0, Name: "Mediterranean Avenue", Price: 60},
-       {GroupID: 0, Houses: 0, Name: "Baltic Avenue", Price: 60},
-       {GroupID: 1, Houses: 0, Name: "Oriental Avenue", Price: 100},
-       {GroupID: 1, Houses: 0, Name: "Vermont Avenue", Price: 100},
-       {GroupID: 1, Houses: 0, Name: "Connecticut Avenue", Price: 120},
-       {GroupID: 2, Houses: 0, Name: "St. Charles Place", Price: 140},
-       {GroupID: 2, Houses: 0, Name: "States Avenue", Price: 140},
-       {GroupID: 2, Houses: 0, Name: "Virginia Avenue", Price: 160},
-       {GroupID: 3, Houses: 0, Name: "St. James Place", Price: 180},
-       {GroupID: 3, Houses: 0, Name: "Tennessee Avenue", Price: 180},
-       {GroupID: 3, Houses: 0, Name: "New York Avenue", Price: 200},
-       {GroupID: 4, Houses: 0, Name: "Kentucky Avenue", Price: 220},
-       {GroupID: 4, Houses: 0, Name: "Indiana Avenue", Price: 220},
-       {GroupID: 4, Houses: 0, Name: "Illinois Avenue", Price: 240},
-       {GroupID: 5, Houses: 0, Name: "Atlantic Avenue", Price: 260},
-       {GroupID: 5, Houses: 0, Name: "Ventnor Avenue", Price: 260},
-       {GroupID: 5, Houses: 0, Name: "Marvin Gardens", Price: 280},
-       {GroupID: 6, Houses: 0, Name: "Pacific Avenue", Price: 300},
-       {GroupID: 6, Houses: 0, Name: "North Carolina Avenue", Price: 300},
-       {GroupID: 6, Houses: 0, Name: "Pennsylvania Avenue", Price: 320},
-       {GroupID: 7, Houses: 0, Name: "Park Place", Price: 350},
-       {GroupID: 7, Houses: 0, Name: "Boardwalk", Price: 400},
+       {GroupID: GroupBrown, Houses: 0, Name: "Mediterranean Avenue", Price: 60},
+       {GroupID: GroupBrown, Houses: 0, Name: "Baltic Avenue", Price: 60},
+       {GroupID: GroupLightBlue, Houses: 0, Name: "Oriental Avenue", Price: 100},
+       {GroupID: GroupLightBlue, Houses: 0, Name: "Vermont Avenue", Price: 100},
+       {GroupID: GroupLightBlue, Houses: 0, Name: "Connecticut Avenue", Price: 120},
+       {GroupID: GroupPink, Houses: 0, Name: "St. Charles Place", Price: 140},
+       {GroupID: GroupPink, Houses: 0, Name: "States Avenue", Price: 140},
+       {GroupID: GroupPink, Houses: 0, Name: "Virginia Avenue", Price: 160},
+       {GroupID: GroupOrange, Houses: 0, Name: "St. James Place", Price: 180},
+       {GroupID: GroupOrange, Houses: 0, Name: "Tennessee Avenue", Price: 180},
+       {GroupID: GroupOrange, Houses: 0, Name: "New York Avenue", Price: 200},
+       {GroupID: GroupRed, Houses: 0, Name: "Kentucky Avenue", Price: 220},
+       {GroupID: GroupRed, Houses: 0, Name: "Indiana Avenue", Price: 220},
+       {GroupID: GroupRed, Houses: 0, Name: "Illinois Avenue", Price: 240},
+       {GroupID: GroupYellow, Houses: 0, Name: "Atlantic Avenue", Price: 260},
+       {GroupID: GroupYellow, Houses: 0, Name: "Ventnor Avenue", Price: 260},
+       {GroupID: GroupYellow, Houses: 0, Name: "Marvin Gardens", Price: 280},
+       {GroupID: GroupGreen, Houses: 0, Name: "Pacific Avenue", Price: 300},
+       {GroupID: GroupGreen, Houses: 0, Name: "North Carolina Avenue", Price: 300},
+       {GroupID: GroupGreen, Houses: 0, Name: "Pennsylvania Avenue", Price: 320},
+       {GroupID: GroupDarkBlue, Houses: 0, Name: "Park Place", Price: 350},
+       {GroupID: GroupDarkBlue, Houses: 0, Name: "Boardwalk", Price: 400},
 }
 
 var ColorPropertyRents = [][6]int32{
@@ -114,11 +138,19 @@ var RailroadProperties = []PropertyStatic{
        {Name: "Short Line", Price: 200},
 }
 
+const RailroadPrice int32 = 200
+const RailroadRent = [4]int32{25, 50, 100, 200}
+const RailroadMortgageValue int32 = 100
+
 var UtilityProperties = []PropertyStatic{
        {Name: "Electric Company", Price: 150},
        {Name: "Waterworks", Price: 150},
 }
 
+const UtilityPrice int32 = 150
+const UtilityRentMult = [2]int32{4, 10}
+const UtilityMortgageValue int32 = 75
+
 type TaxSpace struct {
        Name   string
        Amount int32
@@ -129,7 +161,7 @@ var TaxSpaces = []TaxSpace{
        {Name: "Luxury Tax", Amount: 100},
 }
 
-var PropertyOwned = []int32{} // playerID
+var PropertyOwners = []int32{} // playerID
 
 var SpaceToRespProperty = make(map[int32]int32)
 var SpaceToOwnableProperty = make(map[int32]int32)
@@ -149,7 +181,7 @@ func init() {
 
                if propertyType == TypeColor || propertyType == TypeRailroad || propertyType == TypeUtility {
                        SpaceToOwnableProperty[spaceID] = ownableIndex
-                       PropertyOwned = append(PropertyOwned, -1)
+                       PropertyOwners = append(PropertyOwners, -1)
                }
 
                switch propertyType {
@@ -223,8 +255,8 @@ func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) {
                GoVisitors = append(GoVisitors, playerID)
        case TypeColor:
                propIndex := SpaceToOwnableProperty[nextPos]
-               if PropertyOwned[propIndex] != -1 { // property owned?
-                       ownerID := PropertyOwned[propIndex]
+               if PropertyOwners[propIndex] != -1 { // property owned?
+                       ownerID := PropertyOwners[propIndex]
                        if ownerID != playerID { // not by you
                                OwnedColorVisitors = append(OwnedColorVisitors, OwnedColorVisitor{visitorID: playerID, ownerID: ownerID, colorID: SpaceToRespProperty[nextPos]})
                        }
@@ -237,8 +269,8 @@ func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) {
                TaxVisitors = append(TaxVisitors, playerID)
        case TypeRailroad:
                propIndex := SpaceToOwnableProperty[nextPos]
-               if PropertyOwned[propIndex] != -1 { // property owned?
-                       ownerID := PropertyOwned[propIndex]
+               if PropertyOwners[propIndex] != -1 { // property owned?
+                       ownerID := PropertyOwners[propIndex]
                        if ownerID != playerID { // not by you
                                OwnedRailroadVisitors = append(OwnedRailroadVisitors, OwnedRailroadVisitor{visitorID: playerID, ownerID: ownerID, railroadID: SpaceToRespProperty[nextPos]})
                        }
@@ -251,8 +283,8 @@ func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) {
                JailVisitors = append(JailVisitors, playerID)
        case TypeUtility:
                propIndex := SpaceToOwnableProperty[nextPos]
-               if PropertyOwned[propIndex] != -1 { // property owned?
-                       ownerID := PropertyOwned[propIndex]
+               if PropertyOwners[propIndex] != -1 { // property owned?
+                       ownerID := PropertyOwners[propIndex]
                        if ownerID != playerID { // not by you
                                OwnedUtilityVisitors = append(OwnedUtilityVisitors, OwnedUtilityVisitor{visitorID: playerID, ownerID: ownerID, utilityID: SpaceToRespProperty[nextPos], diceRoll: diceRoll})
                        }