2022-09-20 15:50:01 +00:00
package models
import (
"container/heap"
2022-10-14 08:08:22 +00:00
. "dnmshared"
2022-09-20 15:50:01 +00:00
"fmt"
"go.uber.org/zap"
2022-11-26 16:00:39 +00:00
"strings"
2022-09-20 15:50:01 +00:00
"sync"
)
// Reference https://github.com/genxium/GoStructPrac.
type RoomHeap [ ] * Room
type RoomMap map [ int32 ] * Room
var (
// NOTE: For the package exported instances of non-primitive types to be accessed as singletons, they must be of pointer types.
RoomHeapMux * sync . Mutex
RoomHeapManagerIns * RoomHeap
RoomMapManagerIns * RoomMap
)
func ( pPq * RoomHeap ) PrintInOrder ( ) {
pq := * pPq
2022-11-26 16:00:39 +00:00
s := make ( [ ] string , 0 )
s = append ( s , fmt . Sprintf ( "The RoomHeap instance now contains:" ) )
2022-09-20 15:50:01 +00:00
for i := 0 ; i < len ( pq ) ; i ++ {
2022-11-26 16:00:39 +00:00
s = append ( s , fmt . Sprintf ( "{index: %d, roomID: %d, score: %.2f} " , i , pq [ i ] . Id , pq [ i ] . Score ) )
2022-09-20 15:50:01 +00:00
}
2022-11-26 16:00:39 +00:00
Logger . Debug ( strings . Join ( s , "\n" ) )
2022-09-20 15:50:01 +00:00
}
func ( pq RoomHeap ) Len ( ) int { return len ( pq ) }
func ( pq RoomHeap ) Less ( i , j int ) bool {
return pq [ i ] . Score > pq [ j ] . Score
}
func ( pq * RoomHeap ) Swap ( i , j int ) {
( * pq ) [ i ] , ( * pq ) [ j ] = ( * pq ) [ j ] , ( * pq ) [ i ]
( * pq ) [ i ] . Index = i
( * pq ) [ j ] . Index = j
}
func ( pq * RoomHeap ) Push ( pItem interface { } ) {
// NOTE: Must take input param type `*Room` here.
n := len ( * pq )
pItem . ( * Room ) . Index = n
* pq = append ( * pq , pItem . ( * Room ) )
}
func ( pq * RoomHeap ) Pop ( ) interface { } {
old := * pq
n := len ( old )
if n == 0 {
return nil
}
pItem := old [ n - 1 ]
if pItem . Score <= float32 ( 0.0 ) {
return nil
}
pItem . Index = - 1 // for safety
* pq = old [ 0 : n - 1 ]
// NOTE: Must return instance which is directly castable to type `*Room` here.
return pItem
}
func ( pq * RoomHeap ) update ( pItem * Room , Score float32 ) {
// NOTE: Must use type `*Room` here.
heap . Fix ( pq , pItem . Index )
}
func ( pq * RoomHeap ) Update ( pItem * Room , Score float32 ) {
pq . update ( pItem , Score )
}
func PrintRoomMap ( ) {
fmt . Printf ( "The RoomMap instance now contains:\n" )
for _ , pR := range * RoomMapManagerIns {
fmt . Printf ( "{roomID: %d, score: %.2f} " , pR . Id , pR . Score )
}
fmt . Printf ( "\n" )
}
func InitRoomHeapManager ( ) {
RoomHeapMux = new ( sync . Mutex )
// Init "pseudo class constants".
InitRoomBattleStateIns ( )
InitPlayerBattleStateIns ( )
initialCountOfRooms := 32
pq := make ( RoomHeap , initialCountOfRooms )
roomMap := make ( RoomMap , initialCountOfRooms )
for i := 0 ; i < initialCountOfRooms ; i ++ {
roomCapacity := 2
pq [ i ] = & Room {
2022-10-02 03:33:40 +00:00
Id : int32 ( i + 1 ) ,
Capacity : roomCapacity ,
Index : i ,
2022-09-20 15:50:01 +00:00
}
roomMap [ pq [ i ] . Id ] = pq [ i ]
2022-10-02 03:33:40 +00:00
pq [ i ] . OnDismissed ( )
2022-09-20 15:50:01 +00:00
}
heap . Init ( & pq )
RoomHeapManagerIns = & pq
RoomMapManagerIns = & roomMap
Logger . Info ( "The RoomHeapManagerIns has been initialized:" , zap . Any ( "addr" , fmt . Sprintf ( "%p" , RoomHeapManagerIns ) ) , zap . Any ( "size" , len ( * RoomHeapManagerIns ) ) )
Logger . Info ( "The RoomMapManagerIns has been initialized:" , zap . Any ( "size" , len ( * RoomMapManagerIns ) ) )
}