 Rem File: Cmd001.bas
 Rem Sub: Accept.Offer
 Rem $Include: 'dndbbs.inc'
 On Error Goto Main.Error
 If Chained<>-2 Then
    Color 15,0
    Print "DNDBBS Command 001 -- Accept.Offer."
    Color 7,0
    End
 Endif
 Call Accept.Offer(TempX)
Main.Resume:
 Chain DND.Path+"DNDBBS.EXE"
 End
Main.Error:
 Resume Main.Resume

Sub Accept.Offer(VarZZ)
 On Local Error Goto Err.Trap6
 If VarZZ Then
    Strng="There are no offers to decline!"
 Else
    Strng="There are no offers to accept!"
 Endif
 VarX2=Node+1
 Call Read.Message.Record(MessWorkFile1,VarX2)
 If MessWorkRecord1.OfferTime<=False Then
    Call IO.O
    Exit Sub
 Endif
 Temp1=MessWorkRecord1.FromNode+1
 TempZ1=MessWorkRecord1.FromNode
 Call Read.Message.Record(MessWorkFile1,Temp1)
 If MessWorkRecord1.OfferTime<=False Then
    Call IO.O
    Exit Sub
 Endif
 Call Read.Message.Record(MessWorkFile1,VarX2)
 If MessWorkRecord1.ToNode<>Node Then
    Call IO.O
    Exit Sub
 Endif
 Call Read.Message.Record(MessWorkFile1,Temp1)
 Temp2!=MessWorkRecord1.UserIndex
 If VarZZ Then
    Strng="Offer declined!"
    Call IO.O
    Out2="Offer declined!"
    Goto End.Swap3
 Endif
 Temp41=False
 Var1#=MessWorkRecord1.GoldFor
 If Var1#>DFalse Then
    If UserRecord.Gold-Var1#<DFalse Then
       Strng="You don't have enough gold to trade!"
       Call IO.O
       Goto End.Swap2
    Endif
 Else
    ' offer <value/item> to <player> for <container>
    Var2!=SFalse
    If MessWorkRecord1.ContainerFor Then
       Temp41=True
       Temp5!=UserRecord.First2
       If Temp5! Then
          Do While Lost.Carrier=False
             Call Read.Record(ContainerFile,Temp5!)
             Var6!=ContainerRecord.Next
             If ContainerRecord.DeletedContainer=False Then
                If ContainerRecord.WithinContainer=SFalse Then
                   If ContainerRecord.SwapFlag Then
                      Var2!=Temp5!
                      Exit Do
                   Endif
                Endif
             Endif
             If Var6!=SFalse Then
                Exit Do
             Endif
             Temp5!=Var6!
          Loop
       Endif
    Else
       ' offer <value/item> to <player> for <item>
       For Temp5!=1! To Lof(MessWorkFile6)/MessWorkRecLen6
          Call Read.Record(MessWorkFile6,Temp5!)
          If MessWorkRecord6.User=User.Index Then
             If MessWorkRecord6.Index=MessWorkRecord1.ItemFor Then
                If MessWorkRecord6.Charges=MessWorkRecord1.ForCharges Then
                   If MessWorkRecord6.Index>SFalse Then
                      Call Read.Record(TreasureFile,MessWorkRecord6.Index)
                   Else
                      If MessWorkRecord6.Index<SFalse Then
                         Call Read.Record(ObjectFile,Abs(MessWorkRecord6.Index))
                      Endif
                   Endif
                   Var2!=Temp5!
                   Exit For
                Endif
             Endif
          Endif
       Next
    Endif
    If Var2!=SFalse Then
       Strng="You don't have the item to trade!"
       Call IO.O
       Goto End.Swap2
    Endif
 Endif
 Temp42=False
 Var1#=MessWorkRecord1.GoldOffer
 If Var1#>DFalse Then
    Temp2#=MessWorkRecord1.Gold-Var1#
    If Temp2#<DFalse Then
       Out2=MessWorkRecord1.UserName
       Out2=Lcase$(Rtrim$(Out2))
       Mid$(Out2,1,1)=Ucase$(Mid$(Out2,1,1))
       Strng=Out2+" doesn't have the Gold to trade!"
       Call IO.O
       Goto End.Swap2
    Endif
 Else
    ' offer <container> to <player> for <value/item>
    Var3=False
    If MessWorkRecord1.ContainerOffer Then
       Temp42=True
       Call Read.Record(UserFile2,Temp2!)
       Temp5!=UserRecord2.First2
       If Temp5! Then
          Do While Lost.Carrier=False
             Call Read.Record(ContainerFile,Temp5!)
             Var6!=ContainerRecord.Next
             If ContainerRecord.DeletedContainer=False Then
                If ContainerRecord.WithinContainer=SFalse Then
                   If ContainerRecord.SwapFlag Then
                      Var3=True
                      Exit Do
                   Endif
                Endif
             Endif
             If Var6!=SFalse Then
                Exit Do
             Endif
             Temp5!=Var6!
          Loop
       Endif
    Else
       ' offer <item> to <player> for <value/item>
       Temp7!=MessWorkRecord1.ItemOffer
       Temp6=MessWorkRecord1.OfferCharges
       For Temp5!=1! To Lof(MessWorkFile6)/MessWorkRecLen6
          Call Read.Record(MessWorkFile6,Temp5!)
          If MessWorkRecord6.User=Temp2! Then
             If MessWorkRecord6.Index=Temp7! Then
                If MessWorkRecord6.Charges=Temp6 Then
                   Var3=True
                   Exit For
                Endif
             Endif
          Endif
       Next
    Endif
    If Var3=False Then
       Out2=MessWorkRecord1.UserName
       Out2=Lcase$(Rtrim$(Out2))
       Mid$(Out2,1,1)=Ucase$(Mid$(Out2,1,1))
       Strng=Out2+" doesn't have the item to trade!"
       Call IO.O
       Goto End.Swap2
    Endif
 Endif
 Call Read.Message.Record(MessWorkFile1,VarX2)
 Var1#=MessWorkRecord1.GoldFor
 If Var1#>DFalse Then
    UserRecord.Gold=UserRecord.Gold-Var1#
 Else
    If Temp41 Then
       ' offer <value/item> to <player> for <container>
       '    move container from user.index to temp2!
       '       call give.object
       Call Find.Container.Swap(User.Index,VarA!)
       If VarA!=SFalse Then
          Goto End.Swap2
       Endif
       Call Give.Player.Container(VarA!,TempZ1)
    Else
       Call Discard(Var2!,True)
    Endif
 Endif
 Call Read.Message.Record(MessWorkFile1,VarX2)
 Var1#=MessWorkRecord1.GoldOffer
 If Var1#>DFalse Then
    Call Read.Message.Record(MessWorkFile7,Temp1)
    MessWorkRecord7.Flags2=MessWorkRecord7.Flags2 Or 2^9
    MessWorkRecord7.Gold=Temp2#
    Call Write.Message.Record(MessWorkFile7,Temp1)
    UserRecord.Gold=UserRecord.Gold+Var1#
 Else
    If Temp42 Then
       ' offer <container> to <player> for <value/item>
       '    move container from temp2! to user.index
       '       call take.object
       Call Find.Container.Swap(Temp2!,VarA!)
       If VarA!=SFalse Then
          Goto End.Swap2
       Endif
       Call Take.Player.Container(VarA!,TempZ1)
    Else
       MessWorkRecord6.User=SFalse
       MessWorkRecord6.Index=SFalse
       Call Write.Record(MessWorkFile6,Temp5!)
       If Temp7!>SFalse Then
          Call Read.Record(TreasureFile,Temp7!)
       Else
          Call Read.Record(ObjectFile,Abs(Temp7!))
       Endif
       Call Add.Inventory(Temp7!,Temp6)
    Endif
 Endif
 Strng="You accept the offer and trade!"
 Call IO.O
 Call Read.Message.Record(MessWorkFile1,VarX2)
 MessWorkRecord1.OfferTime=False
 Call Write.Message.Record(MessWorkFile1,VarX2)
 Temp1=MessWorkRecord1.FromNode+1
 Call Read.Message.Record(MessWorkFile1,Temp1)
 MessWorkRecord1.OfferTime=True ' accepted
 Call Write.Message.Record(MessWorkFile1,Temp1)
 Exit Sub
