github.com/metaworking/channeld@v0.7.3/pkg/channeld/entity_test.go (about) 1 package channeld 2 3 import ( 4 "testing" 5 6 "github.com/metaworking/channeld/pkg/channeldpb" 7 "github.com/metaworking/channeld/pkg/common" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestEntityChannelGroupController(t *testing.T) { 12 InitChannels() 13 14 /* Case 1: When Character A moves across the spatial channel, its PlayerController and PlayerState should be handed over together. 15 */ 16 charA := EntityId(1) 17 pcA := EntityId(2) 18 psA := EntityId(3) 19 chA := createChannelWithId(common.ChannelId(charA), channeldpb.ChannelType_ENTITY, nil) 20 chA.entityController.AddToGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{charA, pcA, psA}) 21 handoverEntities := chA.entityController.GetHandoverEntities() 22 assert.Equal(t, 3, len(handoverEntities)) 23 assert.Contains(t, handoverEntities, charA) 24 assert.Contains(t, handoverEntities, pcA) 25 assert.Contains(t, handoverEntities, psA) 26 27 /* Case 2: When A is attacked by Character B in another spatial server, A and its PlayerController and PlayerState 28 * should be handed over to B's spatial server and then locked from handover. 29 */ 30 charB := EntityId(4) 31 pcB := EntityId(5) 32 psB := EntityId(6) 33 chB := createChannelWithId(common.ChannelId(charB), channeldpb.ChannelType_ENTITY, nil) 34 chB.entityController.AddToGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{charB, pcB, psB}) 35 36 // Triggers cross-server attack 37 chB.entityController.AddToGroup(channeldpb.EntityGroupType_LOCK, []EntityId{charA, charB}) 38 handoverEntities = chA.entityController.GetHandoverEntities() 39 assert.Equal(t, 0, len(handoverEntities), "Character A is attacked and locked by Character B, should not handover") 40 41 /* Case 3: When A leaves the combat, A and its PlayerController and PlayerState should be unlocked from handover, and then 42 * be handed over to the spatial channel corresponding to its current location. 43 */ 44 chA.entityController.RemoveFromGroup(channeldpb.EntityGroupType_LOCK, []EntityId{charA}) 45 handoverEntities = chA.entityController.GetHandoverEntities() 46 assert.Equal(t, 3, len(handoverEntities), "Character A left the combat, should be able to handover") 47 48 handoverEntities = chB.entityController.GetHandoverEntities() 49 assert.Equal(t, 0, len(handoverEntities), "Character B is still in combat, should be locked from handover") 50 51 /* Case 4: When a vehicle moves across the spatial channel, its passengers should be handed over together. 52 * If A gets down from the vehicle, A should be handed over to the spatial channel corresponding to its current location. 53 */ 54 vehicle := EntityId(7) 55 chV := createChannelWithId(common.ChannelId(vehicle), channeldpb.ChannelType_ENTITY, nil) 56 57 charC := EntityId(8) 58 pcC := EntityId(9) 59 psC := EntityId(10) 60 chC := createChannelWithId(common.ChannelId(charC), channeldpb.ChannelType_ENTITY, nil) 61 chC.entityController.AddToGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{charC, pcC, psC}) 62 63 // Character C gets into the vehicle 64 chV.entityController.AddToGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{vehicle, charC}) 65 chC.entityController.AddToGroup(channeldpb.EntityGroupType_LOCK, []EntityId{charC}) 66 // Character A gets into the vehicle 67 chV.entityController.AddToGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{vehicle, charA}) 68 chA.entityController.AddToGroup(channeldpb.EntityGroupType_LOCK, []EntityId{charA}) 69 // Character C moves aross the spatial channel first, but the vehicle hasn't yet. 70 handoverEntities = chC.entityController.GetHandoverEntities() 71 assert.Equal(t, 0, len(handoverEntities), "Character C should be locked from handover") 72 73 // The vehicle moves across the spatial channel 74 handoverEntities = chV.entityController.GetHandoverEntities() 75 assert.Contains(t, handoverEntities, vehicle) 76 assert.Contains(t, handoverEntities, charA, "Character A should be handed over with the vehicle") 77 assert.Contains(t, handoverEntities, charC, "Character C should be handed over with the vehicle") 78 79 // Character A gets down from the vehicle 80 chV.entityController.RemoveFromGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{charA}) 81 chA.entityController.RemoveFromGroup(channeldpb.EntityGroupType_LOCK, []EntityId{charA}) 82 // IMPORTANT: after getting off the vehicle, Character A add its PlayerController and PlayerState back to the handover group again 83 chA.entityController.AddToGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{charA, pcA, psA}) 84 handoverEntities = chA.entityController.GetHandoverEntities() 85 assert.Equal(t, 3, len(handoverEntities), "Character A got off the vehicle, should be able to handover again") 86 87 /* Case 5: When A is in a vehicle and attacked by Character B in another spatial server, A should be pulled off the vehicle and handed over to B's spatial server; 88 * the vehicle and all its passengers can continue to move across the spatial channel be handed over together. 89 */ 90 91 // Character A gets into the vehicle again 92 chV.entityController.AddToGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{vehicle, charA}) 93 // Triggers cross-server attack 94 chB.entityController.AddToGroup(channeldpb.EntityGroupType_LOCK, []EntityId{charA, charB}) 95 // The spatial server of the vehicle should remove Character A from the vehicle's handover group 96 chV.entityController.RemoveFromGroup(channeldpb.EntityGroupType_HANDOVER, []EntityId{charA}) 97 handoverEntities = chA.entityController.GetHandoverEntities() 98 assert.Equal(t, 0, len(handoverEntities), "Character A is attacked and locked by Character B, should not handover") 99 100 // The vehicle moves across the spatial channel 101 handoverEntities = chV.entityController.GetHandoverEntities() 102 assert.Contains(t, handoverEntities, vehicle) 103 assert.NotContains(t, handoverEntities, charA, "Character A should NOT be handed over with the vehicle") 104 assert.Contains(t, handoverEntities, charC, "Character C should be handed over with the vehicle") 105 }