_ := room.UpdatePropertis(roomID, func(properties map[string]interface{}) bool {if _, ok := properties["booked"]; !ok { properties["booked"] =truereturntrue }// The room has already been bookedreturnfalse})
Room の数値プロパティをインクリメントまたはデクリメントする方法
Room モジュールには、数値プロパティをインクリメントおよび/またはデクリメントする API があります。これは、同期的に数値プロパティを更新する必要がある場合に便利です。
// If the property called HP does not exist, it will create itcreated := room.IncrProperty(roomID, "HP", 1000)// Decrement the property called HP by -200decremented := room.IncrProperty(roomID, "HP", -200)// Increment the property called HP by 100incremented := room.IncrProperty(roomID, "HP", 100)
Data Capsule
Data Capsuleは、interface{} のデータを安全に管理するために、Diarkis が提供するデータ構造です。
addrlist, err := room.GetRoomNodeAddressList(roomID)if err !=nil {// Handle errorreturn } req :=make(map[string]interface{}) req["roomID"] = roomID addr := addrlist[0] // the rest in the array are the backups mesh.SendRequest(cmdID, addr, req, func(err error, res map[string]interface{}) {if err !=nil {// Handle errorreturn }// Remote room property updated successfully})
リモートサーバノードでのロジックの更新
// This sets up handleRemoteRoomPropUpdate function to be invoked when mesh.SendRequest sends// the command message as shown abovemesh.Command(cmdID, handleRemoteRoomPropUpdate)funchandleRemoteRoomPropUpdate(req map[string]interface{}) (error, map[string]interface{}) { roomID := mesh.GetString(req, "roomID")var err error _ := room.UpdateProperties(roomID, func(props map[string]interface{}) bool {if _, ok := props["booked"]; !ok { props["booked"] =true// Booked the room successfullyreturntrue }// The room has already been booked... err = errors.New("Room has already been booked")returnfalse }) res :=make(map[string]interface{})return res, err}
Room 参加者を特定のユーザー ID で予約する方法
特定のユーザー ID で Room を予約することができます。予約すると、予約したIDを持つユーザーは、Room の上限を気にすることなく参加することができます。ただし、全員分の予約をしてしまうと、予約をしていないユーザーの参加を拒否することができます。
注: この操作は、Room のオーナーにのみ許可されています。
予約する
memberIDs :=make([]string, 4)memberIDs[0] ="user-id-123"memberIDs[1] ="user-id-456"memberIDs[2] ="user-id-789"memberIDs[3] ="user-id-012"// userData is the owner of the roomroom.Reserve(roomID, userData, memberIDs)
予約を取り消す
memberIDs :=make([]string, 4)memberIDs[0] ="user-id-123"memberIDs[1] ="user-id-456"memberIDs[2] ="user-id-789"memberIDs[3] ="user-id-012"// userData is the owner of the roomroom.Reserve(roomID, userData, memberIDs)
addr, err := room.GetRoomNodeAddress(roomID)if err !=nil {// Handle errorreturn}req :=make(map[string]interface{})req["roomID"] = roomID// passcode comes from the clientreq["passcode"] = passcode// mesh.SendRequest is a inter-pod communication functionmesh.SendRequest(cmdID, addr, req, func(err error, res map[string]interface{}) {if err !=nil {// Handle errorreturn }// The room has been unlocked successfully})
mesh.Command(cmdID, unlockRoom)funcunlockRoom(req map[string]interface{}) (error, map[string]interface{}) { roomID := mesh.GetString(req, "roomID") passcode := mesh.GetString(req, "passcode")var err error _ := room.UpdateProperties(roomID, func(props map[string]interface{}) bool {if _, ok := props["unlocked"].(bool); !ok {// the room is already unlocked err := errors.New("Room already unlocked")returnfalse }if _, ok := props["passcode"].(string); !ok {// missing pass code... err := errors.New("Room is missing passcode...")returnfalse }if passcode != props["passcode"].(string) {// pass code the client has does not match err := errors.New("Pass code does not match")returnfalse }// pass code the client has matches props["unlocked"] =truereturntrue }) res :=make(map[string]interface{})return res, err}
すべての Join 操作に条件を追加する方法
Room の Join 操作には、機能を制御するための条件をオプションで追加することができます。
// This callback function will be called every time a join operation is called. By returning an error, you may reject the client to join.
room.SetJoinCondition(func(roomID string, userData *userData) error) {// Implement custom logic to control join// Example join condition:// The new client has to have "code" that has been pre-defined in the room property code := room.GetProperty(roomID)if code ==nil {// The room does not have a code, we assume the room to be public: proceed to joinreturnnil }// clientCode must be set to userData by userData.Set() before calling this clientCode :=GetRoomCodeFromUser(userData.Get("code"))if code == clientCode {// The code client has matches. Proceed to joinreturnnil }// join rejectedreturn errors.New("ClientCode does not match")})
room.SetOnRoomChange(func(roomID string, memberIDs []string, props map[string]interface{}) { logger.Debug("There has been a change in the room %s", roomID)})
// SetOnAnnounce is raised when Broadcast, Message, Announce are invokedroom.SetOnAnnounce(func(roomID string, ver uint8, cmd uint16, msg []byte) {// We capture the message sent to the room and send it outside such as CDN etc. data :=CreateCDNData(roomID, ver, cmd, msg)SendCDN(data)})