End.Swap2:
 Out2="Offer failed!"
End.Swap3:
 ' remove offer
 Call Read.Message.Record(MessWorkFile1,VarX2)
 MessWorkRecord1.OfferTime=False
 Call Write.Message.Record(MessWorkFile1,VarX2)
 Temp1=MessWorkRecord1.FromNode+1
 Call Read.Message.Record(MessWorkFile1,Temp1)
 MessWorkRecord1.OfferTime=False ' declined/error
 Call Write.Message.Record(MessWorkFile1,Temp1)
 ' send message
 Call Send.Mess(5,Temp2!,SFalse,Out2)
Err.Resume6:
 Exit Sub
Err.Trap6:
 Resume Err.Resume6
End Sub

' find swap container
Sub Find.Container.Swap(Var1!,VarA!)
 On Local Error Goto Err.Trap6x
 Call Read.Record(UserFile2,Var1!)
 VarA!=SFalse
 Temp5!=UserRecord2.First2
 If Temp5! Then
    Do While Lost.Carrier=False
       Call Read.Record(ContainerFile,Temp5!)
       Var6!=ContainerRecord.Next
       If ContainerRecord.DeletedContainer=False Then
          If ContainerRecord.WithinContainer=SFalse Then
             If ContainerRecord.SwapFlag Then
                VarA!=Temp5!
                Exit Do
             Endif
          Endif
       Endif
       If Var6!=SFalse Then
          Exit Do
       Endif
       Temp5!=Var6!
    Loop
 Endif
 If VarA!=SFalse Then
    Strng="Container not found."
    Call IO.O
 Endif
Err.Resume6x:
 Exit Sub
Err.Trap6x:
 Resume Err.Resume6x
End Sub

' move container from user.index to temp2!
Sub Give.Player.Container(TempA!,VarY)
 On Local Error Goto Err.Trap6y
 ' delete user container
 Call Delete.User.Container(TempA!)
 ' add user container
 Call Read.Record(ContainerFile,TempA!)
 ContainerRecord.UserNumber=Temp2!
 ContainerRecord.RoomNumber=SFalse
 Call Add.User.Container2(TempA!)
 ' update online user
 Call Read.Message.Record(MessWorkFile7,VarY+1)
 MessWorkRecord7.First2=UserRecord2.First2
 MessWorkRecord7.Last2=UserRecord2.Last2
 MessWorkRecord7.Weight=ContainerRecord.Weight
 MessWorkRecord7.Flags4=MessWorkRecord7.Flags4 Or 2^9
 Call Write.Message.Record(MessWorkFile7,VarY+1)
 Weight=Weight-ContainerRecord.Weight
 If Weight<DFalse Then
    Weight=DFalse
 Endif
Err.Resume6y:
 Exit Sub
Err.Trap6y:
 Resume Err.Resume6y
End Sub

' move container from temp2! to user.index
Sub Take.Player.Container(TempA!,VarY)
 On Local Error Goto Err.Trap6z
 ' delete user container
 Call Delete.User.Container3(TempA!)
 ' add user container
 Call Read.Record(ContainerFile,TempA!)
 ContainerRecord.UserNumber=User.Index
 ContainerRecord.RoomNumber=SFalse
 Call Add.User.Container(TempA!)
 ' update online user
 Call Read.Message.Record(MessWorkFile7,VarY+1)
 MessWorkRecord7.First2=UserRecord2.First2
 MessWorkRecord7.Last2=UserRecord2.Last2
 MessWorkRecord7.Weight=ContainerRecord.Weight
 MessWorkRecord7.Flags4=MessWorkRecord7.Flags4 Or 2^8
 Call Write.Message.Record(MessWorkFile7,VarY+1)
 Weight=Weight+ContainerRecord.Weight
Err.Resume6z:
 Exit Sub
Err.Trap6z:
 Resume Err.Resume6z
End Sub
